xP: improve comments in protocol code generator

This commit is contained in:
Přemysl Eric Janouch 2022-09-15 05:11:41 +02:00
parent 98b0a4ef3d
commit e87cc90b5e
Signed by: p
GPG Key ID: A0420B94F92B9493

View File

@ -17,7 +17,6 @@ function define_sint(size, shortname, gotype) {
CodegenAppendJSON[shortname] = \ CodegenAppendJSON[shortname] = \
"\tb = strconv.AppendInt(b, int64(%s), 10)\n" "\tb = strconv.AppendInt(b, int64(%s), 10)\n"
if (size == 8) { if (size == 8) {
CodegenSerialize[shortname] = "\tdata = append(data, uint8(%s))\n" CodegenSerialize[shortname] = "\tdata = append(data, uint8(%s))\n"
CodegenDeserialize[shortname] = \ CodegenDeserialize[shortname] = \
@ -41,14 +40,14 @@ function define_sint(size, shortname, gotype) {
} }
function define_uint(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 shortname = "u" size
gotype = "uint" size gotype = "uint" size
define_internal(shortname, gotype) define_internal(shortname, gotype)
CodegenAppendJSON[shortname] = \ CodegenAppendJSON[shortname] = \
"\tb = strconv.AppendUint(b, uint64(%s), 10)\n" "\tb = strconv.AppendUint(b, uint64(%s), 10)\n"
# Both byte and uint8 luckily marshal as base64-encoded JSON strings.
if (size == 8) { if (size == 8) {
CodegenSerialize[shortname] = "\tdata = append(data, %s)\n" CodegenSerialize[shortname] = "\tdata = append(data, %s)\n"
CodegenDeserialize[shortname] = \ CodegenDeserialize[shortname] = \
@ -266,6 +265,7 @@ function codegen_marshal(type, f, marshal) {
if (CodegenAppendJSON[type]) if (CodegenAppendJSON[type])
return sprintf(CodegenAppendJSON[type], f) return sprintf(CodegenAppendJSON[type], f)
# Complex types are json.Marshalers, there's no need to json.Marshal(&f).
if (CodegenIsMarshaler[type]) if (CodegenIsMarshaler[type])
marshal = f ".MarshalJSON()" marshal = f ".MarshalJSON()"
else else
@ -282,8 +282,6 @@ function codegen_marshal(type, f, marshal) {
function codegen_struct_field_marshal(d, cg, camel, f, marshal) { function codegen_struct_field_marshal(d, cg, camel, f, marshal) {
camel = snaketocamel(d["name"]) camel = snaketocamel(d["name"])
f = "s." camel f = "s." camel
# Complex types are json.Marshalers, there's no need to json.Marshal(&f).
if (!d["isarray"]) { if (!d["isarray"]) {
append(cg, "marshal", append(cg, "marshal",
"\tb = append(b, `,\"" decapitalize(camel) "\":`...)\n" \ "\tb = append(b, `,\"" decapitalize(camel) "\":`...)\n" \
@ -291,6 +289,8 @@ function codegen_struct_field_marshal(d, cg, camel, f, marshal) {
return 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") { if (d["type"] == "u8") {
append(cg, "marshal", append(cg, "marshal",
"\tb = append(b, `,\"" decapitalize(camel) "\":\"`...)\n" \ "\tb = append(b, `,\"" decapitalize(camel) "\":\"`...)\n" \