From f8577dc3dcd472fbf0dab318b1837a90c36102bb Mon Sep 17 00:00:00 2001 From: simon Date: Sat, 26 Apr 2025 16:44:48 +0200 Subject: [PATCH] Finished up Send Message generation --- proto/proto.go | 8 +++++++- readme.md | 2 +- templates/blocks.tmpl | 3 ++- todo.md | 2 +- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/proto/proto.go b/proto/proto.go index 6c1a351..1031d00 100644 --- a/proto/proto.go +++ b/proto/proto.go @@ -71,7 +71,8 @@ type CStyleData struct { func CreateCStyle(proto *Proto) (string, error) { funcs := template.FuncMap{ - "snake": toSnakeCase, + "snake": toSnakeCase, + "isLast": isLast, } tmpl, err := template.New("all").Funcs(funcs).ParseGlob("templates/*.tmpl") @@ -134,3 +135,8 @@ func toSnakeCase(str string) string { } return string(out) } + +// just a helper function, pass the length of your array and your current index +func isLast(datalen int, index int) bool { + return index == datalen-1 +} diff --git a/readme.md b/readme.md index 3332629..830cad5 100644 --- a/readme.md +++ b/readme.md @@ -85,7 +85,7 @@ Each message is described as: - [x] Union-Typen - [x] Hook-Funktionen - [x] Message-Dispatcher (Switch-Case) - - [ ] Send-Funktionen + - [x] Send-Funktionen - [ ] Generate Header and Source File --- diff --git a/templates/blocks.tmpl b/templates/blocks.tmpl index 5e292e4..fe668fc 100644 --- a/templates/blocks.tmpl +++ b/templates/blocks.tmpl @@ -54,7 +54,8 @@ void dispatch_message(uint8_t msg_id, void* payload) { {{define "send_functions"}} {{- range .}} -void send_{{.Name | snake}}({{- range .Payload}}{{.DataType}} {{.Name}}, {{- end}}) { +{{ $argLen := len .Payload }} +void send_{{.Name | snake}}({{- range $i, $p := .Payload}}{{$p.DataType}} {{$p.Name}} {{- if not (isLast $argLen $i) }}, {{ end}} {{- end}}) { {{.Name}}Payload payload; // Payload-Daten zuweisen diff --git a/todo.md b/todo.md index f912d03..5f0cb26 100644 --- a/todo.md +++ b/todo.md @@ -5,4 +5,4 @@ in Payload struct it happend already Need it in: - Send Parameter -- Also remove trailing "," in send parameter +- [x] Also remove trailing "," in send parameter