"fmt"
"io"
"os/exec"
+ "path"
"path/filepath"
"sort"
"strings"
inputFileExt = ".api.json" // file extension of the VPP API files
outputFileExt = ".ba.go" // file extension of the Go generated files
- govppApiImportPath = "git.fd.io/govpp.git/api" // import path of the govpp API package
-
constModuleName = "ModuleName" // module name constant
constAPIVersion = "APIVersion" // API version constant
constVersionCrc = "VersionCrc" // version CRC constant
inputFile string // input file with VPP API in JSON
outputFile string // output file with generated Go package
+ importPrefix string // defines import path prefix for importing types
+
inputData []byte // contents of the input file
includeAPIVersion bool // include constant with API version string
}
func generatePackage(ctx *context, w io.Writer) error {
+ logf("----------------------------")
logf("generating package %q", ctx.packageName)
+ logf("----------------------------")
fmt.Fprintln(w, "// Code generated by GoVPP's binapi-generator. DO NOT EDIT.")
fmt.Fprintf(w, "// source: %s\n", ctx.inputFile)
fmt.Fprintln(w)
generateHeader(ctx, w)
+ generateImports(ctx, w)
// generate module desc
fmt.Fprintln(w, "const (")
fmt.Fprintf(w, "package %s\n", ctx.packageName)
fmt.Fprintln(w)
+}
+
+func generateImports(ctx *context, w io.Writer) {
fmt.Fprintln(w, "import (")
- fmt.Fprintf(w, "\tapi \"%s\"\n", govppApiImportPath)
- fmt.Fprintf(w, "\tbytes \"%s\"\n", "bytes")
- fmt.Fprintf(w, "\tcontext \"%s\"\n", "context")
- fmt.Fprintf(w, "\tio \"%s\"\n", "io")
- fmt.Fprintf(w, "\tstrconv \"%s\"\n", "strconv")
+ fmt.Fprintln(w, ` "bytes"`)
+ fmt.Fprintln(w, ` "context"`)
+ fmt.Fprintln(w, ` "io"`)
+ fmt.Fprintln(w, ` "strconv"`)
+ fmt.Fprintln(w)
+ fmt.Fprintf(w, "\tapi \"%s\"\n", "git.fd.io/govpp.git/api")
fmt.Fprintf(w, "\tstruc \"%s\"\n", "github.com/lunixbochs/struc")
if len(ctx.packageData.Imports) > 0 {
fmt.Fprintln(w)
for _, imp := range getImports(ctx) {
- impPkg := getImportPkg(filepath.Dir(ctx.outputFile), imp)
- fmt.Fprintf(w, "\t%s \"%s\"\n", imp, strings.TrimSpace(impPkg))
+ importPath := path.Join(ctx.importPrefix, imp)
+ if importPath == "" {
+ importPath = getImportPath(filepath.Dir(ctx.outputFile), imp)
+ }
+ fmt.Fprintf(w, "\t%s \"%s\"\n", imp, strings.TrimSpace(importPath))
}
}
fmt.Fprintln(w, ")")
fmt.Fprintln(w)
}
-func getImportPkg(outputDir string, pkg string) string {
+func getImportPath(outputDir string, pkg string) string {
absPath, _ := filepath.Abs(filepath.Join(outputDir, "..", pkg))
cmd := exec.Command("go", "list", absPath)
var errbuf, outbuf bytes.Buffer