Пример #1
0
func (p *plugin) Generate(file *generator.FileDescriptor) {
	for _, msg := range file.Messages() {
		for _, os := range overwriters {
			possible := true
			for _, overwriter := range os {
				if overwriter(file.FileDescriptorProto, msg.DescriptorProto) {
					possible = false
				}
			}
			if possible {
				p.checkOverwrite(msg, os)
			}
		}
		p.checkNameSpace(msg)
		for _, field := range msg.GetField() {
			if gogoproto.IsEmbed(field) && gogoproto.IsCustomName(field) {
				fmt.Fprintf(os.Stderr, "ERROR: field %v with custom name %v cannot be embedded", *field.Name, gogoproto.GetCustomName(field))
				os.Exit(1)
			}
		}
		p.checkRepeated(msg)
	}
	for _, e := range file.GetExtension() {
		if gogoproto.IsEmbed(e) {
			fmt.Fprintf(os.Stderr, "ERROR: extended field %v cannot be embedded", generator.CamelCase(*e.Name))
			os.Exit(1)
		}
	}
}
Пример #2
0
func (p *plugin) checkNameSpace(message *generator.Descriptor) map[string]bool {
	ccTypeName := generator.CamelCaseSlice(message.TypeName())
	names := make(map[string]bool)
	for _, field := range message.Field {
		fieldname := generator.CamelCase(*field.Name)
		if gogoproto.IsEmbed(field) {
			desc := p.ObjectNamed(field.GetTypeName())
			moreNames := p.checkNameSpace(desc.(*generator.Descriptor))
			for another := range moreNames {
				if names[another] {
					fmt.Fprintf(os.Stderr, "ERROR: duplicate embedded fieldname %v in type %v\n", fieldname, ccTypeName)
					os.Exit(1)
				}
				names[another] = true
			}
		} else {
			if names[fieldname] {
				fmt.Fprintf(os.Stderr, "ERROR: duplicate embedded fieldname %v in type %v\n", fieldname, ccTypeName)
				os.Exit(1)
			}
			names[fieldname] = true
		}
	}
	return names
}
Пример #3
0
func (p *plugin) checkRepeated(message *generator.Descriptor) {
	ccTypeName := generator.CamelCaseSlice(message.TypeName())
	for _, field := range message.Field {
		if !gogoproto.IsEmbed(field) {
			continue
		}
		if !field.IsRepeated() {
			continue
		}
		fieldname := generator.CamelCase(*field.Name)
		fmt.Fprintf(os.Stderr, "ERROR: found repeated embedded field %s in message %s\n", fieldname, ccTypeName)
		os.Exit(1)
	}
}
Пример #4
0
func (p *plugin) checkOverwrite(message *generator.Descriptor, enablers map[string]gogoproto.EnableFunc) {
	ccTypeName := generator.CamelCaseSlice(message.TypeName())
	names := []string{}
	for name := range enablers {
		names = append(names, name)
	}
	for _, field := range message.Field {
		if gogoproto.IsEmbed(field) {
			fieldname := generator.CamelCase(*field.Name)
			desc := p.ObjectNamed(field.GetTypeName())
			msg := desc.(*generator.Descriptor)
			for errStr, enabled := range enablers {
				if enabled(msg.File(), msg.DescriptorProto) {
					fmt.Fprintf(os.Stderr, "WARNING: found non-%v %v with embedded %v %v\n", names, ccTypeName, errStr, fieldname)
				}
			}
			p.checkOverwrite(msg, enablers)
		}
	}
}
Пример #5
0
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
	}

}
Пример #6
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
}