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 (
"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
}

View File

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

18
testdata/prot1.json vendored
View File

@ -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" }
]
},
{

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