gopy icon indicating copy to clipboard operation
gopy copied to clipboard

struct constructor __init__ method accepts int args as handles for object fields

Open rcoreilly opened this issue 6 years ago • 1 comments

in bind/gen_struct.go -- need some additional type checking logic on that, which also allows valid handles..

rcoreilly avatar Jun 07 '19 19:06 rcoreilly

still a todo it seems -- here's the relevant code:

	for i := 0; i < numFields; i++ {
		f := s.Struct().Field(i)
		if _, err := isPyCompatField(f); err != nil {
			continue
		}
		// NOTE: this will accept int args for any handles / object fields so
		// some kind of additional type-checking logic to prevent that in a way
		// that also allows valid handles to be used as required. This is
		// achieved in the per-field setters (see below) with checks to ensure
		// that a struct field that is a gopy managed object is only
		// assigned gopy managed objects. Fields of basic types (e.g int, string)
		// etc can be assigned to directly.
		g.pywrap.Printf("if  %[1]d < len(args):\n", i)
		g.pywrap.Indent()
		g.pywrap.Printf("self.%s = args[%d]\n", f.Name(), i)
		g.pywrap.Outdent()
		g.pywrap.Printf("if %[1]q in kwargs:\n", f.Name())
		g.pywrap.Indent()
		g.pywrap.Printf("self.%[1]s = kwargs[%[1]q]\n", f.Name())
		g.pywrap.Outdent()
	}

rcoreilly avatar Sep 01 '21 06:09 rcoreilly