func (p *plugin) GenerateField(message *generator.Descriptor, field *descriptor.FieldDescriptorProto) { goTyp, _ := p.GoType(message, field) fieldname := generator.CamelCase(*field.Name) if field.IsMessage() && gogoproto.IsEmbed(field) { fieldname = generator.EmbedFieldName(goTyp) } goTypName := generator.GoTypeToName(goTyp) if field.IsMessage() || p.IsGroup(field) { funcName := "NewPopulated" + goTypName goTypNames := strings.Split(goTypName, ".") if len(goTypNames) == 2 { funcName = goTypNames[0] + ".NewPopulated" + goTypNames[1] } else if len(goTypNames) != 1 { panic(fmt.Errorf("unreachable: too many dots in %v", goTypName)) } funcCall := funcName + "(r, easy)" 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() { 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), `)])`}, "") 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) { p.P(p.varGen.Next(), ` := `, val) p.P(`this.`, fieldname, ` = &`, p.varGen.Current()) } else { p.P(`this.`, fieldname, ` = `, val) } } else if gogoproto.IsCustomType(field) { funcName := "NewPopulated" + goTypName goTypNames := strings.Split(goTypName, ".") if len(goTypNames) == 2 { funcName = goTypNames[0] + ".NewPopulated" + goTypNames[1] } else if len(goTypNames) != 1 { panic(fmt.Errorf("unreachable: too many dots in %v", goTypName)) } funcCall := funcName + "(r)" 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) { p.P(p.varGen.Next(), `:= `, val) p.P(`this.`, fieldname, ` = &`, p.varGen.Current()) } else { p.P(`this.`, fieldname, ` = `, val) } } 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)) p.Out() p.P(`}`) } else if gogoproto.IsNullable(field) { p.P(p.varGen.Next(), ` := `, value(typName)) p.P(`this.`, fieldname, ` = &`, p.varGen.Current()) } else { p.P(`this.`, fieldname, ` = `, value(typName)) } } } }
func (p *union) Generate(file *generator.FileDescriptor) { p.PluginImports = generator.NewPluginImports(p.Generator) for _, message := range file.Messages() { if !gogoproto.IsUnion(file.FileDescriptorProto, message.DescriptorProto) { continue } if message.DescriptorProto.HasExtension() { panic("onlyone does not currently support extensions") } ccTypeName := generator.CamelCaseSlice(message.TypeName()) p.P(`func (this *`, ccTypeName, `) GetValue() interface{} {`) p.In() for _, field := range message.Field { fieldname := p.GetFieldName(message, field) if fieldname == "Value" { panic("cannot have a onlyone message " + ccTypeName + " with a field named Value") } p.P(`if this.`, fieldname, ` != nil {`) p.In() p.P(`return this.`, fieldname) p.Out() p.P(`}`) } p.P(`return nil`) p.Out() p.P(`}`) p.P(``) p.P(`func (this *`, ccTypeName, `) SetValue(value interface{}) bool {`) p.In() p.P(`switch vt := value.(type) {`) p.In() for _, field := range message.Field { fieldname := p.GetFieldName(message, field) goTyp, _ := p.GoType(message, field) p.P(`case `, goTyp, `:`) p.In() p.P(`this.`, fieldname, ` = vt`) p.Out() } p.P(`default:`) p.In() for _, field := range message.Field { fieldname := p.GetFieldName(message, field) if field.IsMessage() { goTyp, _ := p.GoType(message, field) obj := p.ObjectNamed(field.GetTypeName()).(*generator.Descriptor) if gogoproto.IsUnion(obj.File(), obj.DescriptorProto) { p.P(`this.`, fieldname, ` = new(`, generator.GoTypeToName(goTyp), `)`) p.P(`if set := this.`, fieldname, `.SetValue(value); set {`) p.In() p.P(`return true`) p.Out() p.P(`}`) p.P(`this.`, fieldname, ` = nil`) } } } p.P(`return false`) p.Out() p.P(`}`) p.P(`return true`) p.Out() p.P(`}`) } }
func (p *gostring) Generate(file *generator.FileDescriptor) { p.PluginImports = generator.NewPluginImports(p.Generator) p.atleastOne = false p.localName = generator.FileName(file) fmtPkg := p.NewImport("fmt") stringsPkg := p.NewImport("strings") protoPkg := p.NewImport("code.google.com/p/gogoprotobuf/proto") sortPkg := p.NewImport("sort") strconvPkg := p.NewImport("strconv") reflectPkg := p.NewImport("reflect") for _, message := range file.Messages() { if !gogoproto.HasGoString(file.FileDescriptorProto, message.DescriptorProto) { continue } p.atleastOne = true packageName := file.PackageName() ccTypeName := generator.CamelCaseSlice(message.TypeName()) p.P(`func (this *`, ccTypeName, `) GoString() string {`) p.In() p.P(`if this == nil {`) p.In() p.P(`return "nil"`) p.Out() p.P(`}`) plus := "+" out := strings.Join([]string{"s := ", stringsPkg.Use(), ".Join([]string{`&", packageName, ".", ccTypeName, "{` ", plus, " "}, "") for _, field := range message.Field { nullable := gogoproto.IsNullable(field) repeated := field.IsRepeated() fieldname := p.GetFieldName(message, field) if field.IsMessage() || p.IsGroup(field) { out += strings.Join([]string{"`", fieldname, ":` + "}, "") if nullable { out += strings.Join([]string{fmtPkg.Use(), `.Sprintf("%#v", this.`, fieldname, `)`}, "") } else if repeated { out += strings.Join([]string{stringsPkg.Use(), `.Replace(`, fmtPkg.Use(), `.Sprintf("%#v", this.`, fieldname, `)`, ",`&`,``,1)"}, "") } else { out += strings.Join([]string{stringsPkg.Use(), `.Replace(this.`, fieldname, `.GoString()`, ",`&`,``,1)"}, "") } } else { out += strings.Join([]string{"`", fieldname, ":` + "}, "") if field.IsEnum() { if nullable && !repeated { goTyp, _ := p.GoType(message, field) out += strings.Join([]string{`valueToGoString`, p.localName, `(this.`, fieldname, `,"`, packageName, ".", generator.GoTypeToName(goTyp), `"`, ")"}, "") } else { out += strings.Join([]string{fmtPkg.Use(), `.Sprintf("%#v", this.`, fieldname, ")"}, "") } } else { if nullable && !repeated { goTyp, _ := p.GoType(message, field) out += strings.Join([]string{`valueToGoString`, p.localName, `(this.`, fieldname, `,"`, generator.GoTypeToName(goTyp), `"`, ")"}, "") } else { out += strings.Join([]string{fmtPkg.Use(), `.Sprintf("%#v", this.`, fieldname, ")"}, "") } } } out += ", " } if message.DescriptorProto.HasExtension() { if gogoproto.HasExtensionsMap(file.FileDescriptorProto, message.DescriptorProto) { out += strings.Join([]string{"`XXX_extensions: ` + extensionToGoString", p.localName, `(this.XXX_extensions),`}, "") } else { out += strings.Join([]string{"`XXX_extensions:` + ", fmtPkg.Use(), `.Sprintf("%#v", this.XXX_extensions),`}, "") } } out += strings.Join([]string{"`XXX_unrecognized:` + ", fmtPkg.Use(), `.Sprintf("%#v", this.XXX_unrecognized)`}, "") out += "+ `}`" out = strings.Join([]string{out, `}`, `,", "`, ")"}, "") p.P(out) p.P(`return s`) p.Out() p.P(`}`) } if !p.atleastOne { return } p.P(`func valueToGoString`, p.localName, `(v interface{}, typ string) string {`) p.In() p.P(`rv := `, reflectPkg.Use(), `.ValueOf(v)`) p.P(`if rv.IsNil() {`) p.In() p.P(`return "nil"`) p.Out() p.P(`}`) p.P(`pv := `, reflectPkg.Use(), `.Indirect(rv).Interface()`) p.P(`return `, fmtPkg.Use(), `.Sprintf("func(v %v) *%v { return &v } ( %#v )", typ, typ, pv)`) p.Out() p.P(`}`) p.P(`func extensionToGoString`, p.localName, `(e map[int32]`, protoPkg.Use(), `.Extension) string {`) p.In() p.P(`if e == nil { return "nil" }`) p.P(`s := "map[int32]proto.Extension{"`) p.P(`keys := make([]int, 0, len(e))`) p.P(`for k := range e {`) p.In() p.P(`keys = append(keys, int(k))`) p.Out() p.P(`}`) p.P(sortPkg.Use(), `.Ints(keys)`) p.P(`ss := []string{}`) p.P(`for _, k := range keys {`) p.In() p.P(`ss = append(ss, `, strconvPkg.Use(), `.Itoa(k) + ": " + e[int32(k)].GoString())`) p.Out() p.P(`}`) p.P(`s+=`, stringsPkg.Use(), `.Join(ss, ",") + "}"`) p.P(`return s`) p.Out() p.P(`}`) }
func (p *plugin) Generate(file *generator.FileDescriptor) { p.PluginImports = generator.NewPluginImports(p.Generator) protoPkg := p.NewImport("code.google.com/p/gogoprotobuf/proto") for _, message := range file.Messages() { if !gogoproto.IsFace(file.FileDescriptorProto, message.DescriptorProto) { continue } if message.DescriptorProto.HasExtension() { panic("face does not support message with extensions") } if gogoproto.HasGoGetters(file.FileDescriptorProto, message.DescriptorProto) { panic("face requires getters to be disabled please use gogoproto.getters or gogoproto.getters_all and set it to false") } ccTypeName := generator.CamelCaseSlice(message.TypeName()) p.P(`type `, ccTypeName, `Face interface{`) p.In() p.P(`Proto() `, protoPkg.Use(), `.Message`) for _, field := range message.Field { fieldname := generator.CamelCase(*field.Name) goTyp, _ := p.GoType(message, field) if field.IsMessage() && gogoproto.IsEmbed(field) { fieldname = generator.GoTypeToName(goTyp) } p.P(`Get`, fieldname, `() `, goTyp) } p.Out() p.P(`}`) p.P(``) p.P(`func (this *`, ccTypeName, `) Proto() `, protoPkg.Use(), `.Message {`) p.In() p.P(`return this`) p.Out() p.P(`}`) p.P(``) p.P(`func (this *`, ccTypeName, `) TestProto() `, protoPkg.Use(), `.Message {`) p.In() p.P(`return New`, ccTypeName, `FromFace(this)`) p.Out() p.P(`}`) p.P(``) for _, field := range message.Field { fieldname := generator.CamelCase(*field.Name) goTyp, _ := p.GoType(message, field) if field.IsMessage() && gogoproto.IsEmbed(field) { fieldname = generator.GoTypeToName(goTyp) } p.P(`func (this *`, ccTypeName, `) Get`, fieldname, `() `, goTyp, `{`) p.In() p.P(` return this.`, fieldname) p.Out() p.P(`}`) p.P(``) } p.P(``) p.P(`func New`, ccTypeName, `FromFace(that `, ccTypeName, `Face) *`, ccTypeName, ` {`) p.In() p.P(`this := &`, ccTypeName, `{}`) for _, field := range message.Field { fieldname := generator.CamelCase(*field.Name) if field.IsMessage() && gogoproto.IsEmbed(field) { goTyp, _ := p.GoType(message, field) fieldname = generator.GoTypeToName(goTyp) } p.P(`this.`, fieldname, ` = that.Get`, fieldname, `()`) } p.P(`return this`) p.Out() p.P(`}`) p.P(``) } }