diff options
author | Rastislav Szabo <raszabo@cisco.com> | 2019-01-31 07:28:52 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@fd.io> | 2019-01-31 07:28:52 +0000 |
commit | eb571ee5c0cf5a5194d6ae9aaa0be6e24ea2a943 (patch) | |
tree | 2743ea4285fe8dd9ffbc243b76441e8ca695c0e2 /cmd | |
parent | f1f9fe44d54eec75f6484d0b91769204b6812fb5 (diff) | |
parent | 6476a2b64a2e1ea6c0d695127d726a348cc5c99b (diff) |
Merge "Generator improvements"
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/binapi-generator/generate.go | 35 | ||||
-rw-r--r-- | cmd/binapi-generator/objects.go | 90 | ||||
-rw-r--r-- | cmd/binapi-generator/parse.go | 39 |
3 files changed, 93 insertions, 71 deletions
diff --git a/cmd/binapi-generator/generate.go b/cmd/binapi-generator/generate.go index e165c42..48c3a41 100644 --- a/cmd/binapi-generator/generate.go +++ b/cmd/binapi-generator/generate.go @@ -93,9 +93,9 @@ func generatePackage(ctx *context, w *bufio.Writer) error { // generate services if len(ctx.packageData.Services) > 0 { generateServices(ctx, w, ctx.packageData.Services) - } - // TODO: generate implementation for Services interface + // TODO: generate implementation for Services interface + } // generate enums if len(ctx.packageData.Enums) > 0 { @@ -140,16 +140,15 @@ func generatePackage(ctx *context, w *bufio.Writer) error { for _, msg := range ctx.packageData.Messages { generateMessage(ctx, w, &msg) } - } - // generate message registrations - fmt.Fprintln(w) - fmt.Fprintln(w, "func init() {") - for _, msg := range ctx.packageData.Messages { - name := camelCaseName(msg.Name) - fmt.Fprintf(w, "\tapi.RegisterMessage((*%s)(nil), \"%s\")\n", name, ctx.moduleName+"."+name) + // generate message registrations + fmt.Fprintln(w, "func init() {") + for _, msg := range ctx.packageData.Messages { + name := camelCaseName(msg.Name) + fmt.Fprintf(w, "\tapi.RegisterMessage((*%s)(nil), \"%s\")\n", name, ctx.moduleName+"."+name) + } + fmt.Fprintln(w, "}") } - fmt.Fprintln(w, "}") // flush the data: if err := w.Flush(); err != nil { @@ -181,13 +180,13 @@ func generateHeader(ctx *context, w io.Writer) { fmt.Fprintf(w, "\t%3d %s\n", num, obj) } } - printObjNum("message", len(ctx.packageData.Messages)) - printObjNum("type", len(ctx.packageData.Types)) - printObjNum("alias", len(ctx.packageData.Aliases)) + + printObjNum("service", len(ctx.packageData.Services)) printObjNum("enum", len(ctx.packageData.Enums)) + printObjNum("alias", len(ctx.packageData.Aliases)) + printObjNum("type", len(ctx.packageData.Types)) printObjNum("union", len(ctx.packageData.Unions)) - printObjNum("service", len(ctx.packageData.Services)) - fmt.Fprintln(w) + printObjNum("message", len(ctx.packageData.Messages)) fmt.Fprintln(w, "*/") fmt.Fprintf(w, "package %s\n", ctx.packageName) fmt.Fprintln(w) @@ -416,6 +415,10 @@ func (u *%[1]s) String() string { func generateUnionGetterSetter(w io.Writer, structName string, getterField, getterStruct string) { fmt.Fprintf(w, ` +func %[1]s%[2]s(a %[3]s) (u %[1]s) { + u.Set%[2]s(a) + return +} func (u *%[1]s) Set%[2]s(a %[3]s) { var b = new(bytes.Buffer) if err := struc.Pack(b, &a); err != nil { @@ -530,6 +533,8 @@ func generateMessage(ctx *context, w io.Writer, msg *Message) { // generate message type getter method generateMessageTypeGetter(w, name, msgType) + + fmt.Fprintln(w) } // generateField writes generated code for the field into w diff --git a/cmd/binapi-generator/objects.go b/cmd/binapi-generator/objects.go index 97318cb..4b424f5 100644 --- a/cmd/binapi-generator/objects.go +++ b/cmd/binapi-generator/objects.go @@ -1,34 +1,48 @@ package main -import "strings" +import ( + "strings" +) // Package represents collection of objects parsed from VPP binary API JSON data type Package struct { APIVersion string + Services []Service Enums []Enum - Unions []Union - Types []Type Aliases []Alias + Types []Type + Unions []Union Messages []Message - Services []Service RefMap map[string]string } -// MessageType represents the type of a VPP message -type MessageType int +// Service represents VPP binary API service +type Service struct { + Name string + RequestType string + ReplyType string + Stream bool + Events []string +} -const ( - requestMessage MessageType = iota // VPP request message - replyMessage // VPP reply message - eventMessage // VPP event message - otherMessage // other VPP message -) +// Enum represents VPP binary API enum +type Enum struct { + Name string + Type string + Entries []EnumEntry +} -// Message represents VPP binary API message -type Message struct { +// EnumEntry represents VPP binary API enum entry +type EnumEntry struct { + Name string + Value interface{} +} + +// Alias represents VPP binary API alias +type Alias struct { Name string - CRC string - Fields []Field + Type string + Length int } // Type represents VPP binary API type @@ -38,20 +52,30 @@ type Type struct { Fields []Field } -// Alias represents VPP binary API alias -type Alias struct { +// Union represents VPP binary API union +type Union struct { Name string - Type string - Length int + CRC string + Fields []Field } -// Union represents VPP binary API union -type Union struct { +// Message represents VPP binary API message +type Message struct { Name string CRC string Fields []Field } +// MessageType represents the type of a VPP message +type MessageType int + +const ( + requestMessage MessageType = iota // VPP request message + replyMessage // VPP reply message + eventMessage // VPP event message + otherMessage // other VPP message +) + // Field represents VPP binary API object field type Field struct { Name string @@ -64,28 +88,6 @@ func (f *Field) IsArray() bool { return f.Length > 0 || f.SizeFrom != "" } -// Enum represents VPP binary API enum -type Enum struct { - Name string - Type string - Entries []EnumEntry -} - -// EnumEntry represents VPP binary API enum entry -type EnumEntry struct { - Name string - Value interface{} -} - -// Service represents VPP binary API service -type Service struct { - Name string - RequestType string - ReplyType string - Stream bool - Events []string -} - func (s Service) MethodName() string { reqTyp := camelCaseName(s.RequestType) diff --git a/cmd/binapi-generator/parse.go b/cmd/binapi-generator/parse.go index 5bb3e8e..07abebd 100644 --- a/cmd/binapi-generator/parse.go +++ b/cmd/binapi-generator/parse.go @@ -54,6 +54,10 @@ func parsePackage(ctx *context, jsonRoot *jsongo.JSONNode) (*Package, error) { pkg.Enums[i] = *enum pkg.RefMap[toApiType(enum.Name)] = enum.Name } + // sort enums + sort.SliceStable(pkg.Enums, func(i, j int) bool { + return pkg.Enums[i].Name < pkg.Enums[j].Name + }) // parse aliases aliases := jsonRoot.Map("aliases") @@ -88,6 +92,10 @@ func parsePackage(ctx *context, jsonRoot *jsongo.JSONNode) (*Package, error) { pkg.Types[i] = *typ pkg.RefMap[toApiType(typ.Name)] = typ.Name } + // sort types + sort.SliceStable(pkg.Types, func(i, j int) bool { + return pkg.Types[i].Name < pkg.Types[j].Name + }) // parse unions unions := jsonRoot.Map("unions") @@ -102,6 +110,10 @@ func parsePackage(ctx *context, jsonRoot *jsongo.JSONNode) (*Package, error) { pkg.Unions[i] = *union pkg.RefMap[toApiType(union.Name)] = union.Name } + // sort unions + sort.SliceStable(pkg.Unions, func(i, j int) bool { + return pkg.Unions[i].Name < pkg.Unions[j].Name + }) // parse messages messages := jsonRoot.Map("messages") @@ -115,6 +127,10 @@ func parsePackage(ctx *context, jsonRoot *jsongo.JSONNode) (*Package, error) { } pkg.Messages[i] = *msg } + // sort messages + sort.SliceStable(pkg.Messages, func(i, j int) bool { + return pkg.Messages[i].Name < pkg.Messages[j].Name + }) // parse services services := jsonRoot.Map("services") @@ -129,16 +145,15 @@ func parsePackage(ctx *context, jsonRoot *jsongo.JSONNode) (*Package, error) { } pkg.Services[i] = *svc } - - // sort services - sort.Slice(pkg.Services, func(i, j int) bool { - // dumps first - if pkg.Services[i].Stream != pkg.Services[j].Stream { - return pkg.Services[i].Stream - } - return pkg.Services[i].RequestType < pkg.Services[j].RequestType - }) } + // sort services + sort.Slice(pkg.Services, func(i, j int) bool { + // dumps first + if pkg.Services[i].Stream != pkg.Services[j].Stream { + return pkg.Services[i].Stream + } + return pkg.Services[i].RequestType < pkg.Services[j].RequestType + }) printPackage(&pkg) @@ -456,20 +471,20 @@ func parseService(ctx *context, svcName string, svcNode *jsongo.JSONNode) (*Serv // validate service if svc.IsEventService() { if !strings.HasPrefix(svc.RequestType, "want_") { - log.Warnf("Unusual EVENTS SERVICE: %+v\n"+ + log.Debugf("Unusual EVENTS SERVICE: %+v\n"+ "- events service %q does not have 'want_' prefix in request.", svc, svc.Name) } } else if svc.IsDumpService() { if !strings.HasSuffix(svc.RequestType, "_dump") || !strings.HasSuffix(svc.ReplyType, "_details") { - log.Warnf("Unusual STREAM SERVICE: %+v\n"+ + log.Debugf("Unusual STREAM SERVICE: %+v\n"+ "- stream service %q does not have '_dump' suffix in request or reply does not have '_details' suffix.", svc, svc.Name) } } else if svc.IsRequestService() { if !strings.HasSuffix(svc.ReplyType, "_reply") { - log.Warnf("Unusual REQUEST SERVICE: %+v\n"+ + log.Debugf("Unusual REQUEST SERVICE: %+v\n"+ "- service %q does not have '_reply' suffix in reply.", svc, svc.Name) } |