summaryrefslogtreecommitdiffstats
path: root/binapigen
diff options
context:
space:
mode:
authorOndrej Fabry <ofabry@cisco.com>2020-10-02 16:36:32 +0200
committerOndrej Fabry <ofabry@cisco.com>2020-10-02 16:36:32 +0200
commitc2456559a66107441addb96f673191bde09d6977 (patch)
tree30020a936849f78f432cf5b6fe0bbb0489e485b6 /binapigen
parentf751f3f845ef56bbcdb873d81a1c6edbc5a87853 (diff)
Check retval value and convert to error in generated RPC client code
Change-Id: I816b4802cb5fc46239f6db0480fa4cf3645fe2f0 Signed-off-by: Ondrej Fabry <ofabry@cisco.com>
Diffstat (limited to 'binapigen')
-rw-r--r--binapigen/binapigen.go28
-rw-r--r--binapigen/gen_http.go (renamed from binapigen/gen_rest.go)14
-rw-r--r--binapigen/gen_rpc.go12
-rw-r--r--binapigen/run.go3
4 files changed, 39 insertions, 18 deletions
diff --git a/binapigen/binapigen.go b/binapigen/binapigen.go
index 35a07d0..de6a804 100644
--- a/binapigen/binapigen.go
+++ b/binapigen/binapigen.go
@@ -243,8 +243,8 @@ func newStruct(gen *Generator, file *File, apitype vppapi.StructType) *Struct {
},
}
gen.structsByName[typ.Name] = typ
- for _, fieldType := range apitype.Fields {
- field := newField(gen, file, typ, fieldType)
+ for i, fieldType := range apitype.Fields {
+ field := newField(gen, file, typ, fieldType, i)
typ.Fields = append(typ.Fields, field)
}
return typ
@@ -276,8 +276,8 @@ func newUnion(gen *Generator, file *File, apitype vppapi.UnionType) *Union {
},
}
gen.unionsByName[typ.Name] = typ
- for _, fieldType := range apitype.Fields {
- field := newField(gen, file, typ, fieldType)
+ for i, fieldType := range apitype.Fields {
+ field := newField(gen, file, typ, fieldType, i)
typ.Fields = append(typ.Fields, field)
}
return typ
@@ -302,11 +302,12 @@ const (
msgTypeEvent // msg_id, client_index
)
-// message fields
+// common message fields
const (
fieldMsgID = "_vl_msg_id"
fieldClientIndex = "client_index"
fieldContext = "context"
+ fieldRetval = "retval"
)
// field options
@@ -343,7 +344,7 @@ func newMessage(gen *Generator, file *File, apitype vppapi.Message) *Message {
}
}
n++
- field := newField(gen, file, msg, fieldType)
+ field := newField(gen, file, msg, fieldType, n)
msg.Fields = append(msg.Fields, field)
}
return msg
@@ -394,12 +395,24 @@ func getMsgType(m vppapi.Message) (msgType, error) {
return typ, nil
}
+func getRetvalField(m *Message) *Field {
+ for _, field := range m.Fields {
+ if field.Name == fieldRetval {
+ return field
+ }
+ }
+ return nil
+}
+
// Field represents a field for message or struct/union types.
type Field struct {
vppapi.Field
GoName string
+ // Index defines field index in parent.
+ Index int
+
// DefaultValue is a default value of field or
// nil if default value is not defined for field.
DefaultValue interface{}
@@ -423,10 +436,11 @@ type Field struct {
FieldSizeFrom *Field
}
-func newField(gen *Generator, file *File, parent interface{}, apitype vppapi.Field) *Field {
+func newField(gen *Generator, file *File, parent interface{}, apitype vppapi.Field, index int) *Field {
typ := &Field{
Field: apitype,
GoName: camelCaseName(apitype.Name),
+ Index: index,
}
switch p := parent.(type) {
case *Message:
diff --git a/binapigen/gen_rest.go b/binapigen/gen_http.go
index 6ddb57a..4c9697e 100644
--- a/binapigen/gen_rest.go
+++ b/binapigen/gen_http.go
@@ -20,7 +20,7 @@ import (
)
func init() {
- RegisterPlugin("rest", GenerateREST)
+ RegisterPlugin("http", GenerateHTTP)
}
// library dependencies
@@ -30,16 +30,16 @@ const (
jsonPkg = GoImportPath("encoding/json")
)
-func GenerateREST(gen *Generator, file *File) *GenFile {
+func GenerateHTTP(gen *Generator, file *File) *GenFile {
if file.Service == nil {
return nil
}
logf("----------------------------")
- logf(" Generate REST - %s", file.Desc.Name)
+ logf(" Generate HTTP - %s", file.Desc.Name)
logf("----------------------------")
- filename := path.Join(file.FilenamePrefix, file.Desc.Name+"_rest.ba.go")
+ filename := path.Join(file.FilenamePrefix, file.Desc.Name+"_http.ba.go")
g := gen.NewGenFile(filename, file.GoImportPath)
g.file = file
@@ -51,15 +51,15 @@ func GenerateREST(gen *Generator, file *File) *GenFile {
// generate RPC service
if len(file.Service.RPCs) > 0 {
- genRESTHandler(g, file.Service)
+ genHTTPHandler(g, file.Service)
}
return g
}
-func genRESTHandler(g *GenFile, svc *Service) {
+func genHTTPHandler(g *GenFile, svc *Service) {
// generate handler constructor
- g.P("func RESTHandler(rpc ", serviceApiName, ") ", httpPkg.Ident("Handler"), " {")
+ g.P("func HTTPHandler(rpc ", serviceApiName, ") ", httpPkg.Ident("Handler"), " {")
g.P(" mux := ", httpPkg.Ident("NewServeMux"), "()")
// generate http handlers for rpc
diff --git a/binapigen/gen_rpc.go b/binapigen/gen_rpc.go
index ba23f4a..a5974c3 100644
--- a/binapigen/gen_rpc.go
+++ b/binapigen/gen_rpc.go
@@ -71,7 +71,7 @@ func GenerateRPC(gen *Generator, file *File) *GenFile {
func genService(g *GenFile, svc *Service) {
// generate comment
- g.P("// ", serviceApiName, " defines RPC service ", g.file.Desc.Name, ".")
+ g.P("// ", serviceApiName, " defines RPC service ", g.file.Desc.Name, ".")
// generate service interface
g.P("type ", serviceApiName, " interface {")
@@ -160,7 +160,15 @@ func genService(g *GenFile, svc *Service) {
g.P("out := new(", rpc.MsgReply.GoIdent, ")")
g.P("err := c.conn.Invoke(ctx, in, out)")
g.P("if err != nil { return nil, err }")
- g.P("return out, nil")
+ if retvalField := getRetvalField(rpc.MsgReply); retvalField != nil {
+ if fieldType := getFieldType(g, retvalField); fieldType == "int32" {
+ g.P("return out, ", govppApiPkg.Ident("RetvalToVPPApiError"), "(out.", retvalField.GoName, ")")
+ } else {
+ g.P("return out, ", govppApiPkg.Ident("RetvalToVPPApiError"), "(int32(out.", retvalField.GoName, "))")
+ }
+ } else {
+ g.P("return out, nil")
+ }
} else {
g.P("stream, err := c.conn.NewStream(ctx)")
g.P("if err != nil { return err }")
diff --git a/binapigen/run.go b/binapigen/run.go
index 8bb88fb..d632519 100644
--- a/binapigen/run.go
+++ b/binapigen/run.go
@@ -53,7 +53,7 @@ func run(apiDir string, filesToGenerate []string, opts Options, fn func(*Generat
if err != nil {
return fmt.Errorf("cannot resolve import path for output dir %s: %w", opts.OutputDir, err)
}
- logrus.Infof("resolved import path prefix: %s", opts.ImportPrefix)
+ logrus.Debugf("resolved import path prefix: %s", opts.ImportPrefix)
}
gen, err := New(opts, apiFiles, filesToGenerate)
@@ -73,7 +73,6 @@ func run(apiDir string, filesToGenerate []string, opts Options, fn func(*Generat
return err
}
}
-
if err = gen.Generate(); err != nil {
return err
}