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.
This commit is contained in:
Kyle Ellrott 2015-09-02 11:52:49 -07:00 committed by Sebastien Binet
parent 33f3ca06ba
commit e40a2535ee
3 changed files with 44 additions and 0 deletions

View File

@ -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)
}

View File

@ -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{}
}

View File

@ -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 {