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

Method genStructMemberGetter

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

Source from the content-addressed store, hash-verified

176}
177
178func (g *pyGen) genStructMemberGetter(s *Struct, i int, f types.Object) {
179 pkgname := g.cfg.Name
180 ft := f.Type()
181 ret := current.symtype(ft)
182 if ret == nil {
183 return
184 }
185
186 gname := f.Name()
187 if g.cfg.RenameCase {
188 gname = toSnakeCase(gname)
189 }
190
191 if newName, err := extractPythonNameFieldTag(gname, s.Struct().Tag(i)); err == nil {
192 gname = newName
193 }
194
195 cgoFn := fmt.Sprintf("%s_%s_Get", s.ID(), f.Name())
196
197 g.pywrap.Printf("@property\n")
198 g.pywrap.Printf("def %[1]s(self):\n", gname)
199 g.pywrap.Indent()
200 if gdoc := g.pkg.getDoc(s.Obj().Name(), f); gdoc != "" {
201 g.pywrap.Printf(`"""`)
202 g.pywrap.Printf(gdoc)
203 g.pywrap.Println(`"""`)
204 }
205 if ret.hasHandle() {
206 cvnm := ret.pyPkgId(g.pkg.pkg)
207 g.pywrap.Printf("return %s(handle=_%s.%s(self.handle))\n", cvnm, pkgname, cgoFn)
208 } else {
209 g.pywrap.Printf("return _%s.%s(self.handle)\n", pkgname, cgoFn)
210 }
211 g.pywrap.Outdent()
212
213 g.gofile.Printf("//export %s\n", cgoFn)
214 g.gofile.Printf("func %s(handle CGoHandle) %s {\n", cgoFn, ret.cgoname)
215 g.gofile.Indent()
216 g.gofile.Printf("op := ptrFromHandle_%s(handle)\nreturn ", s.ID())
217 if ret.go2py != "" {
218 if ret.hasHandle() && !ret.isPtrOrIface() {
219 g.gofile.Printf("%s(&op.%s)%s", ret.go2py, f.Name(), ret.go2pyParenEx)
220 } else {
221 g.gofile.Printf("%s(op.%s)%s", ret.go2py, f.Name(), ret.go2pyParenEx)
222 }
223 } else {
224 g.gofile.Printf("op.%s", f.Name())
225 }
226 g.gofile.Printf("\n")
227 g.gofile.Outdent()
228 g.gofile.Printf("}\n\n")
229
230 g.pybuild.Printf("mod.add_function('%s', retval('%s'), [param('%s', 'handle')])\n", cgoFn, ret.cpyname, PyHandle)
231}
232
233func (g *pyGen) genStructMemberSetter(s *Struct, i int, f types.Object) {
234 pkgname := g.cfg.Name

Callers 1

genStructMembersMethod · 0.95

Calls 15

toSnakeCaseFunction · 0.85
symtypeMethod · 0.80
StructMethod · 0.80
PrintfMethod · 0.80
IndentMethod · 0.80
getDocMethod · 0.80
PrintlnMethod · 0.80
hasHandleMethod · 0.80
pyPkgIdMethod · 0.80
OutdentMethod · 0.80
isPtrOrIfaceMethod · 0.80

Tested by

no test coverage detected