func (p *plugin) hasLoop(field *descriptor.FieldDescriptorProto, visited []*generator.Descriptor, excludes []*generator.Descriptor) *generator.Descriptor { if field.IsMessage() || p.IsGroup(field) { fieldMessage := p.ObjectNamed(field.GetTypeName()).(*generator.Descriptor) fieldTypeName := generator.CamelCaseSlice(fieldMessage.TypeName()) for _, message := range visited { messageTypeName := generator.CamelCaseSlice(message.TypeName()) if fieldTypeName == messageTypeName { for _, e := range excludes { if fieldTypeName == generator.CamelCaseSlice(e.TypeName()) { return nil } } return fieldMessage } } for _, f := range fieldMessage.Field { visited = append(visited, fieldMessage) loopTo := p.hasLoop(f, visited, excludes) if loopTo != nil { return loopTo } } } return nil }
func (p *plugin) getEnumVal(field *descriptor.FieldDescriptorProto, goTyp string) string { enum := p.ObjectNamed(field.GetTypeName()).(*generator.EnumDescriptor) l := len(enum.Value) values := make([]string, l) for i := range enum.Value { values[i] = strconv.Itoa(int(*enum.Value[i].Number)) } arr := "[]int32{" + strings.Join(values, ",") + "}" val := strings.Join([]string{generator.GoTypeToName(goTyp), `(`, arr, `[r.Intn(`, fmt.Sprintf("%d", l), `)])`}, "") return val }
func GetMap(file *descriptor.FileDescriptorProto, field *descriptor.FieldDescriptorProto) *descriptor.DescriptorProto { if !field.IsMessage() { return nil } typeName := field.GetTypeName() if strings.Contains(typeName, "Map") && !strings.HasSuffix(typeName, "Entry") { typeName += "." + CamelCase(field.GetName()) + "Entry" } ts := strings.Split(typeName, ".") if len(ts) == 1 { return file.GetMessage(typeName) } newTypeName := strings.Join(ts[2:], ".") return file.GetMessage(newTypeName) }
func new(rootPackage string, rootMessage string, descSet *descriptor.FileDescriptorSet, path string) (*fdesc, error) { fieldpaths := strings.Split(path, ".") keys := make([]uint64, len(fieldpaths)) fields := make([]*descriptor.FieldDescriptorProto, len(fieldpaths)) curPackage := rootPackage curMessage := rootMessage last := len(fieldpaths) - 1 var fieldDesc *descriptor.FieldDescriptorProto for i, f := range fieldpaths { fieldName := f fieldDesc = descSet.GetField(curPackage, curMessage, fieldName) if fieldDesc == nil { curPackage, fieldDesc = descSet.FindExtension(curPackage, curMessage, fieldName) if fieldDesc == nil { return nil, &errChild{fieldName: fieldName, pkg: curPackage, msg: curMessage} } typeNames := strings.Split(fieldDesc.GetTypeName(), ".") curMessage = fieldDesc.GetTypeName() if len(typeNames) > 1 { curPackage = typeNames[1] curMessage = typeNames[2] } fieldKey := fieldDesc.GetKeyUint64() keys[i] = fieldKey fields[i] = fieldDesc } else { fieldKey := fieldDesc.GetKeyUint64() if fieldDesc.IsMessage() { curPackage, curMessage = descSet.FindMessage(curPackage, curMessage, fieldName) } else if i != last { return nil, &errMessage{fieldName} } keys[i] = fieldKey fields[i] = fieldDesc } } fd := &fdesc{curPackage, curMessage, fields, fieldDesc, keys, 0} if fieldDesc.GetType() == descriptor.FieldDescriptorProto_TYPE_ENUM { typeNames := strings.Split(fieldDesc.GetTypeName(), ".") enumMessage := fieldDesc.GetTypeName() enumPackage := curPackage if len(typeNames) > 1 { enumPackage = typeNames[1] enumMessage = typeNames[2] } enum := descSet.GetEnum(enumPackage, enumMessage) if enum == nil { return nil, &errChild{fieldName: fieldDesc.GetName(), pkg: enumPackage, msg: enumMessage} } for _, v := range enum.GetValue() { if v.GetNumber() < fd.firstEnumValue { fd.firstEnumValue = v.GetNumber() } } } return fd, nil }
func (g *Generator) IsGroup(field *descriptor.FieldDescriptorProto) bool { if d, ok := g.typeNameToObject[field.GetTypeName()].(*Descriptor); ok { return d.IsGroup() } return false }
func (p *plugin) GenerateField(file *generator.FileDescriptor, message *generator.Descriptor, field *descriptor.FieldDescriptorProto) { proto3 := gogoproto.IsProto3(file.FileDescriptorProto) goTyp, _ := p.GoType(message, field) fieldname := p.GetFieldName(message, field) goTypName := generator.GoTypeToName(goTyp) if generator.IsMap(file.FileDescriptorProto, field) { mapmsg := generator.GetMap(file.FileDescriptorProto, field) mapkey, mapvalue := mapmsg.GetMapFields() keygoTyp, _ := p.GoType(nil, mapkey) valuegoTyp, _ := p.GoType(nil, mapvalue) keytypName := generator.GoTypeToName(keygoTyp) valuetypName := generator.GoTypeToName(valuegoTyp) mapvaluegoType := valuegoTyp if !mapvalue.IsMessage() { mapvaluegoType = strings.Replace(mapvaluegoType, "*", "", 1) } p.P(p.varGen.Next(), ` := r.Intn(10)`) p.P(`this.`, fieldname, ` = make(map[`, strings.Replace(keygoTyp, "*", "", 1), `]`, mapvaluegoType, `)`) p.P(`for i := 0; i < `, p.varGen.Current(), `; i++ {`) p.In() keyval := "" if mapkey.IsString() { keyval = fmt.Sprintf("randString%v(r)", p.localName) } else { keyval = value(keytypName, mapkey.GetType()) } if mapvalue.IsMessage() || p.IsGroup(field) { s := `this.` + fieldname + `[` + keyval + `]` + ` = ` goTypName := generator.GoTypeToName(valuegoTyp) funcCall := getFuncCall(goTypName) s += funcCall p.P(s) } else if mapvalue.IsEnum() { s := `this.` + fieldname + `[` + keyval + `]` + ` = ` + p.getEnumVal(mapvalue, valuegoTyp) p.P(s) } else if mapvalue.IsBytes() { count := p.varGen.Next() p.P(count, ` := r.Intn(100)`) p.P(p.varGen.Next(), ` := `, keyval) p.P(`this.`, fieldname, `[`, p.varGen.Current(), `] = make(`, valuegoTyp, `, `, count, `)`) p.P(`for i := 0; i < `, count, `; i++ {`) p.In() p.P(`this.`, fieldname, `[`, p.varGen.Current(), `][i] = byte(r.Intn(256))`) p.Out() p.P(`}`) } else if mapvalue.IsString() { s := `this.` + fieldname + `[` + keyval + `]` + ` = ` + fmt.Sprintf("randString%v(r)", p.localName) p.P(s) } else { p.P(p.varGen.Next(), ` := `, keyval) p.P(`this.`, fieldname, `[`, p.varGen.Current(), `] = `, value(valuetypName, mapvalue.GetType())) if negative(mapvalue.GetType()) { p.P(`if r.Intn(2) == 0 {`) p.In() p.P(`this.`, fieldname, `[`, p.varGen.Current(), `] *= -1`) p.Out() p.P(`}`) } } p.Out() p.P(`}`) } else if field.IsMessage() || p.IsGroup(field) { funcCall := getFuncCall(goTypName) if field.IsRepeated() { p.P(p.varGen.Next(), ` := r.Intn(10)`) p.P(`this.`, fieldname, ` = make(`, goTyp, `, `, p.varGen.Current(), `)`) p.P(`for i := 0; i < `, p.varGen.Current(), `; i++ {`) p.In() if gogoproto.IsNullable(field) { p.P(`this.`, fieldname, `[i] = `, funcCall) } else { p.P(p.varGen.Next(), `:= `, funcCall) p.P(`this.`, fieldname, `[i] = *`, p.varGen.Current()) } p.Out() p.P(`}`) } else { if gogoproto.IsNullable(field) { p.P(`this.`, fieldname, ` = `, funcCall) } else { p.P(p.varGen.Next(), `:= `, funcCall) p.P(`this.`, fieldname, ` = *`, p.varGen.Current()) } } } else { if field.IsEnum() { val := p.getEnumVal(field, goTyp) if field.IsRepeated() { p.P(p.varGen.Next(), ` := r.Intn(10)`) p.P(`this.`, fieldname, ` = make(`, goTyp, `, `, p.varGen.Current(), `)`) p.P(`for i := 0; i < `, p.varGen.Current(), `; i++ {`) p.In() p.P(`this.`, fieldname, `[i] = `, val) p.Out() p.P(`}`) } else if !gogoproto.IsNullable(field) || proto3 { p.P(`this.`, fieldname, ` = `, val) } else { p.P(p.varGen.Next(), ` := `, val) p.P(`this.`, fieldname, ` = &`, p.varGen.Current()) } } else if gogoproto.IsCustomType(field) { funcCall := getCustomFuncCall(goTypName) if field.IsRepeated() { p.P(p.varGen.Next(), ` := r.Intn(10)`) p.P(`this.`, fieldname, ` = make(`, goTyp, `, `, p.varGen.Current(), `)`) p.P(`for i := 0; i < `, p.varGen.Current(), `; i++ {`) p.In() p.P(p.varGen.Next(), `:= `, funcCall) p.P(`this.`, fieldname, `[i] = *`, p.varGen.Current()) p.Out() p.P(`}`) } else if gogoproto.IsNullable(field) { p.P(`this.`, fieldname, ` = `, funcCall) } else { p.P(p.varGen.Next(), `:= `, funcCall) p.P(`this.`, fieldname, ` = *`, p.varGen.Current()) } } else if field.IsBytes() { if field.IsRepeated() { p.P(p.varGen.Next(), ` := r.Intn(100)`) p.P(`this.`, fieldname, ` = make(`, goTyp, `, `, p.varGen.Current(), `)`) p.P(`for i := 0; i < `, p.varGen.Current(), `; i++ {`) p.In() p.P(p.varGen.Next(), ` := r.Intn(100)`) p.P(`this.`, fieldname, `[i] = make([]byte,`, p.varGen.Current(), `)`) p.P(`for j := 0; j < `, p.varGen.Current(), `; j++ {`) p.In() p.P(`this.`, fieldname, `[i][j] = byte(r.Intn(256))`) p.Out() p.P(`}`) p.Out() p.P(`}`) } else { p.P(p.varGen.Next(), ` := r.Intn(100)`) p.P(`this.`, fieldname, ` = make(`, goTyp, `, `, p.varGen.Current(), `)`) p.P(`for i := 0; i < `, p.varGen.Current(), `; i++ {`) p.In() p.P(`this.`, fieldname, `[i] = byte(r.Intn(256))`) p.Out() p.P(`}`) } } else if field.IsString() { val := fmt.Sprintf("randString%v(r)", p.localName) if field.IsRepeated() { p.P(p.varGen.Next(), ` := r.Intn(10)`) p.P(`this.`, fieldname, ` = make(`, goTyp, `, `, p.varGen.Current(), `)`) p.P(`for i := 0; i < `, p.varGen.Current(), `; i++ {`) p.In() p.P(`this.`, fieldname, `[i] = `, val) p.Out() p.P(`}`) } else if !gogoproto.IsNullable(field) || proto3 { p.P(`this.`, fieldname, ` = `, val) } else { p.P(p.varGen.Next(), `:= `, val) p.P(`this.`, fieldname, ` = &`, p.varGen.Current()) } } else { typName := generator.GoTypeToName(goTyp) if field.IsRepeated() { p.P(p.varGen.Next(), ` := r.Intn(100)`) p.P(`this.`, fieldname, ` = make(`, goTyp, `, `, p.varGen.Current(), `)`) p.P(`for i := 0; i < `, p.varGen.Current(), `; i++ {`) p.In() p.P(`this.`, fieldname, `[i] = `, value(typName, field.GetType())) if negative(field.GetType()) { p.P(`if r.Intn(2) == 0 {`) p.In() p.P(`this.`, fieldname, `[i] *= -1`) p.Out() p.P(`}`) } p.Out() p.P(`}`) } else if !gogoproto.IsNullable(field) || proto3 { p.P(`this.`, fieldname, ` = `, value(typName, field.GetType())) if negative(field.GetType()) { p.P(`if r.Intn(2) == 0 {`) p.In() p.P(`this.`, fieldname, ` *= -1`) p.Out() p.P(`}`) } } else { p.P(p.varGen.Next(), ` := `, value(typName, field.GetType())) if negative(field.GetType()) { p.P(`if r.Intn(2) == 0 {`) p.In() p.P(p.varGen.Current(), ` *= -1`) p.Out() p.P(`}`) } p.P(`this.`, fieldname, ` = &`, p.varGen.Current()) } } } }
func (p *unmarshal) field(file *descriptor.FileDescriptorProto, msg *generator.Descriptor, field *descriptor.FieldDescriptorProto, fieldname string, proto3 bool) { repeated := field.IsRepeated() nullable := gogoproto.IsNullable(field) typ := p.noStarOrSliceType(msg, field) switch *field.Type { case descriptor.FieldDescriptorProto_TYPE_DOUBLE: if !p.unsafe || gogoproto.IsCastType(field) { p.P(`var v uint64`) p.decodeFixed64("v", "uint64") 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 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 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 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 `, typ) p.decodeVarint("v", typ) p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { 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 repeated { p.P(`var v `, typ) p.decodeVarint("v", typ) p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { 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 repeated { p.P(`var v `, typ) p.decodeVarint("v", typ) p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { 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 repeated { p.P(`var v `, typ) p.decodeFixed64("v", typ) p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.decodeFixed64("m."+fieldname, typ) } else { p.P(`var v `, typ) p.decodeFixed64("v", typ) 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 || gogoproto.IsCastType(field) { if repeated { p.P(`var v `, typ) p.decodeFixed32("v", typ) p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.decodeFixed32("m."+fieldname, typ) } else { p.P(`var v `, typ) p.decodeFixed32("v", typ) 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, `, `, 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(`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, `, `, 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(`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 `, typ) p.decodeVarint("v", typ) p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { 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 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 || gogoproto.IsCastType(field) { if repeated { p.P(`var v `, typ) p.decodeFixed32("v", typ) p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.decodeFixed32("m."+fieldname, typ) } else { p.P(`var v `, typ) p.decodeFixed32("v", typ) 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 || gogoproto.IsCastType(field) { if repeated { p.P(`var v `, typ) p.decodeFixed64("v", typ) p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`) } else if proto3 || !nullable { p.decodeFixed64("m."+fieldname, typ) } else { p.P(`var v `, typ) p.decodeFixed64("v", typ) 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 `, typ) p.decodeVarint("v", typ) p.P(`v = `, typ, `((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, `, `, 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) mapField(varName string, 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(`postStringIndex`, varName, ` := iNdEx + int(stringLen`, varName, `)`) p.P(`if postStringIndex`, varName, ` > l {`) p.In() p.P(`return `, p.ioPkg.Use(), `.ErrUnexpectedEOF`) p.Out() p.P(`}`) p.P(varName, ` := string(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(`postmsgIndex := iNdEx + mapmsglen`) 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) p.P(varName, ` := &`, msgname, `{}`) p.P(`if err := `, varName, `.Unmarshal(data[iNdEx:postmsgIndex]); 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(`postbytesIndex := iNdEx + int(mapbyteLen)`) p.P(`if postbytesIndex > l {`) p.In() p.P(`return `, p.ioPkg.Use(), `.ErrUnexpectedEOF`) p.Out() p.P(`}`) 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)`) } }
func TurnOffNullableForNativeTypesOnly(field *descriptor.FieldDescriptorProto) { if field.IsRepeated() || field.IsMessage() { return } SetBoolFieldOption(gogoproto.E_Nullable, false)(field) }
func TurnOffNullable(field *descriptor.FieldDescriptorProto) { if field.IsRepeated() && !field.IsMessage() { return } SetBoolFieldOption(gogoproto.E_Nullable, false)(field) }