Move gengo/examples/defaulter -> code_generator

This includes tests, which all pass.  Also fixes a bunch of lint.
This commit is contained in:
Tim Hockin
2024-01-11 23:47:53 -08:00
parent cb7f0593ae
commit 86d2ecbf9a
34 changed files with 3606 additions and 6 deletions

View File

@@ -24,10 +24,10 @@ import (
// build script dependencies
_ "github.com/onsi/ginkgo/v2/ginkgo"
_ "k8s.io/code-generator/cmd/deepcopy-gen"
_ "k8s.io/code-generator/cmd/defaulter-gen"
_ "k8s.io/code-generator/cmd/go-to-protobuf"
_ "k8s.io/code-generator/cmd/go-to-protobuf/protoc-gen-gogo"
_ "k8s.io/code-generator/cmd/import-boss"
_ "k8s.io/gengo/v2/examples/defaulter-gen/generators"
_ "k8s.io/kube-openapi/cmd/openapi-gen"
// submodule test dependencies

View File

@@ -51,6 +51,7 @@
- baseImportPath: "./staging/src/k8s.io/code-generator"
ignoredSubTrees:
- "./staging/src/k8s.io/code-generator/examples"
- "./staging/src/k8s.io/code-generator/cmd/defaulter-gen/output_tests"
allowedImports:
- k8s.io/gengo
- k8s.io/code-generator

View File

@@ -21,17 +21,20 @@ import (
"github.com/spf13/pflag"
"k8s.io/gengo/v2/args"
"k8s.io/gengo/v2/examples/defaulter-gen/generators"
)
// CustomArgs is used by the gengo framework to pass args specific to this generator.
type CustomArgs generators.CustomArgs
type CustomArgs struct {
OutputFile string
ExtraPeerDirs []string // Always consider these as last-ditch possibilities for conversions.
GoHeaderFile string
}
// NewDefaults returns default arguments for the generator.
func NewDefaults() (*args.GeneratorArgs, *CustomArgs) {
genericArgs := args.Default()
customArgs := &CustomArgs{}
genericArgs.CustomArgs = (*generators.CustomArgs)(customArgs) // convert to upstream type to make type-casts work there
genericArgs.CustomArgs = (*CustomArgs)(customArgs) // convert to upstream type to make type-casts work there
return genericArgs, customArgs
}
@@ -47,7 +50,7 @@ func (ca *CustomArgs) AddFlags(fs *pflag.FlagSet) {
// Validate checks the given arguments.
func Validate(genericArgs *args.GeneratorArgs) error {
custom := genericArgs.CustomArgs.(*generators.CustomArgs)
custom := genericArgs.CustomArgs.(*CustomArgs)
if len(custom.OutputFile) == 0 {
return fmt.Errorf("--output-file must be specified")

File diff suppressed because it is too large Load Diff

View File

@@ -46,8 +46,8 @@ import (
"github.com/spf13/pflag"
generatorargs "k8s.io/code-generator/cmd/defaulter-gen/args"
"k8s.io/code-generator/cmd/defaulter-gen/generators"
"k8s.io/gengo/v2/args"
"k8s.io/gengo/v2/examples/defaulter-gen/generators"
"k8s.io/klog/v2"
)

View File

@@ -0,0 +1,20 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// +k8s:defaulter-gen=covers
// This is a test package.
package empty // import "k8s.io/code-generator/cmd/defaulter-gen/output_tests/empty"

View File

@@ -0,0 +1,29 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package empty
// Only test
type Ttest struct {
BoolField bool
IntField int
StringField string
FloatField float64
}
type TypeMeta struct {
Fortest bool
}

View File

@@ -0,0 +1,33 @@
//go:build !ignore_autogenerated
// +build !ignore_autogenerated
/*
Copyright The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Code generated by defaulter-gen. DO NOT EDIT.
package empty
import (
runtime "k8s.io/apimachinery/pkg/runtime"
)
// RegisterDefaults adds defaulters functions to the given scheme.
// Public to allow building arbitrary schemes.
// All generated defaulters are covering - they call all nested defaulters.
func RegisterDefaults(scheme *runtime.Scheme) error {
return nil
}

View File

@@ -0,0 +1,27 @@
//go:build !ignore_autogenerated
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Ignore this file to prevent zz_generated for this package
//go:generate go run k8s.io/code-generator/cmd/defaulter-gen --output-file zz_generated.defaults.go --go-header-file=../../../examples/hack/boilerplate.go.txt k8s.io/code-generator/cmd/defaulter-gen/output_tests/...
package outputtests
import (
// For go-generate
_ "k8s.io/code-generator/cmd/defaulter-gen/generators"
)

View File

@@ -0,0 +1,32 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package marker
import (
"k8s.io/apimachinery/pkg/runtime"
)
//nolint:unused
func addDefaultingFuncs(scheme *runtime.Scheme) error {
return RegisterDefaults(scheme)
}
func SetDefaults_DefaultedWithFunction(obj *DefaultedWithFunction) {
if obj.S1 == "" {
obj.S1 = "default_function"
}
}

View File

@@ -0,0 +1,20 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// +k8s:defaulter-gen=TypeMeta
// This is a test package.
package marker // import "k8s.io/code-generator/cmd/defaulter-gen/output_tests/marker"

View File

@@ -0,0 +1,20 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package external
// Used for test with multiple packages of the same name
const AConstant string = "AConstantString"

View File

@@ -0,0 +1,20 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package external
// Used for test with multiple packages of the same name
const AnotherConstant string = "AnotherConstantString"

View File

@@ -0,0 +1,19 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package external2
type String string

View File

@@ -0,0 +1,21 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package external3
import "k8s.io/code-generator/cmd/defaulter-gen/output_tests/marker/external2"
type StringPointer *external2.String

View File

@@ -0,0 +1,107 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package marker
import (
runtime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
func (in *Defaulted) DeepCopy() *Defaulted {
if in == nil {
return nil
}
out := new(Defaulted)
in.DeepCopyInto(out)
return out
}
func (in *Defaulted) DeepCopyInto(out *Defaulted) {
*out = *in
}
func (in *Defaulted) DeepCopyObject() runtime.Object {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}
func (in *DefaultedOmitempty) DeepCopy() *DefaultedOmitempty {
if in == nil {
return nil
}
out := new(DefaultedOmitempty)
in.DeepCopyInto(out)
return out
}
func (in *DefaultedOmitempty) DeepCopyInto(out *DefaultedOmitempty) {
*out = *in
}
func (in *DefaultedOmitempty) DeepCopyObject() runtime.Object {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}
func (in *DefaultedWithFunction) DeepCopy() *DefaultedWithFunction {
if in == nil {
return nil
}
out := new(DefaultedWithFunction)
in.DeepCopyInto(out)
return out
}
func (in *DefaultedWithFunction) DeepCopyInto(out *DefaultedWithFunction) {
*out = *in
}
func (in *DefaultedWithFunction) DeepCopyObject() runtime.Object {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}
func (in *Defaulted) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }
func (in *DefaultedOmitempty) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }
func (in *DefaultedWithFunction) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }
func (in *DefaultedWithReference) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }
func (in *DefaultedWithReference) DeepCopy() *DefaultedWithReference {
if in == nil {
return nil
}
out := new(DefaultedWithReference)
in.DeepCopyInto(out)
return out
}
func (in *DefaultedWithReference) DeepCopyInto(out *DefaultedWithReference) {
*out = *in
}
func (in *DefaultedWithReference) DeepCopyObject() runtime.Object {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}

View File

@@ -0,0 +1,412 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package marker
import (
"testing"
"github.com/google/go-cmp/cmp"
"k8s.io/code-generator/cmd/defaulter-gen/output_tests/marker/external"
externalexternal "k8s.io/code-generator/cmd/defaulter-gen/output_tests/marker/external/external"
"k8s.io/code-generator/cmd/defaulter-gen/output_tests/marker/external2"
"k8s.io/code-generator/cmd/defaulter-gen/output_tests/marker/external3"
)
func getPointerFromString(s string) *string {
return &s
}
var (
defaultInt32 int32 = 32
defaultInt64 int64 = 64
)
func Test_Marker(t *testing.T) {
testcases := []struct {
name string
in Defaulted
out Defaulted
}{
{
name: "default",
in: Defaulted{},
out: Defaulted{
StringDefault: "bar",
StringEmptyDefault: "",
StringEmpty: "",
StringPointer: getPointerFromString("default"),
Int64: &defaultInt64,
Int32: &defaultInt32,
IntDefault: 1,
IntEmptyDefault: 0,
IntEmpty: 0,
FloatDefault: 0.5,
FloatEmptyDefault: 0.0,
FloatEmpty: 0.0,
List: []Item{
getPointerFromString("foo"),
getPointerFromString("bar"),
},
Sub: &SubStruct{
S: "foo",
I: 5,
},
OtherSub: SubStruct{
S: "",
I: 1,
},
StructList: []SubStruct{
{
S: "foo1",
I: 1,
},
{
S: "foo2",
I: 1,
},
},
PtrStructList: []*SubStruct{
{
S: "foo1",
I: 1,
},
{
S: "foo2",
I: 1,
},
},
StringList: []string{
"foo",
},
Map: map[string]Item{
"foo": getPointerFromString("bar"),
},
StructMap: map[string]SubStruct{
"foo": {
S: "string",
I: 1,
},
},
PtrStructMap: map[string]*SubStruct{
"foo": {
S: "string",
I: 1,
},
},
AliasPtr: getPointerFromString("banana"),
},
},
{
name: "values-omitempty",
in: Defaulted{
StringDefault: "changed",
IntDefault: 5,
},
out: Defaulted{
StringDefault: "changed",
StringEmptyDefault: "",
StringEmpty: "",
StringPointer: getPointerFromString("default"),
Int64: &defaultInt64,
Int32: &defaultInt32,
IntDefault: 5,
IntEmptyDefault: 0,
IntEmpty: 0,
FloatDefault: 0.5,
FloatEmptyDefault: 0.0,
FloatEmpty: 0.0,
List: []Item{
getPointerFromString("foo"),
getPointerFromString("bar"),
},
Sub: &SubStruct{
S: "foo",
I: 5,
},
StructList: []SubStruct{
{
S: "foo1",
I: 1,
},
{
S: "foo2",
I: 1,
},
},
PtrStructList: []*SubStruct{
{
S: "foo1",
I: 1,
},
{
S: "foo2",
I: 1,
},
},
StringList: []string{
"foo",
},
OtherSub: SubStruct{
S: "",
I: 1,
},
Map: map[string]Item{
"foo": getPointerFromString("bar"),
},
StructMap: map[string]SubStruct{
"foo": {
S: "string",
I: 1,
},
},
PtrStructMap: map[string]*SubStruct{
"foo": {
S: "string",
I: 1,
},
},
AliasPtr: getPointerFromString("banana"),
},
},
{
name: "lists",
in: Defaulted{
List: []Item{
nil,
getPointerFromString("bar"),
},
},
out: Defaulted{
StringDefault: "bar",
StringEmptyDefault: "",
StringEmpty: "",
StringPointer: getPointerFromString("default"),
Int64: &defaultInt64,
Int32: &defaultInt32,
IntDefault: 1,
IntEmptyDefault: 0,
IntEmpty: 0,
FloatDefault: 0.5,
FloatEmptyDefault: 0.0,
FloatEmpty: 0.0,
List: []Item{
getPointerFromString("apple"),
getPointerFromString("bar"),
},
Sub: &SubStruct{
S: "foo",
I: 5,
},
StructList: []SubStruct{
{
S: "foo1",
I: 1,
},
{
S: "foo2",
I: 1,
},
},
PtrStructList: []*SubStruct{
{
S: "foo1",
I: 1,
},
{
S: "foo2",
I: 1,
},
},
StringList: []string{
"foo",
},
OtherSub: SubStruct{
S: "",
I: 1,
},
Map: map[string]Item{
"foo": getPointerFromString("bar"),
},
StructMap: map[string]SubStruct{
"foo": {
S: "string",
I: 1,
},
},
PtrStructMap: map[string]*SubStruct{
"foo": {
S: "string",
I: 1,
},
},
AliasPtr: getPointerFromString("banana"),
},
},
{
name: "stringmap",
in: Defaulted{
Map: map[string]Item{
"foo": nil,
"bar": getPointerFromString("banana"),
},
},
out: Defaulted{
StringDefault: "bar",
StringEmptyDefault: "",
StringEmpty: "",
StringPointer: getPointerFromString("default"),
Int64: &defaultInt64,
Int32: &defaultInt32,
IntDefault: 1,
IntEmptyDefault: 0,
IntEmpty: 0,
FloatDefault: 0.5,
FloatEmptyDefault: 0.0,
FloatEmpty: 0.0,
List: []Item{
getPointerFromString("foo"),
getPointerFromString("bar"),
},
Sub: &SubStruct{
S: "foo",
I: 5,
},
StructList: []SubStruct{
{
S: "foo1",
I: 1,
},
{
S: "foo2",
I: 1,
},
},
PtrStructList: []*SubStruct{
{
S: "foo1",
I: 1,
},
{
S: "foo2",
I: 1,
},
},
StringList: []string{
"foo",
},
OtherSub: SubStruct{
S: "",
I: 1,
},
Map: map[string]Item{
"foo": getPointerFromString("apple"),
"bar": getPointerFromString("banana"),
},
StructMap: map[string]SubStruct{
"foo": {
S: "string",
I: 1,
},
},
PtrStructMap: map[string]*SubStruct{
"foo": {
S: "string",
I: 1,
},
},
AliasPtr: getPointerFromString("banana"),
},
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
SetObjectDefaults_Defaulted(&tc.in)
if diff := cmp.Diff(tc.out, tc.in); len(diff) > 0 {
t.Errorf("Error: Expected and actual output are different \n %s\n", diff)
}
})
}
}
func Test_DefaultingFunction(t *testing.T) {
in := DefaultedWithFunction{}
SetObjectDefaults_DefaultedWithFunction(&in)
out := DefaultedWithFunction{
S1: "default_function",
S2: "default_marker",
}
if diff := cmp.Diff(out, in); len(diff) > 0 {
t.Errorf("Error: Expected and actual output are different \n %s\n", diff)
}
}
func Test_DefaultingReference(t *testing.T) {
dv := DefaultedValueItem(SomeValue)
SomeDefault := SomeDefault
SomeValue := SomeValue
ptrVar9 := string(SomeValue)
ptrVar8 := &ptrVar9
ptrVar7 := (*B1)(&ptrVar8)
ptrVar6 := (*B2)(&ptrVar7)
ptrVar5 := &ptrVar6
ptrVar4 := &ptrVar5
ptrVar3 := &ptrVar4
ptrVar2 := (*B3)(&ptrVar3)
ptrVar1 := &ptrVar2
var external2Str = external2.String(SomeValue)
testcases := []struct {
name string
in DefaultedWithReference
out DefaultedWithReference
}{
{
name: "default",
in: DefaultedWithReference{},
out: DefaultedWithReference{
AliasPointerInside: Item(&SomeDefault),
AliasOverride: Item(&SomeDefault),
AliasConvertDefaultPointer: &dv,
AliasPointerDefault: &dv,
PointerAliasDefault: Item(getPointerFromString("apple")),
AliasNonPointer: SomeValue,
AliasPointer: &SomeValue,
SymbolReference: SomeDefault,
SameNamePackageSymbolReference1: external.AConstant,
SameNamePackageSymbolReference2: externalexternal.AnotherConstant,
PointerConversion: (*B4)(&ptrVar1),
PointerConversionValue: (B4)(ptrVar1),
FullyQualifiedLocalSymbol: string(SomeValue),
ImportFromAliasCast: external3.StringPointer(&external2Str),
},
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
SetObjectDefaults_DefaultedWithReference(&tc.in)
if diff := cmp.Diff(tc.out, tc.in); len(diff) > 0 {
t.Errorf("Error: Expected and actual output are different \n %s\n", diff)
}
})
}
}

View File

@@ -0,0 +1,265 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package marker
import (
"k8s.io/code-generator/cmd/defaulter-gen/output_tests/empty"
"k8s.io/code-generator/cmd/defaulter-gen/output_tests/marker/external3"
)
type Defaulted struct {
empty.TypeMeta
// +default="bar"
StringDefault string
// Default is forced to empty string
// Specifying the default is a no-op
// +default=""
StringEmptyDefault string
// Not specifying a default still defaults for non-omitempty
StringEmpty string
// +default="default"
StringPointer *string
// +default=64
Int64 *int64
// +default=32
Int32 *int32
// +default=1
IntDefault int
// +default=0
IntEmptyDefault int
// Default is forced to 0
IntEmpty int
// +default=0.5
FloatDefault float64
// +default=0.0
FloatEmptyDefault float64
FloatEmpty float64
// +default=["foo", "bar"]
List []Item
// +default={"s": "foo", "i": 5}
Sub *SubStruct
//+default=[{"s": "foo1", "i": 1}, {"s": "foo2"}]
StructList []SubStruct
//+default=[{"s": "foo1", "i": 1}, {"s": "foo2"}]
PtrStructList []*SubStruct
//+default=["foo"]
StringList []string
// Default is forced to empty struct
OtherSub SubStruct
// +default={"foo": "bar"}
Map map[string]Item
// +default={"foo": {"S": "string", "I": 1}}
StructMap map[string]SubStruct
// +default={"foo": {"S": "string", "I": 1}}
PtrStructMap map[string]*SubStruct
// A default specified here overrides the default for the Item type
// +default="banana"
AliasPtr Item
}
type DefaultedOmitempty struct {
empty.TypeMeta `json:",omitempty"`
// +default="bar"
StringDefault string `json:",omitempty"`
// Default is forced to empty string
// Specifying the default is a no-op
// +default=""
StringEmptyDefault string `json:",omitempty"`
// Not specifying a default still defaults for non-omitempty
StringEmpty string `json:",omitempty"`
// +default="default"
StringPointer *string `json:",omitempty"`
// +default=64
Int64 *int64 `json:",omitempty"`
// +default=32
Int32 *int32 `json:",omitempty"`
// +default=1
IntDefault int `json:",omitempty"`
// +default=0
IntEmptyDefault int `json:",omitempty"`
// Default is forced to 0
IntEmpty int `json:",omitempty"`
// +default=0.5
FloatDefault float64 `json:",omitempty"`
// +default=0.0
FloatEmptyDefault float64 `json:",omitempty"`
FloatEmpty float64 `json:",omitempty"`
// +default=["foo", "bar"]
List []Item `json:",omitempty"`
// +default={"s": "foo", "i": 5}
Sub *SubStruct `json:",omitempty"`
//+default=[{"s": "foo1", "i": 1}, {"s": "foo2"}]
StructList []SubStruct `json:",omitempty"`
//+default=[{"s": "foo1", "i": 1}, {"s": "foo2"}]
PtrStructList []*SubStruct `json:",omitempty"`
//+default=["foo"]
StringList []string `json:",omitempty"`
// Default is forced to empty struct
OtherSub SubStruct `json:",omitempty"`
// +default={"foo": "bar"}
Map map[string]Item `json:",omitempty"`
// +default={"foo": {"S": "string", "I": 1}}
StructMap map[string]SubStruct `json:",omitempty"`
// +default={"foo": {"S": "string", "I": 1}}
PtrStructMap map[string]*SubStruct `json:",omitempty"`
// A default specified here overrides the default for the Item type
// +default="banana"
AliasPtr Item `json:",omitempty"`
}
const SomeDefault = "ACoolConstant"
// +default="apple"
type Item *string
type ValueItem string
// +default=ref(SomeValue)
type DefaultedValueItem ValueItem
type PointerValueItem *DefaultedValueItem
type ItemDefaultWiped Item
const SomeValue ValueItem = "Value"
type SubStruct struct {
S string
// +default=1
I int `json:"I,omitempty"`
}
type DefaultedWithFunction struct {
empty.TypeMeta
// +default="default_marker"
S1 string `json:"S1,omitempty"`
// +default="default_marker"
S2 string `json:"S2,omitempty"`
}
type DefaultedWithReference struct {
empty.TypeMeta
// Shows that if we have an alias that is a pointer and have a default
// that is a value convertible to that pointer we can still use it
// +default=ref(SomeValue)
AliasConvertDefaultPointer PointerValueItem
// Shows that default defined on a nested type is not respected through
// an alias
AliasWipedDefault ItemDefaultWiped
// A default defined on a pointer-valued alias is respected
PointerAliasDefault Item
// Can have alias that is a pointer to type of constant
// +default=ref(SomeDefault)
AliasPointerInside Item
// Can override default specified on an alias
// +default=ref(SomeDefault)
AliasOverride Item
// Type-level default is not respected unless a pointer
AliasNonPointerDefault DefaultedValueItem `json:",omitempty"`
// Type-level default is not respected unless a pointer
AliasPointerDefault *DefaultedValueItem
// Can have value typed alias
// +default=ref(SomeValue)
AliasNonPointer ValueItem `json:",omitempty"`
// Can have a pointer to an alias whose default is a non-pointer value
// +default=ref(SomeValue)
AliasPointer *ValueItem `json:",omitempty"`
// Basic ref usage example
// +default=ref(SomeDefault)
SymbolReference string `json:",omitempty"`
// +default=ref(k8s.io/code-generator/cmd/defaulter-gen/output_tests/marker/external.AConstant)
SameNamePackageSymbolReference1 string `json:",omitempty"`
// +default=ref(k8s.io/code-generator/cmd/defaulter-gen/output_tests/marker/external/external.AnotherConstant)
SameNamePackageSymbolReference2 string `json:",omitempty"`
// Should convert ValueItem -> string then up to B4 through addressOf and
// casting
// +default=ref(SomeValue)
PointerConversion *B4
// +default=ref(SomeValue)
PointerConversionValue B4
// +default=ref(k8s.io/code-generator/cmd/defaulter-gen/output_tests/marker.SomeValue)
FullyQualifiedLocalSymbol string
// Construction of external3.StringPointer requires importing external2
// Test that generator can handle it
// +default=ref(SomeValue)
ImportFromAliasCast external3.StringPointer
}
// Super complicated hierarchy of aliases which includes multiple pointers,
// and sibling types.
type B0 *string
type B1 B0
type B2 *B1
type B3 ****B2
type B4 **B3

View File

@@ -0,0 +1,325 @@
//go:build !ignore_autogenerated
// +build !ignore_autogenerated
/*
Copyright The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Code generated by defaulter-gen. DO NOT EDIT.
package marker
import (
"encoding/json"
runtime "k8s.io/apimachinery/pkg/runtime"
external "k8s.io/code-generator/cmd/defaulter-gen/output_tests/marker/external"
externalexternal "k8s.io/code-generator/cmd/defaulter-gen/output_tests/marker/external/external"
external2 "k8s.io/code-generator/cmd/defaulter-gen/output_tests/marker/external2"
)
// RegisterDefaults adds defaulters functions to the given scheme.
// Public to allow building arbitrary schemes.
// All generated defaulters are covering - they call all nested defaulters.
func RegisterDefaults(scheme *runtime.Scheme) error {
scheme.AddTypeDefaultingFunc(&Defaulted{}, func(obj interface{}) { SetObjectDefaults_Defaulted(obj.(*Defaulted)) })
scheme.AddTypeDefaultingFunc(&DefaultedOmitempty{}, func(obj interface{}) { SetObjectDefaults_DefaultedOmitempty(obj.(*DefaultedOmitempty)) })
scheme.AddTypeDefaultingFunc(&DefaultedWithFunction{}, func(obj interface{}) { SetObjectDefaults_DefaultedWithFunction(obj.(*DefaultedWithFunction)) })
scheme.AddTypeDefaultingFunc(&DefaultedWithReference{}, func(obj interface{}) { SetObjectDefaults_DefaultedWithReference(obj.(*DefaultedWithReference)) })
return nil
}
func SetObjectDefaults_Defaulted(in *Defaulted) {
if in.StringDefault == "" {
in.StringDefault = "bar"
}
if in.StringPointer == nil {
var ptrVar1 string = "default"
in.StringPointer = &ptrVar1
}
if in.Int64 == nil {
var ptrVar1 int64 = 64
in.Int64 = &ptrVar1
}
if in.Int32 == nil {
var ptrVar1 int32 = 32
in.Int32 = &ptrVar1
}
if in.IntDefault == 0 {
in.IntDefault = 1
}
if in.FloatDefault == 0 {
in.FloatDefault = 0.5
}
if in.List == nil {
if err := json.Unmarshal([]byte(`["foo", "bar"]`), &in.List); err != nil {
panic(err)
}
}
for i := range in.List {
if in.List[i] == nil {
var ptrVar1 string = "apple"
in.List[i] = &ptrVar1
}
}
if in.Sub == nil {
if err := json.Unmarshal([]byte(`{"s": "foo", "i": 5}`), &in.Sub); err != nil {
panic(err)
}
}
if in.Sub != nil {
if in.Sub.I == 0 {
in.Sub.I = 1
}
}
if in.StructList == nil {
if err := json.Unmarshal([]byte(`[{"s": "foo1", "i": 1}, {"s": "foo2"}]`), &in.StructList); err != nil {
panic(err)
}
}
for i := range in.StructList {
a := &in.StructList[i]
if a.I == 0 {
a.I = 1
}
}
if in.PtrStructList == nil {
if err := json.Unmarshal([]byte(`[{"s": "foo1", "i": 1}, {"s": "foo2"}]`), &in.PtrStructList); err != nil {
panic(err)
}
}
for i := range in.PtrStructList {
a := in.PtrStructList[i]
if a != nil {
if a.I == 0 {
a.I = 1
}
}
}
if in.StringList == nil {
if err := json.Unmarshal([]byte(`["foo"]`), &in.StringList); err != nil {
panic(err)
}
}
if in.OtherSub.I == 0 {
in.OtherSub.I = 1
}
if in.Map == nil {
if err := json.Unmarshal([]byte(`{"foo": "bar"}`), &in.Map); err != nil {
panic(err)
}
}
for i_Map := range in.Map {
if in.Map[i_Map] == nil {
var ptrVar1 string = "apple"
in.Map[i_Map] = &ptrVar1
}
}
if in.StructMap == nil {
if err := json.Unmarshal([]byte(`{"foo": {"S": "string", "I": 1}}`), &in.StructMap); err != nil {
panic(err)
}
}
if in.PtrStructMap == nil {
if err := json.Unmarshal([]byte(`{"foo": {"S": "string", "I": 1}}`), &in.PtrStructMap); err != nil {
panic(err)
}
}
if in.AliasPtr == nil {
var ptrVar1 string = "banana"
in.AliasPtr = &ptrVar1
}
}
func SetObjectDefaults_DefaultedOmitempty(in *DefaultedOmitempty) {
if in.StringDefault == "" {
in.StringDefault = "bar"
}
if in.StringPointer == nil {
var ptrVar1 string = "default"
in.StringPointer = &ptrVar1
}
if in.Int64 == nil {
var ptrVar1 int64 = 64
in.Int64 = &ptrVar1
}
if in.Int32 == nil {
var ptrVar1 int32 = 32
in.Int32 = &ptrVar1
}
if in.IntDefault == 0 {
in.IntDefault = 1
}
if in.FloatDefault == 0 {
in.FloatDefault = 0.5
}
if in.List == nil {
if err := json.Unmarshal([]byte(`["foo", "bar"]`), &in.List); err != nil {
panic(err)
}
}
for i := range in.List {
if in.List[i] == nil {
var ptrVar1 string = "apple"
in.List[i] = &ptrVar1
}
}
if in.Sub == nil {
if err := json.Unmarshal([]byte(`{"s": "foo", "i": 5}`), &in.Sub); err != nil {
panic(err)
}
}
if in.Sub != nil {
if in.Sub.I == 0 {
in.Sub.I = 1
}
}
if in.StructList == nil {
if err := json.Unmarshal([]byte(`[{"s": "foo1", "i": 1}, {"s": "foo2"}]`), &in.StructList); err != nil {
panic(err)
}
}
for i := range in.StructList {
a := &in.StructList[i]
if a.I == 0 {
a.I = 1
}
}
if in.PtrStructList == nil {
if err := json.Unmarshal([]byte(`[{"s": "foo1", "i": 1}, {"s": "foo2"}]`), &in.PtrStructList); err != nil {
panic(err)
}
}
for i := range in.PtrStructList {
a := in.PtrStructList[i]
if a != nil {
if a.I == 0 {
a.I = 1
}
}
}
if in.StringList == nil {
if err := json.Unmarshal([]byte(`["foo"]`), &in.StringList); err != nil {
panic(err)
}
}
if in.OtherSub.I == 0 {
in.OtherSub.I = 1
}
if in.Map == nil {
if err := json.Unmarshal([]byte(`{"foo": "bar"}`), &in.Map); err != nil {
panic(err)
}
}
for i_Map := range in.Map {
if in.Map[i_Map] == nil {
var ptrVar1 string = "apple"
in.Map[i_Map] = &ptrVar1
}
}
if in.StructMap == nil {
if err := json.Unmarshal([]byte(`{"foo": {"S": "string", "I": 1}}`), &in.StructMap); err != nil {
panic(err)
}
}
if in.PtrStructMap == nil {
if err := json.Unmarshal([]byte(`{"foo": {"S": "string", "I": 1}}`), &in.PtrStructMap); err != nil {
panic(err)
}
}
if in.AliasPtr == nil {
var ptrVar1 string = "banana"
in.AliasPtr = &ptrVar1
}
}
func SetObjectDefaults_DefaultedWithFunction(in *DefaultedWithFunction) {
SetDefaults_DefaultedWithFunction(in)
if in.S1 == "" {
in.S1 = "default_marker"
}
if in.S2 == "" {
in.S2 = "default_marker"
}
}
func SetObjectDefaults_DefaultedWithReference(in *DefaultedWithReference) {
if in.AliasConvertDefaultPointer == nil {
ptrVar1 := DefaultedValueItem(SomeValue)
in.AliasConvertDefaultPointer = &ptrVar1
}
if in.PointerAliasDefault == nil {
var ptrVar1 string = "apple"
in.PointerAliasDefault = &ptrVar1
}
if in.AliasPointerInside == nil {
ptrVar1 := string(SomeDefault)
in.AliasPointerInside = &ptrVar1
}
if in.AliasOverride == nil {
ptrVar1 := string(SomeDefault)
in.AliasOverride = &ptrVar1
}
if in.AliasPointerDefault == nil {
ptrVar1 := DefaultedValueItem(SomeValue)
in.AliasPointerDefault = &ptrVar1
}
if in.AliasNonPointer == "" {
in.AliasNonPointer = ValueItem(SomeValue)
}
if in.AliasPointer == nil {
ptrVar1 := ValueItem(SomeValue)
in.AliasPointer = &ptrVar1
}
if in.SymbolReference == "" {
in.SymbolReference = string(SomeDefault)
}
if in.SameNamePackageSymbolReference1 == "" {
in.SameNamePackageSymbolReference1 = string(external.AConstant)
}
if in.SameNamePackageSymbolReference2 == "" {
in.SameNamePackageSymbolReference2 = string(externalexternal.AnotherConstant)
}
if in.PointerConversion == nil {
ptrVar9 := string(SomeValue)
ptrVar8 := &ptrVar9
ptrVar7 := (*B1)(&ptrVar8)
ptrVar6 := (*B2)(&ptrVar7)
ptrVar5 := &ptrVar6
ptrVar4 := &ptrVar5
ptrVar3 := &ptrVar4
ptrVar2 := (*B3)(&ptrVar3)
ptrVar1 := &ptrVar2
in.PointerConversion = (*B4)(&ptrVar1)
}
if in.PointerConversionValue == nil {
ptrVar8 := string(SomeValue)
ptrVar7 := &ptrVar8
ptrVar6 := (*B1)(&ptrVar7)
ptrVar5 := (*B2)(&ptrVar6)
ptrVar4 := &ptrVar5
ptrVar3 := &ptrVar4
ptrVar2 := &ptrVar3
ptrVar1 := (*B3)(&ptrVar2)
in.PointerConversionValue = &ptrVar1
}
if in.FullyQualifiedLocalSymbol == "" {
in.FullyQualifiedLocalSymbol = string(SomeValue)
}
if in.ImportFromAliasCast == nil {
ptrVar1 := external2.String(SomeValue)
in.ImportFromAliasCast = &ptrVar1
}
}

View File

@@ -0,0 +1,33 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pointer
import (
"k8s.io/apimachinery/pkg/runtime"
)
//nolint:unused
func addDefaultingFuncs(scheme *runtime.Scheme) error {
return RegisterDefaults(scheme)
}
func SetDefaults_Tpointer(obj *Tpointer) {
if obj.BoolField == nil {
obj.BoolField = new(bool)
*obj.BoolField = true
}
}

View File

@@ -0,0 +1,20 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// +k8s:defaulter-gen=TypeMeta
// This is a test package.
package pointer // import "k8s.io/code-generator/cmd/defaulter-gen/output_tests/pointer"

View File

@@ -0,0 +1,82 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pointer
import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Tpointer) DeepCopyInto(out *Tpointer) {
*out = *in
out.TypeMeta = in.TypeMeta
if in.BoolField != nil {
in, out := &in.BoolField, &out.BoolField
*out = new(bool)
**out = **in
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Tpointer.
func (in *Tpointer) DeepCopy() *Tpointer {
if in == nil {
return nil
}
out := new(Tpointer)
in.DeepCopyInto(out)
return out
}
func (in *Tpointer) DeepCopyObject() runtime.Object {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Ttest) DeepCopyInto(out *Ttest) {
*out = *in
out.TypeMeta = in.TypeMeta
in.NTP.DeepCopyInto(&out.NTP)
if in.Tp != nil {
in, out := &in.Tp, &out.Tp
*out = new(Tpointer)
(*in).DeepCopyInto(*out)
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Ttest.
func (in *Ttest) DeepCopy() *Ttest {
if in == nil {
return nil
}
out := new(Ttest)
in.DeepCopyInto(out)
return out
}
func (in *Ttest) DeepCopyObject() runtime.Object {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}
func (in *Tpointer) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }
func (in *Ttest) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }

View File

@@ -0,0 +1,33 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pointer
import (
"k8s.io/code-generator/cmd/defaulter-gen/output_tests/empty"
)
type Tpointer struct {
empty.TypeMeta
BoolField *bool
}
// Only test
type Ttest struct {
empty.TypeMeta
NTP Tpointer
Tp *Tpointer
}

View File

@@ -0,0 +1,46 @@
//go:build !ignore_autogenerated
// +build !ignore_autogenerated
/*
Copyright The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Code generated by defaulter-gen. DO NOT EDIT.
package pointer
import (
runtime "k8s.io/apimachinery/pkg/runtime"
)
// RegisterDefaults adds defaulters functions to the given scheme.
// Public to allow building arbitrary schemes.
// All generated defaulters are covering - they call all nested defaulters.
func RegisterDefaults(scheme *runtime.Scheme) error {
scheme.AddTypeDefaultingFunc(&Tpointer{}, func(obj interface{}) { SetObjectDefaults_Tpointer(obj.(*Tpointer)) })
scheme.AddTypeDefaultingFunc(&Ttest{}, func(obj interface{}) { SetObjectDefaults_Ttest(obj.(*Ttest)) })
return nil
}
func SetObjectDefaults_Tpointer(in *Tpointer) {
SetDefaults_Tpointer(in)
}
func SetObjectDefaults_Ttest(in *Ttest) {
SetObjectDefaults_Tpointer(&in.NTP)
if in.Tp != nil {
SetObjectDefaults_Tpointer(in.Tp)
}
}

View File

@@ -0,0 +1,33 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package slices
import (
"k8s.io/apimachinery/pkg/runtime"
)
//nolint:unused
func addDefaultingFuncs(scheme *runtime.Scheme) error {
return RegisterDefaults(scheme)
}
func SetDefaults_Ttest(obj *Ttest) {
if obj.BoolField == nil {
obj.BoolField = new(bool)
*obj.BoolField = true
}
}

View File

@@ -0,0 +1,20 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// +k8s:defaulter-gen=TypeMeta
// This is a test package.
package slices // import "k8s.io/code-generator/cmd/defaulter-gen/output_tests/slices"

View File

@@ -0,0 +1,118 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package slices
import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Ttest) DeepCopyInto(out *Ttest) {
*out = *in
out.TypeMeta = in.TypeMeta
if in.BoolField != nil {
in, out := &in.BoolField, &out.BoolField
*out = new(bool)
**out = **in
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Ttest.
func (in *Ttest) DeepCopy() *Ttest {
if in == nil {
return nil
}
out := new(Ttest)
in.DeepCopyInto(out)
return out
}
func (in *Ttest) DeepCopyObject() runtime.Object {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *TtestList) DeepCopyInto(out *TtestList) {
*out = *in
out.TypeMeta = in.TypeMeta
if in.Items != nil {
in, out := &in.Items, &out.Items
*out = make([]Ttest, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TtestList.
func (in *TtestList) DeepCopy() *TtestList {
if in == nil {
return nil
}
out := new(TtestList)
in.DeepCopyInto(out)
return out
}
func (in *TtestList) DeepCopyObject() runtime.Object {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *TtestPointerList) DeepCopyInto(out *TtestPointerList) {
*out = *in
out.TypeMeta = in.TypeMeta
if in.Items != nil {
in, out := &in.Items, &out.Items
*out = make([]*Ttest, len(*in))
for i := range *in {
if (*in)[i] != nil {
in, out := &(*in)[i], &(*out)[i]
*out = new(Ttest)
(*in).DeepCopyInto(*out)
}
}
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TtestPointerList.
func (in *TtestPointerList) DeepCopy() *TtestPointerList {
if in == nil {
return nil
}
out := new(TtestPointerList)
in.DeepCopyInto(out)
return out
}
func (in *TtestPointerList) DeepCopyObject() runtime.Object {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}
func (in *Ttest) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }
func (in *TtestList) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }
func (in *TtestPointerList) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }

View File

@@ -0,0 +1,37 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package slices
import (
"k8s.io/code-generator/cmd/defaulter-gen/output_tests/empty"
)
// Only test
type Ttest struct {
empty.TypeMeta
BoolField *bool
}
type TtestList struct {
empty.TypeMeta
Items []Ttest
}
type TtestPointerList struct {
empty.TypeMeta
Items []*Ttest
}

View File

@@ -0,0 +1,56 @@
//go:build !ignore_autogenerated
// +build !ignore_autogenerated
/*
Copyright The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Code generated by defaulter-gen. DO NOT EDIT.
package slices
import (
runtime "k8s.io/apimachinery/pkg/runtime"
)
// RegisterDefaults adds defaulters functions to the given scheme.
// Public to allow building arbitrary schemes.
// All generated defaulters are covering - they call all nested defaulters.
func RegisterDefaults(scheme *runtime.Scheme) error {
scheme.AddTypeDefaultingFunc(&Ttest{}, func(obj interface{}) { SetObjectDefaults_Ttest(obj.(*Ttest)) })
scheme.AddTypeDefaultingFunc(&TtestList{}, func(obj interface{}) { SetObjectDefaults_TtestList(obj.(*TtestList)) })
scheme.AddTypeDefaultingFunc(&TtestPointerList{}, func(obj interface{}) { SetObjectDefaults_TtestPointerList(obj.(*TtestPointerList)) })
return nil
}
func SetObjectDefaults_Ttest(in *Ttest) {
SetDefaults_Ttest(in)
}
func SetObjectDefaults_TtestList(in *TtestList) {
for i := range in.Items {
a := &in.Items[i]
SetObjectDefaults_Ttest(a)
}
}
func SetObjectDefaults_TtestPointerList(in *TtestPointerList) {
for i := range in.Items {
a := in.Items[i]
if a != nil {
SetObjectDefaults_Ttest(a)
}
}
}

View File

@@ -0,0 +1,33 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package wholepkg
import (
"k8s.io/apimachinery/pkg/runtime"
)
//nolint:unused
func addDefaultingFuncs(scheme *runtime.Scheme) error {
return RegisterDefaults(scheme)
}
func SetDefaults_StructPrimitives(obj *StructPrimitives) {
if obj.BoolField == nil {
obj.BoolField = new(bool)
*obj.BoolField = true
}
}

View File

@@ -0,0 +1,20 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// +k8s:defaulter-gen=TypeMeta
// This is a test package.
package wholepkg // import "k8s.io/code-generator/cmd/defaulter-gen/output_tests/wholepkg"

View File

@@ -0,0 +1,315 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package wholepkg
import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *StructEverything) DeepCopyInto(out *StructEverything) {
*out = *in
out.TypeMeta = in.TypeMeta
if in.BoolPtrField != nil {
in, out := &in.BoolPtrField, &out.BoolPtrField
*out = new(bool)
**out = **in
}
if in.IntPtrField != nil {
in, out := &in.IntPtrField, &out.IntPtrField
*out = new(int)
**out = **in
}
if in.StringPtrField != nil {
in, out := &in.StringPtrField, &out.StringPtrField
*out = new(string)
**out = **in
}
if in.FloatPtrField != nil {
in, out := &in.FloatPtrField, &out.FloatPtrField
*out = new(float64)
**out = **in
}
in.PointerStructField.DeepCopyInto(&out.PointerStructField)
if in.SliceBoolField != nil {
in, out := &in.SliceBoolField, &out.SliceBoolField
*out = make([]bool, len(*in))
copy(*out, *in)
}
if in.SliceByteField != nil {
in, out := &in.SliceByteField, &out.SliceByteField
*out = make([]byte, len(*in))
copy(*out, *in)
}
if in.SliceIntField != nil {
in, out := &in.SliceIntField, &out.SliceIntField
*out = make([]int, len(*in))
copy(*out, *in)
}
if in.SliceStringField != nil {
in, out := &in.SliceStringField, &out.SliceStringField
*out = make([]string, len(*in))
copy(*out, *in)
}
if in.SliceFloatField != nil {
in, out := &in.SliceFloatField, &out.SliceFloatField
*out = make([]float64, len(*in))
copy(*out, *in)
}
in.SlicesStructField.DeepCopyInto(&out.SlicesStructField)
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new StructEverything.
func (in *StructEverything) DeepCopy() *StructEverything {
if in == nil {
return nil
}
out := new(StructEverything)
in.DeepCopyInto(out)
return out
}
func (in *StructEverything) DeepCopyObject() runtime.Object {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *StructPointer) DeepCopyInto(out *StructPointer) {
*out = *in
out.TypeMeta = in.TypeMeta
in.PointerStructPrimitivesField.DeepCopyInto(&out.PointerStructPrimitivesField)
if in.PointerPointerStructPrimitivesField != nil {
in, out := &in.PointerPointerStructPrimitivesField, &out.PointerPointerStructPrimitivesField
*out = new(StructPrimitives)
(*in).DeepCopyInto(*out)
}
in.PointerStructPrimitivesAliasField.DeepCopyInto(&out.PointerStructPrimitivesAliasField)
in.PointerPointerStructPrimitivesAliasField.DeepCopyInto(&out.PointerPointerStructPrimitivesAliasField)
in.PointerStructStructPrimitives.DeepCopyInto(&out.PointerStructStructPrimitives)
if in.PointerPointerStructStructPrimitives != nil {
in, out := &in.PointerPointerStructStructPrimitives, &out.PointerPointerStructStructPrimitives
*out = new(StructStructPrimitives)
(*in).DeepCopyInto(*out)
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new StructPointer.
func (in *StructPointer) DeepCopy() *StructPointer {
if in == nil {
return nil
}
out := new(StructPointer)
in.DeepCopyInto(out)
return out
}
func (in *StructPointer) DeepCopyObject() runtime.Object {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *StructPrimitives) DeepCopyInto(out *StructPrimitives) {
*out = *in
out.TypeMeta = in.TypeMeta
if in.BoolField != nil {
in, out := &in.BoolField, &out.BoolField
*out = new(bool)
**out = **in
}
if in.IntField != nil {
in, out := &in.IntField, &out.IntField
*out = new(int)
**out = **in
}
if in.StringField != nil {
in, out := &in.StringField, &out.StringField
*out = new(string)
**out = **in
}
if in.FloatField != nil {
in, out := &in.FloatField, &out.FloatField
*out = new(float64)
**out = **in
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new StructPrimitives.
func (in *StructPrimitives) DeepCopy() *StructPrimitives {
if in == nil {
return nil
}
out := new(StructPrimitives)
in.DeepCopyInto(out)
return out
}
func (in *StructPrimitives) DeepCopyObject() runtime.Object {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *StructPrimitivesAlias) DeepCopyInto(out *StructPrimitivesAlias) {
*out = *in
out.TypeMeta = in.TypeMeta
if in.BoolField != nil {
in, out := &in.BoolField, &out.BoolField
*out = new(bool)
**out = **in
}
if in.IntField != nil {
in, out := &in.IntField, &out.IntField
*out = new(int)
**out = **in
}
if in.StringField != nil {
in, out := &in.StringField, &out.StringField
*out = new(string)
**out = **in
}
if in.FloatField != nil {
in, out := &in.FloatField, &out.FloatField
*out = new(float64)
**out = **in
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new StructPrimitivesAlias.
func (in *StructPrimitivesAlias) DeepCopy() *StructPrimitivesAlias {
if in == nil {
return nil
}
out := new(StructPrimitivesAlias)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *StructSlices) DeepCopyInto(out *StructSlices) {
*out = *in
out.TypeMeta = in.TypeMeta
if in.SliceStructPrimitivesField != nil {
in, out := &in.SliceStructPrimitivesField, &out.SliceStructPrimitivesField
*out = make([]StructPrimitives, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
if in.SlicePointerStructPrimitivesField != nil {
in, out := &in.SlicePointerStructPrimitivesField, &out.SlicePointerStructPrimitivesField
*out = make([]*StructPrimitives, len(*in))
for i := range *in {
if (*in)[i] != nil {
in, out := &(*in)[i], &(*out)[i]
*out = new(StructPrimitives)
(*in).DeepCopyInto(*out)
}
}
}
if in.SliceStructPrimitivesAliasField != nil {
in, out := &in.SliceStructPrimitivesAliasField, &out.SliceStructPrimitivesAliasField
*out = make([]StructPrimitivesAlias, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
if in.SlicePointerStructPrimitivesAliasField != nil {
in, out := &in.SlicePointerStructPrimitivesAliasField, &out.SlicePointerStructPrimitivesAliasField
*out = make([]*StructPrimitivesAlias, len(*in))
for i := range *in {
if (*in)[i] != nil {
in, out := &(*in)[i], &(*out)[i]
*out = new(StructPrimitivesAlias)
(*in).DeepCopyInto(*out)
}
}
}
if in.SliceStructStructPrimitives != nil {
in, out := &in.SliceStructStructPrimitives, &out.SliceStructStructPrimitives
*out = make([]StructStructPrimitives, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
if in.SlicePointerStructStructPrimitives != nil {
in, out := &in.SlicePointerStructStructPrimitives, &out.SlicePointerStructStructPrimitives
*out = make([]*StructStructPrimitives, len(*in))
for i := range *in {
if (*in)[i] != nil {
in, out := &(*in)[i], &(*out)[i]
*out = new(StructStructPrimitives)
(*in).DeepCopyInto(*out)
}
}
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new StructSlices.
func (in *StructSlices) DeepCopy() *StructSlices {
if in == nil {
return nil
}
out := new(StructSlices)
in.DeepCopyInto(out)
return out
}
func (in *StructSlices) DeepCopyObject() runtime.Object {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *StructStructPrimitives) DeepCopyInto(out *StructStructPrimitives) {
*out = *in
out.TypeMeta = in.TypeMeta
in.StructField.DeepCopyInto(&out.StructField)
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new StructStructPrimitives.
func (in *StructStructPrimitives) DeepCopy() *StructStructPrimitives {
if in == nil {
return nil
}
out := new(StructStructPrimitives)
in.DeepCopyInto(out)
return out
}
func (in *StructStructPrimitives) DeepCopyObject() runtime.Object {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}
func (in *StructEverything) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }
func (in *StructPointer) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }
func (in *StructPrimitives) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }
func (in *StructSlices) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }
func (in *StructStructPrimitives) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }

View File

@@ -0,0 +1,74 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package wholepkg
import (
"k8s.io/code-generator/cmd/defaulter-gen/output_tests/empty"
)
// Only primitives
type StructPrimitives struct {
empty.TypeMeta
BoolField *bool
IntField *int
StringField *string
FloatField *float64
}
type StructPrimitivesAlias StructPrimitives
type StructStructPrimitives struct {
empty.TypeMeta
StructField StructPrimitives
}
// Pointer
type StructPointer struct {
empty.TypeMeta
PointerStructPrimitivesField StructPrimitives
PointerPointerStructPrimitivesField *StructPrimitives
PointerStructPrimitivesAliasField StructPrimitivesAlias
PointerPointerStructPrimitivesAliasField StructPrimitivesAlias
PointerStructStructPrimitives StructStructPrimitives
PointerPointerStructStructPrimitives *StructStructPrimitives
}
// Slices
type StructSlices struct {
empty.TypeMeta
SliceStructPrimitivesField []StructPrimitives
SlicePointerStructPrimitivesField []*StructPrimitives
SliceStructPrimitivesAliasField []StructPrimitivesAlias
SlicePointerStructPrimitivesAliasField []*StructPrimitivesAlias
SliceStructStructPrimitives []StructStructPrimitives
SlicePointerStructStructPrimitives []*StructStructPrimitives
}
// Everything
type StructEverything struct {
empty.TypeMeta
BoolPtrField *bool
IntPtrField *int
StringPtrField *string
FloatPtrField *float64
PointerStructField StructPointer
SliceBoolField []bool
SliceByteField []byte
SliceIntField []int
SliceStringField []string
SliceFloatField []float64
SlicesStructField StructSlices
}

View File

@@ -0,0 +1,85 @@
//go:build !ignore_autogenerated
// +build !ignore_autogenerated
/*
Copyright The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Code generated by defaulter-gen. DO NOT EDIT.
package wholepkg
import (
runtime "k8s.io/apimachinery/pkg/runtime"
)
// RegisterDefaults adds defaulters functions to the given scheme.
// Public to allow building arbitrary schemes.
// All generated defaulters are covering - they call all nested defaulters.
func RegisterDefaults(scheme *runtime.Scheme) error {
scheme.AddTypeDefaultingFunc(&StructEverything{}, func(obj interface{}) { SetObjectDefaults_StructEverything(obj.(*StructEverything)) })
scheme.AddTypeDefaultingFunc(&StructPointer{}, func(obj interface{}) { SetObjectDefaults_StructPointer(obj.(*StructPointer)) })
scheme.AddTypeDefaultingFunc(&StructPrimitives{}, func(obj interface{}) { SetObjectDefaults_StructPrimitives(obj.(*StructPrimitives)) })
scheme.AddTypeDefaultingFunc(&StructSlices{}, func(obj interface{}) { SetObjectDefaults_StructSlices(obj.(*StructSlices)) })
scheme.AddTypeDefaultingFunc(&StructStructPrimitives{}, func(obj interface{}) { SetObjectDefaults_StructStructPrimitives(obj.(*StructStructPrimitives)) })
return nil
}
func SetObjectDefaults_StructEverything(in *StructEverything) {
SetObjectDefaults_StructPointer(&in.PointerStructField)
SetObjectDefaults_StructSlices(&in.SlicesStructField)
}
func SetObjectDefaults_StructPointer(in *StructPointer) {
SetObjectDefaults_StructPrimitives(&in.PointerStructPrimitivesField)
if in.PointerPointerStructPrimitivesField != nil {
SetObjectDefaults_StructPrimitives(in.PointerPointerStructPrimitivesField)
}
SetObjectDefaults_StructStructPrimitives(&in.PointerStructStructPrimitives)
if in.PointerPointerStructStructPrimitives != nil {
SetObjectDefaults_StructStructPrimitives(in.PointerPointerStructStructPrimitives)
}
}
func SetObjectDefaults_StructPrimitives(in *StructPrimitives) {
SetDefaults_StructPrimitives(in)
}
func SetObjectDefaults_StructSlices(in *StructSlices) {
for i := range in.SliceStructPrimitivesField {
a := &in.SliceStructPrimitivesField[i]
SetObjectDefaults_StructPrimitives(a)
}
for i := range in.SlicePointerStructPrimitivesField {
a := in.SlicePointerStructPrimitivesField[i]
if a != nil {
SetObjectDefaults_StructPrimitives(a)
}
}
for i := range in.SliceStructStructPrimitives {
a := &in.SliceStructStructPrimitives[i]
SetObjectDefaults_StructStructPrimitives(a)
}
for i := range in.SlicePointerStructStructPrimitives {
a := in.SlicePointerStructStructPrimitives[i]
if a != nil {
SetObjectDefaults_StructStructPrimitives(a)
}
}
}
func SetObjectDefaults_StructStructPrimitives(in *StructStructPrimitives) {
SetObjectDefaults_StructPrimitives(&in.StructField)
}