本文整理匯總了Golang中github.com/gogo/protobuf/protoc-gen-gogo/generator.Descriptor類的典型用法代碼示例。如果您正苦於以下問題:Golang Descriptor類的具體用法?Golang Descriptor怎麽用?Golang Descriptor使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Descriptor類的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: checkNameSpace
func (p *plugin) checkNameSpace(message *generator.Descriptor) map[string]bool {
ccTypeName := generator.CamelCaseSlice(message.TypeName())
names := make(map[string]bool)
for _, field := range message.Field {
fieldname := generator.CamelCase(*field.Name)
if field.IsMessage() && gogoproto.IsEmbed(field) {
desc := p.ObjectNamed(field.GetTypeName())
moreNames := p.checkNameSpace(desc.(*generator.Descriptor))
for another := range moreNames {
if names[another] {
fmt.Fprintf(os.Stderr, "ERROR: duplicate embedded fieldname %v in type %v\n", fieldname, ccTypeName)
os.Exit(1)
}
names[another] = true
}
} else {
if names[fieldname] {
fmt.Fprintf(os.Stderr, "ERROR: duplicate embedded fieldname %v in type %v\n", fieldname, ccTypeName)
os.Exit(1)
}
names[fieldname] = true
}
}
return names
}
示例2: genOneOfWriter
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
}
示例3: hasLoop
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
}
}
for _, f := range fieldMessage.Field {
visited = append(visited, fieldMessage)
loopTo := p.hasLoop(f, visited, excludes)
if loopTo != nil {
return loopTo
}
}
}
return nil
}
示例4: checkRepeated
func (p *plugin) checkRepeated(message *generator.Descriptor) {
ccTypeName := generator.CamelCaseSlice(message.TypeName())
for _, field := range message.Field {
if !gogoproto.IsEmbed(field) {
continue
}
if !field.IsRepeated() {
continue
}
fieldname := generator.CamelCase(*field.Name)
fmt.Fprintf(os.Stderr, "ERROR: found repeated embedded field %s in message %s\n", fieldname, ccTypeName)
os.Exit(1)
}
}
示例5: genOneOf
func (d *deepCopyGen) genOneOf(m *generator.Descriptor, oneof *descriptor.OneofDescriptorProto, fields []*descriptor.FieldDescriptorProto) {
oneOfName := generator.CamelCase(oneof.GetName())
d.P("if o.", oneOfName, " != nil {")
d.In()
d.P("switch o.", oneOfName, ".(type) {")
for _, f := range fields {
ccTypeName := generator.CamelCaseSlice(m.TypeName())
fName := generator.CamelCase(*f.Name)
if gogoproto.IsCustomName(f) {
fName = gogoproto.GetCustomName(f)
}
tName := ccTypeName + "_" + fName
d.P("case *", tName, ":")
d.In()
d.P("v := ", tName, " {")
d.In()
var rhs string
if f.IsMessage() {
goType := d.TypeName(d.ObjectNamed(f.GetTypeName())) // elides [] or *
rhs = "&" + goType + "{}"
} else {
rhs = "o.Get" + fName + "()"
}
d.P(fName, ": ", rhs, ",")
d.Out()
d.P("}")
if f.IsMessage() {
d.genCopyFunc("v."+fName, "o.Get"+fName+"()")
}
d.P("m.", oneOfName, " = &v")
d.Out()
}
d.Out()
d.P("}")
d.Out()
d.P("}")
d.P()
}
示例6: checkOverwrite
func (p *plugin) checkOverwrite(message *generator.Descriptor, enablers map[string]gogoproto.EnableFunc) {
ccTypeName := generator.CamelCaseSlice(message.TypeName())
names := []string{}
for name := range enablers {
names = append(names, name)
}
for _, field := range message.Field {
if field.IsMessage() && gogoproto.IsEmbed(field) {
fieldname := generator.CamelCase(*field.Name)
desc := p.ObjectNamed(field.GetTypeName())
msg := desc.(*generator.Descriptor)
for errStr, enabled := range enablers {
if enabled(msg.File(), msg.DescriptorProto) {
fmt.Fprintf(os.Stderr, "WARNING: found non-%v %v with embedded %v %v\n", names, ccTypeName, errStr, fieldname)
}
}
p.checkOverwrite(msg, enablers)
}
}
}
示例7: Builder
func Builder(visited map[string]struct{}, root bool, fieldname string, help string, repeated bool, msg *generator.Descriptor, g *generator.Generator, proto3 bool, buildField FieldBuilder) string {
s := []string{`function build` + typ(fieldname, repeated, msg.DescriptorProto) + `(json) {`}
if repeated {
s = append(s, `var s = '<div class="node" type="`+typ(fieldname, repeated, msg.DescriptorProto)+`" fieldname="`+fieldname+`" repeated="true">';`)
} else {
s = append(s, `if (json == undefined) {
return "";
}
`)
s = append(s, `var s = '<div class="node" type="`+typ(fieldname, repeated, msg.DescriptorProto)+`" fieldname="`+fieldname+`" repeated="false">';`)
}
if !root {
s = append(s, `s += '<div class="row"><div class="col-sm-2">'`)
s = append(s, `s += '<a href="#" class="del-child btn btn-danger btn-xs" role="button" fieldname="`+fieldname+`">Remove</a>'`)
s = append(s, `s += '</div><div class="col-sm-10">'`)
s = append(s, `s += '<label class="heading">`+fieldname+`</label>'`)
s = append(s, `s += '</div></div>'`)
}
ms := []string{}
for i, f := range msg.GetField() {
help := g.Comments(fmt.Sprintf("%s,%d,%d", msg.Path(), 2, i))
if f.IsMessage() {
fieldMsg := g.ObjectNamed(f.GetTypeName()).(*generator.Descriptor)
if _, ok := visited[msg.GetName()+"."+f.GetName()]; !ok {
visited[msg.GetName()+"."+f.GetName()] = struct{}{}
ms = append(ms, Builder(visited, false, f.GetName(), help, f.IsRepeated(), fieldMsg, g, proto3, buildField))
}
}
s = append(s, buildField(g.AllFiles(), msg.DescriptorProto, f, help, proto3))
}
if root {
s = append(s, `
s += '</div>';
var node = $(s);
activateLinks(node);
return node;
}`)
} else {
s = append(s, `
s += '</div>';
return s;
}`)
}
f := strings.Join(s, "\n")
ms = append(ms, f)
return strings.Join(ms, "\n\n")
}
示例8: genMsgDeepCopy
func (d *deepCopyGen) genMsgDeepCopy(m *generator.Descriptor) {
if !plugin.DeepcopyEnabled(m.Options) {
return
}
ccTypeName := generator.CamelCaseSlice(m.TypeName())
d.gen.P("func (m *", ccTypeName, ") Copy() *", ccTypeName, "{")
d.gen.P("\tif m == nil {")
d.gen.P("\t\treturn nil")
d.gen.P("\t}")
d.gen.P()
d.gen.P("\to := &", ccTypeName, "{")
var funcs []func()
oneOfFuncs := make(map[string][]func())
for _, f := range m.Field {
fName := generator.CamelCase(*f.Name)
if gogoproto.IsCustomName(f) {
fName = gogoproto.GetCustomName(f)
}
notNullablePrefix := ""
if !gogoproto.IsNullable(f) {
notNullablePrefix = "*"
}
// Handle all kinds of message type
if f.IsMessage() {
// Handle map type
if mapfunc := d.genMapWriter(m, f, notNullablePrefix); mapfunc != nil {
funcs = append(funcs, mapfunc)
continue
}
// Handle any message which is not repeated or part of oneof
if !f.IsRepeated() && f.OneofIndex == nil {
d.gen.P("\t\t", fName, ": ", notNullablePrefix, "m.", fName, ".Copy(),")
continue
}
}
// Handle repeated field
if f.IsRepeated() {
funcs = append(funcs, d.genRepeatedWriter(m, f, notNullablePrefix))
continue
}
// Handle oneof type
if f.OneofIndex != nil {
d.genOneOfWriter(m, f, oneOfFuncs)
continue
}
// Handle all kinds of scalar type
d.gen.P("\t\t", fName, ": m.", fName, ",")
}
d.gen.P("\t}")
d.gen.P()
for _, fn := range funcs {
fn()
}
// Sort map keys from oneOfFuncs so that generated code has consistent
// ordering.
oneOfKeys := make([]string, 0, len(oneOfFuncs))
for key := range oneOfFuncs {
oneOfKeys = append(oneOfKeys, key)
}
sort.Strings(oneOfKeys)
for _, key := range oneOfKeys {
oneOfNameFuncs := oneOfFuncs[key]
for _, oneOfFunc := range oneOfNameFuncs {
oneOfFunc()
}
d.gen.P("\t}")
d.gen.P()
}
d.gen.P("\treturn o")
d.gen.P("}")
d.gen.P()
}
示例9: genMsgDeepCopy
func (d *deepCopyGen) genMsgDeepCopy(m *generator.Descriptor) {
ccTypeName := generator.CamelCaseSlice(m.TypeName())
// Generate backwards compatible, type-safe Copy() function.
d.P("func (m *", ccTypeName, ") Copy() *", ccTypeName, "{")
d.In()
d.P("if m == nil {")
d.In()
d.P("return nil")
d.Out()
d.P("}")
d.P("o := &", ccTypeName, "{}")
d.P("o.CopyFrom(m)")
d.P("return o")
d.Out()
d.P("}")
d.P()
if len(m.Field) == 0 {
d.P("func (m *", ccTypeName, ") CopyFrom(src interface{})", " {}")
return
}
d.P("func (m *", ccTypeName, ") CopyFrom(src interface{})", " {")
d.P()
d.P("o := src.(*", ccTypeName, ")")
// shallow copy handles all scalars
d.P("*m = *o")
oneofByIndex := [][]*descriptor.FieldDescriptorProto{}
for _, f := range m.Field {
fName := generator.CamelCase(*f.Name)
if gogoproto.IsCustomName(f) {
fName = gogoproto.GetCustomName(f)
}
// Handle oneof type, we defer them to a loop below
if f.OneofIndex != nil {
if len(oneofByIndex) <= int(*f.OneofIndex) {
oneofByIndex = append(oneofByIndex, []*descriptor.FieldDescriptorProto{})
}
oneofByIndex[*f.OneofIndex] = append(oneofByIndex[*f.OneofIndex], f)
continue
}
// Handle all kinds of message type
if f.IsMessage() {
// Handle map type
if d.genMap(m, f) {
continue
}
// Handle any message which is not repeated or part of oneof
if !f.IsRepeated() && f.OneofIndex == nil {
if !gogoproto.IsNullable(f) {
d.genCopyFunc("&m."+fName, "&o."+fName)
} else {
d.P("if o.", fName, " != nil {")
d.In()
// allocate dst object
d.P("m.", fName, " = &", d.TypeName(d.ObjectNamed(f.GetTypeName())), "{}")
// copy into the allocated struct
d.genCopyFunc("m."+fName, "o."+fName)
d.Out()
d.P("}")
}
continue
}
}
// Handle repeated field
if f.IsRepeated() {
d.genRepeated(m, f)
continue
}
// skip: field was a scalar handled by shallow copy!
}
for i, oo := range m.GetOneofDecl() {
d.genOneOf(m, oo, oneofByIndex[i])
}
d.P("}")
d.P()
}
示例10: generateMessage
func (p *plugin) generateMessage(file *generator.FileDescriptor, message *generator.Descriptor) {
ccTypeName := generator.CamelCaseSlice(message.TypeName())
p.P(`func (this *`, ccTypeName, `) Compare(that interface{}) int {`)
p.In()
p.generateMsgNullAndTypeCheck(ccTypeName)
oneofs := make(map[string]struct{})
for _, field := range message.Field {
oneof := field.OneofIndex != nil
if oneof {
fieldname := p.GetFieldName(message, field)
if _, ok := oneofs[fieldname]; ok {
continue
} else {
oneofs[fieldname] = struct{}{}
}
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 {`)
p.In()
p.P(`return c`)
p.Out()
p.P(`}`)
} else {
p.generateField(file, message, field)
}
}
if message.DescriptorProto.HasExtension() {
fieldname := "XXX_extensions"
if gogoproto.HasExtensionsMap(file.FileDescriptorProto, message.DescriptorProto) {
p.P(`extkeys := make([]int32, 0, len(this.`, fieldname, `)+len(that1.`, fieldname, `))`)
p.P(`for k, _ := range this.`, fieldname, ` {`)
p.In()
p.P(`extkeys = append(extkeys, k)`)
p.Out()
p.P(`}`)
p.P(`for k, _ := range that1.`, fieldname, ` {`)
p.In()
p.P(`if _, ok := this.`, fieldname, `[k]; !ok {`)
p.In()
p.P(`extkeys = append(extkeys, k)`)
p.Out()
p.P(`}`)
p.Out()
p.P(`}`)
p.P(p.sortkeysPkg.Use(), `.Int32s(extkeys)`)
p.P(`for _, k := range extkeys {`)
p.In()
p.P(`if v, ok := this.`, fieldname, `[k]; ok {`)
p.In()
p.P(`if v2, ok := that1.`, fieldname, `[k]; ok {`)
p.In()
p.P(`if c := v.Compare(&v2); c != 0 {`)
p.In()
p.P(`return c`)
p.Out()
p.P(`}`)
p.Out()
p.P(`} else {`)
p.In()
p.P(`return 1`)
p.Out()
p.P(`}`)
p.Out()
p.P(`} else {`)
p.In()
p.P(`return -1`)
p.Out()
p.P(`}`)
p.Out()
p.P(`}`)
} else {
p.P(`if c := `, p.bytesPkg.Use(), `.Compare(this.`, fieldname, `, that1.`, fieldname, `); c != 0 {`)
p.In()
p.P(`return c`)
p.Out()
p.P(`}`)
}
}
if gogoproto.HasUnrecognized(file.FileDescriptorProto, message.DescriptorProto) {
fieldname := "XXX_unrecognized"
p.P(`if c := `, p.bytesPkg.Use(), `.Compare(this.`, fieldname, `, that1.`, fieldname, `); c != 0 {`)
p.In()
p.P(`return c`)
p.Out()
p.P(`}`)
}
p.P(`return 0`)
p.Out()
p.P(`}`)
//.........這裏部分代碼省略.........
示例11: generateMessage
func (p *plugin) generateMessage(file *generator.FileDescriptor, message *generator.Descriptor, verbose bool) {
ccTypeName := generator.CamelCaseSlice(message.TypeName())
if verbose {
p.P(`func (this *`, ccTypeName, `) VerboseEqual(that interface{}) error {`)
} else {
p.P(`func (this *`, ccTypeName, `) Equal(that interface{}) bool {`)
}
p.In()
p.generateMsgNullAndTypeCheck(ccTypeName, verbose)
oneofs := make(map[string]struct{})
for _, field := range message.Field {
oneof := field.OneofIndex != nil
if oneof {
fieldname := p.GetFieldName(message, field)
if _, ok := oneofs[fieldname]; ok {
continue
} else {
oneofs[fieldname] = struct{}{}
}
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, ` == 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()
if verbose {
p.P(`} else if err := this.`, fieldname, `.VerboseEqual(that1.`, fieldname, `); err != nil {`)
} else {
p.P(`} else if !this.`, fieldname, `.Equal(that1.`, fieldname, `) {`)
}
p.In()
if verbose {
p.P(`return err`)
} else {
p.P(`return false`)
}
p.Out()
p.P(`}`)
} else {
p.generateField(file, message, field, verbose)
}
}
if message.DescriptorProto.HasExtension() {
fieldname := "XXX_extensions"
if gogoproto.HasExtensionsMap(file.FileDescriptorProto, message.DescriptorProto) {
p.P(`for k, v := range this.`, fieldname, ` {`)
p.In()
p.P(`if v2, ok := that1.`, fieldname, `[k]; ok {`)
p.In()
p.P(`if !v.Equal(&v2) {`)
p.In()
if verbose {
p.P(`return `, p.fmtPkg.Use(), `.Errorf("`, fieldname, ` this[%v](%v) Not Equal that[%v](%v)", k, this.`, fieldname, `[k], k, that1.`, fieldname, `[k])`)
} else {
p.P(`return false`)
}
p.Out()
p.P(`}`)
p.Out()
p.P(`} else {`)
p.In()
if verbose {
p.P(`return `, p.fmtPkg.Use(), `.Errorf("`, fieldname, `[%v] Not In that", k)`)
} else {
p.P(`return false`)
}
p.Out()
p.P(`}`)
p.Out()
p.P(`}`)
p.P(`for k, _ := range that1.`, fieldname, ` {`)
p.In()
p.P(`if _, ok := this.`, fieldname, `[k]; !ok {`)
p.In()
if verbose {
p.P(`return `, p.fmtPkg.Use(), `.Errorf("`, fieldname, `[%v] Not In this", k)`)
} else {
p.P(`return false`)
}
p.Out()
p.P(`}`)
p.Out()
p.P(`}`)
} else {
//.........這裏部分代碼省略.........
示例12: generateMessage
func (p *plugin) generateMessage(file *generator.FileDescriptor, message *generator.Descriptor, verbose bool) {
proto3 := gogoproto.IsProto3(file.FileDescriptorProto)
ccTypeName := generator.CamelCaseSlice(message.TypeName())
if verbose {
p.P(`func (this *`, ccTypeName, `) VerboseEqual(that interface{}) error {`)
} else {
p.P(`func (this *`, ccTypeName, `) Equal(that interface{}) bool {`)
}
p.In()
p.P(`if that == nil {`)
p.In()
p.P(`if this == nil {`)
p.In()
if verbose {
p.P(`return nil`)
} else {
p.P(`return true`)
}
p.Out()
p.P(`}`)
if verbose {
p.P(`return `, p.fmtPkg.Use(), `.Errorf("that == nil && this != nil")`)
} else {
p.P(`return false`)
}
p.Out()
p.P(`}`)
p.P(``)
p.P(`that1, ok := that.(*`, ccTypeName, `)`)
p.P(`if !ok {`)
p.In()
if verbose {
p.P(`return `, p.fmtPkg.Use(), `.Errorf("that is not of type *`, ccTypeName, `")`)
} else {
p.P(`return false`)
}
p.Out()
p.P(`}`)
p.P(`if that1 == nil {`)
p.In()
p.P(`if this == nil {`)
p.In()
if verbose {
p.P(`return nil`)
} else {
p.P(`return true`)
}
p.Out()
p.P(`}`)
if verbose {
p.P(`return `, p.fmtPkg.Use(), `.Errorf("that is type *`, ccTypeName, ` but is nil && this != nil")`)
} else {
p.P(`return false`)
}
p.Out()
p.P(`} else if this == nil {`)
p.In()
if verbose {
p.P(`return `, p.fmtPkg.Use(), `.Errorf("that is type *`, ccTypeName, `but is not nil && this == nil")`)
} else {
p.P(`return false`)
}
p.Out()
p.P(`}`)
for _, field := range message.Field {
fieldname := p.GetFieldName(message, field)
repeated := field.IsRepeated()
ctype := gogoproto.IsCustomType(field)
nullable := gogoproto.IsNullable(field)
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, `) {`)
}
} 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 {
//.........這裏部分代碼省略.........