コード例 #1
0
ファイル: deepcopy.go プロジェクト: yongtang/swarmkit
func (d *deepCopyGen) Generate(file *generator.FileDescriptor) {
	d.PluginImports = generator.NewPluginImports(d.Generator)

	// TODO(stevvooe): Ideally, this could be taken as a parameter to the
	// deepcopy plugin to control the package import, but this is good enough,
	// for now.
	d.copyPkg = d.NewImport("github.com/docker/swarmkit/api/deepcopy")

	d.P()
	for _, m := range file.Messages() {
		if m.DescriptorProto.GetOptions().GetMapEntry() {
			continue
		}

		if !plugin.DeepcopyEnabled(m.Options) {
			continue
		}

		d.genMsgDeepCopy(m)
	}
	d.P()
}
コード例 #2
0
ファイル: deepcopy.go プロジェクト: docker/swarmkit
func (d *deepCopyGen) genMsgDeepCopy(m *generator.Descriptor) {
	if !plugin.DeepcopyEnabled(m.Options) {
		return
	}

	ccTypeName := generator.CamelCaseSlice(m.TypeName())
	d.gen.P("func (m *", ccTypeName, ") Copy() *", ccTypeName, "{")
	d.gen.P("\tif m == nil {")
	d.gen.P("\t\treturn nil")
	d.gen.P("\t}")
	d.gen.P()

	d.gen.P("\to := &", ccTypeName, "{")

	var funcs []func()
	oneOfFuncs := make(map[string][]func())
	for _, f := range m.Field {
		fName := generator.CamelCase(*f.Name)
		if gogoproto.IsCustomName(f) {
			fName = gogoproto.GetCustomName(f)
		}

		notNullablePrefix := ""
		if !gogoproto.IsNullable(f) {
			notNullablePrefix = "*"

		}

		// Handle all kinds of message type
		if f.IsMessage() {
			// Handle map type
			if mapfunc := d.genMapWriter(m, f, notNullablePrefix); mapfunc != nil {
				funcs = append(funcs, mapfunc)
				continue
			}

			// Handle any message which is not repeated or part of oneof
			if !f.IsRepeated() && f.OneofIndex == nil {
				d.gen.P("\t\t", fName, ": ", notNullablePrefix, "m.", fName, ".Copy(),")
				continue
			}
		}

		// Handle repeated field
		if f.IsRepeated() {
			funcs = append(funcs, d.genRepeatedWriter(m, f, notNullablePrefix))
			continue
		}

		// Handle oneof type
		if f.OneofIndex != nil {
			d.genOneOfWriter(m, f, oneOfFuncs)
			continue
		}

		// Handle all kinds of scalar type
		d.gen.P("\t\t", fName, ": m.", fName, ",")
	}

	d.gen.P("\t}")
	d.gen.P()

	for _, fn := range funcs {
		fn()
	}

	// Sort map keys from oneOfFuncs so that generated code has consistent
	// ordering.
	oneOfKeys := make([]string, 0, len(oneOfFuncs))
	for key := range oneOfFuncs {
		oneOfKeys = append(oneOfKeys, key)
	}
	sort.Strings(oneOfKeys)

	for _, key := range oneOfKeys {
		oneOfNameFuncs := oneOfFuncs[key]
		for _, oneOfFunc := range oneOfNameFuncs {
			oneOfFunc()
		}
		d.gen.P("\t}")
		d.gen.P()
	}

	d.gen.P("\treturn o")
	d.gen.P("}")
	d.gen.P()
}