Beispiel #1
0
func (p *plugin) GenerateField(message *generator.Descriptor, field *descriptor.FieldDescriptorProto) {
	goTyp, _ := p.GoType(message, field)
	fieldname := p.GetFieldName(message, field)
	goTypName := generator.GoTypeToName(goTyp)
	ctype := ""
	if gogoproto.IsCustomType(field) {
		_, typ, err := generator.GetCustomType(field)
		if err != nil {
			panic(err)
		}
		ctype = typ
	}
	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()
			p.P(p.varGen.Next(), `:= `, funcCall)
			p.P(`this.`, generator.SizerName(fieldname), ` += 1`)
			p.P(`this.`, fieldname, `[i] = `, p.varGen.Current())
			p.Out()
			p.P(`}`)
		} else {
			p.P(p.varGen.Next(), `:= `, funcCall)
			p.P(`this.`, generator.SetterName(fieldname), ` = true`)
			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.`, generator.SizerName(fieldname), ` += 1`)
				p.P(`this.`, fieldname, `[i] = `, val)
				p.Out()
				p.P(`}`)
			} else {
				p.P(`this.`, generator.SetterName(fieldname), ` = true`)
				p.P(`this.`, fieldname, ` = `, val)
			}
		} else if field.IsString() {
			val := fmt.Sprintf("randString%v(r)", p.localName)
			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.`, generator.SizerName(fieldname), ` += 1`)
				p.P(`this.`, fieldname, `[i] = `, ctype, `(`, val, `)`)
				p.Out()
				p.P(`}`)
			} else {
				p.P(`this.`, generator.SetterName(fieldname), ` = true`)
				p.P(`this.`, fieldname, ` = `, ctype, `(`, val, `)`)
			}
		} 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.`, generator.SizerName(fieldname), ` += 1`)
				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(`}`)
			} else {
				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(`this.`, generator.SetterName(fieldname), ` = true`)
				p.P(`this.`, fieldname, `[i] = byte(r.Intn(256))`)
				p.Out()
				p.P(`}`)
			}
		} else {
			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(`this.`, generator.SizerName(fieldname), ` += 1`)
				p.P(`this.`, fieldname, `[i] = `, ctype, `(`, value(field), `)`)
				if negative(field) {
					p.P(`if r.Intn(2) == 0 {`)
					p.In()
					p.P(`this.`, fieldname, `[i] *= `, ctype, `(-1)`)
					p.Out()
					p.P(`}`)
				}
				p.Out()
				p.P(`}`)
			} else {
				p.P(`this.`, generator.SetterName(fieldname), ` = true`)
				p.P(`this.`, fieldname, ` = `, ctype, `(`, value(field), `)`)
				if negative(field) {
					p.P(`if r.Intn(2) == 0 {`)
					p.In()
					p.P(`this.`, fieldname, ` *= `, ctype, `(-1)`)
					p.Out()
					p.P(`}`)
				}
			}
		}
	}
}
Beispiel #2
0
func (p *testAPI) Generate(imports generator.PluginImports, file *generator.FileDescriptor) bool {
	used := false
	testingPkg := imports.NewImport("testing")
	randPkg := imports.NewImport("math/rand")
	timePkg := imports.NewImport("time")
	for _, message := range file.Messages() {
		ccTypeName := generator.CamelCaseSlice(message.TypeName())
		if !gogoproto.HasTestGen(file.FileDescriptorProto, message.DescriptorProto) {
			continue
		}
		used = true
		p.P(`func Test`, ccTypeName, `API(t *`, testingPkg.Use(), `.T) {`)
		p.In()
		p.P(`popr := `, randPkg.Use(), `.New(`, randPkg.Use(), `.NewSource(`, timePkg.Use(),
			`.Now().UnixNano()))`)
		p.P(`p := NewPopulated`, ccTypeName, `(popr, false)`)
		p.P(`msg := &`, ccTypeName, `{}`)
		p.P(`if !apiEmpty`, ccTypeName, `(msg, t) {`)
		p.In()
		p.P(`t.Fatalf("`, ccTypeName, ` should be empty")`)
		p.Out()
		p.P(`}`)
		p.P(`apiCopy`, ccTypeName, `(msg, p, t)`)
		if gogoproto.HasVerboseEqual(file.FileDescriptorProto, message.DescriptorProto) {
			p.P(`if err := p.VerboseEqual(msg); err != nil {`)
			p.In()
			p.P(`t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err)`)
			p.Out()
			p.P(`}`)
		}
		p.P(`if apiEmpty`, ccTypeName, `(p, t) != apiEmpty`, ccTypeName, `(msg, t) {`)
		p.In()
		p.P(`t.Fatalf("`, ccTypeName, ` should not be empty")`)
		p.Out()
		p.P(`}`)
		p.P(`if !p.Equal(msg) {`)
		p.In()
		p.P(`t.Fatalf("%#v !Proto %#v", msg, p)`)
		p.Out()
		p.P(`}`)
		p.P(`msg.Clear()`)
		p.P(`if !apiEmpty`, ccTypeName, `(msg, t) {`)
		p.In()
		p.P(`t.Fatalf("`, ccTypeName, ` should be empty")`)
		p.Out()
		p.P(`}`)
		p.Out()
		p.P(`}`)
		p.P()

		p.P(`func apiCopy`, ccTypeName, `(dst *`, ccTypeName, `, src *`, ccTypeName, `, t *`,
			testingPkg.Use(), `.T) {`)
		p.In()
		p.P(`if dst == nil || src == nil {`)
		p.In()
		p.P(`t.Fatalf("Cannot copy to(%v) or from(%v) nil message", dst, src)`)
		p.Out()
		p.P(`}`)
		for _, field := range message.Field {
			if gogoproto.IsEmbed(field) {
				p.P(`t.Skip("Cannot copy embed field")`)
				break
			}
			if gogoproto.IsCustomType(field) {
				p.P(`t.Skip("Cannot copy costum field")`)
				break
			}
			fieldName := generator.CamelCase(p.GetFieldName(message, field))
			fieldType, _ := p.GoType(message, field)
			fieldTypeName := generator.GoTypeToName(fieldType)
			if generator.IsRepeated(field) {
				p.P(`for i := 0; i < src.`, fieldName, `Size(); i++ {`)
				p.In()
				if generator.IsMessageType(field) {
					p.P(`src`, fieldName, `, _ := src.Get`, fieldName, `(i)`)
					p.P(`dst`, fieldName, `, _ := dst.Add`, fieldName, `()`)
					p.P(`apiCopy`, fieldTypeName, `(dst`, fieldName, `, src`, fieldName, `, t)`)
				} else {
					p.P(`value, _ := src.Get`, fieldName, `(i)`)
					p.P(`dst.Add`, fieldName, `(value)`)
				}
				p.Out()
				p.P(`}`)
			} else {
				p.P(`if src.Has`, fieldName, `() {`)
				p.In()
				if generator.IsMessageType(field) {
					p.P(`src`, fieldName, ` := src.Get`, fieldName, `()`)
					p.P(`dst`, fieldName, `, _ := dst.Mutate`, fieldName, `()`)
					p.P(`apiCopy`, fieldTypeName, `(dst`, fieldName, `, src`, fieldName, `, t)`)
				} else {
					p.P(`dst.Set`, fieldName, `(src.Get`, fieldName, `())`)
				}
				p.Out()
				p.P(`}`)
			}
		}
		p.P(`src.XXX_unrecognized = dst.XXX_unrecognized`)
		if len(message.ExtensionRange) > 0 {
			p.P(`src.XXX_extensions = dst.XXX_extensions`)
		}
		p.Out()
		p.P(`}`)
		p.P()

		p.P(`func apiEmpty`, ccTypeName, `(msg *`, ccTypeName, `, t *`, testingPkg.Use(), `.T) bool {`)
		p.In()
		p.P(`if msg == nil {`)
		p.In()
		p.P(`return true`)
		p.Out()
		p.P(`}`)
		for _, field := range message.Field {
			if gogoproto.IsEmbed(field) {
				p.P(`t.Skip("Cannot check embed field")`)
				break
			}
			if gogoproto.IsCustomType(field) {
				p.P(`t.Skip("Cannot check costum field")`)
				break
			}
			fieldName := generator.CamelCase(p.GetFieldName(message, field))
			if generator.IsRepeated(field) {
				p.P(`if msg.`, fieldName, `Size() != 0 {`)
			} else {
				p.P(`if msg.Has`, fieldName, `() {`)
			}
			p.In()
			p.P(`return false`)
			p.Out()
			p.P(`}`)
		}
		p.P(`return true`)
		p.Out()
		p.P(`}`)
		p.P()
	}
	return used
}
Beispiel #3
0
func (p *union) Generate(file *generator.FileDescriptor) {
	p.PluginImports = generator.NewPluginImports(p.Generator)

	for _, message := range file.Messages() {
		if !gogoproto.IsUnion(file.FileDescriptorProto, message.DescriptorProto) {
			continue
		}
		if message.DescriptorProto.HasExtension() {
			panic("onlyone does not currently support extensions")
		}

		ccTypeName := generator.CamelCaseSlice(message.TypeName())
		p.P(`func (this *`, ccTypeName, `) GetValue() interface{} {`)
		p.In()
		for _, field := range message.Field {
			fieldname := p.GetFieldName(message, field)
			if fieldname == "Value" {
				panic("cannot have a onlyone message " + ccTypeName + " with a field named Value")
			}
			p.P(`if this.`, generator.SetterName(fieldname), ` == true {`)
			p.In()
			p.P(`return this.`, fieldname)
			p.Out()
			p.P(`}`)
		}
		p.P(`return nil`)
		p.Out()
		p.P(`}`)
		p.P(``)
		p.P(`func (this *`, ccTypeName, `) SetValue(value interface{}) bool {`)
		p.In()
		p.P(`switch vt := value.(type) {`)
		p.In()
		for _, field := range message.Field {
			fieldname := p.GetFieldName(message, field)
			goTyp, _ := p.GoType(message, field)
			p.P(`case `, goTyp, `:`)
			p.In()
			p.P(`this.`, generator.SetterName(fieldname), ` = true`)
			p.P(`this.`, fieldname, ` = vt`)
			p.Out()
		}
		p.P(`default:`)
		p.In()
		for _, field := range message.Field {
			fieldname := p.GetFieldName(message, field)
			if field.IsMessage() {
				goTyp, _ := p.GoType(message, field)
				obj := p.ObjectNamed(field.GetTypeName()).(*generator.Descriptor)

				if gogoproto.IsUnion(obj.File(), obj.DescriptorProto) {
					p.P(`this.`, fieldname, ` = new(`, generator.GoTypeToName(goTyp), `)`)
					p.P(`if set := this.`, fieldname, `.SetValue(value); set {`)
					p.In()
					p.P(`return true`)
					p.Out()
					p.P(`}`)
					p.P(`this.`, fieldname, ` = nil`)
				}
			}
		}
		p.P(`return false`)
		p.Out()
		p.P(`}`)
		p.P(`return true`)
		p.Out()
		p.P(`}`)
	}
}