func generateDeepCopies(t *testing.T, version string) bytes.Buffer {
	g := runtime.NewDeepCopyGenerator(api.Scheme.Raw())
	g.OverwritePackage(version, "")
	testedVersion := version
	if version == "api" {
		testedVersion = api.Scheme.Raw().InternalVersion
	}
	for _, knownType := range api.Scheme.KnownTypes(testedVersion) {
		if err := g.AddType(knownType); err != nil {
			glog.Errorf("error while generating deep-copy functions for %v: %v", knownType, err)
		}
	}

	var functions bytes.Buffer
	functionsWriter := bufio.NewWriter(&functions)
	if err := g.WriteImports(functionsWriter, version); err != nil {
		t.Fatalf("couldn't generate deep-copy function imports: %v", err)
	}
	if err := g.WriteDeepCopyFunctions(functionsWriter); err != nil {
		t.Fatalf("couldn't generate deep-copy functions: %v", err)
	}
	if err := g.RegisterDeepCopyFunctions(functionsWriter, version); err != nil {
		t.Fatalf("couldn't generate deep-copy function names: %v", err)
	}
	if err := functionsWriter.Flush(); err != nil {
		t.Fatalf("error while flushing writer")
	}

	return functions
}
Esempio n. 2
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	flag.Parse()

	var funcOut io.Writer
	if *functionDest == "-" {
		funcOut = os.Stdout
	} else {
		file, err := os.Create(*functionDest)
		if err != nil {
			glog.Fatalf("Couldn't open %v: %v", *functionDest, err)
		}
		defer file.Close()
		funcOut = file
	}

	knownVersion := *version
	if knownVersion == "api" {
		knownVersion = api.Scheme.Raw().InternalVersion
	}
	generator := pkg_runtime.NewDeepCopyGenerator(api.Scheme.Raw())

	for _, overwrite := range strings.Split(*overwrites, ",") {
		vals := strings.Split(overwrite, "=")
		generator.OverwritePackage(vals[0], vals[1])
	}
	for _, knownType := range api.Scheme.KnownTypes(knownVersion) {
		if err := generator.AddType(knownType); err != nil {
			glog.Errorf("error while generating deep copy functions for %v: %v", knownType, err)
		}
	}
	if err := generator.WriteImports(funcOut, *version); err != nil {
		glog.Fatalf("error while writing imports: %v", err)
	}
	if err := generator.WriteDeepCopyFunctions(funcOut); err != nil {
		glog.Fatalf("error while writing deep copy functions: %v", err)
	}
	if err := generator.RegisterDeepCopyFunctions(funcOut, *version); err != nil {
		glog.Fatalf("error while registering deep copy functions: %v", err)
	}
}