MCPcopy Index your code
hub / github.com/go-python/gopy / genStructMemberSetter

Method genStructMemberSetter

bind/gen_struct.go:233–290  ·  view source on GitHub ↗
(s *Struct, i int, f types.Object)

Source from the content-addressed store, hash-verified

231}
232
233func (g *pyGen) genStructMemberSetter(s *Struct, i int, f types.Object) {
234 pkgname := g.cfg.Name
235 ft := f.Type()
236 ret := current.symtype(ft)
237 if ret == nil {
238 return
239 }
240
241 gname := f.Name()
242 if g.cfg.RenameCase {
243 gname = toSnakeCase(gname)
244 }
245
246 if newName, err := extractPythonNameFieldTag(gname, s.Struct().Tag(i)); err == nil {
247 gname = newName
248 }
249
250 cgoFn := fmt.Sprintf("%s_%s_Set", s.ID(), f.Name())
251
252 g.pywrap.Printf("@%s.setter\n", gname)
253 g.pywrap.Printf("def %[1]s(self, value):\n", gname)
254 g.pywrap.Indent()
255 g.pywrap.Printf("if isinstance(value, go.GoClass):\n")
256 g.pywrap.Indent()
257 g.pywrap.Printf("_%s.%s(self.handle, value.handle)\n", pkgname, cgoFn)
258 g.pywrap.Outdent()
259 g.pywrap.Printf("else:\n")
260 g.pywrap.Indent()
261 // See comment in genStructInit about ensuring that gopy managed
262 // objects are only assigned to from gopy managed objects.
263 utyp := f.Type()
264 if _, isNamed := utyp.(*types.Named); isNamed {
265 utyp = utyp.Underlying()
266 }
267 switch utyp.(type) {
268 case *types.Basic:
269 g.pywrap.Printf("_%s.%s(self.handle, value)\n", pkgname, cgoFn)
270 default:
271 g.pywrap.Printf("raise TypeError(\"supplied argument type {t} is not a go.GoClass\".format(t=type(value)))\n")
272 }
273 g.pywrap.Outdent()
274 g.pywrap.Outdent()
275
276 g.gofile.Printf("//export %s\n", cgoFn)
277 g.gofile.Printf("func %s(handle CGoHandle, val %s) {\n", cgoFn, ret.cgoname)
278 g.gofile.Indent()
279 g.gofile.Printf("op := ptrFromHandle_%s(handle)\n", s.ID())
280 if ret.py2go != "" {
281 g.gofile.Printf("op.%s = %s(val)%s", f.Name(), ret.py2go, ret.py2goParenEx)
282 } else {
283 g.gofile.Printf("op.%s = val", f.Name())
284 }
285 g.gofile.Printf("\n")
286 g.gofile.Outdent()
287 g.gofile.Printf("}\n\n")
288
289 g.pybuild.Printf("mod.add_function('%s', None, [param('%s', 'handle'), param('%s', 'val')])\n", cgoFn, PyHandle, ret.cpyname)
290}

Callers 1

genStructMembersMethod · 0.95

Calls 9

toSnakeCaseFunction · 0.85
symtypeMethod · 0.80
StructMethod · 0.80
PrintfMethod · 0.80
IndentMethod · 0.80
OutdentMethod · 0.80
IDMethod · 0.65
NameMethod · 0.45

Tested by

no test coverage detected