@@ -9,35 +9,51 @@ var generateFileTemplate = `package {{.OutputPackage}}
99// - size: {{.Plugin.Size}} bytes
1010// - sha256: {{.Plugin.Sha256}}
1111
12- import ({{ range $key, $val := .Plugin.ImportsNames }}
13- {{if isStandardImport $val $key }}"{{ $val }}"{{else}}{{ $key }} "{{ $val }}"{{end }}{{end}}
12+ {{ $imports := .Plugin.Imports }}
13+ import ({{ range $imports }}
14+ {{.}}
15+ {{end}}
1416)
15- {{$useVarReference := .Config.DereferenceVariables|not}}{{$pluginPackage := .Plugin.Package}}{{$receiver := .Config.OutputName}}
17+ {{$useVarReference := .Config.DereferenceVariables|not}}{{$pluginPackage := .Plugin.Package}}{{$receiver := .Config.OutputName}}{{$interface := .Config.AsInterface}}
1618// {{.Config.OutputName}} wraps symbols (functions and variables) exported by plugin {{.Plugin.Package}}
1719//
1820// See docs at https://godoc.org/{{$pluginPackage}}
19- type {{.Config.OutputName}} struct {
21+ type {{if $interface}}_{{end}}{{ .Config.OutputName}} struct {
2022 // Exported functions
2123 {{range .Plugin.Functions}}_{{ .Name }} {{ .Signature }}
2224 {{end}}
25+ {{if .Config.HideVariables | not }}
2326 // Exported variables (public references)
2427 {{range .Plugin.Variables}}
2528 // See docs at https://godoc.org/{{$pluginPackage}}#{{ .Name }}
2629 {{ .Name }} {{if $useVarReference}}*{{end}}{{ .Signature }}{{end}}
30+ {{end}}
2731}
32+
33+ {{if $interface}}
34+ // {{.Config.OutputName}} wraps functions exported by plugin {{.Plugin.Package}}
35+ //
36+ // See docs at https://godoc.org/{{$pluginPackage}}
37+ type {{.Config.OutputName}} interface {
38+ // Exported functions
39+ {{range .Plugin.Functions}}{{ .Name }} {{ .TrimmedSignature }}
40+ {{end}}
41+ }
42+ {{end}}
43+
2844{{range .Plugin.Functions}}
2945// {{.Name}} function was exported from plugin {{$pluginPackage}} symbol '{{.Name}}'
3046//
3147// See docs at https://godoc.org/{{$pluginPackage}}#{{.Name}}
32- func (p *{{$receiver}}) {{.Name}}{{.TrimmedSignature}} {
48+ func (p *{{if $interface}}_{{end}}{{ $receiver}}) {{.Name}}{{.TrimmedSignature}} {
3349 {{ if .ReturnsVoid | not }}return {{ end }}p._{{ .Name }}({{ .ArgumentsCall }})
3450}
3551{{end}}
3652
3753// String returnes textual representation of the wrapper. It provides info on exported symbols and variables.
38- func (p *{{$receiver}}) String() string {
54+ func (p *{{if $interface}}_{{end}}{{ $receiver}}) String() string {
3955 var lines []string
40- lines = append(lines, "Wrapper info :")
56+ lines = append(lines, "{{if $interface}}Interface{{else}}Struct{{end}} {{.Config.OutputName}} :")
4157 lines = append(lines, "\t- Generated on: {{.Build.Date}}")
4258 lines = append(lines, "\t- Command: {{.Build.Command}}")
4359 lines = append(lines, "\nPlugin info:")
@@ -47,19 +63,26 @@ func (p *{{$receiver}}) String() string {
4763 lines = append(lines, "\nExported functions ({{.Plugin.Functions|len}}):")
4864 {{ range .Plugin.Functions }}lines = append(lines, "\t- {{.Name}} {{ .Signature }}")
4965 {{ end }}
66+ {{ if .Config.HideVariables | not }}
67+ {{ if .Plugin.Variables }}
5068 lines = append(lines, "\nExported variables ({{.Plugin.Variables|len}}):")
5169 {{ range .Plugin.Variables }}lines = append(lines, "\t- {{.Name}} {{ .Signature }}")
5270 {{ end }}
53- lines = append(lines, "\nPlugin imports:"){{ range $key, $val := .Plugin.ImportsNames }}{{if isStandardImport $val $key | not }}
71+ {{end}}
72+ {{end}}
73+ {{$imports := .Plugin.NamedImports}}
74+ {{if $imports}}
75+ lines = append(lines, "\nPlugin imports:"){{ range $key, $val := $imports }}
5476 lines = append(lines, "\t- {{ $val }} as {{ $key }}")
55- {{end}}{{ end }}
77+ {{end}}
78+ {{end}}
5679 return strings.Join(lines, "\n")
5780}
5881
59- // Load {{.Config.OutputName}} loads plugin from the given path and binds symbols (variables and functions)
60- // to the {{.Config.OutputName}} struct. {{if .Config.DereferenceVariables}}All variables are derefenences. {{end}}
61- {{ if .Config.CheckSha256 }}// When plugin is loaded sha256 checksum is computed and checked against precomputed once. On mismatch error is returned. {{end}}
62- func Bind{{.Config.OutputName}}(path string) (* {{.Config.OutputName}}, error) {
82+ // Bind {{.Config.OutputName}} loads plugin from the given path and binds {{if .Config.AsInterface}}functions{{else}} symbols (variables and functions){{end}}
83+ // to the {{if .Config.AsInterface}}struct implementing {{.Config. OutputName}} interface{{else}}{{.Config.OutputName}} struct{{end}} . {{if .Config.HideVariables | not}}{{if .Config. DereferenceVariables}}All variables are derefenences. {{end}} {{end}}
84+ {{ if .Config.CheckSha256 }}// When plugin is loaded sha256 checksum is computed and checked against precomputed once. On mismatch error is returned.
85+ {{end}} func Bind{{.Config.OutputName}}(path string) ({{if.Config.AsInterface|not}}*{{end}} {{.Config.OutputName}}, error) {
6386 p, err := plugin.Open(path)
6487
6588 if err != nil {
@@ -92,7 +115,7 @@ func Bind{{.Config.OutputName}}(path string) (*{{.Config.OutputName}}, error) {
92115 return nil, fmt.Errorf("Sha256 checksum mismatch (expected: {{.Plugin.Sha256}}, actual: %s)", checksum)
93116 }{{ end }}
94117
95- ret := new({{.Config.OutputName}})
118+ ret := new({{if .Config.AsInterface}}_{{end}}{{ .Config.OutputName}})
96119 {{range .Plugin.Functions}}
97120 func{{ .Name }}, err := p.Lookup("{{ .Name }}")
98121 if err != nil {
@@ -104,7 +127,9 @@ func Bind{{.Config.OutputName}}(path string) (*{{.Config.OutputName}}, error) {
104127 } else {
105128 return nil, fmt.Errorf("Could not import function '{{ .Name }}', incompatible types '{{ .Signature }}' and '%s'", reflect.TypeOf(func{{ .Name }}))
106129 }
107- {{end}}{{range .Plugin.Variables}}
130+ {{end}}
131+ {{if .Config.HideVariables|not}}
132+ {{range .Plugin.Variables}}
108133 var{{ .Name }}, err := p.Lookup("{{ .Name }}")
109134 if err != nil {
110135 return nil, fmt.Errorf("Could not import variable '{{ .Name }}', symbol not found: %s", err)
@@ -116,6 +141,8 @@ func Bind{{.Config.OutputName}}(path string) (*{{.Config.OutputName}}, error) {
116141 return nil, fmt.Errorf("Could not import variable '{{ .Name }}', incompatible types '{{ .Signature }}' and '%s'", reflect.TypeOf(var{{ .Name }}))
117142 }
118143 {{end}}
144+ {{end}}
145+
119146 return ret, nil
120147}
121148`
0 commit comments