104 lines
2.8 KiB
Python
104 lines
2.8 KiB
Python
import json
|
|
from collections import defaultdict
|
|
|
|
print("package properties")
|
|
print("// This code has been generated with the genProps.py script. Do not modify\n\n")
|
|
|
|
print("import \"bufio\"")
|
|
print("import \"io\"")
|
|
print("import . \"badat.dev/maeqtt/v2/mqtt/types\"")
|
|
|
|
TYPE_TO_GOTYPE = {
|
|
"Byte": "byte",
|
|
"Two Byte Integer": "uint16",
|
|
"Four Byte Integer": "uint32",
|
|
"Binary Data": "[]byte",
|
|
"UTF-8 Encoded String": "string",
|
|
"Variable Byte Integer": "uint32",
|
|
}
|
|
TYPE_DECODE_CODE = {
|
|
"Byte": "val, err := r.ReadByte()",
|
|
"Two Byte Integer": "val, err := DecodeUint16(r)",
|
|
"Four Byte Integer": "val, err := DecodeUint32(r)",
|
|
"Binary Data": "val, err := DecodeBinaryData(r)",
|
|
"UTF-8 Encoded String": "val, err := DecodeUTF8String(r)",
|
|
"Variable Byte Integer": "val, err := DecodeVariableByteInt(r)"
|
|
}
|
|
|
|
TYPE_WRITE_CODE = {
|
|
"Byte": "(func () error {_, err := w.Write([]byte{*p.Value}); return err})()",
|
|
"Two Byte Integer": "WriteUint16(w, *p.Value)",
|
|
"Four Byte Integer": "WriteUint32(w, *p.Value)",
|
|
"Binary Data": "WriteBinaryData(w, *p.Value)",
|
|
"UTF-8 Encoded String": "WriteUTF8String(w, *p.Value)",
|
|
"Variable Byte Integer": "WriteVariableByteInt(w, *p.Value)"
|
|
}
|
|
|
|
applicationToProp = defaultdict(lambda: [])
|
|
|
|
with open("./properties.json") as f:
|
|
properties = json.load(f)
|
|
|
|
for prop in properties:
|
|
prop["name"] = "".join(prop["name"].split(" "))
|
|
|
|
for prop in properties:
|
|
for application in prop["appliesTo"]:
|
|
applicationToProp[application].append(prop)
|
|
|
|
val = prop["val"]
|
|
name= prop["name"]
|
|
if val == "38":
|
|
#needs manual handling
|
|
continue
|
|
|
|
gotype = TYPE_TO_GOTYPE[prop["type"]]
|
|
godecode = TYPE_DECODE_CODE[prop["type"]]
|
|
gowrite = TYPE_WRITE_CODE[prop["type"]]
|
|
|
|
print("""
|
|
type {name} struct {{
|
|
Value *{gotype}
|
|
}}
|
|
|
|
func (p {name}) id() int {{
|
|
return {val}
|
|
}}
|
|
|
|
func (p *{name}) parse(r *bufio.Reader) error {{
|
|
{godecode}
|
|
if err != nil {{
|
|
return err
|
|
}}
|
|
p.Value = &val
|
|
return nil
|
|
}}
|
|
|
|
func (p {name}) hasValue() bool {{
|
|
return p.Value != nil
|
|
}}
|
|
|
|
func (p {name}) write(w io.Writer) error {{
|
|
return {gowrite}
|
|
}}
|
|
""".format(name= name, gotype = gotype, gowrite = gowrite, godecode = godecode, val = val
|
|
))
|
|
|
|
for k,v in applicationToProp.items():
|
|
if k == "Will Properties":
|
|
arrName = "WillProperties"
|
|
else:
|
|
arrName = k.lower().capitalize() + "PacketProperties"
|
|
|
|
print("type", arrName, "struct {")
|
|
|
|
for prop in v:
|
|
print(prop["name"], prop["name"])
|
|
print("}")
|
|
print(f"func (p *{arrName}) ArrayOf() []Property {{")
|
|
print("return []Property {")
|
|
for prop in v:
|
|
propName = prop["name"]
|
|
print(f"&p.{propName},")
|
|
print("}")
|
|
print("}")
|