summaryrefslogtreecommitdiff
path: root/xC-gen-proto-go.awk
diff options
context:
space:
mode:
authorPřemysl Eric Janouch <p@janouch.name>2022-09-29 16:39:26 +0200
committerPřemysl Eric Janouch <p@janouch.name>2022-09-30 03:24:13 +0200
commit86278c154c0e8f88f3df91298b4fcd40f8c7571b (patch)
treeda2aa0f4786337b7245dc931e56f72b144008839 /xC-gen-proto-go.awk
parent941ee2f10c0c6d273d4d7bf591a48068141e2c56 (diff)
downloadxK-86278c154c0e8f88f3df91298b4fcd40f8c7571b.tar.gz
xK-86278c154c0e8f88f3df91298b4fcd40f8c7571b.tar.xz
xK-86278c154c0e8f88f3df91298b4fcd40f8c7571b.zip
Clean up protocol code generators
Diffstat (limited to 'xC-gen-proto-go.awk')
-rw-r--r--xC-gen-proto-go.awk87
1 files changed, 53 insertions, 34 deletions
diff --git a/xC-gen-proto-go.awk b/xC-gen-proto-go.awk
index 477a471..c0d6d48 100644
--- a/xC-gen-proto-go.awk
+++ b/xC-gen-proto-go.awk
@@ -70,7 +70,12 @@ function define_uint(size, shortname, gotype) {
"\t}\n"
}
-function codegen_begin() {
+# Currently two outputs cannot coexist within the same package.
+function codegen_private(name) {
+ return "proto" name
+}
+
+function codegen_begin( funcname) {
define_sint("8")
define_sint("16")
define_sint("32")
@@ -79,32 +84,17 @@ function codegen_begin() {
define_uint("16")
define_uint("32")
define_uint("64")
-
define_internal("bool", "bool")
- CodegenAppendJSON["bool"] = \
- "\tb = strconv.AppendBool(b, %s)\n"
- CodegenSerialize["bool"] = \
- "\tif %s {\n" \
- "\t\tdata = append(data, 1)\n" \
- "\t} else {\n" \
- "\t\tdata = append(data, 0)\n" \
- "\t}\n"
- CodegenDeserialize["bool"] = \
- "\tif data, ok = protoConsumeBoolFrom(data, &%s); !ok {\n" \
- "\t\treturn nil, ok\n" \
- "\t}\n"
-
define_internal("string", "string")
- CodegenSerialize["string"] = \
- "\tif data, ok = protoAppendStringTo(data, %s); !ok {\n" \
- "\t\treturn nil, ok\n" \
- "\t}\n"
- CodegenDeserialize["string"] = \
- "\tif data, ok = protoConsumeStringFrom(data, &%s); !ok {\n" \
- "\t\treturn nil, ok\n" \
- "\t}\n"
- print "package main"
+ # Cater to "go generate", for what it's worth.
+ CodegenPackage = ENV["GOPACKAGE"]
+ if (!CodegenPackage)
+ CodegenPackage = "main"
+
+ print "// Code generated from " FILENAME ". DO NOT EDIT."
+ print ""
+ print "package " CodegenPackage
print ""
print "import ("
print "\t`encoding/base64`"
@@ -117,10 +107,20 @@ function codegen_begin() {
print ")"
print ""
- print "// protoConsumeBoolFrom tries to deserialize a boolean value"
+ CodegenAppendJSON["bool"] = \
+ "\tb = strconv.AppendBool(b, %s)\n"
+ CodegenSerialize["bool"] = \
+ "\tif %s {\n" \
+ "\t\tdata = append(data, 1)\n" \
+ "\t} else {\n" \
+ "\t\tdata = append(data, 0)\n" \
+ "\t}\n"
+
+ funcname = codegen_private("ConsumeBoolFrom")
+ print "// " funcname " tries to deserialize a boolean value"
print "// from the beginning of a byte stream. When successful,"
print "// it returns a subslice with any data that might follow."
- print "func protoConsumeBoolFrom(data []byte, b *bool) ([]byte, bool) {"
+ print "func " funcname "(data []byte, b *bool) ([]byte, bool) {"
print "\tif len(data) < 1 {"
print "\t\treturn nil, false"
print "\t}"
@@ -133,9 +133,15 @@ function codegen_begin() {
print "}"
print ""
- print "// protoAppendStringTo tries to serialize a string value,"
+ CodegenDeserialize["bool"] = \
+ "\tif data, ok = " funcname "(data, &%s); !ok {\n" \
+ "\t\treturn nil, ok\n" \
+ "\t}\n"
+
+ funcname = codegen_private("AppendStringTo")
+ print "// " funcname " tries to serialize a string value,"
print "// appending it to the end of a byte stream."
- print "func protoAppendStringTo(data []byte, s string) ([]byte, bool) {"
+ print "func " funcname "(data []byte, s string) ([]byte, bool) {"
print "\tif len(s) > math.MaxUint32 {"
print "\t\treturn nil, false"
print "\t}"
@@ -144,10 +150,16 @@ function codegen_begin() {
print "}"
print ""
- print "// protoConsumeStringFrom tries to deserialize a string value"
+ CodegenSerialize["string"] = \
+ "\tif data, ok = " funcname "(data, %s); !ok {\n" \
+ "\t\treturn nil, ok\n" \
+ "\t}\n"
+
+ funcname = codegen_private("ConsumeStringFrom")
+ print "// " funcname " tries to deserialize a string value"
print "// from the beginning of a byte stream. When successful,"
print "// it returns a subslice with any data that might follow."
- print "func protoConsumeStringFrom(data []byte, s *string) ([]byte, bool) {"
+ print "func " funcname "(data []byte, s *string) ([]byte, bool) {"
print "\tif len(data) < 4 {"
print "\t\treturn nil, false"
print "\t}"
@@ -163,9 +175,15 @@ function codegen_begin() {
print "}"
print ""
- print "// protoUnmarshalEnumJSON converts a JSON fragment to an integer,"
+ CodegenDeserialize["string"] = \
+ "\tif data, ok = " funcname "(data, &%s); !ok {\n" \
+ "\t\treturn nil, ok\n" \
+ "\t}\n"
+
+ funcname = codegen_private("UnmarshalEnumJSON")
+ print "// " funcname " converts a JSON fragment to an integer,"
print "// ensuring that it's within the expected range of enum values."
- print "func protoUnmarshalEnumJSON(data []byte) (int64, error) {"
+ print "func " funcname "(data []byte) (int64, error) {"
print "\tvar n int64"
print "\tif err := json.Unmarshal(data, &n); err != nil {"
print "\t\treturn 0, err"
@@ -197,7 +215,7 @@ function codegen_enum_value(name, subname, value, cg, goname) {
"\t\t*v = " goname "\n")
}
-function codegen_enum(name, cg, gotype, fields) {
+function codegen_enum(name, cg, gotype, fields, funcname) {
gotype = PrefixCamel name
print "type " gotype " int8"
print ""
@@ -227,11 +245,12 @@ function codegen_enum(name, cg, gotype, fields) {
print "}"
print ""
+ funcname = codegen_private("UnmarshalEnumJSON")
print "func (v *" gotype ") UnmarshalJSON(data []byte) error {"
print "\tvar s string"
print "\tif json.Unmarshal(data, &s) == nil {"
print "\t\t// Handled below."
- print "\t} else if n, err := protoUnmarshalEnumJSON(data); err != nil {"
+ print "\t} else if n, err := " funcname "(data); err != nil {"
print "\t\treturn err"
print "\t} else {"
print "\t\t*v = " gotype "(n)"