func (p *unmarshal) field(file *generator.FileDescriptor, msg *generator.Descriptor, field *descriptor.FieldDescriptorProto, fieldname string, proto3 bool) { repeated := field.IsRepeated() nullable := gogoproto.IsNullable(field) typ := p.noStarOrSliceType(msg, field) oneof := field.OneofIndex != nil switch *field.Type { case descriptor.FieldDescriptorProto_TYPE_DOUBLE: if !p.unsafe || gogoproto.IsCastType(field) { p.P(`var v uint64`) p.decodeFixed64("v", "uint64") if oneof { p.P(`m.`, fieldname, ` = &`, p.OneOfTypeName(msg, field), `{`, typ, "(", p.mathPkg.Use(), `.Float64frombits(v))}`) } else if repeated { p.P(`v2 := `, typ, "(", p.mathPkg.Use(), `.Float64frombits(v))`) p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v2)`) } else if proto3 || !nullable { p.P(`m.`, fieldname, ` = `, typ, "(", p.mathPkg.Use(), `.Float64frombits(v))`) } else { p.P(`v2 := `, typ, "(", p.mathPkg.Use(), `.Float64frombits(v))`) p.P(`m.`, fieldname, ` = &v2`) } } else { if oneof { p.P(`var v float64`) p.unsafeFixed64("v", "float64") p.P(`m.`, fieldname, ` = &`, p.OneOfTypeName(msg, field), `{v}`) } else if repeated { p.P(`var v float64`) p.unsafeFixed64("v", "float64") p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.unsafeFixed64(`m.`+fieldname, "float64") } else { p.P(`var v float64`) p.unsafeFixed64("v", "float64") p.P(`m.`, fieldname, ` = &v`) } } case descriptor.FieldDescriptorProto_TYPE_FLOAT: if !p.unsafe || gogoproto.IsCastType(field) { p.P(`var v uint32`) p.decodeFixed32("v", "uint32") if oneof { p.P(`m.`, fieldname, ` = &`, p.OneOfTypeName(msg, field), `{`, typ, "(", p.mathPkg.Use(), `.Float32frombits(v))}`) } else if repeated { p.P(`v2 := `, typ, "(", p.mathPkg.Use(), `.Float32frombits(v))`) p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v2)`) } else if proto3 || !nullable { p.P(`m.`, fieldname, ` = `, typ, "(", p.mathPkg.Use(), `.Float32frombits(v))`) } else { p.P(`v2 := `, typ, "(", p.mathPkg.Use(), `.Float32frombits(v))`) p.P(`m.`, fieldname, ` = &v2`) } } else { if oneof { p.P(`var v float32`) p.unsafeFixed32("v", "float32") p.P(`m.`, fieldname, ` = &`, p.OneOfTypeName(msg, field), `{v}`) } else if repeated { p.P(`var v float32`) p.unsafeFixed32("v", "float32") p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.unsafeFixed32("m."+fieldname, "float32") } else { p.P(`var v float32`) p.unsafeFixed32("v", "float32") p.P(`m.`, fieldname, ` = &v`) } } case descriptor.FieldDescriptorProto_TYPE_INT64: if oneof { p.P(`var v `, typ) p.decodeVarint("v", typ) p.P(`m.`, fieldname, ` = &`, p.OneOfTypeName(msg, field), `{v}`) } else if repeated { p.P(`var v `, typ) p.decodeVarint("v", typ) p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.P(`m.`, fieldname, ` = 0`) p.decodeVarint("m."+fieldname, typ) } else { p.P(`var v `, typ) p.decodeVarint("v", typ) p.P(`m.`, fieldname, ` = &v`) } case descriptor.FieldDescriptorProto_TYPE_UINT64: if oneof { p.P(`var v `, typ) p.decodeVarint("v", typ) p.P(`m.`, fieldname, ` = &`, p.OneOfTypeName(msg, field), `{v}`) } else if repeated { p.P(`var v `, typ) p.decodeVarint("v", typ) p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.P(`m.`, fieldname, ` = 0`) p.decodeVarint("m."+fieldname, typ) } else { p.P(`var v `, typ) p.decodeVarint("v", typ) p.P(`m.`, fieldname, ` = &v`) } case descriptor.FieldDescriptorProto_TYPE_INT32: if oneof { p.P(`var v `, typ) p.decodeVarint("v", typ) p.P(`m.`, fieldname, ` = &`, p.OneOfTypeName(msg, field), `{v}`) } else if repeated { p.P(`var v `, typ) p.decodeVarint("v", typ) p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.P(`m.`, fieldname, ` = 0`) p.decodeVarint("m."+fieldname, typ) } else { p.P(`var v `, typ) p.decodeVarint("v", typ) p.P(`m.`, fieldname, ` = &v`) } case descriptor.FieldDescriptorProto_TYPE_FIXED64: if !p.unsafe || gogoproto.IsCastType(field) { if oneof { p.P(`var v `, typ) p.decodeFixed64("v", typ) p.P(`m.`, fieldname, ` = &`, p.OneOfTypeName(msg, field), `{v}`) } else if repeated { p.P(`var v `, typ) p.decodeFixed64("v", typ) p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.P(`m.`, fieldname, ` = 0`) p.decodeFixed64("m."+fieldname, typ) } else { p.P(`var v `, typ) p.decodeFixed64("v", typ) p.P(`m.`, fieldname, ` = &v`) } } else { if oneof { p.P(`var v uint64`) p.unsafeFixed64("v", "uint64") p.P(`m.`, fieldname, ` = &`, p.OneOfTypeName(msg, field), `{v}`) } else if repeated { p.P(`var v uint64`) p.unsafeFixed64("v", "uint64") p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.unsafeFixed64("m."+fieldname, "uint64") } else { p.P(`var v uint64`) p.unsafeFixed64("v", "uint64") p.P(`m.`, fieldname, ` = &v`) } } case descriptor.FieldDescriptorProto_TYPE_FIXED32: if !p.unsafe || gogoproto.IsCastType(field) { if oneof { p.P(`var v `, typ) p.decodeFixed32("v", typ) p.P(`m.`, fieldname, ` = &`, p.OneOfTypeName(msg, field), `{v}`) } else if repeated { p.P(`var v `, typ) p.decodeFixed32("v", typ) p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.P(`m.`, fieldname, ` = 0`) p.decodeFixed32("m."+fieldname, typ) } else { p.P(`var v `, typ) p.decodeFixed32("v", typ) p.P(`m.`, fieldname, ` = &v`) } } else { if oneof { p.P(`var v uint32`) p.unsafeFixed32("v", "uint32") p.P(`m.`, fieldname, ` = &`, p.OneOfTypeName(msg, field), `{v}`) } else if repeated { p.P(`var v uint32`) p.unsafeFixed32("v", "uint32") p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.unsafeFixed32("m."+fieldname, "uint32") } else { p.P(`var v uint32`) p.unsafeFixed32("v", "uint32") p.P(`m.`, fieldname, ` = &v`) } } case descriptor.FieldDescriptorProto_TYPE_BOOL: p.P(`var v int`) p.decodeVarint("v", "int") if oneof { p.P(`b := `, typ, `(v != 0)`) p.P(`m.`, fieldname, ` = &`, p.OneOfTypeName(msg, field), `{b}`) } else if repeated { p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, `, typ, `(v != 0))`) } else if proto3 || !nullable { p.P(`m.`, fieldname, ` = `, typ, `(v != 0)`) } else { p.P(`b := `, typ, `(v != 0)`) p.P(`m.`, fieldname, ` = &b`) } case descriptor.FieldDescriptorProto_TYPE_STRING: p.P(`var stringLen uint64`) p.decodeVarint("stringLen", "uint64") p.P(`intStringLen := int(stringLen)`) p.P(`if intStringLen < 0 {`) p.In() p.P(`return ErrInvalidLength` + p.localName) p.Out() p.P(`}`) p.P(`postIndex := iNdEx + intStringLen`) p.P(`if postIndex > l {`) p.In() p.P(`return `, p.ioPkg.Use(), `.ErrUnexpectedEOF`) p.Out() p.P(`}`) if oneof { p.P(`m.`, fieldname, ` = &`, p.OneOfTypeName(msg, field), `{`, typ, `(data[iNdEx:postIndex])}`) } else if repeated { p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, `, typ, `(data[iNdEx:postIndex]))`) } else if proto3 || !nullable { p.P(`m.`, fieldname, ` = `, typ, `(data[iNdEx:postIndex])`) } else { p.P(`s := `, typ, `(data[iNdEx:postIndex])`) p.P(`m.`, fieldname, ` = &s`) } p.P(`iNdEx = postIndex`) case descriptor.FieldDescriptorProto_TYPE_GROUP: panic(fmt.Errorf("unmarshaler does not support group %v", fieldname)) case descriptor.FieldDescriptorProto_TYPE_MESSAGE: desc := p.ObjectNamed(field.GetTypeName()) msgname := p.TypeName(desc) p.P(`var msglen int`) p.decodeVarint("msglen", "int") p.P(`if msglen < 0 {`) p.In() p.P(`return ErrInvalidLength` + p.localName) p.Out() p.P(`}`) p.P(`postIndex := iNdEx + msglen`) p.P(`if postIndex > l {`) p.In() p.P(`return `, p.ioPkg.Use(), `.ErrUnexpectedEOF`) p.Out() p.P(`}`) if oneof { p.P(`v := &`, msgname, `{}`) p.P(`if err := v.Unmarshal(data[iNdEx:postIndex]); err != nil {`) p.In() p.P(`return err`) p.Out() p.P(`}`) p.P(`m.`, fieldname, ` = &`, p.OneOfTypeName(msg, field), `{v}`) } else if generator.IsMap(file.FileDescriptorProto, field) { m := p.GoMapType(nil, field) keygoTyp, _ := p.GoType(nil, m.KeyField) keygoAliasTyp, _ := p.GoType(nil, m.KeyAliasField) // keys may not be pointers keygoTyp = strings.Replace(keygoTyp, "*", "", 1) keygoAliasTyp = strings.Replace(keygoAliasTyp, "*", "", 1) valuegoTyp, _ := p.GoType(nil, m.ValueField) valuegoAliasTyp, _ := p.GoType(nil, m.ValueAliasField) // if the map type is an alias and key or values are aliases (type Foo map[Bar]Baz), // we need to explicitly record their use here. p.RecordTypeUse(m.KeyAliasField.GetTypeName()) p.RecordTypeUse(m.ValueAliasField.GetTypeName()) nullable, valuegoTyp, valuegoAliasTyp = generator.GoMapValueTypes(field, m.ValueField, valuegoTyp, valuegoAliasTyp) p.P(`var keykey uint64`) p.decodeVarint("keykey", "uint64") p.mapField("mapkey", m.KeyAliasField) p.P(`var valuekey uint64`) p.decodeVarint("valuekey", "uint64") p.mapField("mapvalue", m.ValueAliasField) p.P(`if m.`, fieldname, ` == nil {`) p.In() p.P(`m.`, fieldname, ` = make(`, m.GoType, `)`) p.Out() p.P(`}`) s := `m.` + fieldname if keygoTyp == keygoAliasTyp { s += `[mapkey]` } else { s += `[` + keygoAliasTyp + `(mapkey)]` } v := `mapvalue` if m.ValueField.IsMessage() && !nullable { v = `*` + v } if valuegoTyp != valuegoAliasTyp { v = `((` + valuegoAliasTyp + `)(` + v + `))` } p.P(s, ` = `, v) } else if repeated { if nullable { p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, &`, msgname, `{})`) } else { p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, `, msgname, `{})`) } p.P(`if err := m.`, fieldname, `[len(m.`, fieldname, `)-1].Unmarshal(data[iNdEx:postIndex]); err != nil {`) p.In() p.P(`return err`) p.Out() p.P(`}`) } else if nullable { p.P(`if m.`, fieldname, ` == nil {`) p.In() p.P(`m.`, fieldname, ` = &`, msgname, `{}`) p.Out() p.P(`}`) p.P(`if err := m.`, fieldname, `.Unmarshal(data[iNdEx:postIndex]); err != nil {`) p.In() p.P(`return err`) p.Out() p.P(`}`) } else { p.P(`if err := m.`, fieldname, `.Unmarshal(data[iNdEx:postIndex]); err != nil {`) p.In() p.P(`return err`) p.Out() p.P(`}`) } p.P(`iNdEx = postIndex`) case descriptor.FieldDescriptorProto_TYPE_BYTES: p.P(`var byteLen int`) p.decodeVarint("byteLen", "int") p.P(`if byteLen < 0 {`) p.In() p.P(`return ErrInvalidLength` + p.localName) p.Out() p.P(`}`) p.P(`postIndex := iNdEx + byteLen`) p.P(`if postIndex > l {`) p.In() p.P(`return `, p.ioPkg.Use(), `.ErrUnexpectedEOF`) p.Out() p.P(`}`) if !gogoproto.IsCustomType(field) { if oneof { p.P(`v := make([]byte, postIndex-iNdEx)`) p.P(`copy(v, data[iNdEx:postIndex])`) p.P(`m.`, fieldname, ` = &`, p.OneOfTypeName(msg, field), `{v}`) } else if repeated { p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, make([]byte, postIndex-iNdEx))`) p.P(`copy(m.`, fieldname, `[len(m.`, fieldname, `)-1], data[iNdEx:postIndex])`) } else { p.P(`m.`, fieldname, ` = append(m.`, fieldname, `[:0] , data[iNdEx:postIndex]...)`) p.P(`if m.`, fieldname, ` == nil {`) p.In() p.P(`m.`, fieldname, ` = []byte{}`) p.Out() p.P(`}`) } } else { _, ctyp, err := generator.GetCustomType(field) if err != nil { panic(err) } if oneof { p.P(`var vv `, ctyp) p.P(`v := &vv`) p.P(`if err := v.Unmarshal(data[iNdEx:postIndex]); err != nil {`) p.In() p.P(`return err`) p.Out() p.P(`}`) p.P(`m.`, fieldname, ` = &`, p.OneOfTypeName(msg, field), `{*v}`) } else if repeated { p.P(`var v `, ctyp) p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) p.P(`if err := m.`, fieldname, `[len(m.`, fieldname, `)-1].Unmarshal(data[iNdEx:postIndex]); err != nil {`) p.In() p.P(`return err`) p.Out() p.P(`}`) } else if nullable { p.P(`var v `, ctyp) p.P(`m.`, fieldname, ` = &v`) p.P(`if err := m.`, fieldname, `.Unmarshal(data[iNdEx:postIndex]); err != nil {`) p.In() p.P(`return err`) p.Out() p.P(`}`) } else { p.P(`if err := m.`, fieldname, `.Unmarshal(data[iNdEx:postIndex]); err != nil {`) p.In() p.P(`return err`) p.Out() p.P(`}`) } } p.P(`iNdEx = postIndex`) case descriptor.FieldDescriptorProto_TYPE_UINT32: if oneof { p.P(`var v `, typ) p.decodeVarint("v", typ) p.P(`m.`, fieldname, ` = &`, p.OneOfTypeName(msg, field), `{v}`) } else if repeated { p.P(`var v `, typ) p.decodeVarint("v", typ) p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.P(`m.`, fieldname, ` = 0`) p.decodeVarint("m."+fieldname, typ) } else { p.P(`var v `, typ) p.decodeVarint("v", typ) p.P(`m.`, fieldname, ` = &v`) } case descriptor.FieldDescriptorProto_TYPE_ENUM: typName := p.TypeName(p.ObjectNamed(field.GetTypeName())) if oneof { p.P(`var v `, typName) p.decodeVarint("v", typName) p.P(`m.`, fieldname, ` = &`, p.OneOfTypeName(msg, field), `{v}`) } else if repeated { p.P(`var v `, typName) p.decodeVarint("v", typName) p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.P(`m.`, fieldname, ` = 0`) p.decodeVarint("m."+fieldname, typName) } else { p.P(`var v `, typName) p.decodeVarint("v", typName) p.P(`m.`, fieldname, ` = &v`) } case descriptor.FieldDescriptorProto_TYPE_SFIXED32: if !p.unsafe || gogoproto.IsCastType(field) { if oneof { p.P(`var v `, typ) p.decodeFixed32("v", typ) p.P(`m.`, fieldname, ` = &`, p.OneOfTypeName(msg, field), `{v}`) } else if repeated { p.P(`var v `, typ) p.decodeFixed32("v", typ) p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.P(`m.`, fieldname, ` = 0`) p.decodeFixed32("m."+fieldname, typ) } else { p.P(`var v `, typ) p.decodeFixed32("v", typ) p.P(`m.`, fieldname, ` = &v`) } } else { if oneof { p.P(`var v int32`) p.unsafeFixed32("v", "int32") p.P(`m.`, fieldname, ` = &`, p.OneOfTypeName(msg, field), `{v}`) } else if repeated { p.P(`var v int32`) p.unsafeFixed32("v", "int32") p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.unsafeFixed32("m."+fieldname, "int32") } else { p.P(`var v int32`) p.unsafeFixed32("v", "int32") p.P(`m.`, fieldname, ` = &v`) } } case descriptor.FieldDescriptorProto_TYPE_SFIXED64: if !p.unsafe || gogoproto.IsCastType(field) { if oneof { p.P(`var v `, typ) p.decodeFixed64("v", typ) p.P(`m.`, fieldname, ` = &`, p.OneOfTypeName(msg, field), `{v}`) } else if repeated { p.P(`var v `, typ) p.decodeFixed64("v", typ) p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.P(`m.`, fieldname, ` = 0`) p.decodeFixed64("m."+fieldname, typ) } else { p.P(`var v `, typ) p.decodeFixed64("v", typ) p.P(`m.`, fieldname, ` = &v`) } } else { if oneof { p.P(`var v int64`) p.unsafeFixed64("v", "int64") p.P(`m.`, fieldname, ` = &`, p.OneOfTypeName(msg, field), `{v}`) } else if repeated { p.P(`var v int64`) p.unsafeFixed64("v", "int64") p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.unsafeFixed64("m."+fieldname, "int64") } else { p.P(`var v int64`) p.unsafeFixed64("v", "int64") p.P(`m.`, fieldname, ` = &v`) } } case descriptor.FieldDescriptorProto_TYPE_SINT32: p.P(`var v `, typ) p.decodeVarint("v", typ) p.P(`v = `, typ, `((uint32(v) >> 1) ^ uint32(((v&1)<<31)>>31))`) if oneof { p.P(`m.`, fieldname, ` = &`, p.OneOfTypeName(msg, field), `{v}`) } else if repeated { p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.P(`m.`, fieldname, ` = v`) } else { p.P(`m.`, fieldname, ` = &v`) } case descriptor.FieldDescriptorProto_TYPE_SINT64: p.P(`var v uint64`) p.decodeVarint("v", "uint64") p.P(`v = (v >> 1) ^ uint64((int64(v&1)<<63)>>63)`) if oneof { p.P(`m.`, fieldname, ` = &`, p.OneOfTypeName(msg, field), `{`, typ, `(v)}`) } else if repeated { p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, `, typ, `(v))`) } else if proto3 || !nullable { p.P(`m.`, fieldname, ` = `, typ, `(v)`) } else { p.P(`v2 := `, typ, `(v)`) p.P(`m.`, fieldname, ` = &v2`) } default: panic("not implemented") } }
func (p *unmarshal) field(file *descriptor.FileDescriptorProto, field *descriptor.FieldDescriptorProto, fieldname string, proto3 bool) { repeated := field.IsRepeated() nullable := gogoproto.IsNullable(field) switch *field.Type { case descriptor.FieldDescriptorProto_TYPE_DOUBLE: if !p.unsafe { p.P(`var v uint64`) p.decodeFixed64("v", "uint64") if repeated { p.P(`v2 := `, p.mathPkg.Use(), `.Float64frombits(v)`) p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v2)`) } else if proto3 || !nullable { p.P(`m.`, fieldname, ` = `, p.mathPkg.Use(), `.Float64frombits(v)`) } else { p.P(`v2 := `, p.mathPkg.Use(), `.Float64frombits(v)`) p.P(`m.`, fieldname, ` = &v2`) } } else { if repeated { p.P(`var v float64`) p.unsafeFixed64("v", "float64") p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.unsafeFixed64(`m.`+fieldname, "float64") } else { p.P(`var v float64`) p.unsafeFixed64("v", "float64") p.P(`m.`, fieldname, ` = &v`) } } case descriptor.FieldDescriptorProto_TYPE_FLOAT: if !p.unsafe { p.P(`var v uint32`) p.decodeFixed32("v", "uint32") if repeated { p.P(`v2 := `, p.mathPkg.Use(), `.Float32frombits(v)`) p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v2)`) } else if proto3 || !nullable { p.P(`m.`, fieldname, ` = `, p.mathPkg.Use(), `.Float32frombits(v)`) } else { p.P(`v2 := `, p.mathPkg.Use(), `.Float32frombits(v)`) p.P(`m.`, fieldname, ` = &v2`) } } else { if repeated { p.P(`var v float32`) p.unsafeFixed32("v", "float32") p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.unsafeFixed32("m."+fieldname, "float32") } else { p.P(`var v float32`) p.unsafeFixed32("v", "float32") p.P(`m.`, fieldname, ` = &v`) } } case descriptor.FieldDescriptorProto_TYPE_INT64: if repeated { p.P(`var v int64`) p.decodeVarint("v", "int64") p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.decodeVarint("m."+fieldname, "int64") } else { p.P(`var v int64`) p.decodeVarint("v", "int64") p.P(`m.`, fieldname, ` = &v`) } case descriptor.FieldDescriptorProto_TYPE_UINT64: if repeated { p.P(`var v uint64`) p.decodeVarint("v", "uint64") p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.decodeVarint("m."+fieldname, "uint64") } else { p.P(`var v uint64`) p.decodeVarint("v", "uint64") p.P(`m.`, fieldname, ` = &v`) } case descriptor.FieldDescriptorProto_TYPE_INT32: if repeated { p.P(`var v int32`) p.decodeVarint("v", "int32") p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.decodeVarint("m."+fieldname, "int32") } else { p.P(`var v int32`) p.decodeVarint("v", "int32") p.P(`m.`, fieldname, ` = &v`) } case descriptor.FieldDescriptorProto_TYPE_FIXED64: if !p.unsafe { if repeated { p.P(`var v uint64`) p.decodeFixed64("v", "uint64") p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.decodeFixed64("m."+fieldname, "uint64") } else { p.P(`var v uint64`) p.decodeFixed64("v", "uint64") p.P(`m.`, fieldname, ` = &v`) } } else { if repeated { p.P(`var v uint64`) p.unsafeFixed64("v", "uint64") p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.unsafeFixed64("m."+fieldname, "uint64") } else { p.P(`var v uint64`) p.unsafeFixed64("v", "uint64") p.P(`m.`, fieldname, ` = &v`) } } case descriptor.FieldDescriptorProto_TYPE_FIXED32: if !p.unsafe { if repeated { p.P(`var v uint32`) p.decodeFixed32("v", "uint32") p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.decodeFixed32("m."+fieldname, "uint32") } else { p.P(`var v uint32`) p.decodeFixed32("v", "uint32") p.P(`m.`, fieldname, ` = &v`) } } else { if repeated { p.P(`var v uint32`) p.unsafeFixed32("v", "uint32") p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.unsafeFixed32("m."+fieldname, "uint32") } else { p.P(`var v uint32`) p.unsafeFixed32("v", "uint32") p.P(`m.`, fieldname, ` = &v`) } } case descriptor.FieldDescriptorProto_TYPE_BOOL: p.P(`var v int`) p.decodeVarint("v", "int") if repeated { p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, bool(v != 0))`) } else if proto3 || !nullable { p.P(`m.`, fieldname, ` = bool(v != 0)`) } else { p.P(`b := bool(v != 0)`) p.P(`m.`, fieldname, ` = &b`) } case descriptor.FieldDescriptorProto_TYPE_STRING: p.P(`var stringLen uint64`) p.decodeVarint("stringLen", "uint64") p.P(`postIndex := iNdEx + int(stringLen)`) p.P(`if postIndex > l {`) p.In() p.P(`return `, p.ioPkg.Use(), `.ErrUnexpectedEOF`) p.Out() p.P(`}`) if repeated { p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, string(data[iNdEx:postIndex]))`) } else if proto3 || !nullable { p.P(`m.`, fieldname, ` = string(data[iNdEx:postIndex])`) } else { p.P(`s := string(data[iNdEx:postIndex])`) p.P(`m.`, fieldname, ` = &s`) } p.P(`iNdEx = postIndex`) case descriptor.FieldDescriptorProto_TYPE_GROUP: panic(fmt.Errorf("unmarshaler does not support group %v", fieldname)) case descriptor.FieldDescriptorProto_TYPE_MESSAGE: desc := p.ObjectNamed(field.GetTypeName()) msgname := p.TypeName(desc) p.P(`var msglen int`) p.decodeVarint("msglen", "int") p.P(`postIndex := iNdEx + msglen`) p.P(`if postIndex > l {`) p.In() p.P(`return `, p.ioPkg.Use(), `.ErrUnexpectedEOF`) p.Out() p.P(`}`) if generator.IsMap(file, field) { mapMsg := generator.GetMap(file, field) keyField, valueField := mapMsg.GetMapFields() keygoTyp, _ := p.GoType(nil, keyField) keygoTyp = strings.Replace(keygoTyp, "*", "", 1) valuegoTyp, _ := p.GoType(nil, valueField) if !valueField.IsMessage() { valuegoTyp = strings.Replace(valuegoTyp, "*", "", 1) } p.P(`var keykey uint64`) p.decodeVarint("keykey", "uint64") p.mapField("mapkey", keyField) p.P(`var valuekey uint64`) p.decodeVarint("valuekey", "uint64") p.mapField("mapvalue", valueField) p.P(`if m.`, fieldname, ` == nil {`) p.In() p.P(`m.`, fieldname, ` = make(map[`, keygoTyp, `]`, valuegoTyp, `)`) p.Out() p.P(`}`) p.P(`m.`, fieldname, `[mapkey] = mapvalue`) } else if repeated { if nullable { p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, &`, msgname, `{})`) } else { p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, `, msgname, `{})`) } p.P(`if err := m.`, fieldname, `[len(m.`, fieldname, `)-1].Unmarshal(data[iNdEx:postIndex]); err != nil {`) p.In() p.P(`return err`) p.Out() p.P(`}`) } else if nullable { p.P(`if m.`, fieldname, ` == nil {`) p.In() p.P(`m.`, fieldname, ` = &`, msgname, `{}`) p.Out() p.P(`}`) p.P(`if err := m.`, fieldname, `.Unmarshal(data[iNdEx:postIndex]); err != nil {`) p.In() p.P(`return err`) p.Out() p.P(`}`) } else { p.P(`if err := m.`, fieldname, `.Unmarshal(data[iNdEx:postIndex]); err != nil {`) p.In() p.P(`return err`) p.Out() p.P(`}`) } p.P(`iNdEx = postIndex`) case descriptor.FieldDescriptorProto_TYPE_BYTES: p.P(`var byteLen int`) p.decodeVarint("byteLen", "int") p.P(`postIndex := iNdEx + byteLen`) p.P(`if postIndex > l {`) p.In() p.P(`return `, p.ioPkg.Use(), `.ErrUnexpectedEOF`) p.Out() p.P(`}`) if !gogoproto.IsCustomType(field) { if repeated { p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, make([]byte, postIndex-iNdEx))`) p.P(`copy(m.`, fieldname, `[len(m.`, fieldname, `)-1], data[iNdEx:postIndex])`) } else { p.P(`m.`, fieldname, ` = append([]byte{}`, `, data[iNdEx:postIndex]...)`) } } else { _, ctyp, err := generator.GetCustomType(field) if err != nil { panic(err) } if repeated { p.P(`var v `, ctyp) p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) p.P(`if err := m.`, fieldname, `[len(m.`, fieldname, `)-1].Unmarshal(data[iNdEx:postIndex]); err != nil {`) p.In() p.P(`return err`) p.Out() p.P(`}`) } else if nullable { p.P(`var v `, ctyp) p.P(`m.`, fieldname, ` = &v`) p.P(`if err := m.`, fieldname, `.Unmarshal(data[iNdEx:postIndex]); err != nil {`) p.In() p.P(`return err`) p.Out() p.P(`}`) } else { p.P(`if err := m.`, fieldname, `.Unmarshal(data[iNdEx:postIndex]); err != nil {`) p.In() p.P(`return err`) p.Out() p.P(`}`) } } p.P(`iNdEx = postIndex`) case descriptor.FieldDescriptorProto_TYPE_UINT32: if repeated { p.P(`var v uint32`) p.decodeVarint("v", "uint32") p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.decodeVarint("m."+fieldname, "uint32") } else { p.P(`var v uint32`) p.decodeVarint("v", "uint32") p.P(`m.`, fieldname, ` = &v`) } case descriptor.FieldDescriptorProto_TYPE_ENUM: typName := p.TypeName(p.ObjectNamed(field.GetTypeName())) if repeated { p.P(`var v `, typName) p.decodeVarint("v", typName) p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.decodeVarint("m."+fieldname, typName) } else { p.P(`var v `, typName) p.decodeVarint("v", typName) p.P(`m.`, fieldname, ` = &v`) } case descriptor.FieldDescriptorProto_TYPE_SFIXED32: if !p.unsafe { if repeated { p.P(`var v int32`) p.decodeFixed32("v", "int32") p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.decodeFixed32("m."+fieldname, "int32") } else { p.P(`var v int32`) p.decodeFixed32("v", "int32") p.P(`m.`, fieldname, ` = &v`) } } else { if repeated { p.P(`var v int32`) p.unsafeFixed32("v", "int32") p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.unsafeFixed32("m."+fieldname, "int32") } else { p.P(`var v int32`) p.unsafeFixed32("v", "int32") p.P(`m.`, fieldname, ` = &v`) } } case descriptor.FieldDescriptorProto_TYPE_SFIXED64: if !p.unsafe { if repeated { p.P(`var v int64`) p.decodeFixed64("v", "int64") p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.decodeFixed64("m."+fieldname, "int64") } else { p.P(`var v int64`) p.decodeFixed64("v", "int64") p.P(`m.`, fieldname, ` = &v`) } } else { if repeated { p.P(`var v int64`) p.unsafeFixed64("v", "int64") p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.unsafeFixed64("m."+fieldname, "int64") } else { p.P(`var v int64`) p.unsafeFixed64("v", "int64") p.P(`m.`, fieldname, ` = &v`) } } case descriptor.FieldDescriptorProto_TYPE_SINT32: p.P(`var v int32`) p.decodeVarint("v", "int32") p.P(`v = int32((uint32(v) >> 1) ^ uint32(((v&1)<<31)>>31))`) if repeated { p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.P(`m.`, fieldname, ` = v`) } else { p.P(`m.`, fieldname, ` = &v`) } case descriptor.FieldDescriptorProto_TYPE_SINT64: p.P(`var v uint64`) p.decodeVarint("v", "uint64") p.P(`v = (v >> 1) ^ uint64((int64(v&1)<<63)>>63)`) if repeated { p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, int64(v))`) } else if proto3 || !nullable { p.P(`m.`, fieldname, ` = int64(v)`) } else { p.P(`v2 := int64(v)`) p.P(`m.`, fieldname, ` = &v2`) } default: panic("not implemented") } }
func (p *unmarshal) mapField(varName string, customType bool, field *descriptor.FieldDescriptorProto) { switch field.GetType() { case descriptor.FieldDescriptorProto_TYPE_DOUBLE: p.P(`var `, varName, `temp uint64`) p.decodeFixed64(varName+"temp", "uint64") p.P(varName, ` := `, p.mathPkg.Use(), `.Float64frombits(`, varName, `temp)`) case descriptor.FieldDescriptorProto_TYPE_FLOAT: p.P(`var `, varName, `temp uint32`) p.decodeFixed32(varName+"temp", "uint32") p.P(varName, ` := `, p.mathPkg.Use(), `.Float32frombits(`, varName, `temp)`) case descriptor.FieldDescriptorProto_TYPE_INT64: p.P(`var `, varName, ` int64`) p.decodeVarint(varName, "int64") case descriptor.FieldDescriptorProto_TYPE_UINT64: p.P(`var `, varName, ` uint64`) p.decodeVarint(varName, "uint64") case descriptor.FieldDescriptorProto_TYPE_INT32: p.P(`var `, varName, ` int32`) p.decodeVarint(varName, "int32") case descriptor.FieldDescriptorProto_TYPE_FIXED64: p.P(`var `, varName, ` uint64`) p.decodeFixed64(varName, "uint64") case descriptor.FieldDescriptorProto_TYPE_FIXED32: p.P(`var `, varName, ` uint32`) p.decodeFixed32(varName, "uint32") case descriptor.FieldDescriptorProto_TYPE_BOOL: p.P(`var `, varName, `temp int`) p.decodeVarint(varName+"temp", "int") p.P(varName, ` := bool(`, varName, `temp != 0)`) case descriptor.FieldDescriptorProto_TYPE_STRING: p.P(`var stringLen`, varName, ` uint64`) p.decodeVarint("stringLen"+varName, "uint64") p.P(`intStringLen`, varName, ` := int(stringLen`, varName, `)`) p.P(`if intStringLen`, varName, ` < 0 {`) p.In() p.P(`return ErrInvalidLength` + p.localName) p.Out() p.P(`}`) p.P(`postStringIndex`, varName, ` := iNdEx + intStringLen`, varName) p.P(`if postStringIndex`, varName, ` > l {`) p.In() p.P(`return `, p.ioPkg.Use(), `.ErrUnexpectedEOF`) p.Out() p.P(`}`) cast, _ := p.GoType(nil, field) cast = strings.Replace(cast, "*", "", 1) p.P(varName, ` := `, cast, `(dAtA[iNdEx:postStringIndex`, varName, `])`) p.P(`iNdEx = postStringIndex`, varName) case descriptor.FieldDescriptorProto_TYPE_MESSAGE: p.P(`var mapmsglen int`) p.decodeVarint("mapmsglen", "int") p.P(`if mapmsglen < 0 {`) p.In() p.P(`return ErrInvalidLength` + p.localName) p.Out() p.P(`}`) p.P(`postmsgIndex := iNdEx + mapmsglen`) p.P(`if mapmsglen < 0 {`) p.In() p.P(`return ErrInvalidLength` + p.localName) p.Out() p.P(`}`) p.P(`if postmsgIndex > l {`) p.In() p.P(`return `, p.ioPkg.Use(), `.ErrUnexpectedEOF`) p.Out() p.P(`}`) desc := p.ObjectNamed(field.GetTypeName()) msgname := p.TypeName(desc) buf := `dAtA[iNdEx:postmsgIndex]` if gogoproto.IsStdTime(field) { p.P(varName, ` := new(time.Time)`) p.P(`if err := `, p.typesPkg.Use(), `.StdTimeUnmarshal(`, varName, `, `, buf, `); err != nil {`) } else if gogoproto.IsStdDuration(field) { p.P(varName, ` := new(time.Duration)`) p.P(`if err := `, p.typesPkg.Use(), `.StdDurationUnmarshal(`, varName, `, `, buf, `); err != nil {`) } else { p.P(varName, ` := &`, msgname, `{}`) p.P(`if err := `, varName, `.Unmarshal(`, buf, `); err != nil {`) } p.In() p.P(`return err`) p.Out() p.P(`}`) p.P(`iNdEx = postmsgIndex`) case descriptor.FieldDescriptorProto_TYPE_BYTES: p.P(`var mapbyteLen uint64`) p.decodeVarint("mapbyteLen", "uint64") p.P(`intMapbyteLen := int(mapbyteLen)`) p.P(`if intMapbyteLen < 0 {`) p.In() p.P(`return ErrInvalidLength` + p.localName) p.Out() p.P(`}`) p.P(`postbytesIndex := iNdEx + intMapbyteLen`) p.P(`if postbytesIndex > l {`) p.In() p.P(`return `, p.ioPkg.Use(), `.ErrUnexpectedEOF`) p.Out() p.P(`}`) if customType { _, ctyp, err := generator.GetCustomType(field) if err != nil { panic(err) } p.P(`var `, varName, `1 `, ctyp) p.P(`var `, varName, ` = &`, varName, `1`) p.P(`if err := `, varName, `.Unmarshal(dAtA[iNdEx:postbytesIndex]); err != nil {`) p.In() p.P(`return err`) p.Out() p.P(`}`) } else { p.P(varName, ` := make([]byte, mapbyteLen)`) p.P(`copy(`, varName, `, dAtA[iNdEx:postbytesIndex])`) } p.P(`iNdEx = postbytesIndex`) case descriptor.FieldDescriptorProto_TYPE_UINT32: p.P(`var `, varName, ` uint32`) p.decodeVarint(varName, "uint32") case descriptor.FieldDescriptorProto_TYPE_ENUM: typName := p.TypeName(p.ObjectNamed(field.GetTypeName())) p.P(`var `, varName, ` `, typName) p.decodeVarint(varName, typName) case descriptor.FieldDescriptorProto_TYPE_SFIXED32: p.P(`var `, varName, ` int32`) p.decodeFixed32(varName, "int32") case descriptor.FieldDescriptorProto_TYPE_SFIXED64: p.P(`var `, varName, ` int64`) p.decodeFixed64(varName, "int64") case descriptor.FieldDescriptorProto_TYPE_SINT32: p.P(`var `, varName, `temp int32`) p.decodeVarint(varName+"temp", "int32") p.P(varName, `temp = int32((uint32(`, varName, `temp) >> 1) ^ uint32(((`, varName, `temp&1)<<31)>>31))`) p.P(varName, ` := int32(`, varName, `temp)`) case descriptor.FieldDescriptorProto_TYPE_SINT64: p.P(`var `, varName, `temp uint64`) p.decodeVarint(varName+"temp", "uint64") p.P(varName, `temp = (`, varName, `temp >> 1) ^ uint64((int64(`, varName, `temp&1)<<63)>>63)`) p.P(varName, ` := int64(`, varName, `temp)`) } }