本文整理汇总了Golang中code/google/com/p/gogoprotobuf/protoc-gen-gogo/descriptor.FieldDescriptorProto.IsRepeated方法的典型用法代码示例。如果您正苦于以下问题:Golang FieldDescriptorProto.IsRepeated方法的具体用法?Golang FieldDescriptorProto.IsRepeated怎么用?Golang FieldDescriptorProto.IsRepeated使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类code/google/com/p/gogoprotobuf/protoc-gen-gogo/descriptor.FieldDescriptorProto
的用法示例。
在下文中一共展示了FieldDescriptorProto.IsRepeated方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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")
}
//.........这里部分代码省略.........
示例2: 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(`}`)
//.........这里部分代码省略.........