generator fix - better identification of message types 27/8527/1
authorRastislav Szabo <raszabo@cisco.com>
Mon, 25 Sep 2017 19:24:21 +0000 (21:24 +0200)
committerRastislav Szabo <raszabo@cisco.com>
Mon, 25 Sep 2017 19:24:21 +0000 (21:24 +0200)
Change-Id: I00dbd57acba706b4a842e2b6c6df2d7b7ab7c37a
Signed-off-by: Rastislav Szabo <raszabo@cisco.com>
cmd/binapi-generator/generator.go
cmd/binapi-generator/generator_test.go

index d80f687..e3efe0f 100644 (file)
@@ -250,26 +250,33 @@ func generateMessage(ctx *context, w io.Writer, msg *jsongo.JSONNode, isType boo
        // generate struct fields into the slice & determine message type
        fields := make([]string, 0)
        msgType := otherMessage
+       wasClientIndex := false
        for j := 0; j < msg.Len(); j++ {
                if jsongo.TypeArray == msg.At(j).GetType() {
                        fld := msg.At(j)
-                       err := processMessageField(ctx, &fields, fld)
-                       if err != nil {
-                               return err
-                       }
-                       // determine whether ths is a request / reply / other message
-                       if j == 2 {
+                       if !isType {
+                               // determine whether ths is a request / reply / other message
                                fieldName, ok := fld.At(1).Get().(string)
                                if ok {
-                                       if fieldName == "client_index" {
-                                               msgType = requestMessage
-                                       } else if fieldName == "context" {
-                                               msgType = replyMessage
-                                       } else {
-                                               msgType = otherMessage
+                                       if j == 2 {
+                                               if fieldName == "client_index" {
+                                                       wasClientIndex = true
+                                               } else if fieldName == "context" {
+                                                       // reply needs "context" as the second member
+                                                       msgType = replyMessage
+                                               }
+                                       } else if j == 3 {
+                                               if wasClientIndex && fieldName == "context" {
+                                                       // request needs "client_index" as the second member and "context" as the third member
+                                                       msgType = requestMessage
+                                               }
                                        }
                                }
                        }
+                       err := processMessageField(ctx, &fields, fld, isType)
+                       if err != nil {
+                               return err
+                       }
                }
        }
 
@@ -319,7 +326,7 @@ func generateMessage(ctx *context, w io.Writer, msg *jsongo.JSONNode, isType boo
 }
 
 // processMessageField process JSON describing one message field into Go code emitted into provided slice of message fields
-func processMessageField(ctx *context, fields *[]string, fld *jsongo.JSONNode) error {
+func processMessageField(ctx *context, fields *[]string, fld *jsongo.JSONNode, isType bool) error {
        if fld.Len() < 2 || fld.At(0).GetType() != jsongo.TypeValue || fld.At(1).GetType() != jsongo.TypeValue {
                return errors.New("invalid JSON for message field specified")
        }
@@ -337,7 +344,7 @@ func processMessageField(ctx *context, fields *[]string, fld *jsongo.JSONNode) e
        if fieldNameLower == "crc" || fieldNameLower == "_vl_msg_id" {
                return nil
        }
-       if len(*fields) == 0 && (fieldNameLower == "client_index" || fieldNameLower == "context") {
+       if !isType && len(*fields) == 0 && (fieldNameLower == "client_index" || fieldNameLower == "context") {
                return nil
        }
 
index 7527a98..c7d89ce 100644 (file)
@@ -245,7 +245,7 @@ func TestGenerateMessageFieldTypes(t *testing.T) {
                for j := 0; j < types.At(i).Len(); j++ {
                        field := types.At(i).At(j)
                        if jsongo.TypeArray == field.GetType() {
-                               err := processMessageField(testCtx, &fields, field)
+                               err := processMessageField(testCtx, &fields, field, otherMessage)
                                Expect(err).ShouldNot(HaveOccurred())
                                Expect(fields[j-1]).To(BeEquivalentTo(expectedTypes[j-1]))
                        }
@@ -282,7 +282,7 @@ func TestGenerateMessageFieldMessages(t *testing.T) {
                                        specificFieldName == "client_index" || specificFieldName == "context" {
                                        continue
                                }
-                               err := processMessageField(testCtx, &fields, field)
+                               err := processMessageField(testCtx, &fields, field, requestMessage)
                                Expect(err).ShouldNot(HaveOccurred())
                                Expect(fields[customIndex]).To(BeEquivalentTo(expectedTypes[customIndex]))
                                customIndex++