本文整理汇总了Golang中limbo/services/protobuf/protoc-gen-gogo/descriptor.FieldDescriptorProto类的典型用法代码示例。如果您正苦于以下问题:Golang FieldDescriptorProto类的具体用法?Golang FieldDescriptorProto怎么用?Golang FieldDescriptorProto使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FieldDescriptorProto类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: generateRequiredTest
func (g *validation) generateRequiredTest(msg *generator.Descriptor, field *pb.FieldDescriptorProto, fieldName string) {
if field.IsRepeated() {
g.P(`if `, fieldName, ` == nil || len(`, fieldName, `) == 0 {`)
g.P(`return `, g.errorsPkg.Use(), `.NotValidf("`, field.Name, ` is required")`)
g.P(`}`)
return
}
switch field.GetType() {
case pb.FieldDescriptorProto_TYPE_BYTES:
g.P(`if `, fieldName, ` == nil || len(`, fieldName, `) == 0 {`)
g.P(`return `, g.errorsPkg.Use(), `.NotValidf("`, field.Name, ` is required")`)
g.P(`}`)
case pb.FieldDescriptorProto_TYPE_STRING:
g.P(`if `, fieldName, ` == "" {`)
g.P(`return `, g.errorsPkg.Use(), `.NotValidf("`, field.Name, ` is required")`)
g.P(`}`)
case pb.FieldDescriptorProto_TYPE_MESSAGE:
if gogoproto.IsNullable(field) {
g.P(`if `, fieldName, ` == nil {`)
g.P(`return `, g.errorsPkg.Use(), `.NotValidf("`, field.Name, ` is required")`)
g.P(`}`)
}
}
}
示例2: 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
}
示例3: generateTests
func (g *validation) generateTests(msg *generator.Descriptor, field *pb.FieldDescriptorProto, fieldName string, idx int, patterns map[string]string) {
if limbo.IsRequiredProperty(field) {
g.generateRequiredTest(msg, field, fieldName)
}
if n, ok := limbo.GetMinItems(field); ok {
g.generateMinItemsTest(msg, field, fieldName, int(n))
}
if n, ok := limbo.GetMaxItems(field); ok {
g.generateMaxItemsTest(msg, field, fieldName, int(n))
}
if pattern, ok := limbo.GetPattern(field); ok {
patternVar := fmt.Sprintf("valPattern_%s_%d", msg.GetName(), idx)
patterns[patternVar] = pattern
g.generatePatternTest(msg, field, fieldName, pattern, patternVar)
}
if n, ok := limbo.GetMinLength(field); ok {
g.generateMinLengthTest(msg, field, fieldName, int(n))
}
if n, ok := limbo.GetMaxLength(field); ok {
g.generateMaxLengthTest(msg, field, fieldName, int(n))
}
if field.GetType() == pb.FieldDescriptorProto_TYPE_MESSAGE {
g.generateSubMessageTest(msg, field, fieldName)
}
}
示例4: getJSONName
func getJSONName(field *pb.FieldDescriptorProto) string {
name := field.GetJsonName()
if name == "" {
name = field.GetName()
}
return name
}
示例5: IsRequiredProperty
func IsRequiredProperty(field *pb.FieldDescriptorProto) bool {
if field.Options != nil && proto.HasExtension(field.Options, E_Required) {
return proto.GetBoolExtension(field.Options, E_Required, false)
}
if field.IsRequired() {
return true
}
return false
}
示例6: TurnOffNullableForNativeTypesWithoutDefaultsOnly
func TurnOffNullableForNativeTypesWithoutDefaultsOnly(field *descriptor.FieldDescriptorProto) {
if field.IsRepeated() || field.IsMessage() {
return
}
if field.DefaultValue != nil {
return
}
SetBoolFieldOption(gogoproto.E_Nullable, false)(field)
}
示例7: 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(`}`)
}
}
}
示例8: NeedsNilCheck
func NeedsNilCheck(proto3 bool, field *google_protobuf.FieldDescriptorProto) bool {
nullable := IsNullable(field)
if field.IsMessage() || IsCustomType(field) {
return nullable
}
if proto3 {
return false
}
return nullable || *field.Type == google_protobuf.FieldDescriptorProto_TYPE_BYTES
}
示例9: generateMaxItemsTest
func (g *validation) generateMaxItemsTest(msg *generator.Descriptor, field *pb.FieldDescriptorProto, fieldName string, maxItems int) {
if !field.IsRepeated() {
g.gen.Fail("limbo.maxItems can only be used on repeated fields.")
}
g.P(`if len(`, fieldName, `) < `, maxItems, ` {`)
g.P(`return `, g.errorsPkg.Use(), `.NotValidf("number of items in `, field.Name, ` (%q) is to small (maximum: `, maxItems, `)", len(`, fieldName, `))`)
g.P(`}`)
}
示例10: 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
}
示例11: 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
}
示例12: GoMapValueTypes
// GoMapValueTypes returns the map value Go type and the alias map value Go type (for casting), taking into
// account whether the map is nullable or the value is a message.
func GoMapValueTypes(mapField, valueField *descriptor.FieldDescriptorProto, goValueType, goValueAliasType string) (nullable bool, outGoType string, outGoAliasType string) {
nullable = gogoproto.IsNullable(mapField) && valueField.IsMessage()
if nullable {
// ensure the non-aliased Go value type is a pointer for consistency
if strings.HasPrefix(goValueType, "*") {
outGoType = goValueType
} else {
outGoType = "*" + goValueType
}
outGoAliasType = goValueAliasType
} else {
outGoType = strings.Replace(goValueType, "*", "", 1)
outGoAliasType = strings.Replace(goValueAliasType, "*", "", 1)
}
return
}
示例13: generateMaxLengthTest
func (g *validation) generateMaxLengthTest(msg *generator.Descriptor, field *pb.FieldDescriptorProto, fieldName string, maxLength int) {
if field.GetType() != pb.FieldDescriptorProto_TYPE_STRING {
g.gen.Fail("limbo.maxLength can only be used on string fields.")
}
if field.IsRepeated() {
g.P(`for idx, value :=range `, fieldName, `{`)
g.P(`if len(value) > `, maxLength, ` {`)
g.P(`return `, g.errorsPkg.Use(), `.NotValidf("`, field.Name, `[%d] (%q) is to long (maximum length: `, maxLength, `)", idx, value)`)
g.P(`}`)
g.P(`}`)
return
}
g.P(`if len(`, fieldName, `) > `, maxLength, ` {`)
g.P(`return `, g.errorsPkg.Use(), `.NotValidf("`, field.Name, ` (%q) is to long (maximum length: `, maxLength, `)", `, fieldName, `)`)
g.P(`}`)
}
示例14: generatePatternTest
func (g *validation) generatePatternTest(msg *generator.Descriptor, field *pb.FieldDescriptorProto, fieldName string, pattern, patternVar string) {
if field.GetType() != pb.FieldDescriptorProto_TYPE_STRING {
g.gen.Fail("limbo.pattern can only be used on string fields.")
}
if field.IsRepeated() {
g.P(`for idx, value :=range `, fieldName, `{`)
g.P(`if !`, patternVar, `.MatchString(value) {`)
g.P(`return `, g.errorsPkg.Use(), `.NotValidf("`, field.Name, `[%d] (%q) does not match pattern %s", idx, value, `, strconv.Quote(pattern), `)`)
g.P(`}`)
g.P(`}`)
return
}
g.P(`if !`, patternVar, `.MatchString(`, fieldName, `) {`)
g.P(`return `, g.errorsPkg.Use(), `.NotValidf("`, field.Name, ` (%q) does not match pattern %s", `, fieldName, `, `, strconv.Quote(pattern), `)`)
g.P(`}`)
}
示例15: GetMapKeyField
func (g *Generator) GetMapKeyField(field, keyField *descriptor.FieldDescriptorProto) *descriptor.FieldDescriptorProto {
if !gogoproto.IsCastKey(field) {
return keyField
}
keyField = proto.Clone(keyField).(*descriptor.FieldDescriptorProto)
if keyField.Options == nil {
keyField.Options = &descriptor.FieldOptions{}
}
keyType := gogoproto.GetCastKey(field)
if err := proto.SetExtension(keyField.Options, gogoproto.E_Casttype, &keyType); err != nil {
g.Fail(err.Error())
}
return keyField
}