From e87cc90b5e20ba90d4f5c9ea349d9cf41b5ae58c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Eric=20Janouch?= Date: Thu, 15 Sep 2022 05:11:41 +0200 Subject: [PATCH] xP: improve comments in protocol code generator --- xC-gen-proto-go.awk | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/xC-gen-proto-go.awk b/xC-gen-proto-go.awk index 48371eb..477a471 100644 --- a/xC-gen-proto-go.awk +++ b/xC-gen-proto-go.awk @@ -17,7 +17,6 @@ function define_sint(size, shortname, gotype) { CodegenAppendJSON[shortname] = \ "\tb = strconv.AppendInt(b, int64(%s), 10)\n" - if (size == 8) { CodegenSerialize[shortname] = "\tdata = append(data, uint8(%s))\n" CodegenDeserialize[shortname] = \ @@ -41,14 +40,14 @@ function define_sint(size, shortname, gotype) { } function define_uint(size, shortname, gotype) { + # Both []byte and []uint8 luckily marshal as base64-encoded JSON strings, + # so there's no need to rename the type as an exception. shortname = "u" size gotype = "uint" size define_internal(shortname, gotype) CodegenAppendJSON[shortname] = \ "\tb = strconv.AppendUint(b, uint64(%s), 10)\n" - - # Both byte and uint8 luckily marshal as base64-encoded JSON strings. if (size == 8) { CodegenSerialize[shortname] = "\tdata = append(data, %s)\n" CodegenDeserialize[shortname] = \ @@ -266,6 +265,7 @@ function codegen_marshal(type, f, marshal) { if (CodegenAppendJSON[type]) return sprintf(CodegenAppendJSON[type], f) + # Complex types are json.Marshalers, there's no need to json.Marshal(&f). if (CodegenIsMarshaler[type]) marshal = f ".MarshalJSON()" else @@ -282,8 +282,6 @@ function codegen_marshal(type, f, marshal) { function codegen_struct_field_marshal(d, cg, camel, f, marshal) { camel = snaketocamel(d["name"]) f = "s." camel - - # Complex types are json.Marshalers, there's no need to json.Marshal(&f). if (!d["isarray"]) { append(cg, "marshal", "\tb = append(b, `,\"" decapitalize(camel) "\":`...)\n" \ @@ -291,6 +289,8 @@ function codegen_struct_field_marshal(d, cg, camel, f, marshal) { return } + # Note that we do not produce `null` for nil slices, unlike encoding/json. + # And arrays never get deserialized as such. if (d["type"] == "u8") { append(cg, "marshal", "\tb = append(b, `,\"" decapitalize(camel) "\":\"`...)\n" \