Added array length parameter in json
This commit is contained in:
parent
ad0b3c431d
commit
2e081d7690
@ -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"
|
||||||
)
|
)
|
||||||
@ -29,6 +27,7 @@ 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
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,8 +13,12 @@ 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 }}
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|||||||
18
testdata/prot1.json
vendored
18
testdata/prot1.json
vendored
@ -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" }
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user