本文整理汇总了Golang中code/google/com/p/gogoprotobuf/protoc-gen-gogo/descriptor.FieldDescriptorProto类的典型用法代码示例。如果您正苦于以下问题:Golang FieldDescriptorProto类的具体用法?Golang FieldDescriptorProto怎么用?Golang FieldDescriptorProto使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FieldDescriptorProto类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: hasLoop
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
}
开发者ID:raceli,项目名称:gogoprotobuf,代码行数:25,代码来源:populate.go
示例2: IsGroup
func (g *Generator) IsGroup(field *descriptor.FieldDescriptorProto) bool {
if d, ok := g.typeNameToObject[field.GetTypeName()].(*Descriptor); ok {
return d.IsGroup()
}
return false
}
开发者ID:raceli,项目名称:gogoprotobuf,代码行数:6,代码来源:helper.go
示例3: new
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
}
开发者ID:raceli,项目名称:gogoprotobuf,代码行数:58,代码来源:new.go
示例4: field
func (p *unmarshal) field(field *descriptor.FieldDescriptorProto, fieldname string) {
repeated := field.IsRepeated()
nullable := gogoproto.IsNullable(field)
switch *field.Type {
case descriptor.FieldDescriptorProto_TYPE_DOUBLE:
if !p.unsafe {
if repeated {
p.P(`var v uint64`)
p.decodeFixed64("v", "uint64")
p.P(`v2 := `, p.mathPkg.Use(), `.Float64frombits(v)`)
p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v2)`)
} else if nullable {
p.P(`var v uint64`)
p.decodeFixed64("v", "uint64")
p.P(`v2 := `, p.mathPkg.Use(), `.Float64frombits(v)`)
p.P(`m.`, fieldname, ` = &v2`)
} else {
p.P(`var v uint64`)
p.decodeFixed64("v", "uint64")
p.P(`m.`, fieldname, ` = `, p.mathPkg.Use(), `.Float64frombits(v)`)
}
} else {
if repeated {
p.P(`var v float64`)
p.unsafeFixed64("v", "float64")
p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`)
} else if nullable {
p.P(`var v float64`)
p.unsafeFixed64("v", "float64")
p.P(`m.`, fieldname, ` = &v`)
} else {
p.unsafeFixed64(`m.`+fieldname, "float64")
}
}
case descriptor.FieldDescriptorProto_TYPE_FLOAT:
if !p.unsafe {
if repeated {
p.P(`var v uint32`)
p.decodeFixed32("v", "uint32")
p.P(`v2 := `, p.mathPkg.Use(), `.Float32frombits(v)`)
p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v2)`)
} else if nullable {
p.P(`var v uint32`)
p.decodeFixed32("v", "uint32")
p.P(`v2 := `, p.mathPkg.Use(), `.Float32frombits(v)`)
p.P(`m.`, fieldname, ` = &v2`)
} else {
p.P(`var v uint32`)
p.decodeFixed32("v", "uint32")
p.P(`m.`, fieldname, ` = `, p.mathPkg.Use(), `.Float32frombits(v)`)
}
} else {
if repeated {
p.P(`var v float32`)
p.unsafeFixed32("v", "float32")
p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`)
} else if nullable {
p.P(`var v float32`)
p.unsafeFixed32("v", "float32")
p.P(`m.`, fieldname, ` = &v`)
} else {
p.unsafeFixed32("m."+fieldname, "float32")
}
}
case descriptor.FieldDescriptorProto_TYPE_INT64:
if repeated {
p.P(`var v int64`)
p.decodeVarint("v", "int64")
p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`)
} else if nullable {
p.P(`var v int64`)
p.decodeVarint("v", "int64")
p.P(`m.`, fieldname, ` = &v`)
} else {
p.decodeVarint("m."+fieldname, "int64")
}
case descriptor.FieldDescriptorProto_TYPE_UINT64:
if repeated {
p.P(`var v uint64`)
p.decodeVarint("v", "uint64")
p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`)
} else if nullable {
p.P(`var v uint64`)
p.decodeVarint("v", "uint64")
p.P(`m.`, fieldname, ` = &v`)
} else {
p.decodeVarint("m."+fieldname, "uint64")
}
case descriptor.FieldDescriptorProto_TYPE_INT32:
if repeated {
p.P(`var v int32`)
p.decodeVarint("v", "int32")
p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`)
} else if nullable {
p.P(`var v int32`)
p.decodeVarint("v", "int32")
p.P(`m.`, fieldname, ` = &v`)
} else {
p.decodeVarint("m."+fieldname, "int32")
}
//.........这里部分代码省略.........
开发者ID:Huangyan9188,项目名称:gogoprotobuf,代码行数:101,代码来源:unmarshal.go
示例5: GenerateField
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(`}`)
//.........这里部分代码省略.........
开发者ID:raceli,项目名称:gogoprotobuf,代码行数:101,代码来源:populate.go
注:本文中的code/google/com/p/gogoprotobuf/protoc-gen-gogo/descriptor.FieldDescriptorProto类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论