+
+func decodeBaseType(g *GenFile, typ, orig, name string, length int, sizefrom string, alloc bool) {
+ isArray := length > 0 || sizefrom != ""
+ if isArray {
+ var size string
+ switch {
+ case length > 0:
+ size = strconv.Itoa(length)
+ case sizefrom != "":
+ size = sizefrom
+ }
+ switch typ {
+ case U8:
+ if alloc {
+ g.P(name, " = make([]byte, ", size, ")")
+ g.P("copy(", name, ", buf.DecodeBytes(len(", name, ")))")
+ } else {
+ g.P("copy(", name, "[:], buf.DecodeBytes(", size, "))")
+ }
+ return
+ case I8, I16, U16, I32, U32, I64, U64, F64:
+ if alloc {
+ g.P(name, " = make([]", orig, ", ", size, ")")
+ }
+ g.P("for i := 0; i < len(", name, "); i++ {")
+ name = fmt.Sprintf("%s[i]", name)
+ }
+ }
+ conv := func(s string) string {
+ if gotype, ok := BaseTypesGo[typ]; !ok || gotype != orig {
+ return fmt.Sprintf("%s(%s)", orig, s)
+ }
+ return s
+ }
+ switch typ {
+ case I8, I16, I32, I64:
+ typsize := BaseTypeSizes[typ]
+ g.P(name, " = ", conv(fmt.Sprintf("buf.DecodeInt%d()", typsize*8)))
+ case U8, U16, U32, U64:
+ typsize := BaseTypeSizes[typ]
+ g.P(name, " = ", conv(fmt.Sprintf("buf.DecodeUint%d()", typsize*8)))
+ case F64:
+ g.P(name, " = ", conv("buf.DecodeFloat64()"))
+ case BOOL:
+ g.P(name, " = buf.DecodeBool()")
+ case STRING:
+ g.P(name, " = buf.DecodeString(", length, ")")
+ default:
+ logrus.Panicf("\t// ??? %s %s\n", name, typ)
+ }
+ if isArray {
+ switch typ {
+ case I8, U8, I16, U16, I32, U32, I64, U64, F64:
+ g.P("}")
+ }
+ }
+}