func (p *plugin) Generate(file *generator.FileDescriptor) { p.atleastOne = false p.PluginImports = generator.NewPluginImports(p.Generator) p.varGen = NewVarGen() proto3 := gogoproto.IsProto3(file.FileDescriptorProto) p.localName = generator.FileName(file) protoPkg := p.NewImport("github.com/gogo/protobuf/proto") if !gogoproto.ImportsGoGoProto(file.FileDescriptorProto) { protoPkg = p.NewImport("github.com/golang/protobuf/proto") } for _, message := range file.Messages() { if !gogoproto.HasPopulate(file.FileDescriptorProto, message.DescriptorProto) { continue } if message.DescriptorProto.GetOptions().GetMapEntry() { continue } p.atleastOne = true ccTypeName := generator.CamelCaseSlice(message.TypeName()) p.P(`func NewPopulated`, ccTypeName, `(r randy`, p.localName, `, easy bool) *`, ccTypeName, ` {`) p.In() p.P(`this := &`, ccTypeName, `{}`) if gogoproto.IsUnion(message.File(), message.DescriptorProto) && len(message.Field) > 0 { loopLevels := make([]int, len(message.Field)) maxLoopLevel := 0 for i, field := range message.Field { loopLevels[i] = p.loops(field, message) if loopLevels[i] > maxLoopLevel { maxLoopLevel = loopLevels[i] } } ran := 0 for i := range loopLevels { ran += int(math.Pow10(maxLoopLevel - loopLevels[i])) } p.P(`fieldNum := r.Intn(`, fmt.Sprintf("%d", ran), `)`) p.P(`switch fieldNum {`) k := 0 for i, field := range message.Field { is := []string{} ran := int(math.Pow10(maxLoopLevel - loopLevels[i])) for j := 0; j < ran; j++ { is = append(is, fmt.Sprintf("%d", j+k)) } k += ran p.P(`case `, strings.Join(is, ","), `:`) p.In() p.GenerateField(file, message, field) p.Out() } p.P(`}`) } else { var maxFieldNumber int32 for _, field := range message.Field { if field.IsRequired() || (!gogoproto.IsNullable(field) && !field.IsRepeated()) || proto3 { p.GenerateField(file, message, field) } else { p.P(`if r.Intn(10) != 0 {`) p.In() p.GenerateField(file, message, field) p.Out() p.P(`}`) } if field.GetNumber() > maxFieldNumber { maxFieldNumber = field.GetNumber() } } if message.DescriptorProto.HasExtension() { p.P(`if !easy && r.Intn(10) != 0 {`) p.In() p.P(`l := r.Intn(5)`) p.P(`for i := 0; i < l; i++ {`) p.In() if len(message.DescriptorProto.GetExtensionRange()) > 1 { p.P(`eIndex := r.Intn(`, strconv.Itoa(len(message.DescriptorProto.GetExtensionRange())), `)`) p.P(`fieldNumber := 0`) p.P(`switch eIndex {`) for i, e := range message.DescriptorProto.GetExtensionRange() { p.P(`case `, strconv.Itoa(i), `:`) p.In() p.P(`fieldNumber = r.Intn(`, strconv.Itoa(int(e.GetEnd()-e.GetStart())), `) + `, strconv.Itoa(int(e.GetStart()))) p.Out() if e.GetEnd() > maxFieldNumber { maxFieldNumber = e.GetEnd() } } p.P(`}`) } else { e := message.DescriptorProto.GetExtensionRange()[0] p.P(`fieldNumber := r.Intn(`, strconv.Itoa(int(e.GetEnd()-e.GetStart())), `) + `, strconv.Itoa(int(e.GetStart()))) if e.GetEnd() > maxFieldNumber { maxFieldNumber = e.GetEnd() } } p.P(`wire := r.Intn(4)`) p.P(`if wire == 3 { wire = 5 }`) p.P(`data := randField`, p.localName, `(nil, r, fieldNumber, wire)`) p.P(protoPkg.Use(), `.SetRawExtension(this, int32(fieldNumber), data)`) p.Out() p.P(`}`) p.Out() p.P(`}`) } if maxFieldNumber < (1 << 10) { p.P(`if !easy && r.Intn(10) != 0 {`) p.In() if gogoproto.HasUnrecognized(file.FileDescriptorProto, message.DescriptorProto) { p.P(`this.XXX_unrecognized = randUnrecognized`, p.localName, `(r, `, strconv.Itoa(int(maxFieldNumber+1)), `)`) } p.Out() p.P(`}`) } } p.P(`return this`) p.Out() p.P(`}`) p.P(``) } if !p.atleastOne { return } p.P(`type randy`, p.localName, ` interface {`) p.In() p.P(`Float32() float32`) p.P(`Float64() float64`) p.P(`Int63() int64`) p.P(`Int31() int32`) p.P(`Uint32() uint32`) p.P(`Intn(n int) int`) p.Out() p.P(`}`) p.P(`func randUTF8Rune`, p.localName, `(r randy`, p.localName, `) rune {`) p.In() p.P(`ru := r.Intn(62)`) p.P(`if ru < 10 {`) p.In() p.P(`return rune(ru+48)`) p.Out() p.P(`} else if ru < 36 {`) p.In() p.P(`return rune(ru+55)`) p.Out() p.P(`}`) p.P(`return rune(ru+61)`) p.Out() p.P(`}`) p.P(`func randString`, p.localName, `(r randy`, p.localName, `) string {`) p.In() p.P(p.varGen.Next(), ` := r.Intn(100)`) p.P(`tmps := make([]rune, `, p.varGen.Current(), `)`) p.P(`for i := 0; i < `, p.varGen.Current(), `; i++ {`) p.In() p.P(`tmps[i] = randUTF8Rune`, p.localName, `(r)`) p.Out() p.P(`}`) p.P(`return string(tmps)`) p.Out() p.P(`}`) p.P(`func randUnrecognized`, p.localName, `(r randy`, p.localName, `, maxFieldNumber int) (data []byte) {`) p.In() p.P(`l := r.Intn(5)`) p.P(`for i := 0; i < l; i++ {`) p.In() p.P(`wire := r.Intn(4)`) p.P(`if wire == 3 { wire = 5 }`) p.P(`fieldNumber := maxFieldNumber + r.Intn(100)`) p.P(`data = randField`, p.localName, `(data, r, fieldNumber, wire)`) p.Out() p.P(`}`) p.P(`return data`) p.Out() p.P(`}`) p.P(`func randField`, p.localName, `(data []byte, r randy`, p.localName, `, fieldNumber int, wire int) []byte {`) p.In() p.P(`key := uint32(fieldNumber)<<3 | uint32(wire)`) p.P(`switch wire {`) p.P(`case 0:`) p.In() p.P(`data = encodeVarintPopulate`, p.localName, `(data, uint64(key))`) p.P(p.varGen.Next(), ` := r.Int63()`) p.P(`if r.Intn(2) == 0 {`) p.In() p.P(p.varGen.Current(), ` *= -1`) p.Out() p.P(`}`) p.P(`data = encodeVarintPopulate`, p.localName, `(data, uint64(`, p.varGen.Current(), `))`) p.Out() p.P(`case 1:`) p.In() p.P(`data = encodeVarintPopulate`, p.localName, `(data, uint64(key))`) p.P(`data = append(data, byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)))`) p.Out() p.P(`case 2:`) p.In() p.P(`data = encodeVarintPopulate`, p.localName, `(data, uint64(key))`) p.P(`ll := r.Intn(100)`) p.P(`data = encodeVarintPopulate`, p.localName, `(data, uint64(ll))`) p.P(`for j := 0; j < ll; j++ {`) p.In() p.P(`data = append(data, byte(r.Intn(256)))`) p.Out() p.P(`}`) p.Out() p.P(`default:`) p.In() p.P(`data = encodeVarintPopulate`, p.localName, `(data, uint64(key))`) p.P(`data = append(data, byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)))`) p.Out() p.P(`}`) p.P(`return data`) p.Out() p.P(`}`) p.P(`func encodeVarintPopulate`, p.localName, `(data []byte, v uint64) []byte {`) p.In() p.P(`for v >= 1<<7 {`) p.In() p.P(`data = append(data, uint8(uint64(v)&0x7f|0x80))`) p.P(`v >>= 7`) p.Out() p.P(`}`) p.P(`data = append(data, uint8(v))`) p.P(`return data`) p.Out() p.P(`}`) }
func (p *plugin) Generate(file *generator.FileDescriptor) { p.atleastOne = false p.PluginImports = generator.NewPluginImports(p.Generator) p.varGen = NewVarGen() proto3 := gogoproto.IsProto3(file.FileDescriptorProto) p.typesPkg = p.NewImport("github.com/gogo/protobuf/types") p.localName = generator.FileName(file) protoPkg := p.NewImport("github.com/gogo/protobuf/proto") if !gogoproto.ImportsGoGoProto(file.FileDescriptorProto) { protoPkg = p.NewImport("github.com/golang/protobuf/proto") } for _, message := range file.Messages() { if !gogoproto.HasPopulate(file.FileDescriptorProto, message.DescriptorProto) { continue } if message.DescriptorProto.GetOptions().GetMapEntry() { continue } p.atleastOne = true ccTypeName := generator.CamelCaseSlice(message.TypeName()) loopLevels := make([]int, len(message.Field)) maxLoopLevel := 0 for i, field := range message.Field { loopLevels[i] = p.loops(field, message) if loopLevels[i] > maxLoopLevel { maxLoopLevel = loopLevels[i] } } ranTotal := 0 for i := range loopLevels { ranTotal += int(math.Pow10(maxLoopLevel - loopLevels[i])) } p.P(`func NewPopulated`, ccTypeName, `(r randy`, p.localName, `, easy bool) *`, ccTypeName, ` {`) p.In() p.P(`this := &`, ccTypeName, `{}`) if gogoproto.IsUnion(message.File(), message.DescriptorProto) && len(message.Field) > 0 { p.P(`fieldNum := r.Intn(`, fmt.Sprintf("%d", ranTotal), `)`) p.P(`switch fieldNum {`) k := 0 for i, field := range message.Field { is := []string{} ran := int(math.Pow10(maxLoopLevel - loopLevels[i])) for j := 0; j < ran; j++ { is = append(is, fmt.Sprintf("%d", j+k)) } k += ran p.P(`case `, strings.Join(is, ","), `:`) p.In() p.GenerateField(file, message, field) p.Out() } p.P(`}`) } else { var maxFieldNumber int32 oneofs := make(map[string]struct{}) for fieldIndex, field := range message.Field { if field.GetNumber() > maxFieldNumber { maxFieldNumber = field.GetNumber() } oneof := field.OneofIndex != nil if !oneof { if field.IsRequired() || (!gogoproto.IsNullable(field) && !field.IsRepeated()) || (proto3 && !field.IsMessage()) { p.GenerateField(file, message, field) } else { if loopLevels[fieldIndex] > 0 { p.P(`if r.Intn(10) == 0 {`) } else { p.P(`if r.Intn(10) != 0 {`) } p.In() p.GenerateField(file, message, field) p.Out() p.P(`}`) } } else { fieldname := p.GetFieldName(message, field) if _, ok := oneofs[fieldname]; ok { continue } else { oneofs[fieldname] = struct{}{} } fieldNumbers := []int32{} for _, f := range message.Field { fname := p.GetFieldName(message, f) if fname == fieldname { fieldNumbers = append(fieldNumbers, f.GetNumber()) } } p.P(`oneofNumber_`, fieldname, ` := `, fmt.Sprintf("%#v", fieldNumbers), `[r.Intn(`, strconv.Itoa(len(fieldNumbers)), `)]`) p.P(`switch oneofNumber_`, fieldname, ` {`) for _, f := range message.Field { fname := p.GetFieldName(message, f) if fname != fieldname { continue } p.P(`case `, strconv.Itoa(int(f.GetNumber())), `:`) p.In() ccTypeName := p.OneOfTypeName(message, f) p.P(`this.`, fname, ` = NewPopulated`, ccTypeName, `(r, easy)`) p.Out() } p.P(`}`) } } if message.DescriptorProto.HasExtension() { p.P(`if !easy && r.Intn(10) != 0 {`) p.In() p.P(`l := r.Intn(5)`) p.P(`for i := 0; i < l; i++ {`) p.In() if len(message.DescriptorProto.GetExtensionRange()) > 1 { p.P(`eIndex := r.Intn(`, strconv.Itoa(len(message.DescriptorProto.GetExtensionRange())), `)`) p.P(`fieldNumber := 0`) p.P(`switch eIndex {`) for i, e := range message.DescriptorProto.GetExtensionRange() { p.P(`case `, strconv.Itoa(i), `:`) p.In() p.P(`fieldNumber = r.Intn(`, strconv.Itoa(int(e.GetEnd()-e.GetStart())), `) + `, strconv.Itoa(int(e.GetStart()))) p.Out() if e.GetEnd() > maxFieldNumber { maxFieldNumber = e.GetEnd() } } p.P(`}`) } else { e := message.DescriptorProto.GetExtensionRange()[0] p.P(`fieldNumber := r.Intn(`, strconv.Itoa(int(e.GetEnd()-e.GetStart())), `) + `, strconv.Itoa(int(e.GetStart()))) if e.GetEnd() > maxFieldNumber { maxFieldNumber = e.GetEnd() } } p.P(`wire := r.Intn(4)`) p.P(`if wire == 3 { wire = 5 }`) p.P(`dAtA := randField`, p.localName, `(nil, r, fieldNumber, wire)`) p.P(protoPkg.Use(), `.SetRawExtension(this, int32(fieldNumber), dAtA)`) p.Out() p.P(`}`) p.Out() p.P(`}`) } if maxFieldNumber < (1 << 10) { p.P(`if !easy && r.Intn(10) != 0 {`) p.In() if gogoproto.HasUnrecognized(file.FileDescriptorProto, message.DescriptorProto) { p.P(`this.XXX_unrecognized = randUnrecognized`, p.localName, `(r, `, strconv.Itoa(int(maxFieldNumber+1)), `)`) } p.Out() p.P(`}`) } } p.P(`return this`) p.Out() p.P(`}`) p.P(``) //Generate NewPopulated functions for oneof fields m := proto.Clone(message.DescriptorProto).(*descriptor.DescriptorProto) for _, f := range m.Field { oneof := f.OneofIndex != nil if !oneof { continue } ccTypeName := p.OneOfTypeName(message, f) p.P(`func NewPopulated`, ccTypeName, `(r randy`, p.localName, `, easy bool) *`, ccTypeName, ` {`) p.In() p.P(`this := &`, ccTypeName, `{}`) vanity.TurnOffNullableForNativeTypesWithoutDefaultsOnly(f) p.GenerateField(file, message, f) p.P(`return this`) p.Out() p.P(`}`) } } if !p.atleastOne { return } p.P(`type randy`, p.localName, ` interface {`) p.In() p.P(`Float32() float32`) p.P(`Float64() float64`) p.P(`Int63() int64`) p.P(`Int31() int32`) p.P(`Uint32() uint32`) p.P(`Intn(n int) int`) p.Out() p.P(`}`) p.P(`func randUTF8Rune`, p.localName, `(r randy`, p.localName, `) rune {`) p.In() p.P(`ru := r.Intn(62)`) p.P(`if ru < 10 {`) p.In() p.P(`return rune(ru+48)`) p.Out() p.P(`} else if ru < 36 {`) p.In() p.P(`return rune(ru+55)`) p.Out() p.P(`}`) p.P(`return rune(ru+61)`) p.Out() p.P(`}`) p.P(`func randString`, p.localName, `(r randy`, p.localName, `) string {`) p.In() p.P(p.varGen.Next(), ` := r.Intn(100)`) p.P(`tmps := make([]rune, `, p.varGen.Current(), `)`) p.P(`for i := 0; i < `, p.varGen.Current(), `; i++ {`) p.In() p.P(`tmps[i] = randUTF8Rune`, p.localName, `(r)`) p.Out() p.P(`}`) p.P(`return string(tmps)`) p.Out() p.P(`}`) p.P(`func randUnrecognized`, p.localName, `(r randy`, p.localName, `, maxFieldNumber int) (dAtA []byte) {`) p.In() p.P(`l := r.Intn(5)`) p.P(`for i := 0; i < l; i++ {`) p.In() p.P(`wire := r.Intn(4)`) p.P(`if wire == 3 { wire = 5 }`) p.P(`fieldNumber := maxFieldNumber + r.Intn(100)`) p.P(`dAtA = randField`, p.localName, `(dAtA, r, fieldNumber, wire)`) p.Out() p.P(`}`) p.P(`return dAtA`) p.Out() p.P(`}`) p.P(`func randField`, p.localName, `(dAtA []byte, r randy`, p.localName, `, fieldNumber int, wire int) []byte {`) p.In() p.P(`key := uint32(fieldNumber)<<3 | uint32(wire)`) p.P(`switch wire {`) p.P(`case 0:`) p.In() p.P(`dAtA = encodeVarintPopulate`, p.localName, `(dAtA, uint64(key))`) p.P(p.varGen.Next(), ` := r.Int63()`) p.P(`if r.Intn(2) == 0 {`) p.In() p.P(p.varGen.Current(), ` *= -1`) p.Out() p.P(`}`) p.P(`dAtA = encodeVarintPopulate`, p.localName, `(dAtA, uint64(`, p.varGen.Current(), `))`) p.Out() p.P(`case 1:`) p.In() p.P(`dAtA = encodeVarintPopulate`, p.localName, `(dAtA, uint64(key))`) p.P(`dAtA = append(dAtA, byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)))`) p.Out() p.P(`case 2:`) p.In() p.P(`dAtA = encodeVarintPopulate`, p.localName, `(dAtA, uint64(key))`) p.P(`ll := r.Intn(100)`) p.P(`dAtA = encodeVarintPopulate`, p.localName, `(dAtA, uint64(ll))`) p.P(`for j := 0; j < ll; j++ {`) p.In() p.P(`dAtA = append(dAtA, byte(r.Intn(256)))`) p.Out() p.P(`}`) p.Out() p.P(`default:`) p.In() p.P(`dAtA = encodeVarintPopulate`, p.localName, `(dAtA, uint64(key))`) p.P(`dAtA = append(dAtA, byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)), byte(r.Intn(256)))`) p.Out() p.P(`}`) p.P(`return dAtA`) p.Out() p.P(`}`) p.P(`func encodeVarintPopulate`, p.localName, `(dAtA []byte, v uint64) []byte {`) p.In() p.P(`for v >= 1<<7 {`) p.In() p.P(`dAtA = append(dAtA, uint8(uint64(v)&0x7f|0x80))`) p.P(`v >>= 7`) p.Out() p.P(`}`) p.P(`dAtA = append(dAtA, uint8(v))`) p.P(`return dAtA`) p.Out() p.P(`}`) }