本文整理汇总了Golang中limbo/services/protobuf/protoc-gen-gogo/descriptor.FieldDescriptorProto.GetTypeName方法的典型用法代码示例。如果您正苦于以下问题:Golang FieldDescriptorProto.GetTypeName方法的具体用法?Golang FieldDescriptorProto.GetTypeName怎么用?Golang FieldDescriptorProto.GetTypeName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类limbo/services/protobuf/protoc-gen-gogo/descriptor.FieldDescriptorProto
的用法示例。
在下文中一共展示了FieldDescriptorProto.GetTypeName方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: generateSubMessageTest
func (g *validation) generateSubMessageTest(msg *generator.Descriptor, field *pb.FieldDescriptorProto, fieldName string) {
var casttyp = "value"
var typ = g.gen.TypeName(g.gen.ObjectNamed(field.GetTypeName()))
var zeroValuer = "value"
if gogoproto.IsCastType(field) {
if gogoproto.IsNullable(field) {
casttyp = "((*" + typ + ")(" + casttyp + "))"
} else {
casttyp = "((*" + typ + ")(&" + casttyp + "))"
zeroValuer = "((" + typ + ")(value))"
}
}
if g.gen.IsMap(field) {
// g.gen.GetMapKeyField(field*descriptor.FieldDescriptorProto, keyField*descriptor.FieldDescriptorProto)
} else if field.IsRepeated() {
g.P(`for _, value :=range `, fieldName, `{`)
if gogoproto.IsNullable(field) {
g.P(`if value != nil {`)
g.P(`if err := `, casttyp, `.Validate(); err != nil {`)
g.P(`return `, g.errorsPkg.Use(), `.Trace(err)`)
g.P(`}`)
g.P(`}`)
} else {
g.P(`if (`, typ, `{}) != `, zeroValuer, ` {`)
g.P(`if err := `, casttyp, `.Validate(); err != nil {`)
g.P(`return `, g.errorsPkg.Use(), `.Trace(err)`)
g.P(`}`)
g.P(`}`)
}
g.P(`}`)
} else {
if gogoproto.IsNullable(field) {
g.P(`{`)
g.P(`value := `, fieldName)
g.P(`if value != nil {`)
g.P(`if err := `, casttyp, `.Validate(); err != nil {`)
g.P(`return `, g.errorsPkg.Use(), `.Trace(err)`)
g.P(`}`)
g.P(`}`)
g.P(`}`)
} else {
g.P(`{`)
g.P(`value := `, fieldName)
g.P(`if (`, typ, `{}) != `, zeroValuer, ` {`)
g.P(`if err := `, casttyp, `.Validate(); err != nil {`)
g.P(`return `, g.errorsPkg.Use(), `.Trace(err)`)
g.P(`}`)
g.P(`}`)
g.P(`}`)
}
}
}
示例3: IsMap
func (g *Generator) IsMap(field *descriptor.FieldDescriptorProto) bool {
if !field.IsMessage() {
return false
}
byName := g.ObjectNamed(field.GetTypeName())
desc, ok := byName.(*Descriptor)
if byName == nil || !ok || !desc.GetOptions().GetMapEntry() {
return false
}
return true
}
示例4: getEnumVal
func (p *plugin) getEnumVal(field *descriptor.FieldDescriptorProto, goTyp string) string {
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), `)])`}, "")
return val
}
示例5: generateField
//.........这里部分代码省略.........
sum = append(sum, `len(v)+sov`+p.localName+`(uint64(len(v)))`)
case descriptor.FieldDescriptorProto_TYPE_SINT32,
descriptor.FieldDescriptorProto_TYPE_SINT64:
sum = append(sum, `soz`+p.localName+`(uint64(v))`)
case descriptor.FieldDescriptorProto_TYPE_MESSAGE:
if nullable {
p.P(`l = 0`)
p.P(`if v != nil {`)
p.In()
if valuegoTyp != valuegoAliasTyp {
p.P(`l = ((`, valuegoTyp, `)(v)).`, sizeName, `()`)
} else {
p.P(`l = v.`, sizeName, `()`)
}
p.Out()
p.P(`}`)
} else {
if valuegoTyp != valuegoAliasTyp {
p.P(`l = ((*`, valuegoTyp, `)(&v)).`, sizeName, `()`)
} else {
p.P(`l = v.`, sizeName, `()`)
}
}
sum = append(sum, `l+sov`+p.localName+`(uint64(l))`)
}
p.P(`mapEntrySize := `, strings.Join(sum, "+"))
p.P(`n+=mapEntrySize+`, fieldKeySize, `+sov`, p.localName, `(uint64(mapEntrySize))`)
p.Out()
p.P(`}`)
} else if repeated {
p.P(`for _, e := range m.`, fieldname, ` { `)
p.In()
if gogoproto.IsCastType(field) {
prototyp := p.TypeName(p.ObjectNamed(field.GetTypeName()))
if nullable {
p.P(`l=((*`, prototyp, `)(e)).`, sizeName, `()`)
} else {
p.P(`l=((*`, prototyp, `)(&e)).`, sizeName, `()`)
}
} else {
p.P(`l=e.Size()`)
}
p.P(`n+=`, strconv.Itoa(key), `+l+sov`, p.localName, `(uint64(l))`)
p.Out()
p.P(`}`)
} else {
if gogoproto.IsCastType(field) {
prototyp := p.TypeName(p.ObjectNamed(field.GetTypeName()))
if nullable {
p.P(`l=((*`, prototyp, `)(m.`, fieldname, `)).`, sizeName, `()`)
} else {
p.P(`l=((*`, prototyp, `)(&m.`, fieldname, `)).`, sizeName, `()`)
}
} 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()
示例6: fieldToSchema
func (g *jsonschema) fieldToSchema(field *pb.FieldDescriptorProto) (map[string]interface{}, string) {
if field.Options != nil {
v, _ := proto.GetExtension(field.Options, limbo.E_HideInSwagger)
hidePtr, _ := v.(*bool)
if hidePtr != nil && *hidePtr == true {
return nil, ""
}
}
var (
def map[string]interface{}
dep string
)
switch field.GetType() {
case pb.FieldDescriptorProto_TYPE_BOOL:
def = map[string]interface{}{
"type": "boolean",
}
case pb.FieldDescriptorProto_TYPE_FLOAT:
def = map[string]interface{}{
"type": "number",
"format": "float",
}
case pb.FieldDescriptorProto_TYPE_DOUBLE:
def = map[string]interface{}{
"type": "number",
"format": "double",
}
case pb.FieldDescriptorProto_TYPE_FIXED32,
pb.FieldDescriptorProto_TYPE_FIXED64,
pb.FieldDescriptorProto_TYPE_UINT32,
pb.FieldDescriptorProto_TYPE_UINT64:
def = map[string]interface{}{
"type": "integer",
}
case pb.FieldDescriptorProto_TYPE_INT32,
pb.FieldDescriptorProto_TYPE_SFIXED32,
pb.FieldDescriptorProto_TYPE_SINT32:
def = map[string]interface{}{
"type": "integer",
"format": "int32",
}
case pb.FieldDescriptorProto_TYPE_INT64,
pb.FieldDescriptorProto_TYPE_SFIXED64,
pb.FieldDescriptorProto_TYPE_SINT64:
def = map[string]interface{}{
"type": "integer",
"format": "int64",
}
case pb.FieldDescriptorProto_TYPE_STRING:
def = map[string]interface{}{
"type": "string",
}
if x, ok := limbo.GetFormat(field); ok {
def["format"] = x
}
if x, ok := limbo.GetPattern(field); ok {
def["pattern"] = x
}
if x, ok := limbo.GetMinLength(field); ok {
def["minLength"] = x
}
if x, ok := limbo.GetMaxLength(field); ok {
def["maxLength"] = x
}
case pb.FieldDescriptorProto_TYPE_BYTES:
def = map[string]interface{}{
"type": "string",
"format": "base64",
}
case pb.FieldDescriptorProto_TYPE_ENUM:
dep = strings.TrimPrefix(field.GetTypeName(), ".")
def = map[string]interface{}{
"$ref": dep,
}
case pb.FieldDescriptorProto_TYPE_MESSAGE:
dep = strings.TrimPrefix(field.GetTypeName(), ".")
def = map[string]interface{}{
"$ref": dep,
}
default:
panic("unsupported " + field.GetType().String())
}
if field.IsRepeated() {
def = map[string]interface{}{
"type": "array",
//.........这里部分代码省略.........
示例7: generateField
//.........这里部分代码省略.........
p.In()
p.P(`return 0, `, p.errorsPkg.Use(), `.New("proto: map has nil element")`)
p.Out()
p.P(`}`)
}
if valuegoTyp != valuegoAliasTyp {
if nullable {
// cast back to the type that has the generated methods on it
accessor = `((` + valuegoTyp + `)(` + accessor + `))`
} else {
accessor = `((*` + valuegoTyp + `)(&` + accessor + `))`
}
} else if !nullable {
accessor = `(&v)`
}
if protoSizer {
p.P(`msgSize := `, accessor, `.ProtoSize()`)
} else {
p.P(`msgSize := `, accessor, `.Size()`)
}
sum = append(sum, `msgSize + sov`+p.localName+`(uint64(msgSize))`)
}
p.P(`mapSize := `, strings.Join(sum, " + "))
p.callVarint("mapSize")
p.encodeKey(1, wireToType(keywire))
p.mapField(numGen, m.KeyField.GetType(), "k", protoSizer)
p.encodeKey(2, wireToType(valuewire))
p.mapField(numGen, m.ValueField.GetType(), accessor, protoSizer)
p.Out()
p.P(`}`)
} else if repeated {
casttyp := "msg"
if gogoproto.IsCastType(field) {
prototyp := p.TypeName(p.ObjectNamed(field.GetTypeName()))
if nullable {
casttyp = "((*" + prototyp + ")(" + casttyp + "))"
} else {
casttyp = "((*" + prototyp + ")(&" + casttyp + "))"
}
}
p.P(`for _, msg := range m.`, fieldname, ` {`)
p.In()
p.encodeKey(fieldNumber, wireType)
if protoSizer {
p.callVarint(casttyp + ".ProtoSize()")
} else {
p.callVarint(casttyp + ".Size()")
}
p.P(`n, err := `, casttyp, `.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 {
casttyp := `m.` + fieldname
if gogoproto.IsCastType(field) {
prototyp := p.TypeName(p.ObjectNamed(field.GetTypeName()))
if nullable {
casttyp = "((*" + prototyp + ")(" + casttyp + "))"
} else {
casttyp = "((*" + prototyp + ")(&" + casttyp + "))"
}
示例8: IsGroup
func (g *Generator) IsGroup(field *descriptor.FieldDescriptorProto) bool {
if d, ok := g.typeNameToObject[field.GetTypeName()].(*Descriptor); ok {
return d.IsGroup()
}
return false
}
示例9: field
//.........这里部分代码省略.........
} else {
p.P(`b := `, typ, `(v != 0)`)
p.P(`m.`, fieldname, ` = &b`)
}
case descriptor.FieldDescriptorProto_TYPE_STRING:
p.P(`var stringLen uint64`)
p.decodeVarint("stringLen", "uint64")
p.P(`intStringLen := int(stringLen)`)
p.P(`if intStringLen < 0 {`)
p.In()
p.P(`return ErrInvalidLength` + p.localName)
p.Out()
p.P(`}`)
p.P(`postIndex := iNdEx + intStringLen`)
p.P(`if postIndex > l {`)
p.In()
p.P(`return `, p.ioPkg.Use(), `.ErrUnexpectedEOF`)
p.Out()
p.P(`}`)
if oneof {
p.P(`m.`, fieldname, ` = &`, p.OneOfTypeName(msg, field), `{`, typ, `(data[iNdEx:postIndex])}`)
} else if repeated {
p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, `, typ, `(data[iNdEx:postIndex]))`)
} else if proto3 || !nullable {
p.P(`m.`, fieldname, ` = `, typ, `(data[iNdEx:postIndex])`)
} else {
p.P(`s := `, typ, `(data[iNdEx:postIndex])`)
p.P(`m.`, fieldname, ` = &s`)
}
p.P(`iNdEx = postIndex`)
case descriptor.FieldDescriptorProto_TYPE_GROUP:
panic(fmt.Errorf("unmarshaler does not support group %v", fieldname))
case descriptor.FieldDescriptorProto_TYPE_MESSAGE:
desc := p.ObjectNamed(field.GetTypeName())
msgname := p.TypeName(desc)
p.P(`var msglen int`)
p.decodeVarint("msglen", "int")
p.P(`if msglen < 0 {`)
p.In()
p.P(`return ErrInvalidLength` + p.localName)
p.Out()
p.P(`}`)
p.P(`postIndex := iNdEx + msglen`)
p.P(`if postIndex > l {`)
p.In()
p.P(`return `, p.ioPkg.Use(), `.ErrUnexpectedEOF`)
p.Out()
p.P(`}`)
if oneof {
casttyp := "v"
if gogoproto.IsCastType(field) {
prototyp := p.TypeName(p.ObjectNamed(field.GetTypeName()))
casttyp = "((*" + prototyp + ")(v))"
}
p.P(`v := &`, msgname, `{}`)
p.P(`if err := `, casttyp, `.Unmarshal(data[iNdEx:postIndex]); err != nil {`)
p.In()
p.P(`return err`)
p.Out()
p.P(`}`)
p.P(`m.`, fieldname, ` = &`, p.OneOfTypeName(msg, field), `{v}`)
} else if p.IsMap(field) {
m := p.GoMapType(nil, field)
keygoTyp, _ := p.GoType(nil, m.KeyField)
keygoAliasTyp, _ := p.GoType(nil, m.KeyAliasField)
示例10: mapField
func (p *unmarshal) mapField(varName string, field *descriptor.FieldDescriptorProto) {
switch field.GetType() {
case descriptor.FieldDescriptorProto_TYPE_DOUBLE:
p.P(`var `, varName, `temp uint64`)
p.decodeFixed64(varName+"temp", "uint64")
p.P(varName, ` := `, p.mathPkg.Use(), `.Float64frombits(`, varName, `temp)`)
case descriptor.FieldDescriptorProto_TYPE_FLOAT:
p.P(`var `, varName, `temp uint32`)
p.decodeFixed32(varName+"temp", "uint32")
p.P(varName, ` := `, p.mathPkg.Use(), `.Float32frombits(`, varName, `temp)`)
case descriptor.FieldDescriptorProto_TYPE_INT64:
p.P(`var `, varName, ` int64`)
p.decodeVarint(varName, "int64")
case descriptor.FieldDescriptorProto_TYPE_UINT64:
p.P(`var `, varName, ` uint64`)
p.decodeVarint(varName, "uint64")
case descriptor.FieldDescriptorProto_TYPE_INT32:
p.P(`var `, varName, ` int32`)
p.decodeVarint(varName, "int32")
case descriptor.FieldDescriptorProto_TYPE_FIXED64:
p.P(`var `, varName, ` uint64`)
p.decodeFixed64(varName, "uint64")
case descriptor.FieldDescriptorProto_TYPE_FIXED32:
p.P(`var `, varName, ` uint32`)
p.decodeFixed32(varName, "uint32")
case descriptor.FieldDescriptorProto_TYPE_BOOL:
p.P(`var `, varName, `temp int`)
p.decodeVarint(varName+"temp", "int")
p.P(varName, ` := bool(`, varName, `temp != 0)`)
case descriptor.FieldDescriptorProto_TYPE_STRING:
p.P(`var stringLen`, varName, ` uint64`)
p.decodeVarint("stringLen"+varName, "uint64")
p.P(`intStringLen`, varName, ` := int(stringLen`, varName, `)`)
p.P(`if intStringLen`, varName, ` < 0 {`)
p.In()
p.P(`return ErrInvalidLength` + p.localName)
p.Out()
p.P(`}`)
p.P(`postStringIndex`, varName, ` := iNdEx + intStringLen`, varName)
p.P(`if postStringIndex`, varName, ` > l {`)
p.In()
p.P(`return `, p.ioPkg.Use(), `.ErrUnexpectedEOF`)
p.Out()
p.P(`}`)
cast, _ := p.GoType(nil, field)
cast = strings.Replace(cast, "*", "", 1)
p.P(varName, ` := `, cast, `(data[iNdEx:postStringIndex`, varName, `])`)
p.P(`iNdEx = postStringIndex`, varName)
case descriptor.FieldDescriptorProto_TYPE_MESSAGE:
p.P(`var mapmsglen int`)
p.decodeVarint("mapmsglen", "int")
p.P(`if mapmsglen < 0 {`)
p.In()
p.P(`return ErrInvalidLength` + p.localName)
p.Out()
p.P(`}`)
p.P(`postmsgIndex := iNdEx + mapmsglen`)
p.P(`if mapmsglen < 0 {`)
p.In()
p.P(`return ErrInvalidLength` + p.localName)
p.Out()
p.P(`}`)
p.P(`if postmsgIndex > l {`)
p.In()
p.P(`return `, p.ioPkg.Use(), `.ErrUnexpectedEOF`)
p.Out()
p.P(`}`)
desc := p.ObjectNamed(field.GetTypeName())
msgname := p.TypeName(desc)
p.P(varName, ` := &`, msgname, `{}`)
p.P(`if err := `, varName, `.Unmarshal(data[iNdEx:postmsgIndex]); err != nil {`)
p.In()
p.P(`return err`)
p.Out()
p.P(`}`)
p.P(`iNdEx = postmsgIndex`)
case descriptor.FieldDescriptorProto_TYPE_BYTES:
p.P(`var mapbyteLen uint64`)
p.decodeVarint("mapbyteLen", "uint64")
p.P(`intMapbyteLen := int(mapbyteLen)`)
p.P(`if intMapbyteLen < 0 {`)
p.In()
p.P(`return ErrInvalidLength` + p.localName)
p.Out()
p.P(`}`)
p.P(`postbytesIndex := iNdEx + intMapbyteLen`)
p.P(`if postbytesIndex > l {`)
p.In()
p.P(`return `, p.ioPkg.Use(), `.ErrUnexpectedEOF`)
p.Out()
p.P(`}`)
p.P(varName, ` := make([]byte, mapbyteLen)`)
p.P(`copy(`, varName, `, data[iNdEx:postbytesIndex])`)
p.P(`iNdEx = postbytesIndex`)
case descriptor.FieldDescriptorProto_TYPE_UINT32:
p.P(`var `, varName, ` uint32`)
p.decodeVarint(varName, "uint32")
case descriptor.FieldDescriptorProto_TYPE_ENUM:
typName := p.TypeName(p.ObjectNamed(field.GetTypeName()))
p.P(`var `, varName, ` `, typName)
//.........这里部分代码省略.........