1 // Copyright (c) 2020 Cisco and/or its affiliates.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
25 "github.com/bennyscetbun/jsongo"
28 var debug = strings.Contains(os.Getenv("DEBUG_GOVPP"), "parser")
30 func logf(f string, v ...interface{}) {
38 fileAPIVersion = "vl_api_version"
39 fileOptions = "options"
41 fileMessages = "messages"
44 fileAliases = "aliases"
45 fileServices = "services"
46 fileImports = "imports"
50 aliasLength = "length"
53 serviceReply = "reply"
54 serviceStream = "stream"
55 serviceStreamMsg = "stream_msg"
56 serviceEvents = "events"
59 func parseJSON(data []byte) (module *File, err error) {
61 jsonRoot := new(jsongo.Node)
62 if err := json.Unmarshal(data, jsonRoot); err != nil {
63 return nil, fmt.Errorf("unmarshalling JSON failed: %v", err)
66 logf("file contains:")
67 for _, key := range jsonRoot.GetKeys() {
68 if jsonRoot.At(key).Len() > 0 {
69 logf(" - %2d %s", jsonRoot.At(key).Len(), key)
76 crc := jsonRoot.At(fileAPIVersion)
77 if crc.GetType() == jsongo.TypeValue {
78 module.CRC = crc.MustGetString()
82 opt := jsonRoot.Map(fileOptions)
83 if opt.GetType() == jsongo.TypeMap {
84 module.Options = make(map[string]string)
85 for _, key := range opt.GetKeys() {
86 optionKey := key.(string)
87 optionVal := opt.At(key).MustGetString()
88 module.Options[optionKey] = optionVal
93 importsNode := jsonRoot.Map(fileImports)
94 module.Imports = make([]string, 0, importsNode.Len())
95 uniq := make(map[string]struct{})
96 for i := 0; i < importsNode.Len(); i++ {
97 importNode := importsNode.At(i)
98 imp := importNode.MustGetString()
99 if _, ok := uniq[imp]; ok {
100 logf("duplicate import found: %v", imp)
103 uniq[imp] = struct{}{}
104 module.Imports = append(module.Imports, imp)
107 // avoid duplicate objects
108 known := make(map[string]struct{})
109 exists := func(name string) bool {
110 if _, ok := known[name]; ok {
111 logf("duplicate object found: %v", name)
114 known[name] = struct{}{}
119 enumsNode := jsonRoot.Map(fileEnums)
120 module.EnumTypes = make([]EnumType, 0)
121 for i := 0; i < enumsNode.Len(); i++ {
122 enum, err := parseEnum(enumsNode.At(i))
126 if exists(enum.Name) {
129 module.EnumTypes = append(module.EnumTypes, *enum)
133 aliasesNode := jsonRoot.Map(fileAliases)
134 if aliasesNode.GetType() == jsongo.TypeMap {
135 module.AliasTypes = make([]AliasType, 0)
136 for _, key := range aliasesNode.GetKeys() {
137 aliasName := key.(string)
138 alias, err := parseAlias(aliasName, aliasesNode.At(key))
142 if exists(alias.Name) {
145 module.AliasTypes = append(module.AliasTypes, *alias)
149 // parse struct types
150 typesNode := jsonRoot.Map(fileTypes)
151 module.StructTypes = make([]StructType, 0)
152 for i := 0; i < typesNode.Len(); i++ {
153 structyp, err := parseStruct(typesNode.At(i))
157 if exists(structyp.Name) {
160 module.StructTypes = append(module.StructTypes, *structyp)
164 unionsNode := jsonRoot.Map(fileUnions)
165 module.UnionTypes = make([]UnionType, 0)
166 for i := 0; i < unionsNode.Len(); i++ {
167 union, err := parseUnion(unionsNode.At(i))
171 if exists(union.Name) {
174 module.UnionTypes = append(module.UnionTypes, *union)
178 messagesNode := jsonRoot.Map(fileMessages)
179 if messagesNode.GetType() == jsongo.TypeArray {
180 module.Messages = make([]Message, messagesNode.Len())
181 for i := 0; i < messagesNode.Len(); i++ {
182 msg, err := parseMessage(messagesNode.At(i))
186 module.Messages[i] = *msg
191 servicesNode := jsonRoot.Map(fileServices)
192 if servicesNode.GetType() == jsongo.TypeMap {
193 module.Service = &Service{
194 RPCs: make([]RPC, servicesNode.Len()),
196 for i, key := range servicesNode.GetKeys() {
197 rpcName := key.(string)
198 svc, err := parseServiceRPC(rpcName, servicesNode.At(key))
202 module.Service.RPCs[i] = *svc
209 // parseEnum parses VPP binary API enum object from JSON node
210 func parseEnum(enumNode *jsongo.Node) (*EnumType, error) {
211 if enumNode.Len() == 0 || enumNode.At(0).GetType() != jsongo.TypeValue {
212 return nil, errors.New("invalid JSON for enum specified")
215 enumName, ok := enumNode.At(0).Get().(string)
217 return nil, fmt.Errorf("enum name is %T, not a string", enumNode.At(0).Get())
219 enumType, ok := enumNode.At(enumNode.Len() - 1).At(enumType).Get().(string)
221 return nil, fmt.Errorf("enum type invalid or missing")
229 // loop through enum entries, skip first (name) and last (enumtype)
230 for j := 1; j < enumNode.Len()-1; j++ {
231 if enumNode.At(j).GetType() == jsongo.TypeArray {
232 entry := enumNode.At(j)
234 if entry.Len() < 2 || entry.At(0).GetType() != jsongo.TypeValue || entry.At(1).GetType() != jsongo.TypeValue {
235 return nil, errors.New("invalid JSON for enum entry specified")
238 entryName, ok := entry.At(0).Get().(string)
240 return nil, fmt.Errorf("enum entry name is %T, not a string", entry.At(0).Get())
242 entryVal := entry.At(1).Get().(float64)
244 enum.Entries = append(enum.Entries, EnumEntry{
246 Value: uint32(entryVal),
254 // parseUnion parses VPP binary API union object from JSON node
255 func parseUnion(unionNode *jsongo.Node) (*UnionType, error) {
256 if unionNode.Len() == 0 || unionNode.At(0).GetType() != jsongo.TypeValue {
257 return nil, errors.New("invalid JSON for union specified")
260 unionName, ok := unionNode.At(0).Get().(string)
262 return nil, fmt.Errorf("union name is %T, not a string", unionNode.At(0).Get())
269 // loop through union fields, skip first (name)
270 for j := 1; j < unionNode.Len(); j++ {
271 if unionNode.At(j).GetType() == jsongo.TypeArray {
272 fieldNode := unionNode.At(j)
274 field, err := parseField(fieldNode)
279 union.Fields = append(union.Fields, *field)
286 // parseStruct parses VPP binary API type object from JSON node
287 func parseStruct(typeNode *jsongo.Node) (*StructType, error) {
288 if typeNode.Len() == 0 || typeNode.At(0).GetType() != jsongo.TypeValue {
289 return nil, errors.New("invalid JSON for type specified")
292 typeName, ok := typeNode.At(0).Get().(string)
294 return nil, fmt.Errorf("type name is %T, not a string", typeNode.At(0).Get())
301 // loop through type fields, skip first (name)
302 for j := 1; j < typeNode.Len(); j++ {
303 if typeNode.At(j).GetType() == jsongo.TypeArray {
304 fieldNode := typeNode.At(j)
306 field, err := parseField(fieldNode)
311 typ.Fields = append(typ.Fields, *field)
318 // parseAlias parses VPP binary API alias object from JSON node
319 func parseAlias(aliasName string, aliasNode *jsongo.Node) (*AliasType, error) {
320 if aliasNode.Len() == 0 || aliasNode.At(aliasType).GetType() != jsongo.TypeValue {
321 return nil, errors.New("invalid JSON for alias specified")
328 if typeNode := aliasNode.At(aliasType); typeNode.GetType() == jsongo.TypeValue {
329 typ, ok := typeNode.Get().(string)
331 return nil, fmt.Errorf("alias type is %T, not a string", typeNode.Get())
338 if lengthNode := aliasNode.At(aliasLength); lengthNode.GetType() == jsongo.TypeValue {
339 length, ok := lengthNode.Get().(float64)
341 return nil, fmt.Errorf("alias length is %T, not a float64", lengthNode.Get())
343 alias.Length = int(length)
349 // parseMessage parses VPP binary API message object from JSON node
350 func parseMessage(msgNode *jsongo.Node) (*Message, error) {
351 if msgNode.Len() < 2 || msgNode.At(0).GetType() != jsongo.TypeValue {
352 return nil, errors.New("invalid JSON for message specified")
355 msgName, ok := msgNode.At(0).Get().(string)
357 return nil, fmt.Errorf("message name is %T, not a string", msgNode.At(0).Get())
359 msgCRC, ok := msgNode.At(msgNode.Len() - 1).At(messageCrc).Get().(string)
361 return nil, fmt.Errorf("message crc invalid or missing")
369 // loop through message fields, skip first (name) and last (crc)
370 for j := 1; j < msgNode.Len()-1; j++ {
371 if msgNode.At(j).GetType() == jsongo.TypeArray {
372 fieldNode := msgNode.At(j)
374 field, err := parseField(fieldNode)
379 msg.Fields = append(msg.Fields, *field)
386 // parseField parses VPP binary API object field from JSON node
387 func parseField(field *jsongo.Node) (*Field, error) {
388 if field.Len() < 2 || field.At(0).GetType() != jsongo.TypeValue || field.At(1).GetType() != jsongo.TypeValue {
389 return nil, errors.New("invalid JSON for field specified")
392 fieldType, ok := field.At(0).Get().(string)
394 return nil, fmt.Errorf("field type is %T, not a string", field.At(0).Get())
396 fieldName, ok := field.At(1).Get().(string)
398 return nil, fmt.Errorf("field name is %T, not a string", field.At(1).Get())
406 if field.Len() >= 3 {
407 switch field.At(2).GetType() {
408 case jsongo.TypeValue:
409 fieldLength, ok := field.At(2).Get().(float64)
411 return nil, fmt.Errorf("field length is %T, not float64", field.At(2).Get())
413 f.Length = int(fieldLength)
417 fieldMeta := field.At(2)
418 if fieldMeta.Len() == 0 {
421 f.Meta = map[string]interface{}{}
422 for _, key := range fieldMeta.GetKeys() {
423 metaName := key.(string)
424 metaValue := fieldMeta.At(key).Get()
425 f.Meta[metaName] = metaValue
429 return nil, errors.New("invalid JSON for field specified")
432 if field.Len() >= 4 {
433 fieldLengthFrom, ok := field.At(3).Get().(string)
435 return nil, fmt.Errorf("field length from is %T, not a string", field.At(3).Get())
437 f.SizeFrom = fieldLengthFrom
443 // parseServiceRPC parses VPP binary API service object from JSON node
444 func parseServiceRPC(rpcName string, rpcNode *jsongo.Node) (*RPC, error) {
445 if rpcNode.Len() == 0 || rpcNode.At(serviceReply).GetType() != jsongo.TypeValue {
446 return nil, errors.New("invalid JSON for service RPC specified")
453 if replyNode := rpcNode.At(serviceReply); replyNode.GetType() == jsongo.TypeValue {
454 reply, ok := replyNode.Get().(string)
456 return nil, fmt.Errorf("service RPC reply is %T, not a string", replyNode.Get())
462 if streamNode := rpcNode.At(serviceStream); streamNode.GetType() == jsongo.TypeValue {
464 rpc.Stream, ok = streamNode.Get().(bool)
466 return nil, fmt.Errorf("service RPC stream is %T, not a boolean", streamNode.Get())
471 if streamMsgNode := rpcNode.At(serviceStreamMsg); streamMsgNode.GetType() == jsongo.TypeValue {
473 rpc.StreamMsg, ok = streamMsgNode.Get().(string)
475 return nil, fmt.Errorf("service RPC stream msg is %T, not a string", streamMsgNode.Get())
479 // events service (event subscription)
480 if eventsNode := rpcNode.At(serviceEvents); eventsNode.GetType() == jsongo.TypeArray {
481 for j := 0; j < eventsNode.Len(); j++ {
482 event := eventsNode.At(j).Get().(string)
483 rpc.Events = append(rpc.Events, event)