github.com/SkycoinProject/gomobile@v0.0.0-20190312151609-d3739f865fa6/bind/bind.go (about) 1 // Copyright 2014 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package bind implements a code generator for gobind. 6 // 7 // See the documentation on the gobind command for usage details 8 // and the list of currently supported types. 9 // (http://godoc.org/golang.org/x/mobile/cmd/gobind) 10 package bind // import "golang.org/x/mobile/bind" 11 12 // TODO(crawshaw): slice support 13 // TODO(crawshaw): channel support 14 15 import ( 16 "bytes" 17 "go/format" 18 "go/token" 19 "go/types" 20 "io" 21 ) 22 23 type ( 24 GeneratorConfig struct { 25 Writer io.Writer 26 Fset *token.FileSet 27 Pkg *types.Package 28 AllPkg []*types.Package 29 } 30 31 fileType int 32 ) 33 34 // GenGo generates a Go stub to support foreign language APIs. 35 func GenGo(conf *GeneratorConfig) error { 36 buf := new(bytes.Buffer) 37 g := &goGen{ 38 Generator: &Generator{ 39 Printer: &Printer{Buf: buf, IndentEach: []byte("\t")}, 40 Fset: conf.Fset, 41 AllPkg: conf.AllPkg, 42 Pkg: conf.Pkg, 43 }, 44 } 45 g.Init() 46 if err := g.gen(); err != nil { 47 return err 48 } 49 src := buf.Bytes() 50 srcf, err := format.Source(src) 51 if err != nil { 52 conf.Writer.Write(src) // for debugging 53 return err 54 } 55 _, err = conf.Writer.Write(srcf) 56 return err 57 }