コード例 #1
0
ファイル: populate.go プロジェクト: dropbox/goprotoc
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(`}`)
				}
			}
		}
	}
}
コード例 #2
0
ファイル: stringer.go プロジェクト: dropbox/goprotoc
func (p *stringer) Generate(file *generator.FileDescriptor) {
	p.PluginImports = generator.NewPluginImports(p.Generator)
	p.atleastOne = false

	p.localName = generator.FileName(file)

	stringsPkg := p.NewImport("strings")
	for _, message := range file.Messages() {
		if !gogoproto.IsStringer(file.FileDescriptorProto, message.DescriptorProto) {
			continue
		}
		p.atleastOne = true
		ccTypeName := generator.CamelCaseSlice(message.TypeName())
		p.P(`func (this *`, ccTypeName, `) String() string {`)
		p.In()
		p.P(`if this == nil {`)
		p.In()
		p.P(`return "nil"`)
		p.Out()
		p.P(`}`)
		p.P("s := ", stringsPkg.Use(), ".Join([]string{`&", ccTypeName, "{`,")
		for _, field := range message.Field {
			fieldname := p.GetFieldName(message, field)
			if field.IsMessage() || p.IsGroup(field) {
				desc := p.ObjectNamed(field.GetTypeName())
				msgname := p.TypeName(desc)
				msgnames := strings.Split(msgname, ".")
				typeName := msgnames[len(msgnames)-1]
				if field.IsRepeated() {
					p.P("`", fieldname, ":`", ` + `, stringsPkg.Use(), `.Replace(`, p.Pkg["fmt"],
						`.Sprintf("%v", this.`, fieldname, `[:this.`, generator.SizerName(fieldname),
						`]), "`, typeName, `","`, msgname, `"`, ", 1) + `,", "`,")
				} else {
					fieldValue := "this.Get" + generator.CamelCase(fieldname) + "()"
					if gogoproto.IsCustomType(field) || gogoproto.IsEmbed(field) {
						fieldValue = "this." + fieldname
					}
					p.P("`", fieldname, ":`", ` + `, stringsPkg.Use(), `.Replace(`, p.Pkg["fmt"],
						`.Sprintf("%v", `, fieldValue, `), "`, typeName, `","`, msgname, `"`,
						", 1) + `,", "`,")
				}
			} else if field.IsRepeated() {
				p.P("`", fieldname, ":`", ` + `, p.Pkg["fmt"], `.Sprintf("%v", this.`, fieldname,
					"[:this.", generator.SizerName(fieldname), "]) + `,", "`,")
			} else {
				fieldValue := "this.Get" + generator.CamelCase(fieldname) + "()"
				if gogoproto.IsCustomType(field) || gogoproto.IsEmbed(field) {
					fieldValue = "this." + fieldname
				}
				p.P("`", fieldname, ":`", ` + `, p.Pkg["fmt"], `.Sprintf("%v", `, fieldValue, ") + `,", "`,")
			}
		}
		if message.DescriptorProto.HasExtension() {
			if gogoproto.HasExtensionsMap(file.FileDescriptorProto, message.DescriptorProto) {
				p.P("`XXX_extensions:` + proto.StringFromExtensionsMap(this.XXX_extensions) + `,`,")
			} else {
				p.P("`XXX_extensions:` + proto.StringFromExtensionsBytes(this.XXX_extensions) + `,`,")
			}
		}
		p.P("`XXX_unrecognized:` + ", p.Pkg["fmt"], `.Sprintf("%v", this.XXX_unrecognized) + `, "`,`,")
		p.P("`}`,")
		p.P(`}`, `,""`, ")")
		p.P(`return s`)
		p.Out()
		p.P(`}`)
	}

	if !p.atleastOne {
		return
	}

}
コード例 #3
0
ファイル: equal.go プロジェクト: dropbox/goprotoc
func (p *plugin) generateMessage(message *generator.Descriptor, verbose bool, hasExtensionsMap bool) {
	ccTypeName := generator.CamelCaseSlice(message.TypeName())
	if verbose {
		p.P(`func (this *`, ccTypeName, `) VerboseEqual(that interface{}) error {`)
	} else {
		p.P(`func (this *`, ccTypeName, `) Equal(that interface{}) bool {`)
	}
	p.In()
	p.P(`if that == nil {`)
	p.In()
	p.P(`if this == nil {`)
	p.In()
	if verbose {
		p.P(`return nil`)
	} else {
		p.P(`return true`)
	}
	p.Out()
	p.P(`}`)
	if verbose {
		p.P(`return `, p.Pkg["fmt"], `.Errorf("that == nil && this != nil")`)
	} else {
		p.P(`return false`)
	}
	p.Out()
	p.P(`}`)
	p.P(``)
	p.P(`that1, ok := that.(*`, ccTypeName, `)`)
	p.P(`if !ok {`)
	p.In()
	if verbose {
		p.P(`return `, p.Pkg["fmt"], `.Errorf("that is not of type *`, ccTypeName, `")`)
	} else {
		p.P(`return false`)
	}
	p.Out()
	p.P(`}`)
	p.P(`if that1 == nil {`)
	p.In()
	p.P(`if this == nil {`)
	p.In()
	if verbose {
		p.P(`return nil`)
	} else {
		p.P(`return true`)
	}
	p.Out()
	p.P(`}`)
	if verbose {
		p.P(`return `, p.Pkg["fmt"], `.Errorf("that is type *`, ccTypeName, ` but is nil && this != nil")`)
	} else {
		p.P(`return false`)
	}
	p.Out()
	p.P(`} else if this == nil {`)
	p.In()
	if verbose {
		p.P(`return `, p.Pkg["fmt"], `.Errorf("that is type *`, ccTypeName, `but is not nil && this == nil")`)
	} else {
		p.P(`return false`)
	}
	p.Out()
	p.P(`}`)

	for _, field := range message.Field {
		fieldname := p.GetFieldName(message, field)
		repeated := field.IsRepeated()
		if repeated {
			p.P(`if this.`, generator.SizerName(fieldname), ` != that1.`, generator.SizerName(fieldname), ` {`)
		} else {
			p.P(`if this.`, generator.SetterName(fieldname), ` != that1.`, generator.SetterName(fieldname), ` {`)
		}
		p.In()
		if verbose {
			p.P(`return `, p.Pkg["fmt"], `.Errorf("that.`, fieldname, ` is not equal to this.`, fieldname, `")`)
		} else {
			p.P(`return false`)
		}
		p.Out()
		p.P(`}`)

		if !repeated {
			if field.IsMessage() || p.IsGroup(field) {
				p.P(`if this.`, generator.SetterName(fieldname), ` && !this.`, fieldname, `.Equal(that1.`, fieldname, `) {`)
			} else if field.IsBytes() {
				p.P(`if this.`, generator.SetterName(fieldname), ` && !`, p.bytesPkg.Use(), `.Equal(this.`, fieldname, `, that1.`, fieldname, `) {`)
			} else {
				p.P(`if this.`, generator.SetterName(fieldname), ` && this.`, fieldname, ` != that1.`, fieldname, `{`)
			}
			p.In()
			if verbose {
				p.P(`return `, p.Pkg["fmt"], `.Errorf("`, fieldname, ` this(%v) Not Equal that(%v)", this.`, fieldname, `, that1.`, fieldname, `)`)
			} else {
				p.P(`return false`)
			}
			p.Out()
			p.P(`}`)
		} else {
			p.P(`for i := 0; i < this.`, generator.SizerName(fieldname), `; i++ {`)
			p.In()
			if field.IsMessage() || p.IsGroup(field) {
				p.P(`if !this.`, fieldname, `[i].Equal(that1.`, fieldname, `[i]) {`)
			} else if field.IsBytes() {
				p.P(`if !`, p.bytesPkg.Use(), `.Equal(this.`, fieldname, `[i], that1.`, fieldname, `[i]) {`)
			} else {
				p.P(`if this.`, fieldname, `[i] != that1.`, fieldname, `[i] {`)
			}
			p.In()
			if verbose {
				p.P(`return `, p.Pkg["fmt"], `.Errorf("`, fieldname, ` this[%v](%v) Not Equal that[%v](%v)", i, this.`, fieldname, `[i], i, that1.`, fieldname, `[i])`)
			} else {
				p.P(`return false`)
			}
			p.Out()
			p.P(`}`)
			p.Out()
			p.P(`}`)
		}
	}
	if message.DescriptorProto.HasExtension() {
		fieldname := "XXX_extensions"
		if hasExtensionsMap {
			p.P(`for k, v := range this.`, fieldname, ` {`)
			p.In()
			p.P(`if v2, ok := that1.`, fieldname, `[k]; ok {`)
			p.In()
			p.P(`if !v.Equal(&v2) {`)
			p.In()
			if verbose {
				p.P(`return `, p.Pkg["fmt"], `.Errorf("`, fieldname, ` this[%v](%v) Not Equal that[%v](%v)", k, this.`, fieldname, `[k], k, that1.`, fieldname, `[k])`)
			} else {
				p.P(`return false`)
			}
			p.Out()
			p.P(`}`)
			p.Out()
			p.P(`} else  {`)
			p.In()
			if verbose {
				p.P(`return `, p.Pkg["fmt"], `.Errorf("`, fieldname, `[%v] Not In that", k)`)
			} else {
				p.P(`return false`)
			}
			p.Out()
			p.P(`}`)
			p.Out()
			p.P(`}`)

			p.P(`for k, _ := range that1.`, fieldname, ` {`)
			p.In()
			p.P(`if _, ok := this.`, fieldname, `[k]; !ok {`)
			p.In()
			if verbose {
				p.P(`return `, p.Pkg["fmt"], `.Errorf("`, fieldname, `[%v] Not In this", k)`)
			} else {
				p.P(`return false`)
			}
			p.Out()
			p.P(`}`)
			p.Out()
			p.P(`}`)
		} else {
			p.P(`if !`, p.bytesPkg.Use(), `.Equal(this.`, fieldname, `, that1.`, fieldname, `) {`)
			p.In()
			if verbose {
				p.P(`return `, p.Pkg["fmt"], `.Errorf("`, fieldname, ` this(%v) Not Equal that(%v)", this.`, fieldname, `, that1.`, fieldname, `)`)
			} else {
				p.P(`return false`)
			}
			p.Out()
			p.P(`}`)
		}
	}
	fieldname := "XXX_unrecognized"
	p.P(`if !`, p.bytesPkg.Use(), `.Equal(this.`, fieldname, `, that1.`, fieldname, `) {`)
	p.In()
	if verbose {
		p.P(`return `, p.Pkg["fmt"], `.Errorf("`, fieldname, ` this(%v) Not Equal that(%v)", this.`, fieldname, `, that1.`, fieldname, `)`)
	} else {
		p.P(`return false`)
	}
	p.Out()
	p.P(`}`)
	if verbose {
		p.P(`return nil`)
	} else {
		p.P(`return true`)
	}
	p.Out()
	p.P(`}`)
}