-
- encodeFields(fields, "m")
-
- fmt.Fprintf(w, "\tvar buf []byte\n")
- fmt.Fprintf(w, "\tif b == nil {\n")
- fmt.Fprintf(w, "\tbuf = make([]byte, m.Size())\n")
- fmt.Fprintf(w, "\t} else {\n")
- fmt.Fprintf(w, "\tbuf = b\n")
- fmt.Fprintf(w, "\t}\n")
- fmt.Fprint(w, buf.String())
-
- fmt.Fprintf(w, "return buf, nil\n")
-
- fmt.Fprintf(w, "}\n")
-}
-
-func generateMessageUnmarshal(ctx *GenFile, w io.Writer, name string, fields []*Field) {
- fmt.Fprintf(w, "func (m *%[1]s) Unmarshal(tmp []byte) error {\n", name)
-
- fmt.Fprintf(w, "\to := binary.BigEndian\n")
- fmt.Fprintf(w, "\t_ = o\n")
- fmt.Fprintf(w, "\tpos := 0\n")
- fmt.Fprintf(w, "\t_ = pos\n")
-
- decodeBaseType := func(typ, orig, name string, length int, sizefrom string, alloc bool) bool {
- t, ok := BaseTypeNames[typ]
- if !ok {
- return false
- }
-
- isArray := length > 0 || sizefrom != ""
-
- switch t {
- case I8, U8, I16, U16, I32, U32, I64, U64, F64:
- if isArray {
- if alloc {
- if length != 0 {
- fmt.Fprintf(w, "\t%s = make([]%s, %d)\n", name, orig, length)
- } else if sizefrom != "" {
- fmt.Fprintf(w, "\t%s = make([]%s, %s)\n", name, orig, sizefrom)
- }
- }
- fmt.Fprintf(w, "\tfor i := 0; i < len(%s); i++ {\n", name)
- }
- }
-
- switch t {
- case I8, U8:
- if isArray {
- fmt.Fprintf(w, "\t%s[i] = %s(tmp[pos])\n", name, convertToGoType(ctx.file, typ))
- } else {
- fmt.Fprintf(w, "\t%s = %s(tmp[pos])\n", name, orig)
- }
- fmt.Fprintf(w, "\tpos += 1\n")
- if isArray {
- fmt.Fprintf(w, "\t}\n")
- }
- case I16, U16:
- if isArray {
- fmt.Fprintf(w, "\t%s[i] = %s(o.Uint16(tmp[pos:pos+2]))\n", name, orig)
- } else {
- fmt.Fprintf(w, "\t%s = %s(o.Uint16(tmp[pos:pos+2]))\n", name, orig)
- }
- fmt.Fprintf(w, "\tpos += 2\n")
- if isArray {
- fmt.Fprintf(w, "\t}\n")
- }
- case I32, U32:
- if isArray {
- fmt.Fprintf(w, "\t%s[i] = %s(o.Uint32(tmp[pos:pos+4]))\n", name, orig)
- } else {
- fmt.Fprintf(w, "\t%s = %s(o.Uint32(tmp[pos:pos+4]))\n", name, orig)
- }
- fmt.Fprintf(w, "\tpos += 4\n")
- if isArray {
- fmt.Fprintf(w, "\t}\n")
- }
- case I64, U64:
- if isArray {
- fmt.Fprintf(w, "\t%s[i] = %s(o.Uint64(tmp[pos:pos+8]))\n", name, orig)
- } else {
- fmt.Fprintf(w, "\t%s = %s(o.Uint64(tmp[pos:pos+8]))\n", name, orig)
- }
- fmt.Fprintf(w, "\tpos += 8\n")
- if isArray {
- fmt.Fprintf(w, "\t}\n")
- }
- case F64:
- if isArray {
- fmt.Fprintf(w, "\t%s[i] = %s(math.Float64frombits(o.Uint64(tmp[pos:pos+8])))\n", name, orig)
- } else {
- fmt.Fprintf(w, "\t%s = %s(math.Float64frombits(o.Uint64(tmp[pos:pos+8])))\n", name, orig)
- }
- fmt.Fprintf(w, "\tpos += 8\n")
- if isArray {
- fmt.Fprintf(w, "\t}\n")
- }
- case BOOL:
- fmt.Fprintf(w, "\t%s = tmp[pos] != 0\n", name)
- fmt.Fprintf(w, "\tpos += 1\n")
- case STRING:
- if length != 0 {
- fmt.Fprintf(w, "\t{\n")
- fmt.Fprintf(w, "\tnul := bytes.Index(tmp[pos:pos+%d], []byte{0x00})\n", length)
- fmt.Fprintf(w, "\t%[1]s = codec.DecodeString(tmp[pos:pos+nul])\n", name)
- fmt.Fprintf(w, "\tpos += %d\n", length)
- fmt.Fprintf(w, "\t}\n")
- } else {
- fmt.Fprintf(w, "\t{\n")
- fmt.Fprintf(w, "\tsiz := o.Uint32(tmp[pos:pos+4])\n")
- fmt.Fprintf(w, "\tpos += 4\n")
- fmt.Fprintf(w, "\t%[1]s = codec.DecodeString(tmp[pos:pos+int(siz)])\n", name)
- fmt.Fprintf(w, "\tpos += len(%s)\n", name)
- fmt.Fprintf(w, "\t}\n")
- }
- default:
- fmt.Fprintf(w, "\t// ??? %s %s\n", name, typ)
- return false
- }
- return true