func (p *enumstringer) Generate(file *generator.FileDescriptor) { p.PluginImports = generator.NewPluginImports(p.Generator) p.atleastOne = false p.localName = generator.FileName(file) strconvPkg := p.NewImport("strconv") for _, enum := range file.Enums() { if !gogoproto.IsEnumStringer(file.FileDescriptorProto, enum.EnumDescriptorProto) { continue } if gogoproto.IsGoEnumStringer(file.FileDescriptorProto, enum.EnumDescriptorProto) { panic("old enum string method needs to be disabled, please use gogoproto.old_enum_stringer or gogoproto.old_enum_string_all and set it to false") } p.atleastOne = true ccTypeName := generator.CamelCaseSlice(enum.TypeName()) p.P("func (x ", ccTypeName, ") String() string {") p.In() p.P(`s, ok := `, ccTypeName, `_name[int32(x)]`) p.P(`if ok {`) p.In() p.P(`return s`) p.Out() p.P(`}`) p.P(`return `, strconvPkg.Use(), `.Itoa(int(x))`) p.Out() p.P(`}`) } if !p.atleastOne { return } }
func (p *test) Generate(imports generator.PluginImports, file *generator.FileDescriptor) bool { used := false testingPkg := imports.NewImport("testing") for _, message := range file.Messages() { if !gogoproto.HasDescription(file.FileDescriptorProto, message.DescriptorProto) || !gogoproto.HasTestGen(file.FileDescriptorProto, message.DescriptorProto) { continue } used = true } if used { localName := generator.FileName(file) p.P(`func Test`, localName, `Description(t *`, testingPkg.Use(), `.T) {`) p.In() p.P(localName, `Description()`) p.Out() p.P(`}`) } return used }
func (p *plugin) Generate(file *generator.FileDescriptor) { p.used = false localName := generator.FileName(file) for _, message := range file.Messages() { if !gogoproto.HasDescription(file.FileDescriptorProto, message.DescriptorProto) { continue } p.used = true ccTypeName := generator.CamelCaseSlice(message.TypeName()) p.P(`func (this *`, ccTypeName, `) Description() (desc *google_protobuf.FileDescriptorSet) {`) p.In() p.P(`return `, localName, `Description()`) p.Out() p.P(`}`) } if p.used { p.P(`func `, localName, `Description() (desc *google_protobuf.FileDescriptorSet) {`) p.In() //Don't generate SourceCodeInfo, since it will create too much code. ss := make([]*descriptor.SourceCodeInfo, 0) for _, f := range p.Generator.AllFiles().GetFile() { ss = append(ss, f.SourceCodeInfo) f.SourceCodeInfo = nil } s := fmt.Sprintf("%#v", p.Generator.AllFiles()) for i, f := range p.Generator.AllFiles().GetFile() { f.SourceCodeInfo = ss[i] } p.P(`return `, s) p.Out() p.P(`}`) } }
func (p *stringer) 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") reflectPkg := p.NewImport("reflect") for _, message := range file.Messages() { if !gogoproto.IsStringer(file.FileDescriptorProto, message.DescriptorProto) { continue } if gogoproto.EnabledGoStringer(file.FileDescriptorProto, message.DescriptorProto) { panic("old string method needs to be disabled, please use gogoproto.goproto_stringer or gogoproto.goproto_stringer_all and set it to false") } p.atleastOne = true ccTypeName := generator.CamelCaseSlice(message.TypeName()) p.P(`func (this *`, ccTypeName, `) String() string {`) p.In() p.P(`if this == nil {`) p.In() p.P(`return "nil"`) p.Out() p.P(`}`) p.P("s := ", stringsPkg.Use(), ".Join([]string{`&", ccTypeName, "{`,") for _, field := range message.Field { nullable := gogoproto.IsNullable(field) repeated := field.IsRepeated() fieldname := generator.CamelCase(*field.Name) if field.IsMessage() || p.IsGroup(field) { desc := p.ObjectNamed(field.GetTypeName()) msgname := p.TypeName(desc) msgnames := strings.Split(msgname, ".") typeName := msgnames[len(msgnames)-1] if gogoproto.IsEmbed(field) { fieldname = typeName } if nullable { p.P("`", fieldname, ":`", ` + `, stringsPkg.Use(), `.Replace(`, fmtPkg.Use(), `.Sprintf("%v", this.`, fieldname, `), "`, typeName, `","`, msgname, `"`, ", 1) + `,", "`,") } else if repeated { p.P("`", fieldname, ":`", ` + `, stringsPkg.Use(), `.Replace(`, stringsPkg.Use(), `.Replace(`, fmtPkg.Use(), `.Sprintf("%v", this.`, fieldname, `), "`, typeName, `","`, msgname, `"`, ", 1),`&`,``,1) + `,", "`,") } else { p.P("`", fieldname, ":`", ` + `, stringsPkg.Use(), `.Replace(`, stringsPkg.Use(), `.Replace(this.`, fieldname, `.String(), "`, typeName, `","`, msgname, `"`, ", 1),`&`,``,1) + `,", "`,") } } else { if nullable && !repeated { p.P("`", fieldname, ":`", ` + valueToString`, p.localName, `(this.`, fieldname, ") + `,", "`,") } else { p.P("`", fieldname, ":`", ` + `, fmtPkg.Use(), `.Sprintf("%v", this.`, fieldname, ") + `,", "`,") } } } if message.DescriptorProto.HasExtension() { p.P("`XXX_extensions:` + ", fmtPkg.Use(), `.Sprintf("%v", this.XXX_extensions) + `, "`,`,") } p.P("`XXX_unrecognized:` + ", fmtPkg.Use(), `.Sprintf("%v", this.XXX_unrecognized) + `, "`,`,") p.P("`}`,") p.P(`}`, `,""`, ")") p.P(`return s`) p.Out() p.P(`}`) } if !p.atleastOne { return } p.P(`func valueToString`, p.localName, `(v interface{}) 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("*%v", pv)`) p.Out() p.P(`}`) }
func (p *size) Generate(file *generator.FileDescriptor) { p.PluginImports = generator.NewPluginImports(p.Generator) p.atleastOne = false p.localName = generator.FileName(file) protoPkg := p.NewImport("code.google.com/p/gogoprotobuf/proto") for _, message := range file.Messages() { if !gogoproto.IsSizer(file.FileDescriptorProto, message.DescriptorProto) { continue } p.atleastOne = true ccTypeName := generator.CamelCaseSlice(message.TypeName()) p.P(`func (m *`, ccTypeName, `) Size() (n int) {`) p.In() p.P(`var l int`) p.P(`_ = l`) for _, field := range message.Field { fieldname := p.GetFieldName(message, field) nullable := gogoproto.IsNullable(field) repeated := field.IsRepeated() if repeated { p.P(`if len(m.`, fieldname, `) > 0 {`) p.In() } else if nullable { 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 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 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: 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 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_INT32: if packed { p.P(`l = 0`) p.P(`for _, e := range m.`, fieldname, ` {`) p.In() p.P(`l+=sov`, p.localName, `(uint64(uint32(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(uint32(e)))`) p.Out() p.P(`}`) } else if nullable { p.P(`n+=`, strconv.Itoa(key), `+sov`, p.localName, `(uint64(uint32(*m.`, fieldname, `)))`) } else { p.P(`n+=`, strconv.Itoa(key), `+sov`, p.localName, `(uint64(uint32(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 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 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 repeated { p.P(`for _, e := range m.`, fieldname, ` { `) p.In() p.P(`l=e.Size()`) p.P(`n+=`, strconv.Itoa(key), `+l+sov`, p.localName, `(uint64(l))`) p.Out() p.P(`}`) } else { p.P(`l=m.`, fieldname, `.Size()`) 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 { 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.Size()`) p.P(`n+=`, strconv.Itoa(key), `+l+sov`, p.localName, `(uint64(l))`) p.Out() p.P(`}`) } else { p.P(`l=m.`, fieldname, `.Size()`) 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 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 nullable || repeated { p.Out() p.P(`}`) } } if message.DescriptorProto.HasExtension() { p.P(`if m.XXX_extensions != nil {`) p.In() if gogoproto.HasExtensionsMap(file.FileDescriptorProto, message.DescriptorProto) { p.P(`n += `, protoPkg.Use(), `.SizeOfExtensionMap(m.XXX_extensions)`) } else { p.P(`n+=len(m.XXX_extensions)`) } p.Out() p.P(`}`) } p.P(`if m.XXX_unrecognized != nil {`) p.In() p.P(`n+=len(m.XXX_unrecognized)`) p.Out() p.P(`}`) p.P(`return n`) p.Out() p.P(`}`) } if !p.atleastOne { return } p.sizeVarint() p.sizeZigZag() }
func (p *marshalto) Generate(file *generator.FileDescriptor) { numGen := NewNumGen() p.PluginImports = generator.NewPluginImports(p.Generator) p.atleastOne = false p.localName = generator.FileName(file) protoPkg := p.NewImport("code.google.com/p/gogoprotobuf/proto") p.unsafePkg = p.NewImport("unsafe") for _, message := range file.Messages() { if !gogoproto.IsUnsafeMarshaler(file.FileDescriptorProto, message.DescriptorProto) { continue } ccTypeName := generator.CamelCaseSlice(message.TypeName()) if gogoproto.IsMarshaler(file.FileDescriptorProto, message.DescriptorProto) { panic(fmt.Sprintf("unsafe_marshaler and marshaler enabled for %v", ccTypeName)) } p.atleastOne = true p.P(`func (m *`, ccTypeName, `) Marshal() (data []byte, err error) {`) p.In() p.P(`size := m.Size()`) p.P(`data = make([]byte, size)`) p.P(`n, err := m.MarshalTo(data)`) p.P(`if err != nil {`) p.In() p.P(`return nil, err`) p.Out() p.P(`}`) p.P(`return data[:n], nil`) p.Out() p.P(`}`) p.P(``) p.P(`func (m *`, ccTypeName, `) MarshalTo(data []byte) (n int, err error) {`) p.In() p.P(`var i int`) p.P(`_ = i`) p.P(`var l int`) p.P(`_ = l`) for _, field := range message.Field { fieldname := generator.CamelCase(*field.Name) if field.IsMessage() { desc := p.ObjectNamed(field.GetTypeName()) msgname := p.TypeName(desc) msgnames := strings.Split(msgname, ".") typeName := msgnames[len(msgnames)-1] if gogoproto.IsEmbed(field) { fieldname = typeName } } nullable := gogoproto.IsNullable(field) repeated := field.IsRepeated() if repeated { p.P(`if len(m.`, fieldname, `) > 0 {`) p.In() } else if nullable { 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 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 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 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 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`) 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.encodeVarint("num") 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: 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 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: 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 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 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 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 repeated { p.P(`for _, msg := range m.`, fieldname, ` {`) p.In() p.encodeKey(fieldNumber, wireType) 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) 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 { 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) 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) 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 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 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 nullable || repeated { p.Out() p.P(`}`) } } if message.DescriptorProto.HasExtension() { p.P(`if len(m.XXX_extensions) > 0 {`) p.In() p.P(`n, err := `, protoPkg.Use(), `.EncodeExtensionMap(m.XXX_extensions, 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(`}`) } p.P(`if m.XXX_unrecognized != nil {`) p.In() p.P(`i+=copy(data[i:], m.XXX_unrecognized)`) p.Out() p.P(`}`) p.P(`return i, nil`) p.Out() p.P(`}`) } if p.atleastOne { p.P(`func encodeFixed64`, p.localName, `(data []byte, offset int, v uint64) int {`) p.In() p.P(`data[offset] = uint8(v)`) p.P(`data[offset+1] = uint8(v >> 8)`) p.P(`data[offset+2] = uint8(v >> 16)`) p.P(`data[offset+3] = uint8(v >> 24)`) p.P(`data[offset+4] = uint8(v >> 32)`) p.P(`data[offset+5] = uint8(v >> 40)`) p.P(`data[offset+6] = uint8(v >> 48)`) p.P(`data[offset+7] = uint8(v >> 56)`) p.P(`return offset+8`) p.Out() p.P(`}`) p.P(`func encodeFixed32`, p.localName, `(data []byte, offset int, v uint32) int {`) p.In() p.P(`data[offset] = uint8(v)`) p.P(`data[offset+1] = uint8(v >> 8)`) p.P(`data[offset+2] = uint8(v >> 16)`) p.P(`data[offset+3] = uint8(v >> 24)`) p.P(`return offset+4`) p.Out() p.P(`}`) p.P(`func encodeVarint`, p.localName, `(data []byte, offset int, v uint64) int {`) p.In() p.P(`for v >= 1<<7 {`) p.In() p.P(`data[offset] = uint8(v&0x7f|0x80)`) p.P(`v >>= 7`) p.P(`offset++`) p.Out() p.P(`}`) p.P(`data[offset] = uint8(v)`) p.P(`return offset+1`) p.Out() p.P(`}`) } }
func (p *plugin) Generate(file *generator.FileDescriptor) { p.atleastOne = false p.PluginImports = generator.NewPluginImports(p.Generator) p.varGen = NewVarGen() p.localName = generator.FileName(file) protoPkg := p.NewImport("code.google.com/p/gogoprotobuf/proto") for _, message := range file.Messages() { if !gogoproto.HasPopulate(file.FileDescriptorProto, message.DescriptorProto) { 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(message, field) p.Out() } p.P(`}`) } else { var maxFieldNumber int32 for _, field := range message.Field { if field.IsRequired() || (!gogoproto.IsNullable(field) && !field.IsRepeated()) { p.GenerateField(message, field) } else { p.P(`if r.Intn(10) != 0 {`) p.In() p.GenerateField(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() 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(`}`) surrogateRange := surrogateMax - surrogateMin maxRand := maxRune - surrogateRange p.P(`func randUTF8Rune`, p.localName, `(r randy`, p.localName, `) rune {`) p.In() p.P(`res := rune(r.Uint32() % `, fmt.Sprintf("%d", maxRand), `)`) p.P(`if `, fmt.Sprintf("%d", surrogateMin), ` <= res {`) p.In() p.P(`res += `, fmt.Sprintf("%d", surrogateRange)) p.Out() p.P(`}`) p.P(`return res`) 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(`data = encodeVarintPopulate`, p.localName, `(data, uint64(r.Int63()))`) 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 *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(`}`) }