diff options
author | Rastislav Szabo <raszabo@cisco.com> | 2017-09-25 21:24:21 +0200 |
---|---|---|
committer | Rastislav Szabo <raszabo@cisco.com> | 2017-09-25 21:24:21 +0200 |
commit | 70ae7c10259161994e49bae8d095dcce840dfed4 (patch) | |
tree | 52ec23e70a5e3b7b679447004497ffa6063e211e | |
parent | 03bc1c23595e998e8fa75a65e23bedfbec115643 (diff) |
generator fix - better identification of message types
Change-Id: I00dbd57acba706b4a842e2b6c6df2d7b7ab7c37a
Signed-off-by: Rastislav Szabo <raszabo@cisco.com>
-rw-r--r-- | cmd/binapi-generator/generator.go | 35 | ||||
-rw-r--r-- | cmd/binapi-generator/generator_test.go | 4 |
2 files changed, 23 insertions, 16 deletions
diff --git a/cmd/binapi-generator/generator.go b/cmd/binapi-generator/generator.go index d80f687..e3efe0f 100644 --- a/cmd/binapi-generator/generator.go +++ b/cmd/binapi-generator/generator.go @@ -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 } diff --git a/cmd/binapi-generator/generator_test.go b/cmd/binapi-generator/generator_test.go index 7527a98..c7d89ce 100644 --- a/cmd/binapi-generator/generator_test.go +++ b/cmd/binapi-generator/generator_test.go @@ -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++ |