Generator improvements
[govpp.git] / cmd / binapi-generator / parse.go
index 5dfbe91..07abebd 100644 (file)
@@ -23,11 +23,6 @@ import (
        "github.com/bennyscetbun/jsongo"
 )
 
-// toApiType returns name that is used as type reference in VPP binary API
-func toApiType(name string) string {
-       return fmt.Sprintf("vl_api_%s_t", name)
-}
-
 // parsePackage parses provided JSON data into objects prepared for code generation
 func parsePackage(ctx *context, jsonRoot *jsongo.JSONNode) (*Package, error) {
        logf(" %s contains: %d services, %d messages, %d types, %d enums, %d unions, %d aliases (version: %s)",
@@ -59,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")
@@ -75,6 +74,10 @@ func parsePackage(ctx *context, jsonRoot *jsongo.JSONNode) (*Package, error) {
                        pkg.RefMap[toApiType(alias.Name)] = alias.Name
                }
        }
+       // sort aliases to ensure consistent order
+       sort.Slice(pkg.Aliases, func(i, j int) bool {
+               return pkg.Aliases[i].Name < pkg.Aliases[j].Name
+       })
 
        // parse types
        types := jsonRoot.Map("types")
@@ -89,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")
@@ -103,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")
@@ -116,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")
@@ -130,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)
 
@@ -457,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)
                }
@@ -479,6 +493,11 @@ func parseService(ctx *context, svcName string, svcNode *jsongo.JSONNode) (*Serv
        return &svc, nil
 }
 
+// toApiType returns name that is used as type reference in VPP binary API
+func toApiType(name string) string {
+       return fmt.Sprintf("vl_api_%s_t", name)
+}
+
 // convertToGoType translates the VPP binary API type into Go type
 func convertToGoType(ctx *context, binapiType string) (typ string) {
        if t, ok := binapiTypes[binapiType]; ok {
@@ -488,9 +507,14 @@ func convertToGoType(ctx *context, binapiType string) (typ string) {
                // specific types (enums/types/unions)
                typ = camelCaseName(r)
        } else {
-               // fallback type
-               log.Warnf("found unknown VPP binary API type %q, using byte", binapiType)
-               typ = "byte"
+               switch binapiType {
+               case "bool", "string":
+                       typ = binapiType
+               default:
+                       // fallback type
+                       log.Warnf("found unknown VPP binary API type %q, using byte", binapiType)
+                       typ = "byte"
+               }
        }
        return typ
 }