From 2e081d76908a819401728d3a969091ce1676a5f5 Mon Sep 17 00:00:00 2001 From: simon Date: Wed, 23 Apr 2025 17:52:14 +0200 Subject: [PATCH] Added array length parameter in json --- proto/proto.go | 22 +++++++--------------- templates/blocks.tmpl | 4 ++++ testdata/prot1.json | 18 +++++++++++++----- todo.md | 8 ++++++++ 4 files changed, 32 insertions(+), 20 deletions(-) create mode 100644 todo.md diff --git a/proto/proto.go b/proto/proto.go index 8649f83..6c1a351 100644 --- a/proto/proto.go +++ b/proto/proto.go @@ -3,10 +3,8 @@ package proto import ( "bytes" "encoding/json" - "fmt" "log" "strconv" - "strings" "text/template" "unicode" ) @@ -27,8 +25,9 @@ type ProtoMessage struct { } type ProtoMessagePayload struct { - Name string `json:"name"` - DataType string `json:"type"` + Name string `json:"name"` + DataType string `json:"type"` + ArrayLength int `json:"array,omitempty"` } type Proto struct { @@ -98,16 +97,6 @@ func CreateCStyle(proto *Proto) (string, error) { payloads = append(payloads, tmp) } - for _, payload := range payloads { - for i, entry := range payload.Entries { - in := strings.Index(entry.DataType, "[") - if in > 0 { - payload.Entries[i].Name = fmt.Sprintf("%s%s", payload.Entries[i].Name, payload.Entries[i].DataType[in:]) - payload.Entries[i].DataType = payload.Entries[i].DataType[:in] - } - } - } - var data = CStyleData{ CenumPCTOESP: Cenum{ EnumName: "PC_TO_ESP_MESSAGE_IDS", @@ -123,7 +112,10 @@ func CreateCStyle(proto *Proto) (string, error) { } var buf bytes.Buffer - tmpl.ExecuteTemplate(&buf, "cpart", data) + err = tmpl.ExecuteTemplate(&buf, "cpart", data) + if err != nil { + log.Printf("Error Rendering template: %v", err) + } return buf.String(), nil } diff --git a/templates/blocks.tmpl b/templates/blocks.tmpl index 7415de6..5e292e4 100644 --- a/templates/blocks.tmpl +++ b/templates/blocks.tmpl @@ -13,7 +13,11 @@ typedef struct { // empty payload {{- end}} {{- range .Entries }} + {{- if (.ArrayLength) }} + {{.DataType}} {{.Name}}[{{.ArrayLength}}]; + {{- else }} {{.DataType}} {{.Name}}; + {{- end}} {{- end }} }{{ .Name }}Payload; {{ end }} diff --git a/testdata/prot1.json b/testdata/prot1.json index be3ae1f..963d9f8 100644 --- a/testdata/prot1.json +++ b/testdata/prot1.json @@ -6,19 +6,27 @@ "checksum": "xor" }, "messages_esp_to_pc": [ + { + "name": "Clients", + "id": "0xE1", + "payload": [ + { "name": "clientCount", "type": "uint8_t" }, + { "name": "clientAvaiableBitMask", "type": "uint32_t" } + ] + }, { "name": "Status", "id": "0xE2", "payload": [ - { "name": "clientid", "type": "uint8_t" }, - { "name": "mac", "type": "uint8_t[6]" } + { "name": "clientId", "type": "uint8_t" }, + { "name": "mac", "type": "uint8_t", "array": 6 } ] }, { "name": "Pong", "id": "0xD1", "payload": [ - { "name": "clientid", "type": "uint8_t" }, + { "name": "clientId", "type": "uint8_t" }, { "name": "ping", "type": "uint32_t" } ] } @@ -28,14 +36,14 @@ "name": "RequestPing", "id": "0xE1", "payload": [ - { "name": "clientid", "type": "uint8_t" } + { "name": "clientId", "type": "uint8_t" } ] }, { "name": "RequestStatus", "id": "0xE2", "payload": [ - { "name": "clientid", "type": "uint8_t" } + { "name": "clientId", "type": "uint8_t" } ] }, { diff --git a/todo.md b/todo.md new file mode 100644 index 0000000..f912d03 --- /dev/null +++ b/todo.md @@ -0,0 +1,8 @@ +# Current Work + +Added ArrayLenght need to implement it everywhere in the template +in Payload struct it happend already + +Need it in: +- Send Parameter +- Also remove trailing "," in send parameter