summaryrefslogtreecommitdiffstats
path: root/cmd/binapi-generator
diff options
context:
space:
mode:
authorOndrej Fabry <ofabry@cisco.com>2020-03-18 08:52:42 +0100
committerOndrej Fabry <ofabry@cisco.com>2020-03-18 08:52:42 +0100
commitd4d3b9c6fa67fe454f8d8adc7502b3b645ecab10 (patch)
tree17f421e8bcfd5a3ba15372bd6b5830946ac6cd2f /cmd/binapi-generator
parent2df59463fcbb1a5aec2173712b32fb9740157a9d (diff)
Release 0.3.0v0.3.0
- regenerate examples/binapi for latest VPP from stable/2001 - add import-prefix flag to set custom prefix (fallbacks to using go list) Change-Id: Ib09f134cf9662e348be2575448964de2b9e5c1ee Signed-off-by: Ondrej Fabry <ofabry@cisco.com>
Diffstat (limited to 'cmd/binapi-generator')
-rw-r--r--cmd/binapi-generator/generate.go18
-rw-r--r--cmd/binapi-generator/generate_test.go8
-rw-r--r--cmd/binapi-generator/main.go2
3 files changed, 21 insertions, 7 deletions
diff --git a/cmd/binapi-generator/generate.go b/cmd/binapi-generator/generate.go
index fb6cee5..a8de5d5 100644
--- a/cmd/binapi-generator/generate.go
+++ b/cmd/binapi-generator/generate.go
@@ -19,6 +19,7 @@ import (
"fmt"
"io"
"os/exec"
+ "path"
"path/filepath"
"sort"
"strings"
@@ -53,6 +54,8 @@ type context struct {
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
@@ -99,13 +102,16 @@ func newContext(inputFile, outputDir string) (*context, error) {
}
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 (")
@@ -234,6 +240,9 @@ func generateHeader(ctx *context, w io.Writer) {
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")
@@ -244,15 +253,18 @@ func generateHeader(ctx *context, w io.Writer) {
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
diff --git a/cmd/binapi-generator/generate_test.go b/cmd/binapi-generator/generate_test.go
index bff5406..4bec874 100644
--- a/cmd/binapi-generator/generate_test.go
+++ b/cmd/binapi-generator/generate_test.go
@@ -45,7 +45,7 @@ func TestGenerateFromFile(t *testing.T) {
outDir := "test_output_directory"
// remove directory created during test
defer os.RemoveAll(outDir)
- err := generateFromFile("testdata/acl.api.json", outDir)
+ err := generateFromFile("testdata/acl.api.json", outDir, nil)
Expect(err).ShouldNot(HaveOccurred())
fileInfo, err := os.Stat(outDir + "/acl/acl.ba.go")
Expect(err).ShouldNot(HaveOccurred())
@@ -56,7 +56,7 @@ func TestGenerateFromFile(t *testing.T) {
func TestGenerateFromFileInputError(t *testing.T) {
RegisterTestingT(t)
outDir := "test_output_directory"
- err := generateFromFile("testdata/nonexisting.json", outDir)
+ err := generateFromFile("testdata/nonexisting.json", outDir, nil)
Expect(err).Should(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("invalid input file name"))
}
@@ -64,7 +64,7 @@ func TestGenerateFromFileInputError(t *testing.T) {
func TestGenerateFromFileReadJsonError(t *testing.T) {
RegisterTestingT(t)
outDir := "test_output_directory"
- err := generateFromFile("testdata/input-read-json-error.json", outDir)
+ err := generateFromFile("testdata/input-read-json-error.json", outDir, nil)
Expect(err).Should(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("invalid input file name"))
}
@@ -79,7 +79,7 @@ func TestGenerateFromFileGeneratePackageError(t *testing.T) {
}
os.RemoveAll(outDir)
}()
- err := generateFromFile("testdata/input-generate-error.json", outDir)
+ err := generateFromFile("testdata/input-generate-error.json", outDir, nil)
Expect(err).Should(HaveOccurred())
}
diff --git a/cmd/binapi-generator/main.go b/cmd/binapi-generator/main.go
index e0e2f08..8bf765b 100644
--- a/cmd/binapi-generator/main.go
+++ b/cmd/binapi-generator/main.go
@@ -41,6 +41,7 @@ var (
includeServices = flag.Bool("include-services", true, "Include RPC service api and client implementation.")
includeComments = flag.Bool("include-comments", false, "Include JSON API source in comments for each object.")
includeBinapiNames = flag.Bool("include-binapi-names", false, "Include binary API names in struct tag.")
+ importPrefix = flag.String("import-prefix", "", "Define import path prefix to be used to import types.")
continueOnError = flag.Bool("continue-onerror", false, "Continue with next file on error.")
debugMode = flag.Bool("debug", os.Getenv("GOVPP_DEBUG") != "", "Enable debug mode.")
@@ -179,6 +180,7 @@ func generateFromFile(inputFile, outputDir string, typesPkgs []*context) error {
ctx.includeComments = *includeComments
ctx.includeBinapiNames = *includeBinapiNames
ctx.includeServices = *includeServices
+ ctx.importPrefix = *importPrefix
// read API definition from input file
ctx.inputData, err = ioutil.ReadFile(ctx.inputFile)