func (d *deepCopyGen) genRepeated(m *generator.Descriptor, f *descriptor.FieldDescriptorProto) { fName := generator.CamelCase(*f.Name) if gogoproto.IsCustomName(f) { fName = gogoproto.GetCustomName(f) } typename, _ := d.GoType(m, f) d.P("if o.", fName, " != nil {") d.In() if f.IsMessage() { d.P("m.", fName, " = make(", typename, ", len(o.", fName, "))") // TODO(stevvooe): Handle custom type here? goType := d.TypeName(d.ObjectNamed(f.GetTypeName())) // elides [] or * d.P("for i := range m.", fName, " {") d.In() if !gogoproto.IsNullable(f) { d.genCopyFunc("&m."+fName+"[i]", "&o."+fName+"[i]") } else { d.P("m.", fName, "[i] = &", goType, "{}") d.genCopyFunc("m."+fName+"[i]", "o."+fName+"[i]") } d.Out() d.P("}") } else { d.P("m.", fName, " = make(", typename, ", len(o.", fName, "))") d.P("copy(m.", fName, ", ", "o.", fName, ")") } d.Out() d.P("}") d.P() }
func (d *deepCopyGen) genRepeatedWriter(m *generator.Descriptor, f *descriptor.FieldDescriptorProto, notNullablePrefix string) func() { fName := generator.CamelCase(*f.Name) if gogoproto.IsCustomName(f) { fName = gogoproto.GetCustomName(f) } typename, _ := d.gen.GoType(m, f) isMessage := f.IsMessage() repeatedFunc := func() { d.gen.P("\tif m.", fName, " != nil {") d.gen.P("\t\to.", fName, " = make(", typename, ", 0, len(m.", fName, "))") if isMessage { d.gen.P("\t\tfor _, v := range m.", fName, " {") d.gen.P("\t\t\to.", fName, " = append(o.", fName, ", ", notNullablePrefix, "v.Copy())") d.gen.P("\t\t}") } else { d.gen.P("\t\to.", fName, " = append(o.", fName, ", m.", fName, "...)") } d.gen.P("\t}") d.gen.P() } return repeatedFunc }
func (d *deepCopyGen) genOneOfWriter(m *generator.Descriptor, f *descriptor.FieldDescriptorProto, oneOfFuncs map[string][]func()) { ccTypeName := generator.CamelCaseSlice(m.TypeName()) fName := generator.CamelCase(*f.Name) if gogoproto.IsCustomName(f) { fName = gogoproto.GetCustomName(f) } odp := m.OneofDecl[int(*f.OneofIndex)] oneOfName := generator.CamelCase(odp.GetName()) oneOfNameFuncs, ok := oneOfFuncs[oneOfName] if !ok { oneOfFunc := func() { d.gen.P("\tswitch m.", oneOfName, ".(type) {") } oneOfNameFuncs = append(oneOfNameFuncs, oneOfFunc) } isMessage := f.IsMessage() oneOfFunc := func() { tName := ccTypeName + "_" + fName d.gen.P("\tcase *", tName, ":") d.gen.P("\t\ti := &", tName, " {") if isMessage { d.gen.P("\t\t\t", fName, ": m.Get", fName, "().Copy(),") } else { d.gen.P("\t\t\t", fName, ": m.Get", fName, "(),") } d.gen.P("\t\t}") d.gen.P() d.gen.P("\t\to.", oneOfName, " = i") } oneOfNameFuncs = append(oneOfNameFuncs, oneOfFunc) oneOfFuncs[oneOfName] = oneOfNameFuncs }
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 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 TurnOffNullableForNativeTypesWithoutDefaultsOnly(field *descriptor.FieldDescriptorProto) { if field.IsRepeated() || field.IsMessage() { return } if field.DefaultValue != nil { return } SetBoolFieldOption(gogoproto.E_Nullable, false)(field) }
func GetMap(file *descriptor.FileDescriptorProto, field *descriptor.FieldDescriptorProto) *descriptor.DescriptorProto { if !field.IsMessage() { return nil } typeName := strings.TrimPrefix(field.GetTypeName(), "."+file.GetPackage()+".") if strings.Contains(typeName, "Map") && !strings.HasSuffix(typeName, "Entry") { typeName += "." + CamelCase(field.GetName()) + "Entry" } return file.GetMessage(typeName) }
func NeedsNilCheck(proto3 bool, field *google_protobuf.FieldDescriptorProto) bool { nullable := IsNullable(field) if field.IsMessage() || IsCustomType(field) { return nullable } if proto3 { return false } return nullable || *field.Type == google_protobuf.FieldDescriptorProto_TYPE_BYTES }
func (g *Generator) IsMap(field *descriptor.FieldDescriptorProto) bool { if !field.IsMessage() { return false } byName := g.ObjectNamed(field.GetTypeName()) desc, ok := byName.(*Descriptor) if byName == nil || !ok || !desc.GetOptions().GetMapEntry() { return false } return true }
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) }
// GoMapValueTypes returns the map value Go type and the alias map value Go type (for casting), taking into // account whether the map is nullable or the value is a message. func GoMapValueTypes(mapField, valueField *descriptor.FieldDescriptorProto, goValueType, goValueAliasType string) (nullable bool, outGoType string, outGoAliasType string) { nullable = gogoproto.IsNullable(mapField) && (valueField.IsMessage() || gogoproto.IsCustomType(mapField)) if nullable { // ensure the non-aliased Go value type is a pointer for consistency if strings.HasPrefix(goValueType, "*") { outGoType = goValueType } else { outGoType = "*" + goValueType } outGoAliasType = goValueAliasType } else { outGoType = strings.Replace(goValueType, "*", "", 1) outGoAliasType = strings.Replace(goValueAliasType, "*", "", 1) } return }
func (p *plugin) hasLoop(field *descriptor.FieldDescriptorProto, visited []*generator.Descriptor, excludes []*generator.Descriptor) *generator.Descriptor { if field.IsMessage() || p.IsGroup(field) || p.IsMap(field) { var fieldMessage *generator.Descriptor if p.IsMap(field) { m := p.GoMapType(nil, field) if !m.ValueField.IsMessage() { return nil } fieldMessage = p.ObjectNamed(m.ValueField.GetTypeName()).(*generator.Descriptor) } else { 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 } } pkg := strings.Split(field.GetTypeName(), ".")[1] for _, f := range fieldMessage.Field { if strings.HasPrefix(f.GetTypeName(), "."+pkg+".") { visited = append(visited, fieldMessage) loopTo := p.hasLoop(f, visited, excludes) if loopTo != nil { return loopTo } } } } return nil }
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) } 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)) if negative(valuetypName) { 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)) if negative(typName) { 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)) if negative(typName) { 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)) if negative(typName) { 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 *marshalto) generateField(proto3 bool, numGen NumGen, file *generator.FileDescriptor, message *generator.Descriptor, field *descriptor.FieldDescriptorProto) { fieldname := p.GetOneOfFieldName(message, field) nullable := gogoproto.IsNullable(field) repeated := field.IsRepeated() required := field.IsRequired() protoSizer := gogoproto.IsProtoSizer(file.FileDescriptorProto, message.DescriptorProto) if required && nullable { p.P(`if m.`, fieldname, `== nil {`) p.In() if !gogoproto.ImportsGoGoProto(file.FileDescriptorProto) { p.P(`return 0, new(`, p.protoPkg.Use(), `.RequiredNotSetError)`) } else { p.P(`return 0, `, p.protoPkg.Use(), `.NewRequiredNotSetError("`, field.GetName(), `")`) } p.Out() p.P(`} else {`) } else if repeated { p.P(`if len(m.`, fieldname, `) > 0 {`) p.In() } else if ((!proto3 || field.IsMessage()) && nullable) || (*field.Type == descriptor.FieldDescriptorProto_TYPE_BYTES && !gogoproto.IsCustomType(field)) { p.P(`if m.`, fieldname, ` != nil {`) p.In() } packed := field.IsPacked() wireType := field.WireType() fieldNumber := field.GetNumber() if packed { wireType = proto.WireBytes } switch *field.Type { case descriptor.FieldDescriptorProto_TYPE_DOUBLE: if !p.unsafe || gogoproto.IsCastType(field) { if packed { p.encodeKey(fieldNumber, wireType) p.callVarint(`len(m.`, fieldname, `) * 8`) p.P(`for _, num := range m.`, fieldname, ` {`) p.In() p.P(`f`, numGen.Next(), ` := `, p.mathPkg.Use(), `.Float64bits(float64(num))`) p.encodeFixed64("f" + numGen.Current()) p.Out() p.P(`}`) } else if repeated { p.P(`for _, num := range m.`, fieldname, ` {`) p.In() p.encodeKey(fieldNumber, wireType) p.P(`f`, numGen.Next(), ` := `, p.mathPkg.Use(), `.Float64bits(float64(num))`) p.encodeFixed64("f" + numGen.Current()) p.Out() p.P(`}`) } else if proto3 { p.P(`if m.`, fieldname, ` != 0 {`) p.In() p.encodeKey(fieldNumber, wireType) p.callFixed64(p.mathPkg.Use(), `.Float64bits(float64(m.`+fieldname, `))`) p.Out() p.P(`}`) } else if !nullable { p.encodeKey(fieldNumber, wireType) p.callFixed64(p.mathPkg.Use(), `.Float64bits(float64(m.`+fieldname, `))`) } else { p.encodeKey(fieldNumber, wireType) p.callFixed64(p.mathPkg.Use(), `.Float64bits(float64(*m.`+fieldname, `))`) } } else { if packed { p.encodeKey(fieldNumber, wireType) p.callVarint(`len(m.`, fieldname, `) * 8`) p.P(`for _, num := range m.`, fieldname, ` {`) p.In() p.unsafeFixed64("num", "float64") p.Out() p.P(`}`) } else if repeated { p.P(`for _, num := range m.`, fieldname, ` {`) p.In() p.encodeKey(fieldNumber, wireType) p.unsafeFixed64("num", "float64") p.Out() p.P(`}`) } else if proto3 { p.P(`if m.`, fieldname, ` != 0 {`) p.In() p.encodeKey(fieldNumber, wireType) p.unsafeFixed64(`m.`+fieldname, "float64") p.Out() p.P(`}`) } else if !nullable { p.encodeKey(fieldNumber, wireType) p.unsafeFixed64(`m.`+fieldname, "float64") } else { p.encodeKey(fieldNumber, wireType) p.unsafeFixed64(`*m.`+fieldname, `float64`) } } case descriptor.FieldDescriptorProto_TYPE_FLOAT: if !p.unsafe || gogoproto.IsCastType(field) { if packed { p.encodeKey(fieldNumber, wireType) p.callVarint(`len(m.`, fieldname, `) * 4`) p.P(`for _, num := range m.`, fieldname, ` {`) p.In() p.P(`f`, numGen.Next(), ` := `, p.mathPkg.Use(), `.Float32bits(float32(num))`) p.encodeFixed32("f" + numGen.Current()) p.Out() p.P(`}`) } else if repeated { p.P(`for _, num := range m.`, fieldname, ` {`) p.In() p.encodeKey(fieldNumber, wireType) p.P(`f`, numGen.Next(), ` := `, p.mathPkg.Use(), `.Float32bits(float32(num))`) p.encodeFixed32("f" + numGen.Current()) p.Out() p.P(`}`) } else if proto3 { p.P(`if m.`, fieldname, ` != 0 {`) p.In() p.encodeKey(fieldNumber, wireType) p.callFixed32(p.mathPkg.Use(), `.Float32bits(float32(m.`+fieldname, `))`) p.Out() p.P(`}`) } else if !nullable { p.encodeKey(fieldNumber, wireType) p.callFixed32(p.mathPkg.Use(), `.Float32bits(float32(m.`+fieldname, `))`) } else { p.encodeKey(fieldNumber, wireType) p.callFixed32(p.mathPkg.Use(), `.Float32bits(float32(*m.`+fieldname, `))`) } } else { if packed { p.encodeKey(fieldNumber, wireType) p.callVarint(`len(m.`, fieldname, `) * 4`) p.P(`for _, num := range m.`, fieldname, ` {`) p.In() p.unsafeFixed32("num", "float32") p.Out() p.P(`}`) } else if repeated { p.P(`for _, num := range m.`, fieldname, ` {`) p.In() p.encodeKey(fieldNumber, wireType) p.unsafeFixed32("num", "float32") p.Out() p.P(`}`) } else if proto3 { p.P(`if m.`, fieldname, ` != 0 {`) p.In() p.encodeKey(fieldNumber, wireType) p.unsafeFixed32(`m.`+fieldname, `float32`) p.Out() p.P(`}`) } else if !nullable { p.encodeKey(fieldNumber, wireType) p.unsafeFixed32(`m.`+fieldname, `float32`) } else { p.encodeKey(fieldNumber, wireType) p.unsafeFixed32(`*m.`+fieldname, "float32") } } case descriptor.FieldDescriptorProto_TYPE_INT64, descriptor.FieldDescriptorProto_TYPE_UINT64, descriptor.FieldDescriptorProto_TYPE_INT32, descriptor.FieldDescriptorProto_TYPE_UINT32, descriptor.FieldDescriptorProto_TYPE_ENUM: if packed { jvar := "j" + numGen.Next() p.P(`data`, numGen.Next(), ` := make([]byte, len(m.`, fieldname, `)*10)`) p.P(`var `, jvar, ` int`) if *field.Type == descriptor.FieldDescriptorProto_TYPE_INT64 || *field.Type == descriptor.FieldDescriptorProto_TYPE_INT32 { p.P(`for _, num1 := range m.`, fieldname, ` {`) p.In() p.P(`num := uint64(num1)`) } else { p.P(`for _, num := range m.`, fieldname, ` {`) p.In() } p.P(`for num >= 1<<7 {`) p.In() p.P(`data`, numGen.Current(), `[`, jvar, `] = uint8(uint64(num)&0x7f|0x80)`) p.P(`num >>= 7`) p.P(jvar, `++`) p.Out() p.P(`}`) p.P(`data`, numGen.Current(), `[`, jvar, `] = uint8(num)`) p.P(jvar, `++`) p.Out() p.P(`}`) p.encodeKey(fieldNumber, wireType) p.callVarint(jvar) p.P(`i += copy(data[i:], data`, numGen.Current(), `[:`, jvar, `])`) } else if repeated { p.P(`for _, num := range m.`, fieldname, ` {`) p.In() p.encodeKey(fieldNumber, wireType) p.callVarint("num") p.Out() p.P(`}`) } else if proto3 { p.P(`if m.`, fieldname, ` != 0 {`) p.In() p.encodeKey(fieldNumber, wireType) p.callVarint(`m.`, fieldname) p.Out() p.P(`}`) } else if !nullable { p.encodeKey(fieldNumber, wireType) p.callVarint(`m.`, fieldname) } else { p.encodeKey(fieldNumber, wireType) p.callVarint(`*m.`, fieldname) } case descriptor.FieldDescriptorProto_TYPE_FIXED64, descriptor.FieldDescriptorProto_TYPE_SFIXED64: if !p.unsafe { if packed { p.encodeKey(fieldNumber, wireType) p.callVarint(`len(m.`, fieldname, `) * 8`) p.P(`for _, num := range m.`, fieldname, ` {`) p.In() p.encodeFixed64("num") p.Out() p.P(`}`) } else if repeated { p.P(`for _, num := range m.`, fieldname, ` {`) p.In() p.encodeKey(fieldNumber, wireType) p.encodeFixed64("num") p.Out() p.P(`}`) } else if proto3 { p.P(`if m.`, fieldname, ` != 0 {`) p.In() p.encodeKey(fieldNumber, wireType) p.callFixed64("m." + fieldname) p.Out() p.P(`}`) } else if !nullable { p.encodeKey(fieldNumber, wireType) p.callFixed64("m." + fieldname) } else { p.encodeKey(fieldNumber, wireType) p.callFixed64("*m." + fieldname) } } else { typeName := "int64" if *field.Type == descriptor.FieldDescriptorProto_TYPE_FIXED64 { typeName = "uint64" } if packed { p.encodeKey(fieldNumber, wireType) p.callVarint(`len(m.`, fieldname, `) * 8`) p.P(`for _, num := range m.`, fieldname, ` {`) p.In() p.unsafeFixed64("num", typeName) p.Out() p.P(`}`) } else if repeated { p.P(`for _, num := range m.`, fieldname, ` {`) p.In() p.encodeKey(fieldNumber, wireType) p.unsafeFixed64("num", typeName) p.Out() p.P(`}`) } else if proto3 { p.P(`if m.`, fieldname, ` != 0 {`) p.In() p.encodeKey(fieldNumber, wireType) p.unsafeFixed64("m."+fieldname, typeName) p.Out() p.P(`}`) } else if !nullable { p.encodeKey(fieldNumber, wireType) p.unsafeFixed64("m."+fieldname, typeName) } else { p.encodeKey(fieldNumber, wireType) p.unsafeFixed64("*m."+fieldname, typeName) } } case descriptor.FieldDescriptorProto_TYPE_FIXED32, descriptor.FieldDescriptorProto_TYPE_SFIXED32: if !p.unsafe { if packed { p.encodeKey(fieldNumber, wireType) p.callVarint(`len(m.`, fieldname, `) * 4`) p.P(`for _, num := range m.`, fieldname, ` {`) p.In() p.encodeFixed32("num") p.Out() p.P(`}`) } else if repeated { p.P(`for _, num := range m.`, fieldname, ` {`) p.In() p.encodeKey(fieldNumber, wireType) p.encodeFixed32("num") p.Out() p.P(`}`) } else if proto3 { p.P(`if m.`, fieldname, ` != 0 {`) p.In() p.encodeKey(fieldNumber, wireType) p.callFixed32("m." + fieldname) p.Out() p.P(`}`) } else if !nullable { p.encodeKey(fieldNumber, wireType) p.callFixed32("m." + fieldname) } else { p.encodeKey(fieldNumber, wireType) p.callFixed32("*m." + fieldname) } } else { typeName := "int32" if *field.Type == descriptor.FieldDescriptorProto_TYPE_FIXED32 { typeName = "uint32" } if packed { p.encodeKey(fieldNumber, wireType) p.callVarint(`len(m.`, fieldname, `) * 4`) p.P(`for _, num := range m.`, fieldname, ` {`) p.In() p.unsafeFixed32("num", typeName) p.Out() p.P(`}`) } else if repeated { p.P(`for _, num := range m.`, fieldname, ` {`) p.In() p.encodeKey(fieldNumber, wireType) p.unsafeFixed32("num", typeName) p.Out() p.P(`}`) } else if proto3 { p.P(`if m.`, fieldname, ` != 0 {`) p.In() p.encodeKey(fieldNumber, wireType) p.unsafeFixed32("m."+fieldname, typeName) p.Out() p.P(`}`) } else if !nullable { p.encodeKey(fieldNumber, wireType) p.unsafeFixed32("m."+fieldname, typeName) } else { p.encodeKey(fieldNumber, wireType) p.unsafeFixed32("*m."+fieldname, typeName) } } case descriptor.FieldDescriptorProto_TYPE_BOOL: if packed { p.encodeKey(fieldNumber, wireType) p.callVarint(`len(m.`, fieldname, `)`) p.P(`for _, b := range m.`, fieldname, ` {`) p.In() p.P(`if b {`) p.In() p.P(`data[i] = 1`) p.Out() p.P(`} else {`) p.In() p.P(`data[i] = 0`) p.Out() p.P(`}`) p.P(`i++`) p.Out() p.P(`}`) } else if repeated { p.P(`for _, b := range m.`, fieldname, ` {`) p.In() p.encodeKey(fieldNumber, wireType) p.P(`if b {`) p.In() p.P(`data[i] = 1`) p.Out() p.P(`} else {`) p.In() p.P(`data[i] = 0`) p.Out() p.P(`}`) p.P(`i++`) p.Out() p.P(`}`) } else if proto3 { p.P(`if m.`, fieldname, ` {`) p.In() p.encodeKey(fieldNumber, wireType) p.P(`if m.`, fieldname, ` {`) p.In() p.P(`data[i] = 1`) p.Out() p.P(`} else {`) p.In() p.P(`data[i] = 0`) p.Out() p.P(`}`) p.P(`i++`) p.Out() p.P(`}`) } else if !nullable { p.encodeKey(fieldNumber, wireType) p.P(`if m.`, fieldname, ` {`) p.In() p.P(`data[i] = 1`) p.Out() p.P(`} else {`) p.In() p.P(`data[i] = 0`) p.Out() p.P(`}`) p.P(`i++`) } else { p.encodeKey(fieldNumber, wireType) p.P(`if *m.`, fieldname, ` {`) p.In() p.P(`data[i] = 1`) p.Out() p.P(`} else {`) p.In() p.P(`data[i] = 0`) p.Out() p.P(`}`) p.P(`i++`) } case descriptor.FieldDescriptorProto_TYPE_STRING: if repeated { p.P(`for _, s := range m.`, fieldname, ` {`) p.In() p.encodeKey(fieldNumber, wireType) p.P(`l = len(s)`) p.encodeVarint("l") p.P(`i+=copy(data[i:], s)`) p.Out() p.P(`}`) } else if proto3 { p.P(`if len(m.`, fieldname, `) > 0 {`) p.In() p.encodeKey(fieldNumber, wireType) p.callVarint(`len(m.`, fieldname, `)`) p.P(`i+=copy(data[i:], m.`, fieldname, `)`) p.Out() p.P(`}`) } else if !nullable { p.encodeKey(fieldNumber, wireType) p.callVarint(`len(m.`, fieldname, `)`) p.P(`i+=copy(data[i:], m.`, fieldname, `)`) } else { p.encodeKey(fieldNumber, wireType) p.callVarint(`len(*m.`, fieldname, `)`) p.P(`i+=copy(data[i:], *m.`, fieldname, `)`) } case descriptor.FieldDescriptorProto_TYPE_GROUP: panic(fmt.Errorf("marshaler does not support group %v", fieldname)) case descriptor.FieldDescriptorProto_TYPE_MESSAGE: if generator.IsMap(file.FileDescriptorProto, field) { m := p.GoMapType(nil, field) _, keywire := p.GoType(nil, m.KeyField) valuegoTyp, valuewire := p.GoType(nil, m.ValueField) valuegoAliasTyp, _ := p.GoType(nil, m.ValueAliasField) nullable, valuegoTyp, valuegoAliasTyp = generator.GoMapValueTypes(field, m.ValueField, valuegoTyp, valuegoAliasTyp) keyKeySize := keySize(1, wireToType(keywire)) valueKeySize := keySize(2, wireToType(valuewire)) p.P(`for k, _ := range m.`, fieldname, ` {`) p.In() p.encodeKey(fieldNumber, wireType) sum := []string{strconv.Itoa(keyKeySize)} switch m.KeyField.GetType() { case descriptor.FieldDescriptorProto_TYPE_DOUBLE, descriptor.FieldDescriptorProto_TYPE_FIXED64, descriptor.FieldDescriptorProto_TYPE_SFIXED64: sum = append(sum, `8`) case descriptor.FieldDescriptorProto_TYPE_FLOAT, descriptor.FieldDescriptorProto_TYPE_FIXED32, descriptor.FieldDescriptorProto_TYPE_SFIXED32: sum = append(sum, `4`) case descriptor.FieldDescriptorProto_TYPE_INT64, descriptor.FieldDescriptorProto_TYPE_UINT64, descriptor.FieldDescriptorProto_TYPE_UINT32, descriptor.FieldDescriptorProto_TYPE_ENUM, descriptor.FieldDescriptorProto_TYPE_INT32: sum = append(sum, `sov`+p.localName+`(uint64(k))`) case descriptor.FieldDescriptorProto_TYPE_BOOL: sum = append(sum, `1`) case descriptor.FieldDescriptorProto_TYPE_STRING, descriptor.FieldDescriptorProto_TYPE_BYTES: sum = append(sum, `len(k)+sov`+p.localName+`(uint64(len(k)))`) case descriptor.FieldDescriptorProto_TYPE_SINT32, descriptor.FieldDescriptorProto_TYPE_SINT64: sum = append(sum, `soz`+p.localName+`(uint64(k))`) } p.P(`v := m.`, fieldname, `[k]`) accessor := `v` sum = append(sum, strconv.Itoa(valueKeySize)) switch m.ValueField.GetType() { case descriptor.FieldDescriptorProto_TYPE_DOUBLE, descriptor.FieldDescriptorProto_TYPE_FIXED64, descriptor.FieldDescriptorProto_TYPE_SFIXED64: sum = append(sum, strconv.Itoa(8)) case descriptor.FieldDescriptorProto_TYPE_FLOAT, descriptor.FieldDescriptorProto_TYPE_FIXED32, descriptor.FieldDescriptorProto_TYPE_SFIXED32: sum = append(sum, strconv.Itoa(4)) case descriptor.FieldDescriptorProto_TYPE_INT64, descriptor.FieldDescriptorProto_TYPE_UINT64, descriptor.FieldDescriptorProto_TYPE_UINT32, descriptor.FieldDescriptorProto_TYPE_ENUM, descriptor.FieldDescriptorProto_TYPE_INT32: sum = append(sum, `sov`+p.localName+`(uint64(v))`) case descriptor.FieldDescriptorProto_TYPE_BOOL: sum = append(sum, `1`) case descriptor.FieldDescriptorProto_TYPE_STRING, descriptor.FieldDescriptorProto_TYPE_BYTES: sum = append(sum, `len(v)+sov`+p.localName+`(uint64(len(v)))`) case descriptor.FieldDescriptorProto_TYPE_SINT32, descriptor.FieldDescriptorProto_TYPE_SINT64: sum = append(sum, `soz`+p.localName+`(uint64(v))`) case descriptor.FieldDescriptorProto_TYPE_MESSAGE: if nullable { p.P(`if v == nil {`) p.In() p.P(`return 0, `, p.errorsPkg.Use(), `.New("proto: map has nil element")`) p.Out() p.P(`}`) } if valuegoTyp != valuegoAliasTyp { if nullable { // cast back to the type that has the generated methods on it accessor = `((` + valuegoTyp + `)(` + accessor + `))` } else { accessor = `((*` + valuegoTyp + `)(&` + accessor + `))` } } else if !nullable { accessor = `(&v)` } if protoSizer { p.P(`msgSize := `, accessor, `.ProtoSize()`) } else { p.P(`msgSize := `, accessor, `.Size()`) } sum = append(sum, `msgSize + sov`+p.localName+`(uint64(msgSize))`) } p.P(`mapSize := `, strings.Join(sum, " + ")) p.callVarint("mapSize") p.encodeKey(1, wireToType(keywire)) p.mapField(numGen, m.KeyField.GetType(), "k", protoSizer) p.encodeKey(2, wireToType(valuewire)) p.mapField(numGen, m.ValueField.GetType(), accessor, protoSizer) p.Out() p.P(`}`) } else if repeated { p.P(`for _, msg := range m.`, fieldname, ` {`) p.In() p.encodeKey(fieldNumber, wireType) if protoSizer { p.callVarint("msg.ProtoSize()") } else { p.callVarint("msg.Size()") } p.P(`n, err := msg.MarshalTo(data[i:])`) p.P(`if err != nil {`) p.In() p.P(`return 0, err`) p.Out() p.P(`}`) p.P(`i+=n`) p.Out() p.P(`}`) } else { p.encodeKey(fieldNumber, wireType) if protoSizer { p.callVarint(`m.`, fieldname, `.ProtoSize()`) } else { p.callVarint(`m.`, fieldname, `.Size()`) } p.P(`n`, numGen.Next(), `, err := m.`, fieldname, `.MarshalTo(data[i:])`) p.P(`if err != nil {`) p.In() p.P(`return 0, err`) p.Out() p.P(`}`) p.P(`i+=n`, numGen.Current()) } case descriptor.FieldDescriptorProto_TYPE_BYTES: if !gogoproto.IsCustomType(field) { if repeated { p.P(`for _, b := range m.`, fieldname, ` {`) p.In() p.encodeKey(fieldNumber, wireType) p.callVarint("len(b)") p.P(`i+=copy(data[i:], b)`) p.Out() p.P(`}`) } else if proto3 { p.P(`if len(m.`, fieldname, `) > 0 {`) p.In() p.encodeKey(fieldNumber, wireType) p.callVarint(`len(m.`, fieldname, `)`) p.P(`i+=copy(data[i:], m.`, fieldname, `)`) p.Out() p.P(`}`) } else { p.encodeKey(fieldNumber, wireType) p.callVarint(`len(m.`, fieldname, `)`) p.P(`i+=copy(data[i:], m.`, fieldname, `)`) } } else { if repeated { p.P(`for _, msg := range m.`, fieldname, ` {`) p.In() p.encodeKey(fieldNumber, wireType) if protoSizer { p.callVarint(`msg.ProtoSize()`) } else { p.callVarint(`msg.Size()`) } p.P(`n, err := msg.MarshalTo(data[i:])`) p.P(`if err != nil {`) p.In() p.P(`return 0, err`) p.Out() p.P(`}`) p.P(`i+=n`) p.Out() p.P(`}`) } else { p.encodeKey(fieldNumber, wireType) if protoSizer { p.callVarint(`m.`, fieldname, `.ProtoSize()`) } else { p.callVarint(`m.`, fieldname, `.Size()`) } p.P(`n`, numGen.Next(), `, err := m.`, fieldname, `.MarshalTo(data[i:])`) p.P(`if err != nil {`) p.In() p.P(`return 0, err`) p.Out() p.P(`}`) p.P(`i+=n`, numGen.Current()) } } case descriptor.FieldDescriptorProto_TYPE_SINT32: if packed { datavar := "data" + numGen.Next() jvar := "j" + numGen.Next() p.P(datavar, ` := make([]byte, len(m.`, fieldname, ")*5)") p.P(`var `, jvar, ` int`) p.P(`for _, num := range m.`, fieldname, ` {`) p.In() xvar := "x" + numGen.Next() p.P(xvar, ` := (uint32(num) << 1) ^ uint32((num >> 31))`) p.P(`for `, xvar, ` >= 1<<7 {`) p.In() p.P(datavar, `[`, jvar, `] = uint8(uint64(`, xvar, `)&0x7f|0x80)`) p.P(jvar, `++`) p.P(xvar, ` >>= 7`) p.Out() p.P(`}`) p.P(datavar, `[`, jvar, `] = uint8(`, xvar, `)`) p.P(jvar, `++`) p.Out() p.P(`}`) p.encodeKey(fieldNumber, wireType) p.callVarint(jvar) p.P(`i+=copy(data[i:], `, datavar, `[:`, jvar, `])`) } else if repeated { p.P(`for _, num := range m.`, fieldname, ` {`) p.In() p.encodeKey(fieldNumber, wireType) p.P(`x`, numGen.Next(), ` := (uint32(num) << 1) ^ uint32((num >> 31))`) p.encodeVarint("x" + numGen.Current()) p.Out() p.P(`}`) } else if proto3 { p.P(`if m.`, fieldname, ` != 0 {`) p.In() p.encodeKey(fieldNumber, wireType) p.callVarint(`(uint32(m.`, fieldname, `) << 1) ^ uint32((m.`, fieldname, ` >> 31))`) p.Out() p.P(`}`) } else if !nullable { p.encodeKey(fieldNumber, wireType) p.callVarint(`(uint32(m.`, fieldname, `) << 1) ^ uint32((m.`, fieldname, ` >> 31))`) } else { p.encodeKey(fieldNumber, wireType) p.callVarint(`(uint32(*m.`, fieldname, `) << 1) ^ uint32((*m.`, fieldname, ` >> 31))`) } case descriptor.FieldDescriptorProto_TYPE_SINT64: if packed { jvar := "j" + numGen.Next() xvar := "x" + numGen.Next() datavar := "data" + numGen.Next() p.P(`var `, jvar, ` int`) p.P(datavar, ` := make([]byte, len(m.`, fieldname, `)*10)`) p.P(`for _, num := range m.`, fieldname, ` {`) p.In() p.P(xvar, ` := (uint64(num) << 1) ^ uint64((num >> 63))`) p.P(`for `, xvar, ` >= 1<<7 {`) p.In() p.P(datavar, `[`, jvar, `] = uint8(uint64(`, xvar, `)&0x7f|0x80)`) p.P(jvar, `++`) p.P(xvar, ` >>= 7`) p.Out() p.P(`}`) p.P(datavar, `[`, jvar, `] = uint8(`, xvar, `)`) p.P(jvar, `++`) p.Out() p.P(`}`) p.encodeKey(fieldNumber, wireType) p.callVarint(jvar) p.P(`i+=copy(data[i:], `, datavar, `[:`, jvar, `])`) } else if repeated { p.P(`for _, num := range m.`, fieldname, ` {`) p.In() p.encodeKey(fieldNumber, wireType) p.P(`x`, numGen.Next(), ` := (uint64(num) << 1) ^ uint64((num >> 63))`) p.encodeVarint("x" + numGen.Current()) p.Out() p.P(`}`) } else if proto3 { p.P(`if m.`, fieldname, ` != 0 {`) p.In() p.encodeKey(fieldNumber, wireType) p.callVarint(`(uint64(m.`, fieldname, `) << 1) ^ uint64((m.`, fieldname, ` >> 63))`) p.Out() p.P(`}`) } else if !nullable { p.encodeKey(fieldNumber, wireType) p.callVarint(`(uint64(m.`, fieldname, `) << 1) ^ uint64((m.`, fieldname, ` >> 63))`) } else { p.encodeKey(fieldNumber, wireType) p.callVarint(`(uint64(*m.`, fieldname, `) << 1) ^ uint64((*m.`, fieldname, ` >> 63))`) } default: panic("not implemented") } if (required && nullable) || ((!proto3 || field.IsMessage()) && nullable) || repeated || (*field.Type == descriptor.FieldDescriptorProto_TYPE_BYTES && !gogoproto.IsCustomType(field)) { p.Out() p.P(`}`) } }
func (p *plugin) generateField(file *generator.FileDescriptor, message *generator.Descriptor, field *descriptor.FieldDescriptorProto, verbose bool) { proto3 := gogoproto.IsProto3(file.FileDescriptorProto) fieldname := p.GetOneOfFieldName(message, field) repeated := field.IsRepeated() ctype := gogoproto.IsCustomType(field) nullable := gogoproto.IsNullable(field) isDuration := gogoproto.IsStdDuration(field) isTimestamp := gogoproto.IsStdTime(field) // oneof := field.OneofIndex != nil if !repeated { if ctype || isTimestamp { if nullable { p.P(`if that1.`, fieldname, ` == nil {`) p.In() p.P(`if this.`, fieldname, ` != nil {`) p.In() if verbose { p.P(`return `, p.fmtPkg.Use(), `.Errorf("this.`, fieldname, ` != nil && that1.`, fieldname, ` == nil")`) } else { p.P(`return false`) } p.Out() p.P(`}`) p.Out() p.P(`} else if !this.`, fieldname, `.Equal(*that1.`, fieldname, `) {`) } else { p.P(`if !this.`, fieldname, `.Equal(that1.`, fieldname, `) {`) } p.In() if verbose { p.P(`return `, p.fmtPkg.Use(), `.Errorf("`, fieldname, ` this(%v) Not Equal that(%v)", this.`, fieldname, `, that1.`, fieldname, `)`) } else { p.P(`return false`) } p.Out() p.P(`}`) } else if isDuration { if nullable { p.generateNullableField(fieldname, verbose) } else { p.P(`if this.`, fieldname, ` != that1.`, fieldname, `{`) } p.In() if verbose { p.P(`return `, p.fmtPkg.Use(), `.Errorf("`, fieldname, ` this(%v) Not Equal that(%v)", this.`, fieldname, `, that1.`, fieldname, `)`) } else { p.P(`return false`) } p.Out() p.P(`}`) } else { if field.IsMessage() || p.IsGroup(field) { if nullable { p.P(`if !this.`, fieldname, `.Equal(that1.`, fieldname, `) {`) } else { p.P(`if !this.`, fieldname, `.Equal(&that1.`, fieldname, `) {`) } } else if field.IsBytes() { p.P(`if !`, p.bytesPkg.Use(), `.Equal(this.`, fieldname, `, that1.`, fieldname, `) {`) } else if field.IsString() { if nullable && !proto3 { p.generateNullableField(fieldname, verbose) } else { p.P(`if this.`, fieldname, ` != that1.`, fieldname, `{`) } } else { if nullable && !proto3 { p.generateNullableField(fieldname, verbose) } else { p.P(`if this.`, fieldname, ` != that1.`, fieldname, `{`) } } p.In() if verbose { p.P(`return `, p.fmtPkg.Use(), `.Errorf("`, fieldname, ` this(%v) Not Equal that(%v)", this.`, fieldname, `, that1.`, fieldname, `)`) } else { p.P(`return false`) } p.Out() p.P(`}`) } } else { p.P(`if len(this.`, fieldname, `) != len(that1.`, fieldname, `) {`) p.In() if verbose { p.P(`return `, p.fmtPkg.Use(), `.Errorf("`, fieldname, ` this(%v) Not Equal that(%v)", len(this.`, fieldname, `), len(that1.`, fieldname, `))`) } else { p.P(`return false`) } p.Out() p.P(`}`) p.P(`for i := range this.`, fieldname, ` {`) p.In() if ctype && !p.IsMap(field) { p.P(`if !this.`, fieldname, `[i].Equal(that1.`, fieldname, `[i]) {`) } else if isTimestamp { if nullable { p.P(`if !this.`, fieldname, `[i].Equal(*that1.`, fieldname, `[i]) {`) } else { p.P(`if !this.`, fieldname, `[i].Equal(that1.`, fieldname, `[i]) {`) } } else if isDuration { if nullable { p.P(`if dthis, dthat := this.`, fieldname, `[i], that1.`, fieldname, `[i]; (dthis != nil && dthat != nil && *dthis != *dthat) || (dthis != nil && dthat == nil) || (dthis == nil && dthat != nil) {`) } else { p.P(`if this.`, fieldname, `[i] != that1.`, fieldname, `[i] {`) } } else { if p.IsMap(field) { m := p.GoMapType(nil, field) valuegoTyp, _ := p.GoType(nil, m.ValueField) valuegoAliasTyp, _ := p.GoType(nil, m.ValueAliasField) nullable, valuegoTyp, valuegoAliasTyp = generator.GoMapValueTypes(field, m.ValueField, valuegoTyp, valuegoAliasTyp) mapValue := m.ValueAliasField if mapValue.IsMessage() || p.IsGroup(mapValue) { if nullable && valuegoTyp == valuegoAliasTyp { p.P(`if !this.`, fieldname, `[i].Equal(that1.`, fieldname, `[i]) {`) } else { // Equal() has a pointer receiver, but map value is a value type a := `this.` + fieldname + `[i]` b := `that1.` + fieldname + `[i]` if valuegoTyp != valuegoAliasTyp { // cast back to the type that has the generated methods on it a = `(` + valuegoTyp + `)(` + a + `)` b = `(` + valuegoTyp + `)(` + b + `)` } p.P(`a := `, a) p.P(`b := `, b) if nullable { p.P(`if !a.Equal(b) {`) } else { p.P(`if !(&a).Equal(&b) {`) } } } else if mapValue.IsBytes() { if ctype { if nullable { p.P(`if !this.`, fieldname, `[i].Equal(*that1.`, fieldname, `[i]) { //nullable`) } else { p.P(`if !this.`, fieldname, `[i].Equal(that1.`, fieldname, `[i]) { //not nullable`) } } else { p.P(`if !`, p.bytesPkg.Use(), `.Equal(this.`, fieldname, `[i], that1.`, fieldname, `[i]) {`) } } else if mapValue.IsString() { p.P(`if this.`, fieldname, `[i] != that1.`, fieldname, `[i] {`) } else { p.P(`if this.`, fieldname, `[i] != that1.`, fieldname, `[i] {`) } } else if field.IsMessage() || p.IsGroup(field) { if nullable { p.P(`if !this.`, fieldname, `[i].Equal(that1.`, fieldname, `[i]) {`) } else { p.P(`if !this.`, fieldname, `[i].Equal(&that1.`, fieldname, `[i]) {`) } } else if field.IsBytes() { p.P(`if !`, p.bytesPkg.Use(), `.Equal(this.`, fieldname, `[i], that1.`, fieldname, `[i]) {`) } else if field.IsString() { p.P(`if this.`, fieldname, `[i] != that1.`, fieldname, `[i] {`) } else { p.P(`if this.`, fieldname, `[i] != that1.`, fieldname, `[i] {`) } } p.In() if verbose { p.P(`return `, p.fmtPkg.Use(), `.Errorf("`, fieldname, ` this[%v](%v) Not Equal that[%v](%v)", i, this.`, fieldname, `[i], i, that1.`, fieldname, `[i])`) } else { p.P(`return false`) } p.Out() p.P(`}`) p.Out() p.P(`}`) } }
func (p *size) generateField(proto3 bool, file *generator.FileDescriptor, message *generator.Descriptor, field *descriptor.FieldDescriptorProto) { fieldname := p.GetOneOfFieldName(message, field) nullable := gogoproto.IsNullable(field) repeated := field.IsRepeated() if repeated { p.P(`if len(m.`, fieldname, `) > 0 {`) p.In() } else if ((!proto3 || field.IsMessage()) && nullable) || (!gogoproto.IsCustomType(field) && *field.Type == descriptor.FieldDescriptorProto_TYPE_BYTES) { p.P(`if m.`, fieldname, ` != nil {`) p.In() } packed := field.IsPacked() _, wire := p.GoType(message, field) wireType := wireToType(wire) fieldNumber := field.GetNumber() if packed { wireType = proto.WireBytes } key := keySize(fieldNumber, wireType) switch *field.Type { case descriptor.FieldDescriptorProto_TYPE_DOUBLE, descriptor.FieldDescriptorProto_TYPE_FIXED64, descriptor.FieldDescriptorProto_TYPE_SFIXED64: if packed { p.P(`n+=`, strconv.Itoa(key), `+sov`, p.localName, `(uint64(len(m.`, fieldname, `)*8))`, `+len(m.`, fieldname, `)*8`) } else if repeated { p.P(`n+=`, strconv.Itoa(key+8), `*len(m.`, fieldname, `)`) } else if proto3 { p.P(`if m.`, fieldname, ` != 0 {`) p.In() p.P(`n+=`, strconv.Itoa(key+8)) p.Out() p.P(`}`) } else if nullable { p.P(`n+=`, strconv.Itoa(key+8)) } else { p.P(`n+=`, strconv.Itoa(key+8)) } case descriptor.FieldDescriptorProto_TYPE_FLOAT, descriptor.FieldDescriptorProto_TYPE_FIXED32, descriptor.FieldDescriptorProto_TYPE_SFIXED32: if packed { p.P(`n+=`, strconv.Itoa(key), `+sov`, p.localName, `(uint64(len(m.`, fieldname, `)*4))`, `+len(m.`, fieldname, `)*4`) } else if repeated { p.P(`n+=`, strconv.Itoa(key+4), `*len(m.`, fieldname, `)`) } else if proto3 { p.P(`if m.`, fieldname, ` != 0 {`) p.In() p.P(`n+=`, strconv.Itoa(key+4)) p.Out() p.P(`}`) } else if nullable { p.P(`n+=`, strconv.Itoa(key+4)) } else { p.P(`n+=`, strconv.Itoa(key+4)) } case descriptor.FieldDescriptorProto_TYPE_INT64, descriptor.FieldDescriptorProto_TYPE_UINT64, descriptor.FieldDescriptorProto_TYPE_UINT32, descriptor.FieldDescriptorProto_TYPE_ENUM, descriptor.FieldDescriptorProto_TYPE_INT32: if packed { p.P(`l = 0`) p.P(`for _, e := range m.`, fieldname, ` {`) p.In() p.P(`l+=sov`, p.localName, `(uint64(e))`) p.Out() p.P(`}`) p.P(`n+=`, strconv.Itoa(key), `+sov`, p.localName, `(uint64(l))+l`) } else if repeated { p.P(`for _, e := range m.`, fieldname, ` {`) p.In() p.P(`n+=`, strconv.Itoa(key), `+sov`, p.localName, `(uint64(e))`) p.Out() p.P(`}`) } else if proto3 { p.P(`if m.`, fieldname, ` != 0 {`) p.In() p.P(`n+=`, strconv.Itoa(key), `+sov`, p.localName, `(uint64(m.`, fieldname, `))`) p.Out() p.P(`}`) } else if nullable { p.P(`n+=`, strconv.Itoa(key), `+sov`, p.localName, `(uint64(*m.`, fieldname, `))`) } else { p.P(`n+=`, strconv.Itoa(key), `+sov`, p.localName, `(uint64(m.`, fieldname, `))`) } case descriptor.FieldDescriptorProto_TYPE_BOOL: if packed { p.P(`n+=`, strconv.Itoa(key), `+sov`, p.localName, `(uint64(len(m.`, fieldname, `)))`, `+len(m.`, fieldname, `)*1`) } else if repeated { p.P(`n+=`, strconv.Itoa(key+1), `*len(m.`, fieldname, `)`) } else if proto3 { p.P(`if m.`, fieldname, ` {`) p.In() p.P(`n+=`, strconv.Itoa(key+1)) p.Out() p.P(`}`) } else if nullable { p.P(`n+=`, strconv.Itoa(key+1)) } else { p.P(`n+=`, strconv.Itoa(key+1)) } case descriptor.FieldDescriptorProto_TYPE_STRING: if repeated { p.P(`for _, s := range m.`, fieldname, ` { `) p.In() p.P(`l = len(s)`) p.P(`n+=`, strconv.Itoa(key), `+l+sov`, p.localName, `(uint64(l))`) p.Out() p.P(`}`) } else if proto3 { p.P(`l=len(m.`, fieldname, `)`) p.P(`if l > 0 {`) p.In() p.P(`n+=`, strconv.Itoa(key), `+l+sov`, p.localName, `(uint64(l))`) p.Out() p.P(`}`) } else if nullable { p.P(`l=len(*m.`, fieldname, `)`) p.P(`n+=`, strconv.Itoa(key), `+l+sov`, p.localName, `(uint64(l))`) } else { p.P(`l=len(m.`, fieldname, `)`) p.P(`n+=`, strconv.Itoa(key), `+l+sov`, p.localName, `(uint64(l))`) } case descriptor.FieldDescriptorProto_TYPE_GROUP: panic(fmt.Errorf("size does not support group %v", fieldname)) case descriptor.FieldDescriptorProto_TYPE_MESSAGE: if generator.IsMap(file.FileDescriptorProto, field) { mapMsg := generator.GetMap(file.FileDescriptorProto, field) keyField, valueField := mapMsg.GetMapFields() _, keywire := p.GoType(nil, keyField) _, valuewire := p.GoType(nil, valueField) _, fieldwire := p.GoType(nil, field) fieldKeySize := keySize(field.GetNumber(), wireToType(fieldwire)) keyKeySize := keySize(1, wireToType(keywire)) valueKeySize := keySize(2, wireToType(valuewire)) p.P(`for k, v := range m.`, fieldname, ` { `) p.In() p.P(`_ = k`) p.P(`_ = v`) sum := []string{strconv.Itoa(keyKeySize)} switch keyField.GetType() { case descriptor.FieldDescriptorProto_TYPE_DOUBLE, descriptor.FieldDescriptorProto_TYPE_FIXED64, descriptor.FieldDescriptorProto_TYPE_SFIXED64: sum = append(sum, `8`) case descriptor.FieldDescriptorProto_TYPE_FLOAT, descriptor.FieldDescriptorProto_TYPE_FIXED32, descriptor.FieldDescriptorProto_TYPE_SFIXED32: sum = append(sum, `4`) case descriptor.FieldDescriptorProto_TYPE_INT64, descriptor.FieldDescriptorProto_TYPE_UINT64, descriptor.FieldDescriptorProto_TYPE_UINT32, descriptor.FieldDescriptorProto_TYPE_ENUM, descriptor.FieldDescriptorProto_TYPE_INT32: sum = append(sum, `sov`+p.localName+`(uint64(k))`) case descriptor.FieldDescriptorProto_TYPE_BOOL: sum = append(sum, `1`) case descriptor.FieldDescriptorProto_TYPE_STRING, descriptor.FieldDescriptorProto_TYPE_BYTES: sum = append(sum, `len(k)+sov`+p.localName+`(uint64(len(k)))`) case descriptor.FieldDescriptorProto_TYPE_SINT32, descriptor.FieldDescriptorProto_TYPE_SINT64: sum = append(sum, `soz`+p.localName+`(uint64(k))`) } sum = append(sum, strconv.Itoa(valueKeySize)) switch valueField.GetType() { case descriptor.FieldDescriptorProto_TYPE_DOUBLE, descriptor.FieldDescriptorProto_TYPE_FIXED64, descriptor.FieldDescriptorProto_TYPE_SFIXED64: sum = append(sum, strconv.Itoa(8)) case descriptor.FieldDescriptorProto_TYPE_FLOAT, descriptor.FieldDescriptorProto_TYPE_FIXED32, descriptor.FieldDescriptorProto_TYPE_SFIXED32: sum = append(sum, strconv.Itoa(4)) case descriptor.FieldDescriptorProto_TYPE_INT64, descriptor.FieldDescriptorProto_TYPE_UINT64, descriptor.FieldDescriptorProto_TYPE_UINT32, descriptor.FieldDescriptorProto_TYPE_ENUM, descriptor.FieldDescriptorProto_TYPE_INT32: sum = append(sum, `sov`+p.localName+`(uint64(v))`) case descriptor.FieldDescriptorProto_TYPE_BOOL: sum = append(sum, `1`) case descriptor.FieldDescriptorProto_TYPE_STRING, descriptor.FieldDescriptorProto_TYPE_BYTES: sum = append(sum, `len(v)+sov`+p.localName+`(uint64(len(v)))`) case descriptor.FieldDescriptorProto_TYPE_SINT32, descriptor.FieldDescriptorProto_TYPE_SINT64: sum = append(sum, `soz`+p.localName+`(uint64(v))`) case descriptor.FieldDescriptorProto_TYPE_MESSAGE: p.P(`l = 0`) p.P(`if v != nil {`) p.In() p.P(`l= v.ProtoSize()`) p.Out() p.P(`}`) sum = append(sum, `l+sov`+p.localName+`(uint64(l))`) } p.P(`mapEntrySize := `, strings.Join(sum, "+")) p.P(`n+=mapEntrySize+`, fieldKeySize, `+sov`, p.localName, `(uint64(mapEntrySize))`) p.Out() p.P(`}`) } else if repeated { p.P(`for _, e := range m.`, fieldname, ` { `) p.In() p.P(`l=e.ProtoSize()`) p.P(`n+=`, strconv.Itoa(key), `+l+sov`, p.localName, `(uint64(l))`) p.Out() p.P(`}`) } else { p.P(`l=m.`, fieldname, `.ProtoSize()`) p.P(`n+=`, strconv.Itoa(key), `+l+sov`, p.localName, `(uint64(l))`) } case descriptor.FieldDescriptorProto_TYPE_BYTES: if !gogoproto.IsCustomType(field) { if repeated { p.P(`for _, b := range m.`, fieldname, ` { `) p.In() p.P(`l = len(b)`) p.P(`n+=`, strconv.Itoa(key), `+l+sov`, p.localName, `(uint64(l))`) p.Out() p.P(`}`) } else if proto3 { p.P(`l=len(m.`, fieldname, `)`) p.P(`if l > 0 {`) p.In() p.P(`n+=`, strconv.Itoa(key), `+l+sov`, p.localName, `(uint64(l))`) p.Out() p.P(`}`) } else { p.P(`l=len(m.`, fieldname, `)`) p.P(`n+=`, strconv.Itoa(key), `+l+sov`, p.localName, `(uint64(l))`) } } else { if repeated { p.P(`for _, e := range m.`, fieldname, ` { `) p.In() p.P(`l=e.ProtoSize()`) p.P(`n+=`, strconv.Itoa(key), `+l+sov`, p.localName, `(uint64(l))`) p.Out() p.P(`}`) } else { p.P(`l=m.`, fieldname, `.ProtoSize()`) p.P(`n+=`, strconv.Itoa(key), `+l+sov`, p.localName, `(uint64(l))`) } } case descriptor.FieldDescriptorProto_TYPE_SINT32, descriptor.FieldDescriptorProto_TYPE_SINT64: if packed { p.P(`l = 0`) p.P(`for _, e := range m.`, fieldname, ` {`) p.In() p.P(`l+=soz`, p.localName, `(uint64(e))`) p.Out() p.P(`}`) p.P(`n+=`, strconv.Itoa(key), `+sov`, p.localName, `(uint64(l))+l`) } else if repeated { p.P(`for _, e := range m.`, fieldname, ` {`) p.In() p.P(`n+=`, strconv.Itoa(key), `+soz`, p.localName, `(uint64(e))`) p.Out() p.P(`}`) } else if proto3 { p.P(`if m.`, fieldname, ` != 0 {`) p.In() p.P(`n+=`, strconv.Itoa(key), `+soz`, p.localName, `(uint64(m.`, fieldname, `))`) p.Out() p.P(`}`) } else if nullable { p.P(`n+=`, strconv.Itoa(key), `+soz`, p.localName, `(uint64(*m.`, fieldname, `))`) } else { p.P(`n+=`, strconv.Itoa(key), `+soz`, p.localName, `(uint64(m.`, fieldname, `))`) } default: panic("not implemented") } if ((!proto3 || field.IsMessage()) && nullable) || repeated || (!gogoproto.IsCustomType(field) && *field.Type == descriptor.FieldDescriptorProto_TYPE_BYTES) { p.Out() p.P(`}`) } }
func (p *plugin) generateField(file *generator.FileDescriptor, message *generator.Descriptor, field *descriptor.FieldDescriptorProto, verbose bool) { proto3 := gogoproto.IsProto3(file.FileDescriptorProto) fieldname := p.GetOneOfFieldName(message, field) repeated := field.IsRepeated() ctype := gogoproto.IsCustomType(field) nullable := gogoproto.IsNullable(field) // oneof := field.OneofIndex != nil if !repeated { if ctype { if nullable { p.P(`if that1.`, fieldname, ` == nil {`) p.In() p.P(`if this.`, fieldname, ` != nil {`) p.In() if verbose { p.P(`return `, p.fmtPkg.Use(), `.Errorf("this.`, fieldname, ` != nil && that1.`, fieldname, ` == nil")`) } else { p.P(`return false`) } p.Out() p.P(`}`) p.Out() p.P(`} else if !this.`, fieldname, `.Equal(*that1.`, fieldname, `) {`) } else { p.P(`if !this.`, fieldname, `.Equal(that1.`, fieldname, `) {`) } p.In() if verbose { p.P(`return `, p.fmtPkg.Use(), `.Errorf("`, fieldname, ` this(%v) Not Equal that(%v)", this.`, fieldname, `, that1.`, fieldname, `)`) } else { p.P(`return false`) } p.Out() p.P(`}`) } else { if field.IsMessage() || p.IsGroup(field) { if nullable { p.P(`if !this.`, fieldname, `.Equal(that1.`, fieldname, `) {`) } else { p.P(`if !this.`, fieldname, `.Equal(&that1.`, fieldname, `) {`) } } else if field.IsBytes() { p.P(`if !`, p.bytesPkg.Use(), `.Equal(this.`, fieldname, `, that1.`, fieldname, `) {`) } else if field.IsString() { if nullable && !proto3 { p.generateNullableField(fieldname, verbose) } else { p.P(`if this.`, fieldname, ` != that1.`, fieldname, `{`) } } else { if nullable && !proto3 { p.generateNullableField(fieldname, verbose) } else { p.P(`if this.`, fieldname, ` != that1.`, fieldname, `{`) } } p.In() if verbose { p.P(`return `, p.fmtPkg.Use(), `.Errorf("`, fieldname, ` this(%v) Not Equal that(%v)", this.`, fieldname, `, that1.`, fieldname, `)`) } else { p.P(`return false`) } p.Out() p.P(`}`) } } else { p.P(`if len(this.`, fieldname, `) != len(that1.`, fieldname, `) {`) p.In() if verbose { p.P(`return `, p.fmtPkg.Use(), `.Errorf("`, fieldname, ` this(%v) Not Equal that(%v)", len(this.`, fieldname, `), len(that1.`, fieldname, `))`) } else { p.P(`return false`) } p.Out() p.P(`}`) p.P(`for i := range this.`, fieldname, ` {`) p.In() if ctype { p.P(`if !this.`, fieldname, `[i].Equal(that1.`, fieldname, `[i]) {`) } else { if generator.IsMap(file.FileDescriptorProto, field) { mapMsg := generator.GetMap(file.FileDescriptorProto, field) _, mapValue := mapMsg.GetMapFields() if mapValue.IsMessage() || p.IsGroup(mapValue) { p.P(`if !this.`, fieldname, `[i].Equal(that1.`, fieldname, `[i]) {`) } else if mapValue.IsBytes() { p.P(`if !`, p.bytesPkg.Use(), `.Equal(this.`, fieldname, `[i], that1.`, fieldname, `[i]) {`) } else if mapValue.IsString() { p.P(`if this.`, fieldname, `[i] != that1.`, fieldname, `[i] {`) } else { p.P(`if this.`, fieldname, `[i] != that1.`, fieldname, `[i] {`) } } else if field.IsMessage() || p.IsGroup(field) { if nullable { p.P(`if !this.`, fieldname, `[i].Equal(that1.`, fieldname, `[i]) {`) } else { p.P(`if !this.`, fieldname, `[i].Equal(&that1.`, fieldname, `[i]) {`) } } else if field.IsBytes() { p.P(`if !`, p.bytesPkg.Use(), `.Equal(this.`, fieldname, `[i], that1.`, fieldname, `[i]) {`) } else if field.IsString() { p.P(`if this.`, fieldname, `[i] != that1.`, fieldname, `[i] {`) } else { p.P(`if this.`, fieldname, `[i] != that1.`, fieldname, `[i] {`) } } p.In() if verbose { p.P(`return `, p.fmtPkg.Use(), `.Errorf("`, fieldname, ` this[%v](%v) Not Equal that[%v](%v)", i, this.`, fieldname, `[i], i, that1.`, fieldname, `[i])`) } else { p.P(`return false`) } p.Out() p.P(`}`) p.Out() p.P(`}`) } }
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.GetOneOfFieldName(message, field) goTypName := generator.GoTypeToName(goTyp) if p.IsMap(field) { m := p.GoMapType(nil, field) keygoTyp, _ := p.GoType(nil, m.KeyField) keygoTyp = strings.Replace(keygoTyp, "*", "", 1) keygoAliasTyp, _ := p.GoType(nil, m.KeyAliasField) keygoAliasTyp = strings.Replace(keygoAliasTyp, "*", "", 1) valuegoTyp, _ := p.GoType(nil, m.ValueField) valuegoAliasTyp, _ := p.GoType(nil, m.ValueAliasField) keytypName := generator.GoTypeToName(keygoTyp) keygoAliasTyp = generator.GoTypeToName(keygoAliasTyp) valuetypAliasName := generator.GoTypeToName(valuegoAliasTyp) nullable, valuegoTyp, valuegoAliasTyp := generator.GoMapValueTypes(field, m.ValueField, valuegoTyp, valuegoAliasTyp) p.P(p.varGen.Next(), ` := r.Intn(10)`) p.P(`this.`, fieldname, ` = make(`, m.GoType, `)`) p.P(`for i := 0; i < `, p.varGen.Current(), `; i++ {`) p.In() keyval := "" if m.KeyField.IsString() { keyval = fmt.Sprintf("randString%v(r)", p.localName) } else { keyval = value(keytypName, m.KeyField.GetType()) } if keygoAliasTyp != keygoTyp { keyval = keygoAliasTyp + `(` + keyval + `)` } if m.ValueField.IsMessage() || p.IsGroup(field) { s := `this.` + fieldname + `[` + keyval + `] = ` goTypName = generator.GoTypeToName(valuegoTyp) funcCall := getFuncCall(goTypName) if !nullable { funcCall = `*` + funcCall } if valuegoTyp != valuegoAliasTyp { funcCall = `(` + valuegoAliasTyp + `)(` + funcCall + `)` } s += funcCall p.P(s) } else if m.ValueField.IsEnum() { s := `this.` + fieldname + `[` + keyval + `]` + ` = ` + p.getEnumVal(m.ValueField, valuegoTyp) p.P(s) } else if m.ValueField.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 m.ValueField.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(valuetypAliasName, m.ValueField.GetType())) if negative(m.ValueField.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(5)`) 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(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(), ` := 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(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] = `, 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 TurnOffNullable(field *descriptor.FieldDescriptorProto) { if field.IsRepeated() && !field.IsMessage() { return } SetBoolFieldOption(gogoproto.E_Nullable, false)(field) }
func BuildField(fileDescriptorSet *descriptor.FileDescriptorSet, msg *descriptor.DescriptorProto, f *descriptor.FieldDescriptorProto, help string, proto3 bool) string { tooltip := "" colon := ":" if len(help) > 0 { tooltip = ` <a href="#" data-toggle="tooltip" title="` + strings.Replace(help, "\n", " ", -1) + `"><span class="glyphicon glyphicon-question-sign" aria-hidden="true"></span></a>` colon = "" } fieldname := f.GetName() if f.IsMessage() { typName := typ(fieldname, f.IsRepeated(), getMessage(f, fileDescriptorSet)) if !f.IsRepeated() { return `s += '<div class="children" type="` + typName + `">' + build` + typName + `(json["` + f.GetName() + `"]); s += '</div>'; s += setLink(json, "` + typName + `", "` + fieldname + `", "` + help + `"); ` } else { return `s += '<div class="children" type="` + typName + `">'; var ` + fieldname + ` = getList(json, "` + fieldname + `"); for (var i = 0; i < ` + fieldname + `.length; i++) { s += build` + typName + `(` + fieldname + `[i]); } s += '</div>'; s += '<a href="#" class="add-child btn btn-success btn-sm" role="button" type="` + typName + `">add ` + fieldname + `</a>` + tooltip + `'; s += '<div class="field form-group"></div>'; ` } } else { if !f.IsRepeated() { if isBool(f) { defaultBool := "\"nothing\"" if proto3 { defaultBool = "false" } if f.DefaultValue != nil { defaultBool = f.GetDefaultValue() } s := `s += '<div class="field form-group"><label class="col-sm-2 control-label">` + fieldname + tooltip + colon + ` </label>'; ` s += `s += '<div class="col-sm-10"><div class="btn-group" data-toggle="buttons">'; ` s += `s += '<label class="btn btn-primary ' + activeradio(` + defaultBool + `, false, json["` + fieldname + `"]) + '"><input type="radio" name="` + fieldname + `" value="false" ' + radioed(` + defaultBool + `, false, json["` + fieldname + `"]) + '/>No</label>'; ` s += `s += '<label class="btn btn-primary ' + activeradio(` + defaultBool + `, true, json["` + fieldname + `"]) + '"><input type="radio" name="` + fieldname + `" value="true" ' + radioed(` + defaultBool + `, true, json["` + fieldname + `"]) + '/>Yes</label>'; ` s += `s += '</div></div></div>'; ` return s } else if isEnum(f) { enum := getEnum(fileDescriptorSet, f) defaultEnum := "\"nothing\"" if proto3 { defaultEnum = "0" } if f.DefaultValue != nil { for _, v := range enum.GetValue() { if v.GetName() == f.GetDefaultValue() { defaultEnum = strconv.Itoa(int(v.GetNumber())) break } } } if len(enum.GetValue()) <= 4 { s := `s += '<div class="field form-group"><label class="col-sm-2 control-label">` + fieldname + tooltip + colon + ` </label>'; ` s += `s += '<div class="col-sm-10"><div class="btn-group" data-toggle="buttons">'; ` for _, v := range enum.GetValue() { num := strconv.Itoa(int(v.GetNumber())) s += `s += '<label class="btn btn-primary ' + activeradio(` + defaultEnum + `, ` + num + `, json["` + fieldname + `"]) + '"><input type="radio" name="` + fieldname + `" value="` + num + `" ' + radioed(` + defaultEnum + `, ` + num + `, json["` + fieldname + `"]) + '/> ` + v.GetName() + `</label>'; ` } s += `s += '</div></div></div>'; ` return s } else { s := `s += '<div class="field form-group"><label class="col-sm-2 control-label">` + fieldname + tooltip + colon + ` </label><div class="col-sm-10">'; s += '<select class="form-control" name="` + fieldname + `">'; ` for _, v := range enum.GetValue() { num := strconv.Itoa(int(v.GetNumber())) s += `s += '<option value="` + num + `" ' + selected(` + defaultEnum + `, ` + num + `, json["` + fieldname + `"]) + '>` + v.GetName() + `</option>'; ` } s += `s += '</select></div></div>'; ` return s } } else if isNumber(f) { def := "\"\"" if proto3 { def = "0" } if f.DefaultValue != nil { def = f.GetDefaultValue() } return `s += '<div class="field form-group"><label class="col-sm-2 control-label">` + fieldname + tooltip + colon + ` </label><div class="col-sm-10"><input class="form-control" name="` + f.GetName() + `" type="number" step="1" '+setValue(` + def + `, json["` + f.GetName() + `"])+'/></div></div>'; ` } else if isFloat(f) { def := "\"\"" if proto3 { def = "0" } if f.DefaultValue != nil { def = f.GetDefaultValue() } return `s += '<div class="field form-group"><label class="col-sm-2 control-label">` + fieldname + tooltip + colon + ` </label><div class="col-sm-10"><input class="form-control" name="` + f.GetName() + `" type="number" step="any" '+setValue(` + def + `, json["` + f.GetName() + `"])+'/></div></div>'; ` } else { def := "undefined" if proto3 { def = "\"\"" } if f.DefaultValue != nil { def = strconv.Quote(f.GetDefaultValue()) } return `s += '<div class="field form-group"><label class="col-sm-2 control-label">` + fieldname + tooltip + colon + ` </label><div class="col-sm-10"><input class="form-control" name="` + f.GetName() + `" type="text" '+setStrValue(` + def + `, json["` + f.GetName() + `"])+'/></div></div>'; ` } } else { if isBool(f) { s := ` s += '<div class="fields" fieldname="` + fieldname + `">'; var ` + fieldname + ` = getList(json, "` + fieldname + `"); for (var i = 0; i < ` + fieldname + `.length; i++) { s += '<div class="field form-group"><label class="col-sm-2 control-label">` + fieldname + `: </label><div class="col-sm-8"><input name="` + fieldname + `" type="checkbox" repeated="true" ' + checked(` + fieldname + `[i]) + '/></div><div class="col-sm-2"><a href="#" class="del-field btn btn-warning btn-sm" role="button">Remove</a></div></div>'; } s += '</div>'; s += '<a href="#" fieldname="` + fieldname + `" class="add-elem btn btn-info btn-sm" role="button" type="bool">add ` + fieldname + `</a>` + tooltip + `'; s += '<div class="field form-group"></div>'; ` return s } else if isNumber(f) || isEnum(f) { s := `s += '<div class="fields" fieldname="` + fieldname + `">'; var ` + fieldname + ` = getList(json, "` + fieldname + `"); for (var i = 0; i < ` + fieldname + `.length; i++) { s += '<div class="field form-group"><label class="col-sm-2 control-label">` + fieldname + `: </label><div class="col-sm-8"><input class="form-control" name="` + fieldname + `" type="number" step="any" repeated="true" '+setRepValue(json["` + f.GetName() + `"][i])+'/></div><div class="col-sm-2"><a href="#" class="del-field btn btn-warning btn-sm" role="button">Remove</a></div></div>'; } s += '</div>'; s += '<a href="#" fieldname="` + fieldname + `" class="add-elem btn btn-info btn-sm" role="button" type="number">add ` + fieldname + `</a>` + tooltip + `'; s += '<div class="field form-group"></div>'; ` return s } else if isFloat(f) { s := `s += '<div class="fields" fieldname="` + fieldname + `">'; var ` + fieldname + ` = getList(json, "` + fieldname + `"); for (var i = 0; i < ` + fieldname + `.length; i++) { s += '<div class="field form-group"><label class="col-sm-2 control-label">` + fieldname + `: </label><div class="col-sm-8"><input class="form-control" name="` + fieldname + `" type="number" step="1" repeated="true" '+setRepValue(json["` + f.GetName() + `"][i])+'/></div><div class="col-sm-2"><a href="#" class="del-field btn btn-warning btn-sm" role="button">Remove</a></div></div>'; } s += '</div>'; s += '<a href="#" fieldname="` + fieldname + `" class="add-elem btn btn-info btn-sm" role="button" type="float">add ` + fieldname + `</a>` + tooltip + `'; s += '<div class="field form-group"></div>'; ` return s } else { s := `s += '<div class="fields" fieldname="` + fieldname + `">'; var ` + fieldname + ` = getList(json, "` + fieldname + `"); for (var i = 0; i < ` + fieldname + `.length; i++) { s += '<div class="field form-group"><label class="col-sm-2 control-label">` + fieldname + `: </label><div class="col-sm-8"><input class="form-control" name="` + fieldname + `" type="text" repeated="true" '+setRepStrValue(json["` + f.GetName() + `"][i])+'/></div><div class="col-sm-2"><a href="#" class="del-field btn btn-warning btn-sm" role="button">Remove</a></div></div>'; } s += '</div>'; s += '<a href="#" fieldname="` + fieldname + `" class="add-elem btn btn-info btn-sm" role="button" type="text">add ` + fieldname + `</a>` + tooltip + `'; s += '<div class="field form-group"></div>'; ` return s } } } panic("unreachable") }
func (p *plugin) generateField(file *generator.FileDescriptor, message *generator.Descriptor, field *descriptor.FieldDescriptorProto) { proto3 := gogoproto.IsProto3(file.FileDescriptorProto) fieldname := p.GetOneOfFieldName(message, field) repeated := field.IsRepeated() ctype := gogoproto.IsCustomType(field) nullable := gogoproto.IsNullable(field) // oneof := field.OneofIndex != nil if !repeated { if ctype { if nullable { p.P(`if that1.`, fieldname, ` == nil {`) p.In() p.P(`if this.`, fieldname, ` != nil {`) p.In() p.P(`return 1`) p.Out() p.P(`}`) p.Out() p.P(`} else if this.`, fieldname, ` == nil {`) p.In() p.P(`return -1`) p.Out() p.P(`} else if c := this.`, fieldname, `.Compare(*that1.`, fieldname, `); c != 0 {`) } else { p.P(`if c := this.`, fieldname, `.Compare(that1.`, fieldname, `); c != 0 {`) } p.In() p.P(`return c`) p.Out() p.P(`}`) } else { if field.IsMessage() || p.IsGroup(field) { if nullable { p.P(`if c := this.`, fieldname, `.Compare(that1.`, fieldname, `); c != 0 {`) } else { p.P(`if c := this.`, fieldname, `.Compare(&that1.`, fieldname, `); c != 0 {`) } p.In() p.P(`return c`) p.Out() p.P(`}`) } else if field.IsBytes() { p.P(`if c := `, p.bytesPkg.Use(), `.Compare(this.`, fieldname, `, that1.`, fieldname, `); c != 0 {`) p.In() p.P(`return c`) p.Out() p.P(`}`) } else if field.IsString() { if nullable && !proto3 { p.generateNullableField(fieldname) } else { p.P(`if this.`, fieldname, ` != that1.`, fieldname, `{`) p.In() p.P(`if this.`, fieldname, ` < that1.`, fieldname, `{`) p.In() p.P(`return -1`) p.Out() p.P(`}`) p.P(`return 1`) p.Out() p.P(`}`) } } else if field.IsBool() { if nullable && !proto3 { p.P(`if this.`, fieldname, ` != nil && that1.`, fieldname, ` != nil {`) p.In() p.P(`if *this.`, fieldname, ` != *that1.`, fieldname, `{`) p.In() p.P(`if !*this.`, fieldname, ` {`) p.In() p.P(`return -1`) p.Out() p.P(`}`) p.P(`return 1`) p.Out() p.P(`}`) p.Out() p.P(`} else if this.`, fieldname, ` != nil {`) p.In() p.P(`return 1`) p.Out() p.P(`} else if that1.`, fieldname, ` != nil {`) p.In() p.P(`return -1`) p.Out() p.P(`}`) } else { p.P(`if this.`, fieldname, ` != that1.`, fieldname, `{`) p.In() p.P(`if !this.`, fieldname, ` {`) p.In() p.P(`return -1`) p.Out() p.P(`}`) p.P(`return 1`) p.Out() p.P(`}`) } } else { if nullable && !proto3 { p.generateNullableField(fieldname) } else { p.P(`if this.`, fieldname, ` != that1.`, fieldname, `{`) p.In() p.P(`if this.`, fieldname, ` < that1.`, fieldname, `{`) p.In() p.P(`return -1`) p.Out() p.P(`}`) p.P(`return 1`) p.Out() p.P(`}`) } } } } else { p.P(`if len(this.`, fieldname, `) != len(that1.`, fieldname, `) {`) p.In() p.P(`if len(this.`, fieldname, `) < len(that1.`, fieldname, `) {`) p.In() p.P(`return -1`) p.Out() p.P(`}`) p.P(`return 1`) p.Out() p.P(`}`) p.P(`for i := range this.`, fieldname, ` {`) p.In() if ctype { p.P(`if c := this.`, fieldname, `[i].Compare(that1.`, fieldname, `[i]); c != 0 {`) p.In() p.P(`return c`) p.Out() p.P(`}`) } else { if p.IsMap(field) { m := p.GoMapType(nil, field) valuegoTyp, _ := p.GoType(nil, m.ValueField) valuegoAliasTyp, _ := p.GoType(nil, m.ValueAliasField) nullable, valuegoTyp, valuegoAliasTyp = generator.GoMapValueTypes(field, m.ValueField, valuegoTyp, valuegoAliasTyp) mapValue := m.ValueAliasField if mapValue.IsMessage() || p.IsGroup(mapValue) { if nullable && valuegoTyp == valuegoAliasTyp { p.P(`if c := this.`, fieldname, `[i].Compare(that1.`, fieldname, `[i]); c != 0 {`) } else { // Compare() has a pointer receiver, but map value is a value type a := `this.` + fieldname + `[i]` b := `that1.` + fieldname + `[i]` if valuegoTyp != valuegoAliasTyp { // cast back to the type that has the generated methods on it a = `(` + valuegoTyp + `)(` + a + `)` b = `(` + valuegoTyp + `)(` + b + `)` } p.P(`a := `, a) p.P(`b := `, b) if nullable { p.P(`if c := a.Compare(b); c != 0 {`) } else { p.P(`if c := (&a).Compare(&b); c != 0 {`) } } p.In() p.P(`return c`) p.Out() p.P(`}`) } else if mapValue.IsBytes() { p.P(`if c := `, p.bytesPkg.Use(), `.Compare(this.`, fieldname, `[i], that1.`, fieldname, `[i]); c != 0 {`) p.In() p.P(`return c`) p.Out() p.P(`}`) } else if mapValue.IsString() { p.P(`if this.`, fieldname, `[i] != that1.`, fieldname, `[i] {`) p.In() p.P(`if this.`, fieldname, `[i] < that1.`, fieldname, `[i] {`) p.In() p.P(`return -1`) p.Out() p.P(`}`) p.P(`return 1`) p.Out() p.P(`}`) } else { p.P(`if this.`, fieldname, `[i] != that1.`, fieldname, `[i] {`) p.In() p.P(`if this.`, fieldname, `[i] < that1.`, fieldname, `[i] {`) p.In() p.P(`return -1`) p.Out() p.P(`}`) p.P(`return 1`) p.Out() p.P(`}`) } } else if field.IsMessage() || p.IsGroup(field) { if nullable { p.P(`if c := this.`, fieldname, `[i].Compare(that1.`, fieldname, `[i]); c != 0 {`) p.In() p.P(`return c`) p.Out() p.P(`}`) } else { p.P(`if c := this.`, fieldname, `[i].Compare(&that1.`, fieldname, `[i]); c != 0 {`) p.In() p.P(`return c`) p.Out() p.P(`}`) } } else if field.IsBytes() { p.P(`if c := `, p.bytesPkg.Use(), `.Compare(this.`, fieldname, `[i], that1.`, fieldname, `[i]); c != 0 {`) p.In() p.P(`return c`) p.Out() p.P(`}`) } else if field.IsString() { p.P(`if this.`, fieldname, `[i] != that1.`, fieldname, `[i] {`) p.In() p.P(`if this.`, fieldname, `[i] < that1.`, fieldname, `[i] {`) p.In() p.P(`return -1`) p.Out() p.P(`}`) p.P(`return 1`) p.Out() p.P(`}`) } else if field.IsBool() { p.P(`if this.`, fieldname, `[i] != that1.`, fieldname, `[i] {`) p.In() p.P(`if !this.`, fieldname, `[i] {`) p.In() p.P(`return -1`) p.Out() p.P(`}`) p.P(`return 1`) p.Out() p.P(`}`) } else { p.P(`if this.`, fieldname, `[i] != that1.`, fieldname, `[i] {`) p.In() p.P(`if this.`, fieldname, `[i] < that1.`, fieldname, `[i] {`) p.In() p.P(`return -1`) p.Out() p.P(`}`) p.P(`return 1`) p.Out() p.P(`}`) } } p.Out() p.P(`}`) } }