Code Review
/
govpp.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
review
|
tree
raw
|
inline
| side by side
Release 0.3.0
[govpp.git]
/
cmd
/
binapi-generator
/
parse.go
diff --git
a/cmd/binapi-generator/parse.go
b/cmd/binapi-generator/parse.go
index
0e4f3ad
..
6598b7b
100644
(file)
--- a/
cmd/binapi-generator/parse.go
+++ b/
cmd/binapi-generator/parse.go
@@
-63,7
+63,8
@@
const (
// field meta info
const (
// field meta info
const (
- fieldMetaLimit = "limit"
+ fieldMetaLimit = "limit"
+ fieldMetaDefault = "default"
)
// module options
)
// module options
@@
-72,10
+73,11
@@
const (
)
// parsePackage parses provided JSON data into objects prepared for code generation
)
// parsePackage parses provided JSON data into objects prepared for code generation
-func parsePackage(ctx *context, jsonRoot *jsongo.
JSON
Node) (*Package, error) {
+func parsePackage(ctx *context, jsonRoot *jsongo.Node) (*Package, error) {
pkg := Package{
pkg := Package{
- Name: ctx.packageName,
- RefMap: make(map[string]string),
+ Name: ctx.packageName,
+ RefMap: make(map[string]string),
+ Imports: map[string]Import{},
}
// parse CRC for API version
}
// parse CRC for API version
@@
-98,7
+100,7
@@
func parsePackage(ctx *context, jsonRoot *jsongo.JSONNode) (*Package, error) {
// parse enums
enums := jsonRoot.Map(objEnums)
// parse enums
enums := jsonRoot.Map(objEnums)
- pkg.Enums = make([]Enum,
enums.Len()
)
+ pkg.Enums = make([]Enum,
0
)
for i := 0; i < enums.Len(); i++ {
enumNode := enums.At(i)
for i := 0; i < enums.Len(); i++ {
enumNode := enums.At(i)
@@
-106,8
+108,14
@@
func parsePackage(ctx *context, jsonRoot *jsongo.JSONNode) (*Package, error) {
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
- pkg.Enums[i] = *enum
- pkg.RefMap[toApiType(enum.Name)] = enum.Name
+
+ enumApi := toApiType(enum.Name)
+ if _, ok := pkg.RefMap[enumApi]; ok {
+ logf("enum %v already known", enumApi)
+ continue
+ }
+ pkg.RefMap[enumApi] = enum.Name
+ pkg.Enums = append(pkg.Enums, *enum)
}
// sort enums
sort.SliceStable(pkg.Enums, func(i, j int) bool {
}
// sort enums
sort.SliceStable(pkg.Enums, func(i, j int) bool {
@@
-117,16
+125,22
@@
func parsePackage(ctx *context, jsonRoot *jsongo.JSONNode) (*Package, error) {
// parse aliases
aliases := jsonRoot.Map(objAliases)
if aliases.GetType() == jsongo.TypeMap {
// parse aliases
aliases := jsonRoot.Map(objAliases)
if aliases.GetType() == jsongo.TypeMap {
- pkg.Aliases = make([]Alias,
aliases.Len()
)
- for
i
, key := range aliases.GetKeys() {
+ pkg.Aliases = make([]Alias,
0
)
+ for
_
, key := range aliases.GetKeys() {
aliasNode := aliases.At(key)
alias, err := parseAlias(ctx, key.(string), aliasNode)
if err != nil {
return nil, err
}
aliasNode := aliases.At(key)
alias, err := parseAlias(ctx, key.(string), aliasNode)
if err != nil {
return nil, err
}
- pkg.Aliases[i] = *alias
- pkg.RefMap[toApiType(alias.Name)] = alias.Name
+
+ aliasApi := toApiType(alias.Name)
+ if _, ok := pkg.RefMap[aliasApi]; ok {
+ logf("alias %v already known", aliasApi)
+ continue
+ }
+ pkg.RefMap[aliasApi] = alias.Name
+ pkg.Aliases = append(pkg.Aliases, *alias)
}
}
// sort aliases to ensure consistent order
}
}
// sort aliases to ensure consistent order
@@
-136,7
+150,7
@@
func parsePackage(ctx *context, jsonRoot *jsongo.JSONNode) (*Package, error) {
// parse types
types := jsonRoot.Map(objTypes)
// parse types
types := jsonRoot.Map(objTypes)
- pkg.Types = make([]Type,
types.Len()
)
+ pkg.Types = make([]Type,
0
)
for i := 0; i < types.Len(); i++ {
typNode := types.At(i)
for i := 0; i < types.Len(); i++ {
typNode := types.At(i)
@@
-144,8
+158,14
@@
func parsePackage(ctx *context, jsonRoot *jsongo.JSONNode) (*Package, error) {
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
- pkg.Types[i] = *typ
- pkg.RefMap[toApiType(typ.Name)] = typ.Name
+
+ typApi := toApiType(typ.Name)
+ if _, ok := pkg.RefMap[typApi]; ok {
+ logf("type %v already known", typApi)
+ continue
+ }
+ pkg.RefMap[typApi] = typ.Name
+ pkg.Types = append(pkg.Types, *typ)
}
// sort types
sort.SliceStable(pkg.Types, func(i, j int) bool {
}
// sort types
sort.SliceStable(pkg.Types, func(i, j int) bool {
@@
-154,7
+174,7
@@
func parsePackage(ctx *context, jsonRoot *jsongo.JSONNode) (*Package, error) {
// parse unions
unions := jsonRoot.Map(objUnions)
// parse unions
unions := jsonRoot.Map(objUnions)
- pkg.Unions = make([]Union,
unions.Len()
)
+ pkg.Unions = make([]Union,
0
)
for i := 0; i < unions.Len(); i++ {
unionNode := unions.At(i)
for i := 0; i < unions.Len(); i++ {
unionNode := unions.At(i)
@@
-162,8
+182,14
@@
func parsePackage(ctx *context, jsonRoot *jsongo.JSONNode) (*Package, error) {
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
- pkg.Unions[i] = *union
- pkg.RefMap[toApiType(union.Name)] = union.Name
+
+ unionApi := toApiType(union.Name)
+ if _, ok := pkg.RefMap[unionApi]; ok {
+ logf("union %v already known", unionApi)
+ continue
+ }
+ pkg.RefMap[unionApi] = union.Name
+ pkg.Unions = append(pkg.Unions, *union)
}
// sort unions
sort.SliceStable(pkg.Unions, func(i, j int) bool {
}
// sort unions
sort.SliceStable(pkg.Unions, func(i, j int) bool {
@@
-216,7
+242,7
@@
func parsePackage(ctx *context, jsonRoot *jsongo.JSONNode) (*Package, error) {
}
// parseEnum parses VPP binary API enum object from JSON node
}
// parseEnum parses VPP binary API enum object from JSON node
-func parseEnum(ctx *context, enumNode *jsongo.
JSON
Node) (*Enum, error) {
+func parseEnum(ctx *context, enumNode *jsongo.Node) (*Enum, error) {
if enumNode.Len() == 0 || enumNode.At(0).GetType() != jsongo.TypeValue {
return nil, errors.New("invalid JSON for enum specified")
}
if enumNode.Len() == 0 || enumNode.At(0).GetType() != jsongo.TypeValue {
return nil, errors.New("invalid JSON for enum specified")
}
@@
-261,7
+287,7
@@
func parseEnum(ctx *context, enumNode *jsongo.JSONNode) (*Enum, error) {
}
// parseUnion parses VPP binary API union object from JSON node
}
// parseUnion parses VPP binary API union object from JSON node
-func parseUnion(ctx *context, unionNode *jsongo.
JSON
Node) (*Union, error) {
+func parseUnion(ctx *context, unionNode *jsongo.Node) (*Union, error) {
if unionNode.Len() == 0 || unionNode.At(0).GetType() != jsongo.TypeValue {
return nil, errors.New("invalid JSON for union specified")
}
if unionNode.Len() == 0 || unionNode.At(0).GetType() != jsongo.TypeValue {
return nil, errors.New("invalid JSON for union specified")
}
@@
-298,7
+324,7
@@
func parseUnion(ctx *context, unionNode *jsongo.JSONNode) (*Union, error) {
}
// parseType parses VPP binary API type object from JSON node
}
// parseType parses VPP binary API type object from JSON node
-func parseType(ctx *context, typeNode *jsongo.
JSON
Node) (*Type, error) {
+func parseType(ctx *context, typeNode *jsongo.Node) (*Type, error) {
if typeNode.Len() == 0 || typeNode.At(0).GetType() != jsongo.TypeValue {
return nil, errors.New("invalid JSON for type specified")
}
if typeNode.Len() == 0 || typeNode.At(0).GetType() != jsongo.TypeValue {
return nil, errors.New("invalid JSON for type specified")
}
@@
-335,7
+361,7
@@
func parseType(ctx *context, typeNode *jsongo.JSONNode) (*Type, error) {
}
// parseAlias parses VPP binary API alias object from JSON node
}
// parseAlias parses VPP binary API alias object from JSON node
-func parseAlias(ctx *context, aliasName string, aliasNode *jsongo.
JSON
Node) (*Alias, error) {
+func parseAlias(ctx *context, aliasName string, aliasNode *jsongo.Node) (*Alias, error) {
if aliasNode.Len() == 0 || aliasNode.At(aliasTypeField).GetType() != jsongo.TypeValue {
return nil, errors.New("invalid JSON for alias specified")
}
if aliasNode.Len() == 0 || aliasNode.At(aliasTypeField).GetType() != jsongo.TypeValue {
return nil, errors.New("invalid JSON for alias specified")
}
@@
-366,7
+392,7
@@
func parseAlias(ctx *context, aliasName string, aliasNode *jsongo.JSONNode) (*Al
}
// parseMessage parses VPP binary API message object from JSON node
}
// parseMessage parses VPP binary API message object from JSON node
-func parseMessage(ctx *context, msgNode *jsongo.
JSON
Node) (*Message, error) {
+func parseMessage(ctx *context, msgNode *jsongo.Node) (*Message, error) {
if msgNode.Len() == 0 || msgNode.At(0).GetType() != jsongo.TypeValue {
return nil, errors.New("invalid JSON for message specified")
}
if msgNode.Len() == 0 || msgNode.At(0).GetType() != jsongo.TypeValue {
return nil, errors.New("invalid JSON for message specified")
}
@@
-404,7
+430,7
@@
func parseMessage(ctx *context, msgNode *jsongo.JSONNode) (*Message, error) {
}
// parseField parses VPP binary API object field from JSON node
}
// parseField parses VPP binary API object field from JSON node
-func parseField(ctx *context, field *jsongo.
JSON
Node) (*Field, error) {
+func parseField(ctx *context, field *jsongo.Node) (*Field, error) {
if field.Len() < 2 || field.At(0).GetType() != jsongo.TypeValue || field.At(1).GetType() != jsongo.TypeValue {
return nil, errors.New("invalid JSON for field specified")
}
if field.Len() < 2 || field.At(0).GetType() != jsongo.TypeValue || field.At(1).GetType() != jsongo.TypeValue {
return nil, errors.New("invalid JSON for field specified")
}
@@
-424,13
+450,16
@@
func parseField(ctx *context, field *jsongo.JSONNode) (*Field, error) {
}
if field.Len() >= 3 {
}
if field.Len() >= 3 {
- if field.At(2).GetType() == jsongo.TypeValue {
+ switch field.At(2).GetType() {
+ case jsongo.TypeValue:
fieldLength, ok := field.At(2).Get().(float64)
if !ok {
return nil, fmt.Errorf("field length is %T, not float64", field.At(2).Get())
}
f.Length = int(fieldLength)
fieldLength, ok := field.At(2).Get().(float64)
if !ok {
return nil, fmt.Errorf("field length is %T, not float64", field.At(2).Get())
}
f.Length = int(fieldLength)
- } else if field.At(2).GetType() == jsongo.TypeMap {
+ f.SpecifiedLen = true
+
+ case jsongo.TypeMap:
fieldMeta := field.At(2)
for _, key := range fieldMeta.GetKeys() {
fieldMeta := field.At(2)
for _, key := range fieldMeta.GetKeys() {
@@
-439,11
+468,13
@@
func parseField(ctx *context, field *jsongo.JSONNode) (*Field, error) {
switch metaName := key.(string); metaName {
case fieldMetaLimit:
f.Meta.Limit = int(metaNode.Get().(float64))
switch metaName := key.(string); metaName {
case fieldMetaLimit:
f.Meta.Limit = int(metaNode.Get().(float64))
+ case fieldMetaDefault:
+ f.Meta.Default = fmt.Sprint(metaNode.Get())
default:
logrus.Warnf("unknown meta info (%s) for field (%s)", metaName, fieldName)
}
}
default:
logrus.Warnf("unknown meta info (%s) for field (%s)", metaName, fieldName)
}
}
- } else {
+ default:
return nil, errors.New("invalid JSON for field specified")
}
}
return nil, errors.New("invalid JSON for field specified")
}
}
@@
-459,7
+490,7
@@
func parseField(ctx *context, field *jsongo.JSONNode) (*Field, error) {
}
// parseService parses VPP binary API service object from JSON node
}
// parseService parses VPP binary API service object from JSON node
-func parseService(ctx *context, svcName string, svcNode *jsongo.
JSON
Node) (*Service, error) {
+func parseService(ctx *context, svcName string, svcNode *jsongo.Node) (*Service, error) {
if svcNode.Len() == 0 || svcNode.At(replyField).GetType() != jsongo.TypeValue {
return nil, errors.New("invalid JSON for service specified")
}
if svcNode.Len() == 0 || svcNode.At(replyField).GetType() != jsongo.TypeValue {
return nil, errors.New("invalid JSON for service specified")
}