Added array length parameter in json

This commit is contained in:
simon 2025-04-23 17:52:14 +02:00
parent ad0b3c431d
commit 2e081d7690
4 changed files with 32 additions and 20 deletions

View File

@ -3,10 +3,8 @@ package proto
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
"log" "log"
"strconv" "strconv"
"strings"
"text/template" "text/template"
"unicode" "unicode"
) )
@ -27,8 +25,9 @@ type ProtoMessage struct {
} }
type ProtoMessagePayload struct { type ProtoMessagePayload struct {
Name string `json:"name"` Name string `json:"name"`
DataType string `json:"type"` DataType string `json:"type"`
ArrayLength int `json:"array,omitempty"`
} }
type Proto struct { type Proto struct {
@ -98,16 +97,6 @@ func CreateCStyle(proto *Proto) (string, error) {
payloads = append(payloads, tmp) 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{ var data = CStyleData{
CenumPCTOESP: Cenum{ CenumPCTOESP: Cenum{
EnumName: "PC_TO_ESP_MESSAGE_IDS", EnumName: "PC_TO_ESP_MESSAGE_IDS",
@ -123,7 +112,10 @@ func CreateCStyle(proto *Proto) (string, error) {
} }
var buf bytes.Buffer 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 return buf.String(), nil
} }

View File

@ -13,7 +13,11 @@ typedef struct {
// empty payload // empty payload
{{- end}} {{- end}}
{{- range .Entries }} {{- range .Entries }}
{{- if (.ArrayLength) }}
{{.DataType}} {{.Name}}[{{.ArrayLength}}];
{{- else }}
{{.DataType}} {{.Name}}; {{.DataType}} {{.Name}};
{{- end}}
{{- end }} {{- end }}
}{{ .Name }}Payload; }{{ .Name }}Payload;
{{ end }} {{ end }}

18
testdata/prot1.json vendored
View File

@ -6,19 +6,27 @@
"checksum": "xor" "checksum": "xor"
}, },
"messages_esp_to_pc": [ "messages_esp_to_pc": [
{
"name": "Clients",
"id": "0xE1",
"payload": [
{ "name": "clientCount", "type": "uint8_t" },
{ "name": "clientAvaiableBitMask", "type": "uint32_t" }
]
},
{ {
"name": "Status", "name": "Status",
"id": "0xE2", "id": "0xE2",
"payload": [ "payload": [
{ "name": "clientid", "type": "uint8_t" }, { "name": "clientId", "type": "uint8_t" },
{ "name": "mac", "type": "uint8_t[6]" } { "name": "mac", "type": "uint8_t", "array": 6 }
] ]
}, },
{ {
"name": "Pong", "name": "Pong",
"id": "0xD1", "id": "0xD1",
"payload": [ "payload": [
{ "name": "clientid", "type": "uint8_t" }, { "name": "clientId", "type": "uint8_t" },
{ "name": "ping", "type": "uint32_t" } { "name": "ping", "type": "uint32_t" }
] ]
} }
@ -28,14 +36,14 @@
"name": "RequestPing", "name": "RequestPing",
"id": "0xE1", "id": "0xE1",
"payload": [ "payload": [
{ "name": "clientid", "type": "uint8_t" } { "name": "clientId", "type": "uint8_t" }
] ]
}, },
{ {
"name": "RequestStatus", "name": "RequestStatus",
"id": "0xE2", "id": "0xE2",
"payload": [ "payload": [
{ "name": "clientid", "type": "uint8_t" } { "name": "clientId", "type": "uint8_t" }
] ]
}, },
{ {

8
todo.md Normal file
View File

@ -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