@@ -19,6 +19,7 @@ import (
19
19
"fmt"
20
20
"io"
21
21
"os/exec"
22
+ "path"
22
23
"path/filepath"
23
24
"sort"
24
25
"strings"
@@ -53,6 +54,8 @@ type context struct {
53
54
inputFile string // input file with VPP API in JSON
54
55
outputFile string // output file with generated Go package
55
56
57
+ importPrefix string // defines import path prefix for importing types
58
+
56
59
inputData []byte // contents of the input file
57
60
58
61
includeAPIVersion bool // include constant with API version string
@@ -99,13 +102,16 @@ func newContext(inputFile, outputDir string) (*context, error) {
99
102
}
100
103
101
104
func generatePackage (ctx * context , w io.Writer ) error {
105
+ logf ("----------------------------" )
102
106
logf ("generating package %q" , ctx .packageName )
107
+ logf ("----------------------------" )
103
108
104
109
fmt .Fprintln (w , "// Code generated by GoVPP's binapi-generator. DO NOT EDIT." )
105
110
fmt .Fprintf (w , "// source: %s\n " , ctx .inputFile )
106
111
fmt .Fprintln (w )
107
112
108
113
generateHeader (ctx , w )
114
+ generateImports (ctx , w )
109
115
110
116
// generate module desc
111
117
fmt .Fprintln (w , "const (" )
@@ -234,6 +240,9 @@ func generateHeader(ctx *context, w io.Writer) {
234
240
fmt .Fprintf (w , "package %s\n " , ctx .packageName )
235
241
fmt .Fprintln (w )
236
242
243
+ }
244
+
245
+ func generateImports (ctx * context , w io.Writer ) {
237
246
fmt .Fprintln (w , "import (" )
238
247
fmt .Fprintf (w , "\t api \" %s\" \n " , govppApiImportPath )
239
248
fmt .Fprintf (w , "\t bytes \" %s\" \n " , "bytes" )
@@ -244,15 +253,18 @@ func generateHeader(ctx *context, w io.Writer) {
244
253
if len (ctx .packageData .Imports ) > 0 {
245
254
fmt .Fprintln (w )
246
255
for _ , imp := range getImports (ctx ) {
247
- impPkg := getImportPkg (filepath .Dir (ctx .outputFile ), imp )
248
- fmt .Fprintf (w , "\t %s \" %s\" \n " , imp , strings .TrimSpace (impPkg ))
256
+ importPath := path .Join (ctx .importPrefix , imp )
257
+ if importPath == "" {
258
+ importPath = getImportPath (filepath .Dir (ctx .outputFile ), imp )
259
+ }
260
+ fmt .Fprintf (w , "\t %s \" %s\" \n " , imp , strings .TrimSpace (importPath ))
249
261
}
250
262
}
251
263
fmt .Fprintln (w , ")" )
252
264
fmt .Fprintln (w )
253
265
}
254
266
255
- func getImportPkg (outputDir string , pkg string ) string {
267
+ func getImportPath (outputDir string , pkg string ) string {
256
268
absPath , _ := filepath .Abs (filepath .Join (outputDir , ".." , pkg ))
257
269
cmd := exec .Command ("go" , "list" , absPath )
258
270
var errbuf , outbuf bytes.Buffer
0 commit comments