本文整理汇总了Golang中github.com/pixty/gogoprotobuf/protoc-gen-gogo/descriptor.FieldDescriptorProto.IsRepeated方法的典型用法代码示例。如果您正苦于以下问题:Golang FieldDescriptorProto.IsRepeated方法的具体用法?Golang FieldDescriptorProto.IsRepeated怎么用?Golang FieldDescriptorProto.IsRepeated使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/pixty/gogoprotobuf/protoc-gen-gogo/descriptor.FieldDescriptorProto
的用法示例。
在下文中一共展示了FieldDescriptorProto.IsRepeated方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: GenerateField
func (p *plugin) GenerateField(file *generator.FileDescriptor, message *generator.Descriptor, field *descriptor.FieldDescriptorProto) {
proto3 := gogoproto.IsProto3(file.FileDescriptorProto)
goTyp, _ := p.GoType(message, field)
fieldname := p.GetFieldName(message, field)
goTypName := generator.GoTypeToName(goTyp)
if generator.IsMap(file.FileDescriptorProto, field) {
mapmsg := generator.GetMap(file.FileDescriptorProto, field)
mapkey, mapvalue := mapmsg.GetMapFields()
keygoTyp, _ := p.GoType(nil, mapkey)
valuegoTyp, _ := p.GoType(nil, mapvalue)
keytypName := generator.GoTypeToName(keygoTyp)
valuetypName := generator.GoTypeToName(valuegoTyp)
mapvaluegoType := valuegoTyp
if !mapvalue.IsMessage() {
mapvaluegoType = strings.Replace(mapvaluegoType, "*", "", 1)
}
p.P(p.varGen.Next(), ` := r.Intn(10)`)
p.P(`this.`, fieldname, ` = make(map[`, strings.Replace(keygoTyp, "*", "", 1), `]`, mapvaluegoType, `)`)
p.P(`for i := 0; i < `, p.varGen.Current(), `; i++ {`)
p.In()
keyval := ""
if mapkey.IsString() {
keyval = fmt.Sprintf("randString%v(r)", p.localName)
} else {
keyval = value(keytypName, mapkey.GetType())
}
if mapvalue.IsMessage() || p.IsGroup(field) {
s := `this.` + fieldname + `[` + keyval + `]` + ` = `
goTypName := generator.GoTypeToName(valuegoTyp)
funcCall := getFuncCall(goTypName)
s += funcCall
p.P(s)
} else if mapvalue.IsEnum() {
s := `this.` + fieldname + `[` + keyval + `]` + ` = ` + p.getEnumVal(mapvalue, valuegoTyp)
p.P(s)
} else if mapvalue.IsBytes() {
count := p.varGen.Next()
p.P(count, ` := r.Intn(100)`)
p.P(p.varGen.Next(), ` := `, keyval)
p.P(`this.`, fieldname, `[`, p.varGen.Current(), `] = make(`, valuegoTyp, `, `, count, `)`)
p.P(`for i := 0; i < `, count, `; i++ {`)
p.In()
p.P(`this.`, fieldname, `[`, p.varGen.Current(), `][i] = byte(r.Intn(256))`)
p.Out()
p.P(`}`)
} else if mapvalue.IsString() {
s := `this.` + fieldname + `[` + keyval + `]` + ` = ` + fmt.Sprintf("randString%v(r)", p.localName)
p.P(s)
} else {
p.P(p.varGen.Next(), ` := `, keyval)
p.P(`this.`, fieldname, `[`, p.varGen.Current(), `] = `, value(valuetypName, mapvalue.GetType()))
if negative(mapvalue.GetType()) {
p.P(`if r.Intn(2) == 0 {`)
p.In()
p.P(`this.`, fieldname, `[`, p.varGen.Current(), `] *= -1`)
p.Out()
p.P(`}`)
}
}
p.Out()
p.P(`}`)
} else if field.IsMessage() || p.IsGroup(field) {
funcCall := getFuncCall(goTypName)
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() {
val := p.getEnumVal(field, goTyp)
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) || proto3 {
p.P(`this.`, fieldname, ` = `, val)
} else {
p.P(p.varGen.Next(), ` := `, val)
p.P(`this.`, fieldname, ` = &`, p.varGen.Current())
//.........这里部分代码省略.........
示例2: TurnOffNullableForNativeTypesOnly
func TurnOffNullableForNativeTypesOnly(field *descriptor.FieldDescriptorProto) {
if field.IsRepeated() || field.IsMessage() {
return
}
SetBoolFieldOption(gogoproto.E_Nullable, false)(field)
}
示例3: field
func (p *unmarshal) field(file *descriptor.FileDescriptorProto, msg *generator.Descriptor, field *descriptor.FieldDescriptorProto, fieldname string, proto3 bool) {
repeated := field.IsRepeated()
nullable := gogoproto.IsNullable(field)
typ := p.noStarOrSliceType(msg, field)
switch *field.Type {
case descriptor.FieldDescriptorProto_TYPE_DOUBLE:
if !p.unsafe || gogoproto.IsCastType(field) {
p.P(`var v uint64`)
p.decodeFixed64("v", "uint64")
if repeated {
p.P(`v2 := `, typ, "(", p.mathPkg.Use(), `.Float64frombits(v))`)
p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v2)`)
} else if proto3 || !nullable {
p.P(`m.`, fieldname, ` = `, typ, "(", p.mathPkg.Use(), `.Float64frombits(v))`)
} else {
p.P(`v2 := `, typ, "(", p.mathPkg.Use(), `.Float64frombits(v))`)
p.P(`m.`, fieldname, ` = &v2`)
}
} else {
if repeated {
p.P(`var v float64`)
p.unsafeFixed64("v", "float64")
p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`)
} else if proto3 || !nullable {
p.unsafeFixed64(`m.`+fieldname, "float64")
} else {
p.P(`var v float64`)
p.unsafeFixed64("v", "float64")
p.P(`m.`, fieldname, ` = &v`)
}
}
case descriptor.FieldDescriptorProto_TYPE_FLOAT:
if !p.unsafe || gogoproto.IsCastType(field) {
p.P(`var v uint32`)
p.decodeFixed32("v", "uint32")
if repeated {
p.P(`v2 := `, typ, "(", p.mathPkg.Use(), `.Float32frombits(v))`)
p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v2)`)
} else if proto3 || !nullable {
p.P(`m.`, fieldname, ` = `, typ, "(", p.mathPkg.Use(), `.Float32frombits(v))`)
} else {
p.P(`v2 := `, typ, "(", p.mathPkg.Use(), `.Float32frombits(v))`)
p.P(`m.`, fieldname, ` = &v2`)
}
} else {
if repeated {
p.P(`var v float32`)
p.unsafeFixed32("v", "float32")
p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`)
} else if proto3 || !nullable {
p.unsafeFixed32("m."+fieldname, "float32")
} else {
p.P(`var v float32`)
p.unsafeFixed32("v", "float32")
p.P(`m.`, fieldname, ` = &v`)
}
}
case descriptor.FieldDescriptorProto_TYPE_INT64:
if repeated {
p.P(`var v `, typ)
p.decodeVarint("v", typ)
p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`)
} else if proto3 || !nullable {
p.decodeVarint("m."+fieldname, typ)
} else {
p.P(`var v `, typ)
p.decodeVarint("v", typ)
p.P(`m.`, fieldname, ` = &v`)
}
case descriptor.FieldDescriptorProto_TYPE_UINT64:
if repeated {
p.P(`var v `, typ)
p.decodeVarint("v", typ)
p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`)
} else if proto3 || !nullable {
p.decodeVarint("m."+fieldname, typ)
} else {
p.P(`var v `, typ)
p.decodeVarint("v", typ)
p.P(`m.`, fieldname, ` = &v`)
}
case descriptor.FieldDescriptorProto_TYPE_INT32:
if repeated {
p.P(`var v `, typ)
p.decodeVarint("v", typ)
p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`)
} else if proto3 || !nullable {
p.decodeVarint("m."+fieldname, typ)
} else {
p.P(`var v `, typ)
p.decodeVarint("v", typ)
p.P(`m.`, fieldname, ` = &v`)
}
case descriptor.FieldDescriptorProto_TYPE_FIXED64:
if !p.unsafe || gogoproto.IsCastType(field) {
if repeated {
p.P(`var v `, typ)
p.decodeFixed64("v", typ)
p.P(`m.`, fieldname, ` = append(m.`, fieldname, `, v)`)
} else if proto3 || !nullable {
//.........这里部分代码省略.........
示例4: TurnOffNullable
func TurnOffNullable(field *descriptor.FieldDescriptorProto) {
if field.IsRepeated() && !field.IsMessage() {
return
}
SetBoolFieldOption(gogoproto.E_Nullable, false)(field)
}