From e40a2535ee1bb2ef06c863abf466a013b7f17f5b Mon Sep 17 00:00:00 2001 From: Kyle Ellrott Date: Wed, 2 Sep 2015 11:52:49 -0700 Subject: [PATCH] bind: ignore private fields in structs This patch adds a check so that the types of private fields aren't exported to the wrapper. This is useful if writing a 'wrapper' package that buffers the python code from complex Go concepts that aren't yet wrapped for python (like pointers and channels). The code: _examples/wrapper/real_code.go and _examples/wrapper/pywrapper/wrapper_code.go can be compiled with the command: gopy bind github.com/go-python/gopy/_examples/wrapper/pywrapper and the functions manipulate Go classes gopy doesn't yet support, but they are private, and not exported, so now the code will compile. Fixes #60. --- _examples/wrapper/pywrapper/wrapper_code.go | 24 +++++++++++++++++++++ _examples/wrapper/real_code.go | 15 +++++++++++++ bind/symtab.go | 5 +++++ 3 files changed, 44 insertions(+) create mode 100644 _examples/wrapper/pywrapper/wrapper_code.go create mode 100644 _examples/wrapper/real_code.go diff --git a/_examples/wrapper/pywrapper/wrapper_code.go b/_examples/wrapper/pywrapper/wrapper_code.go new file mode 100644 index 0000000..a015f94 --- /dev/null +++ b/_examples/wrapper/pywrapper/wrapper_code.go @@ -0,0 +1,24 @@ +// Copyright 2015 The go-python Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package pywrapper + +import ( + "github.com/go-python/gopy/_examples/wrapper" + "fmt" +) + +type WrapperStruct struct { + data *wrapper.RealStruct +} + +func Test() string { + a := wrapper.PointerTest() + fmt.Println("%t", a) + return "Hello" +} + +func (a WrapperStruct) Test() string { + return fmt.Sprintf("%t", a.data) +} \ No newline at end of file diff --git a/_examples/wrapper/real_code.go b/_examples/wrapper/real_code.go new file mode 100644 index 0000000..5b39387 --- /dev/null +++ b/_examples/wrapper/real_code.go @@ -0,0 +1,15 @@ +// Copyright 2015 The go-python Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package wrapper + + +type RealStruct struct { + pointers map[string]*RealStruct + Channel chan int +} + +func PointerTest() *RealStruct { + return &RealStruct{} +} \ No newline at end of file diff --git a/bind/symtab.go b/bind/symtab.go index 119b959..90c7daa 100644 --- a/bind/symtab.go +++ b/bind/symtab.go @@ -99,6 +99,10 @@ type symbol struct { pychk string } +func isPrivate(s string) bool { + return (strings.ToLower(s[0:1]) == s[0:1]) +} + func (s symbol) isType() bool { return (s.kind & skType) != 0 } @@ -569,6 +573,7 @@ func (sym *symtab) addStructType(pkg *types.Package, obj types.Object, t types.T kind |= skStruct pybuf := make([]string, 0, typ.NumFields()) for i := 0; i < typ.NumFields(); i++ { + if isPrivate(typ.Field(i).Name()) { continue } ftyp := typ.Field(i).Type() fsym := sym.symtype(ftyp) if fsym == nil {