|
| 1 | +package main |
| 2 | + |
| 3 | +// Autogenerated by github.com/wendigo/go-bind-plugin on 2016-11-12 19:45:36.989271091 +0100 CET, do not edit! |
| 4 | +// Command: go-bind-plugin -plugin-path ./internal/test_fixtures/generated/basic_plugin/plugin.so -plugin-package ./internal/test_fixtures/basic_plugin -output-name TestWrapper -output-path ./internal/test_fixtures/generated/basic_plugin/plugin.go -output-package main -sha256 -rebuild |
| 5 | +// |
| 6 | +// Plugin ./internal/test_fixtures/generated/basic_plugin/plugin.so info: |
| 7 | +// - package: github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin |
| 8 | +// - size: 2447824 bytes |
| 9 | +// - sha256: e3d68f431cbee5fd88dd41dca29d38f09c148d83ed668612a0e4813808280483 |
| 10 | + |
| 11 | +import ( |
| 12 | + "crypto/sha256" |
| 13 | + "encoding/hex" |
| 14 | + "fmt" |
| 15 | + "io" |
| 16 | + "os" |
| 17 | + "plugin" |
| 18 | + "reflect" |
| 19 | + "strings" |
| 20 | +) |
| 21 | + |
| 22 | +// TestWrapper wraps symbols (functions and variables) exported by plugin github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin |
| 23 | +// |
| 24 | +// See docs at https://godoc.org/github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin |
| 25 | +type TestWrapper struct { |
| 26 | + // Exported functions |
| 27 | + _NonReturningFunction func() |
| 28 | + _ReturningInt32 func() int32 |
| 29 | + _ReturningIntArray func() [3]int32 |
| 30 | + _ReturningStringSlice func() []string |
| 31 | + |
| 32 | + // Exported variables (public references) |
| 33 | + |
| 34 | +} |
| 35 | + |
| 36 | +// NonReturningFunction function was exported from plugin github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin symbol 'NonReturningFunction' |
| 37 | +// |
| 38 | +// See docs at https://godoc.org/github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin#NonReturningFunction |
| 39 | +func (p *TestWrapper) NonReturningFunction() { |
| 40 | + p._NonReturningFunction() |
| 41 | +} |
| 42 | + |
| 43 | +// ReturningInt32 function was exported from plugin github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin symbol 'ReturningInt32' |
| 44 | +// |
| 45 | +// See docs at https://godoc.org/github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin#ReturningInt32 |
| 46 | +func (p *TestWrapper) ReturningInt32() int32 { |
| 47 | + return p._ReturningInt32() |
| 48 | +} |
| 49 | + |
| 50 | +// ReturningIntArray function was exported from plugin github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin symbol 'ReturningIntArray' |
| 51 | +// |
| 52 | +// See docs at https://godoc.org/github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin#ReturningIntArray |
| 53 | +func (p *TestWrapper) ReturningIntArray() [3]int32 { |
| 54 | + return p._ReturningIntArray() |
| 55 | +} |
| 56 | + |
| 57 | +// ReturningStringSlice function was exported from plugin github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin symbol 'ReturningStringSlice' |
| 58 | +// |
| 59 | +// See docs at https://godoc.org/github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin#ReturningStringSlice |
| 60 | +func (p *TestWrapper) ReturningStringSlice() []string { |
| 61 | + return p._ReturningStringSlice() |
| 62 | +} |
| 63 | + |
| 64 | +// String returnes textual representation of the wrapper. It provides info on exported symbols and variables. |
| 65 | +func (p *TestWrapper) String() string { |
| 66 | + var lines []string |
| 67 | + lines = append(lines, "Struct TestWrapper:") |
| 68 | + lines = append(lines, "\t- Generated on: 2016-11-12 19:45:36.989271091 +0100 CET") |
| 69 | + lines = append(lines, "\t- Command: go-bind-plugin -plugin-path ./internal/test_fixtures/generated/basic_plugin/plugin.so -plugin-package ./internal/test_fixtures/basic_plugin -output-name TestWrapper -output-path ./internal/test_fixtures/generated/basic_plugin/plugin.go -output-package main -sha256 -rebuild") |
| 70 | + lines = append(lines, "\nPlugin info:") |
| 71 | + lines = append(lines, "\t- package: github.com/wendigo/go-bind-plugin/cli/internal/test_fixtures/basic_plugin") |
| 72 | + lines = append(lines, "\t- sha256 sum: e3d68f431cbee5fd88dd41dca29d38f09c148d83ed668612a0e4813808280483") |
| 73 | + lines = append(lines, "\t- size: 2447824 bytes") |
| 74 | + lines = append(lines, "\nExported functions (4):") |
| 75 | + lines = append(lines, "\t- NonReturningFunction func()") |
| 76 | + lines = append(lines, "\t- ReturningInt32 func() (int32)") |
| 77 | + lines = append(lines, "\t- ReturningIntArray func() ([3]int32)") |
| 78 | + lines = append(lines, "\t- ReturningStringSlice func() ([]string)") |
| 79 | + |
| 80 | + return strings.Join(lines, "\n") |
| 81 | +} |
| 82 | + |
| 83 | +// BindTestWrapper loads plugin from the given path and binds symbols (variables and functions) |
| 84 | +// to the TestWrapper struct. |
| 85 | +// When plugin is loaded sha256 checksum is computed and checked against precomputed once. On mismatch error is returned. |
| 86 | +func BindTestWrapper(path string) (*TestWrapper, error) { |
| 87 | + p, err := plugin.Open(path) |
| 88 | + |
| 89 | + if err != nil { |
| 90 | + return nil, fmt.Errorf("could not open plugin: %s", err) |
| 91 | + } |
| 92 | + |
| 93 | + fileChecksum := func(path string) (string, error) { |
| 94 | + hasher := sha256.New() |
| 95 | + |
| 96 | + file, err := os.Open(path) |
| 97 | + |
| 98 | + if err != nil { |
| 99 | + return "", err |
| 100 | + } |
| 101 | + defer file.Close() |
| 102 | + |
| 103 | + if _, err := io.Copy(hasher, file); err != nil { |
| 104 | + return "", err |
| 105 | + } |
| 106 | + |
| 107 | + return hex.EncodeToString(hasher.Sum(nil)), nil |
| 108 | + } |
| 109 | + |
| 110 | + checksum, err := fileChecksum(path) |
| 111 | + if err != nil { |
| 112 | + return nil, fmt.Errorf("could not calculate file %s checksum", path) |
| 113 | + } |
| 114 | + |
| 115 | + if checksum != "e3d68f431cbee5fd88dd41dca29d38f09c148d83ed668612a0e4813808280483" { |
| 116 | + return nil, fmt.Errorf("SHA256 checksum mismatch (expected: e3d68f431cbee5fd88dd41dca29d38f09c148d83ed668612a0e4813808280483, actual: %s)", checksum) |
| 117 | + } |
| 118 | + |
| 119 | + ret := new(TestWrapper) |
| 120 | + |
| 121 | + funcNonReturningFunction, err := p.Lookup("NonReturningFunction") |
| 122 | + if err != nil { |
| 123 | + return nil, fmt.Errorf("could not import function 'NonReturningFunction', symbol not found: %s", err) |
| 124 | + } |
| 125 | + |
| 126 | + if typed, ok := funcNonReturningFunction.(func()); ok { |
| 127 | + ret._NonReturningFunction = typed |
| 128 | + } else { |
| 129 | + return nil, fmt.Errorf("could not import function 'NonReturningFunction', incompatible types 'func()' and '%s'", reflect.TypeOf(funcNonReturningFunction)) |
| 130 | + } |
| 131 | + |
| 132 | + funcReturningInt32, err := p.Lookup("ReturningInt32") |
| 133 | + if err != nil { |
| 134 | + return nil, fmt.Errorf("could not import function 'ReturningInt32', symbol not found: %s", err) |
| 135 | + } |
| 136 | + |
| 137 | + if typed, ok := funcReturningInt32.(func() int32); ok { |
| 138 | + ret._ReturningInt32 = typed |
| 139 | + } else { |
| 140 | + return nil, fmt.Errorf("could not import function 'ReturningInt32', incompatible types 'func() (int32)' and '%s'", reflect.TypeOf(funcReturningInt32)) |
| 141 | + } |
| 142 | + |
| 143 | + funcReturningIntArray, err := p.Lookup("ReturningIntArray") |
| 144 | + if err != nil { |
| 145 | + return nil, fmt.Errorf("could not import function 'ReturningIntArray', symbol not found: %s", err) |
| 146 | + } |
| 147 | + |
| 148 | + if typed, ok := funcReturningIntArray.(func() [3]int32); ok { |
| 149 | + ret._ReturningIntArray = typed |
| 150 | + } else { |
| 151 | + return nil, fmt.Errorf("could not import function 'ReturningIntArray', incompatible types 'func() ([3]int32)' and '%s'", reflect.TypeOf(funcReturningIntArray)) |
| 152 | + } |
| 153 | + |
| 154 | + funcReturningStringSlice, err := p.Lookup("ReturningStringSlice") |
| 155 | + if err != nil { |
| 156 | + return nil, fmt.Errorf("could not import function 'ReturningStringSlice', symbol not found: %s", err) |
| 157 | + } |
| 158 | + |
| 159 | + if typed, ok := funcReturningStringSlice.(func() []string); ok { |
| 160 | + ret._ReturningStringSlice = typed |
| 161 | + } else { |
| 162 | + return nil, fmt.Errorf("could not import function 'ReturningStringSlice', incompatible types 'func() ([]string)' and '%s'", reflect.TypeOf(funcReturningStringSlice)) |
| 163 | + } |
| 164 | + |
| 165 | + return ret, nil |
| 166 | +} |
| 167 | + |
| 168 | + |
| 169 | +func main() { |
| 170 | + pl, err := BindTestWrapper("./internal/test_fixtures/generated/basic_plugin/plugin.so") |
| 171 | + |
| 172 | + if err != nil { |
| 173 | + fmt.Println(err) |
| 174 | + os.Exit(1) |
| 175 | + } |
| 176 | + |
| 177 | + fmt.Println(pl.ReturningIntArray()) |
| 178 | +} |
0 commit comments