add prune and remove unused packages

This commit is contained in:
Michelle Au
2019-03-08 14:54:43 -08:00
parent f59b58d164
commit 8c0accad66
17240 changed files with 27 additions and 4750030 deletions

View File

@@ -1,11 +0,0 @@
{
"Rules": [
{
"SelectorRegexp": "k8s[.]io",
"AllowedPrefixes": [
"k8s.io/gengo",
"k8s.io/klog"
]
}
]
}

16
vendor/k8s.io/gengo/.travis.yml generated vendored
View File

@@ -1,16 +0,0 @@
language: go
go:
- "1.10"
- tip
go_import_path: k8s.io/gengo
jobs:
include:
- stage: Run tests
script:
- find . -name vendor -prune -o -name Makefile -execdir make test \;
- go test -v ./...
- stage: Verify examples
script:
- go run ./examples/import-boss/main.go -i k8s.io/gengo/... --verify-only

View File

@@ -1,9 +0,0 @@
# Contributing
Thanks for taking the time to join our community and start contributing!
The [Contributor Guide](https://github.com/kubernetes/community/blob/master/contributors/guide/README.md)
provides detailed instructions on how to get your ideas and bug fixes seen and accepted.
Please remember to sign the [CNCF CLA](https://github.com/kubernetes/community/blob/master/CLA.md) and
read and observe the [Code of Conduct](https://github.com/cncf/foundation/blob/master/code-of-conduct.md).

4
vendor/k8s.io/gengo/OWNERS generated vendored
View File

@@ -1,4 +0,0 @@
approvers:
- lavalamp
- wojtek-t
- sttts

56
vendor/k8s.io/gengo/README.md generated vendored
View File

@@ -1,56 +0,0 @@
# gengo
[![Travis Widget]][Travis] [![GoDoc Widget]][GoDoc] [![GoReport]][GoReportStatus]
[Travis]: https://travis-ci.org/kubernetes/gengo
[Travis Widget]: https://travis-ci.org/kubernetes/gengo.svg?branch=master
[GoDoc]: https://godoc.org/k8s.io/gengo
[GoDoc Widget]: https://godoc.org/k8s.io/gengo?status.svg
[GoReport]: https://goreportcard.com/badge/github.com/kubernetes/gengo
[GoReportStatus]: https://goreportcard.com/report/github.com/kubernetes/gengo
A package for generating things based on go files. This mechanism was first used
in Kubernetes and is split out here for ease of reuse and maintainability.
`go get k8s.io/gengo`
## Examples
A set generator, deep-copy generator, defaulter generator and go-to-protobuf
generator are included here. Also, import-boss will enforce arbitrary rules about
import trees.
## args/
Package args defines common arguments for a generator binary.
## generator/
Package generator defines interfaces for code generators to implement, and
machinery that will execute those code generators.
## types/
Package types contains the type system definition. It is modeled after Go's type
system, but it's intended that you could produce these types by parsing
something else, if you want to write the parser/converter.
We don't directly use the go types in the go typecheck library because they are
based on implementing differing interfaces. A struct-based format is more
convenient input for template driven output.
## parser/
Package parser parses go source files.
## namer/
Package namer defines a naming system, for:
* helping you reference go objects in a syntactically correct way
* keeping track of what you reference, for importing the right packages
* and defining parallel tracks of names, for making public interfaces and
private implementations.
## Contributing
Please see [CONTRIBUTING.md](CONTRIBUTING.md) for instructions on how to contribute.

View File

@@ -1,15 +0,0 @@
# Defined below are the security contacts for this repo.
#
# They are the contact point for the Product Security Team to reach out
# to for triaging and handling of incoming issues.
#
# The below names agree to abide by the
# [Embargo Policy](https://github.com/kubernetes/sig-release/blob/master/security-release-process-documentation/security-release-process.md#embargo-policy)
# and will be removed and replaced if they violate that agreement.
#
# DO NOT REPORT SECURITY VULNERABILITIES DIRECTLY TO THESE NAMES, FOLLOW THE
# INSTRUCTIONS AT https://kubernetes.io/security/
lavalamp
wojtek-t
sttts

View File

@@ -1,35 +0,0 @@
/*
Copyright 2015 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 args
import (
"testing"
"k8s.io/gengo/types"
)
func TestInputIncludes(t *testing.T) {
a := &GeneratorArgs{
InputDirs: []string{"a/b/..."},
}
if !a.InputIncludes(&types.Package{Path: "a/b/c"}) {
t.Errorf("Expected /... syntax to work")
}
if a.InputIncludes(&types.Package{Path: "a/c/b"}) {
t.Errorf("Expected correctness")
}
}

View File

@@ -1,16 +0,0 @@
/*
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.
*/

View File

@@ -1,3 +0,0 @@
# Kubernetes Community Code of Conduct
Please refer to our [Kubernetes Community Code of Conduct](https://git.k8s.io/community/code-of-conduct.md)

View File

@@ -1 +0,0 @@
deepcopy-gen

View File

@@ -1,16 +0,0 @@
TOOL=deepcopy-gen
test:
@if ! git diff --quiet HEAD; then \
echo "FAIL: git client is not clean"; \
false; \
fi
@go build -o /tmp/$(TOOL)
@PKGS=$$(go list ./output_tests/... | paste -sd' ' -); \
/tmp/$(TOOL) --logtostderr --v=4 -i $$(echo $$PKGS | sed 's/ /,/g') -O zz_generated
@if ! git diff --quiet HEAD; then \
echo "FAIL: output files changed"; \
git diff; \
false; \
fi

View File

@@ -1,749 +0,0 @@
/*
Copyright 2016 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 generators
import (
"reflect"
"testing"
"k8s.io/gengo/types"
)
func Test_isRootedUnder(t *testing.T) {
testCases := []struct {
path string
roots []string
expect bool
}{
{
path: "/foo/bar",
roots: nil,
expect: false,
},
{
path: "/foo/bar",
roots: []string{},
expect: false,
},
{
path: "/foo/bar",
roots: []string{
"/bad",
},
expect: false,
},
{
path: "/foo/bar",
roots: []string{
"/foo",
},
expect: true,
},
{
path: "/foo/bar",
roots: []string{
"/bad",
"/foo",
},
expect: true,
},
{
path: "/foo/bar/qux/zorb",
roots: []string{
"/foo/bar/qux",
},
expect: true,
},
{
path: "/foo/bar",
roots: []string{
"/foo/bar",
},
expect: true,
},
{
path: "/foo/barn",
roots: []string{
"/foo/bar",
},
expect: false,
},
{
path: "/foo/bar",
roots: []string{
"/foo/barn",
},
expect: false,
},
{
path: "/foo/bar",
roots: []string{
"",
},
expect: true,
},
}
for i, tc := range testCases {
r := isRootedUnder(tc.path, tc.roots)
if r != tc.expect {
t.Errorf("case[%d]: expected %t, got %t for %q in %q", i, tc.expect, r, tc.path, tc.roots)
}
}
}
func Test_deepCopyMethod(t *testing.T) {
testCases := []struct {
typ types.Type
expect bool
error bool
}{
{
typ: types.Type{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
// No DeepCopy method.
Methods: map[string]*types.Type{},
},
expect: false,
},
{
typ: types.Type{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
Methods: map[string]*types.Type{
// No DeepCopy method.
"method": {
Name: types.Name{Package: "pkgname", Name: "func()"},
Kind: types.Func,
Signature: &types.Signature{
Receiver: &types.Type{
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
Parameters: []*types.Type{},
Results: []*types.Type{},
},
},
},
},
expect: false,
},
{
typ: types.Type{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
Methods: map[string]*types.Type{
// Wrong signature (no result).
"DeepCopy": {
Name: types.Name{Package: "pkgname", Name: "func()"},
Kind: types.Func,
Signature: &types.Signature{
Receiver: &types.Type{
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
Parameters: []*types.Type{},
Results: []*types.Type{},
},
},
},
},
expect: false,
error: true,
},
{
typ: types.Type{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
Methods: map[string]*types.Type{
// Wrong signature (wrong result).
"DeepCopy": {
Name: types.Name{Package: "pkgname", Name: "func() int"},
Kind: types.Func,
Signature: &types.Signature{
Receiver: &types.Type{
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
Parameters: []*types.Type{},
Results: []*types.Type{
{
Name: types.Name{Name: "int"},
Kind: types.Builtin,
},
},
},
},
},
},
expect: false,
error: true,
},
{
typ: types.Type{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
Methods: map[string]*types.Type{
// Wrong signature with pointer receiver, but non-pointer result.
"DeepCopy": {
Name: types.Name{Package: "pkgname", Name: "func() pkgname.typename"},
Kind: types.Func,
Signature: &types.Signature{
Receiver: &types.Type{
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
Parameters: []*types.Type{},
Results: []*types.Type{
{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
},
},
},
},
},
},
expect: false,
error: true,
},
{
typ: types.Type{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
Methods: map[string]*types.Type{
// Wrong signature with non-pointer receiver, but pointer result.
"DeepCopy": {
Name: types.Name{Package: "pkgname", Name: "func() pkgname.typename"},
Kind: types.Func,
Signature: &types.Signature{
Receiver: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
Parameters: []*types.Type{},
Results: []*types.Type{
{
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
},
},
},
},
},
expect: false,
error: true,
},
{
typ: types.Type{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
Methods: map[string]*types.Type{
// Correct signature with non-pointer receiver.
"DeepCopy": {
Name: types.Name{Package: "pkgname", Name: "func() pkgname.typename"},
Kind: types.Func,
Signature: &types.Signature{
Receiver: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
Parameters: []*types.Type{},
Results: []*types.Type{
{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
},
},
},
},
},
},
expect: true,
},
{
typ: types.Type{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
Methods: map[string]*types.Type{
// Correct signature with pointer receiver.
"DeepCopy": {
Name: types.Name{Package: "pkgname", Name: "func() pkgname.typename"},
Kind: types.Func,
Signature: &types.Signature{
Receiver: &types.Type{
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
Parameters: []*types.Type{},
Results: []*types.Type{
{
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
},
},
},
},
},
expect: true,
},
{
typ: types.Type{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
Methods: map[string]*types.Type{
// Wrong signature (has params).
"DeepCopy": {
Name: types.Name{Package: "pkgname", Name: "func(int) pkgname.typename"},
Kind: types.Func,
Signature: &types.Signature{
Receiver: &types.Type{
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
Parameters: []*types.Type{
{
Name: types.Name{Name: "int"},
Kind: types.Builtin,
},
},
Results: []*types.Type{
{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
},
},
},
},
},
},
expect: false,
error: true,
},
{
typ: types.Type{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
Methods: map[string]*types.Type{
// Wrong signature (extra results).
"DeepCopy": {
Name: types.Name{Package: "pkgname", Name: "func() (pkgname.typename, int)"},
Kind: types.Func,
Signature: &types.Signature{
Receiver: &types.Type{
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
Parameters: []*types.Type{},
Results: []*types.Type{
{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
},
{
Name: types.Name{Name: "int"},
Kind: types.Builtin,
},
},
},
},
},
},
expect: false,
error: true,
},
}
for i, tc := range testCases {
r, err := deepCopyMethod(&tc.typ)
if tc.error && err == nil {
t.Errorf("case[%d]: expected an error, got none", i)
} else if !tc.error && err != nil {
t.Errorf("case[%d]: expected no error, got: %v", i, err)
} else if !tc.error && (r != nil) != tc.expect {
t.Errorf("case[%d]: expected result %v, got: %v", i, tc.expect, r)
}
}
}
func Test_deepCopyIntoMethod(t *testing.T) {
testCases := []struct {
typ types.Type
expect bool
error bool
}{
{
typ: types.Type{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
// No DeepCopyInto method.
Methods: map[string]*types.Type{},
},
expect: false,
},
{
typ: types.Type{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
Methods: map[string]*types.Type{
// No DeepCopyInto method.
"method": {
Name: types.Name{Package: "pkgname", Name: "func()"},
Kind: types.Func,
Signature: &types.Signature{
Receiver: &types.Type{
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
Parameters: []*types.Type{},
Results: []*types.Type{},
},
},
},
},
expect: false,
},
{
typ: types.Type{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
Methods: map[string]*types.Type{
// Wrong signature (no parameter).
"DeepCopyInto": {
Name: types.Name{Package: "pkgname", Name: "func()"},
Kind: types.Func,
Signature: &types.Signature{
Receiver: &types.Type{
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
Parameters: []*types.Type{},
Results: []*types.Type{},
},
},
},
},
expect: false,
error: true,
},
{
typ: types.Type{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
Methods: map[string]*types.Type{
// Wrong signature (unexpected result).
"DeepCopyInto": {
Name: types.Name{Package: "pkgname", Name: "func(*pkgname.typename) int"},
Kind: types.Func,
Signature: &types.Signature{
Receiver: &types.Type{
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
Parameters: []*types.Type{
{
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
},
Results: []*types.Type{
{
Name: types.Name{Name: "int"},
Kind: types.Builtin,
},
},
},
},
},
},
expect: false,
error: true,
},
{
typ: types.Type{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
Methods: map[string]*types.Type{
// Wrong signature (non-pointer parameter, pointer receiver).
"DeepCopyInto": {
Name: types.Name{Package: "pkgname", Name: "func(pkgname.typename)"},
Kind: types.Func,
Signature: &types.Signature{
Receiver: &types.Type{
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
Parameters: []*types.Type{
{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
Results: []*types.Type{},
},
},
},
},
expect: false,
error: true,
},
{
typ: types.Type{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
Methods: map[string]*types.Type{
// Wrong signature (non-pointer parameter, non-pointer receiver).
"DeepCopyInto": {
Name: types.Name{Package: "pkgname", Name: "func(pkgname.typename)"},
Kind: types.Func,
Signature: &types.Signature{
Receiver: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
Parameters: []*types.Type{
{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
Results: []*types.Type{},
},
},
},
},
expect: false,
error: true,
},
{
typ: types.Type{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
Methods: map[string]*types.Type{
// Correct signature with non-pointer receiver.
"DeepCopyInto": {
Name: types.Name{Package: "pkgname", Name: "func(*pkgname.typename)"},
Kind: types.Func,
Signature: &types.Signature{
Receiver: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
Parameters: []*types.Type{
{
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
},
Results: []*types.Type{},
},
},
},
},
expect: true,
},
{
typ: types.Type{
Name: types.Name{Package: "pkgname", Name: "typename"},
Kind: types.Builtin,
Methods: map[string]*types.Type{
// Correct signature with pointer receiver.
"DeepCopyInto": {
Name: types.Name{Package: "pkgname", Name: "func(*pkgname.typename)"},
Kind: types.Func,
Signature: &types.Signature{
Receiver: &types.Type{
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
Parameters: []*types.Type{
{
Kind: types.Pointer,
Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
},
},
Results: []*types.Type{},
},
},
},
},
expect: true,
},
}
for i, tc := range testCases {
r, err := deepCopyIntoMethod(&tc.typ)
if tc.error && err == nil {
t.Errorf("case[%d]: expected an error, got none", i)
} else if !tc.error && err != nil {
t.Errorf("case[%d]: expected no error, got: %v", i, err)
} else if !tc.error && (r != nil) != tc.expect {
t.Errorf("case[%d]: expected result %v, got: %v", i, tc.expect, r)
}
}
}
func Test_extractTagParams(t *testing.T) {
testCases := []struct {
comments []string
expect *enabledTagValue
}{
{
comments: []string{
"Human comment",
},
expect: nil,
},
{
comments: []string{
"Human comment",
"+k8s:deepcopy-gen",
},
expect: &enabledTagValue{
value: "",
register: false,
},
},
{
comments: []string{
"Human comment",
"+k8s:deepcopy-gen=package",
},
expect: &enabledTagValue{
value: "package",
register: false,
},
},
{
comments: []string{
"Human comment",
"+k8s:deepcopy-gen=package,register",
},
expect: &enabledTagValue{
value: "package",
register: true,
},
},
{
comments: []string{
"Human comment",
"+k8s:deepcopy-gen=package,register=true",
},
expect: &enabledTagValue{
value: "package",
register: true,
},
},
{
comments: []string{
"Human comment",
"+k8s:deepcopy-gen=package,register=false",
},
expect: &enabledTagValue{
value: "package",
register: false,
},
},
}
for i, tc := range testCases {
r := extractEnabledTag(tc.comments)
if r == nil && tc.expect != nil {
t.Errorf("case[%d]: expected non-nil", i)
}
if r != nil && tc.expect == nil {
t.Errorf("case[%d]: expected nil, got %v", i, *r)
}
if r != nil && *r != *tc.expect {
t.Errorf("case[%d]: expected %v, got %v", i, *tc.expect, *r)
}
}
}
func Test_extractInterfacesTag(t *testing.T) {
testCases := []struct {
comments, secondComments []string
expect []string
}{
{
comments: []string{},
expect: nil,
},
{
comments: []string{
"+k8s:deepcopy-gen:interfaces=k8s.io/kubernetes/runtime.Object",
},
expect: []string{
"k8s.io/kubernetes/runtime.Object",
},
},
{
comments: []string{
"+k8s:deepcopy-gen:interfaces=k8s.io/kubernetes/runtime.Object",
"+k8s:deepcopy-gen:interfaces=k8s.io/kubernetes/runtime.List",
},
expect: []string{
"k8s.io/kubernetes/runtime.Object",
"k8s.io/kubernetes/runtime.List",
},
},
{
comments: []string{
"+k8s:deepcopy-gen:interfaces=k8s.io/kubernetes/runtime.Object",
"+k8s:deepcopy-gen:interfaces=k8s.io/kubernetes/runtime.Object",
},
expect: []string{
"k8s.io/kubernetes/runtime.Object",
"k8s.io/kubernetes/runtime.Object",
},
},
{
secondComments: []string{
"+k8s:deepcopy-gen:interfaces=k8s.io/kubernetes/runtime.Object",
},
expect: []string{
"k8s.io/kubernetes/runtime.Object",
},
},
{
comments: []string{
"+k8s:deepcopy-gen:interfaces=k8s.io/kubernetes/runtime.Object",
},
secondComments: []string{
"+k8s:deepcopy-gen:interfaces=k8s.io/kubernetes/runtime.List",
},
expect: []string{
"k8s.io/kubernetes/runtime.List",
"k8s.io/kubernetes/runtime.Object",
},
},
{
comments: []string{
"+k8s:deepcopy-gen:interfaces=k8s.io/kubernetes/runtime.Object",
},
secondComments: []string{
"+k8s:deepcopy-gen:interfaces=k8s.io/kubernetes/runtime.Object",
},
expect: []string{
"k8s.io/kubernetes/runtime.Object",
"k8s.io/kubernetes/runtime.Object",
},
},
}
for i, tc := range testCases {
typ := &types.Type{
CommentLines: tc.comments,
SecondClosestCommentLines: tc.secondComments,
}
r := extractInterfacesTag(typ)
if r == nil && tc.expect != nil {
t.Errorf("case[%d]: expected non-nil", i)
}
if r != nil && tc.expect == nil {
t.Errorf("case[%d]: expected nil, got %v", i, r)
}
if r != nil && !reflect.DeepEqual(r, tc.expect) {
t.Errorf("case[%d]: expected %v, got %v", i, tc.expect, r)
}
}
}

View File

@@ -1,88 +0,0 @@
/*
Copyright 2015 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.
*/
// deepcopy-gen is a tool for auto-generating DeepCopy functions.
//
// Given a list of input directories, it will generate DeepCopy and DeepCopyInto
// methods that efficiently perform a full deep-copy of each type. If these
// already exist (are predefined by the developer), they are used instead of
// generating new ones.
//
// If interfaces are referenced in types, it is expected that corresponding
// DeepCopyInterfaceName methods exist, e.g. DeepCopyObject for runtime.Object.
// These can be predefined by the developer or generated through tags, see below.
// They must be added to the interfaces themselves manually, e.g.
// type Object interface {
// ...
// DeepCopyObject() Object
// }
//
// All generation is governed by comment tags in the source. Any package may
// request DeepCopy generation by including a comment in the file-comments of
// a doc.go file, of the form:
// // +k8s:deepcopy-gen=package
//
// DeepCopy functions can be generated for individual types, rather than the
// entire package by specifying a comment on the type definion of the form:
// // +k8s:deepcopy-gen=true
//
// When generating for a whole package, individual types may opt out of
// DeepCopy generation by specifying a comment on the type definition of the form:
// // +k8s:deepcopy-gen=false
//
// Additional DeepCopyInterfaceName methods can be generated by specifying a
// comment on the type definition of the form:
// // +k8s:deepcopy-gen:interfaces=k8s.io/kubernetes/runtime.Object,k8s.io/kubernetes/runtime.List
// This leads to the generation of DeepCopyObject and DeepCopyList with the given
// interfaces as return types. We say that the tagged type implements deepcopy for the
// interfaces.
//
// The deepcopy funcs for interfaces using "+k8s:deepcopy-gen:interfaces" use the pointer
// of the type as receiver. For those special cases where the non-pointer object should
// implement the interface, this can be done with:
// // +k8s:deepcopy-gen:nonpointer-interfaces=true
package main
import (
"k8s.io/gengo/args"
"k8s.io/gengo/examples/deepcopy-gen/generators"
"github.com/spf13/pflag"
"k8s.io/klog"
)
func main() {
arguments := args.Default()
// Override defaults.
arguments.OutputFileBaseName = "deepcopy_generated"
// Custom args.
customArgs := &generators.CustomArgs{}
pflag.CommandLine.StringSliceVar(&customArgs.BoundingDirs, "bounding-dirs", customArgs.BoundingDirs,
"Comma-separated list of import paths which bound the types for which deep-copies will be generated.")
arguments.CustomArgs = customArgs
// Run it.
if err := arguments.Execute(
generators.NameSystems(),
generators.DefaultNameSystem(),
generators.Packages,
); err != nil {
klog.Fatalf("Error: %v", err)
}
klog.V(2).Info("Completed successfully.")
}

View File

@@ -1,89 +0,0 @@
/*
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:deepcopy-gen=package
// This is a test package.
package aliases
// Note: the following AliasInterface and AliasAliasInterface +k8s:deepcopy-gen:interfaces tags
// are necessary because Golang flattens interface alias in the type system. I.e. an alias J of
// an interface I is actually equivalent to I. So support deepcopies of those aliases, we have
// to implement all aliases of that interface.
// +k8s:deepcopy-gen:interfaces=k8s.io/gengo/examples/deepcopy-gen/output_tests/aliases.Interface
// +k8s:deepcopy-gen:interfaces=k8s.io/gengo/examples/deepcopy-gen/output_tests/aliases.AliasInterface
// +k8s:deepcopy-gen:interfaces=k8s.io/gengo/examples/deepcopy-gen/output_tests/aliases.AliasAliasInterface
type Foo struct {
X int
}
type Interface interface {
DeepCopyInterface() Interface
DeepCopyAliasInterface() AliasInterface
DeepCopyAliasAliasInterface() AliasAliasInterface
}
type Builtin int
type Slice []int
type Pointer *int
type PointerAlias *Builtin
type Struct Foo
type Map map[string]int
type FooAlias Foo
type FooSlice []Foo
type FooPointer *Foo
type FooMap map[string]Foo
type AliasBuiltin Builtin
type AliasSlice Slice
type AliasPointer Pointer
type AliasStruct Struct
type AliasMap Map
type AliasInterface Interface
type AliasAliasInterface AliasInterface
type AliasInterfaceMap map[string]AliasInterface
type AliasInterfaceSlice []AliasInterface
// Aliases
type Ttest struct {
Builtin Builtin
Slice Slice
Pointer Pointer
PointerAlias PointerAlias
Struct Struct
Map Map
SliceSlice []Slice
MapSlice map[string]Slice
FooAlias FooAlias
FooSlice FooSlice
FooPointer FooPointer
FooMap FooMap
AliasBuiltin AliasBuiltin
AliasSlice AliasSlice
AliasPointer AliasPointer
AliasStruct AliasStruct
AliasMap AliasMap
AliasInterface AliasInterface
AliasAliasInterface AliasAliasInterface
AliasInterfaceMap AliasInterfaceMap
AliasInterfaceSlice AliasInterfaceSlice
}

View File

@@ -1,412 +0,0 @@
// +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 deepcopy-gen. DO NOT EDIT.
package aliases
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in AliasInterfaceMap) DeepCopyInto(out *AliasInterfaceMap) {
{
in := &in
*out = make(AliasInterfaceMap, len(*in))
for key, val := range *in {
if val == nil {
(*out)[key] = nil
} else {
(*out)[key] = val.DeepCopyAliasInterface()
}
}
return
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new AliasInterfaceMap.
func (in AliasInterfaceMap) DeepCopy() AliasInterfaceMap {
if in == nil {
return nil
}
out := new(AliasInterfaceMap)
in.DeepCopyInto(out)
return *out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in AliasInterfaceSlice) DeepCopyInto(out *AliasInterfaceSlice) {
{
in := &in
*out = make(AliasInterfaceSlice, len(*in))
for i := range *in {
if (*in)[i] != nil {
(*out)[i] = (*in)[i].DeepCopyAliasInterface()
}
}
return
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new AliasInterfaceSlice.
func (in AliasInterfaceSlice) DeepCopy() AliasInterfaceSlice {
if in == nil {
return nil
}
out := new(AliasInterfaceSlice)
in.DeepCopyInto(out)
return *out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in AliasMap) DeepCopyInto(out *AliasMap) {
{
in := &in
*out = make(AliasMap, len(*in))
for key, val := range *in {
(*out)[key] = val
}
return
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new AliasMap.
func (in AliasMap) DeepCopy() AliasMap {
if in == nil {
return nil
}
out := new(AliasMap)
in.DeepCopyInto(out)
return *out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in AliasSlice) DeepCopyInto(out *AliasSlice) {
{
in := &in
*out = make(AliasSlice, len(*in))
copy(*out, *in)
return
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new AliasSlice.
func (in AliasSlice) DeepCopy() AliasSlice {
if in == nil {
return nil
}
out := new(AliasSlice)
in.DeepCopyInto(out)
return *out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *AliasStruct) DeepCopyInto(out *AliasStruct) {
*out = *in
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new AliasStruct.
func (in *AliasStruct) DeepCopy() *AliasStruct {
if in == nil {
return nil
}
out := new(AliasStruct)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Foo) DeepCopyInto(out *Foo) {
*out = *in
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Foo.
func (in *Foo) DeepCopy() *Foo {
if in == nil {
return nil
}
out := new(Foo)
in.DeepCopyInto(out)
return out
}
// DeepCopyAliasAliasInterface is an autogenerated deepcopy function, copying the receiver, creating a new AliasAliasInterface.
func (in *Foo) DeepCopyAliasAliasInterface() AliasAliasInterface {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}
// DeepCopyAliasInterface is an autogenerated deepcopy function, copying the receiver, creating a new AliasInterface.
func (in *Foo) DeepCopyAliasInterface() AliasInterface {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}
// DeepCopyInterface is an autogenerated deepcopy function, copying the receiver, creating a new Interface.
func (in *Foo) DeepCopyInterface() Interface {
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 *FooAlias) DeepCopyInto(out *FooAlias) {
*out = *in
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new FooAlias.
func (in *FooAlias) DeepCopy() *FooAlias {
if in == nil {
return nil
}
out := new(FooAlias)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in FooMap) DeepCopyInto(out *FooMap) {
{
in := &in
*out = make(FooMap, len(*in))
for key, val := range *in {
(*out)[key] = val
}
return
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new FooMap.
func (in FooMap) DeepCopy() FooMap {
if in == nil {
return nil
}
out := new(FooMap)
in.DeepCopyInto(out)
return *out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in FooSlice) DeepCopyInto(out *FooSlice) {
{
in := &in
*out = make(FooSlice, len(*in))
copy(*out, *in)
return
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new FooSlice.
func (in FooSlice) DeepCopy() FooSlice {
if in == nil {
return nil
}
out := new(FooSlice)
in.DeepCopyInto(out)
return *out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in Map) DeepCopyInto(out *Map) {
{
in := &in
*out = make(Map, len(*in))
for key, val := range *in {
(*out)[key] = val
}
return
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Map.
func (in Map) DeepCopy() Map {
if in == nil {
return nil
}
out := new(Map)
in.DeepCopyInto(out)
return *out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in Slice) DeepCopyInto(out *Slice) {
{
in := &in
*out = make(Slice, len(*in))
copy(*out, *in)
return
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Slice.
func (in Slice) DeepCopy() Slice {
if in == nil {
return nil
}
out := new(Slice)
in.DeepCopyInto(out)
return *out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Struct) DeepCopyInto(out *Struct) {
*out = *in
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Struct.
func (in *Struct) DeepCopy() *Struct {
if in == nil {
return nil
}
out := new(Struct)
in.DeepCopyInto(out)
return out
}
// 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
if in.Slice != nil {
in, out := &in.Slice, &out.Slice
*out = make(Slice, len(*in))
copy(*out, *in)
}
if in.Pointer != nil {
in, out := &in.Pointer, &out.Pointer
*out = new(int)
**out = **in
}
if in.PointerAlias != nil {
in, out := &in.PointerAlias, &out.PointerAlias
*out = new(Builtin)
**out = **in
}
out.Struct = in.Struct
if in.Map != nil {
in, out := &in.Map, &out.Map
*out = make(Map, len(*in))
for key, val := range *in {
(*out)[key] = val
}
}
if in.SliceSlice != nil {
in, out := &in.SliceSlice, &out.SliceSlice
*out = make([]Slice, len(*in))
for i := range *in {
if (*in)[i] != nil {
in, out := &(*in)[i], &(*out)[i]
*out = make(Slice, len(*in))
copy(*out, *in)
}
}
}
if in.MapSlice != nil {
in, out := &in.MapSlice, &out.MapSlice
*out = make(map[string]Slice, len(*in))
for key, val := range *in {
var outVal []int
if val == nil {
(*out)[key] = nil
} else {
in, out := &val, &outVal
*out = make(Slice, len(*in))
copy(*out, *in)
}
(*out)[key] = outVal
}
}
out.FooAlias = in.FooAlias
if in.FooSlice != nil {
in, out := &in.FooSlice, &out.FooSlice
*out = make(FooSlice, len(*in))
copy(*out, *in)
}
if in.FooPointer != nil {
in, out := &in.FooPointer, &out.FooPointer
*out = new(Foo)
**out = **in
}
if in.FooMap != nil {
in, out := &in.FooMap, &out.FooMap
*out = make(FooMap, len(*in))
for key, val := range *in {
(*out)[key] = val
}
}
if in.AliasSlice != nil {
in, out := &in.AliasSlice, &out.AliasSlice
*out = make(AliasSlice, len(*in))
copy(*out, *in)
}
if in.AliasPointer != nil {
in, out := &in.AliasPointer, &out.AliasPointer
*out = new(int)
**out = **in
}
out.AliasStruct = in.AliasStruct
if in.AliasMap != nil {
in, out := &in.AliasMap, &out.AliasMap
*out = make(AliasMap, len(*in))
for key, val := range *in {
(*out)[key] = val
}
}
if in.AliasInterface != nil {
out.AliasInterface = in.AliasInterface.DeepCopyAliasInterface()
}
if in.AliasAliasInterface != nil {
out.AliasAliasInterface = in.AliasAliasInterface.DeepCopyAliasAliasInterface()
}
if in.AliasInterfaceMap != nil {
in, out := &in.AliasInterfaceMap, &out.AliasInterfaceMap
*out = make(AliasInterfaceMap, len(*in))
for key, val := range *in {
if val == nil {
(*out)[key] = nil
} else {
(*out)[key] = val.DeepCopyAliasInterface()
}
}
}
if in.AliasInterfaceSlice != nil {
in, out := &in.AliasInterfaceSlice, &out.AliasInterfaceSlice
*out = make(AliasInterfaceSlice, len(*in))
for i := range *in {
if (*in)[i] != nil {
(*out)[i] = (*in)[i].DeepCopyAliasInterface()
}
}
}
return
}
// 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
}

View File

@@ -1,35 +0,0 @@
/*
Copyright 2016 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:deepcopy-gen=package
// This is a test package.
package builtins
type Ttest struct {
Byte byte
//Int8 int8 //TODO: int8 becomes byte in SnippetWriter
Int16 int16
Int32 int32
Int64 int64
Uint8 uint8
Uint16 uint16
Uint32 uint32
Uint64 uint64
Float32 float32
Float64 float64
String string
}

View File

@@ -1,37 +0,0 @@
// +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 deepcopy-gen. DO NOT EDIT.
package builtins
// 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
return
}
// 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
}

View File

@@ -1,131 +0,0 @@
/*
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 output_tests
import (
"github.com/google/gofuzz"
"k8s.io/gengo/examples/deepcopy-gen/output_tests/aliases"
"k8s.io/gengo/examples/deepcopy-gen/output_tests/interfaces"
)
// interfaceFuzzers contains fuzzer that set all interface to nil because our
// JSON deepcopy does not work with it.
// TODO: test also interface deepcopy
var interfaceFuzzers = []interface{}{
func(s *aliases.AliasAliasInterface, c fuzz.Continue) {
if c.RandBool() {
*s = nil
} else {
*s = &aliasAliasInterfaceInstance{X: c.Int()}
}
},
func(s *aliases.AliasInterface, c fuzz.Continue) {
if c.RandBool() {
*s = nil
} else {
*s = &aliasAliasInterfaceInstance{X: c.Int()}
}
},
func(s *aliases.Interface, c fuzz.Continue) {
if c.RandBool() {
*s = nil
} else {
*s = &aliasAliasInterfaceInstance{X: c.Int()}
}
},
func(s *aliases.AliasInterfaceMap, c fuzz.Continue) {
if c.RandBool() {
*s = nil
} else {
*s = make(aliases.AliasInterfaceMap)
for i := 0; i < c.Intn(3); i++ {
if c.RandBool() {
(*s)[c.RandString()] = nil
} else {
(*s)[c.RandString()] = &aliasAliasInterfaceInstance{X: c.Int()}
}
}
}
},
func(s *aliases.AliasInterfaceSlice, c fuzz.Continue) {
if c.RandBool() {
*s = nil
} else {
*s = make(aliases.AliasInterfaceSlice, 0, 0)
for i := 0; i < c.Intn(3); i++ {
if c.RandBool() {
*s = append(*s, nil)
} else {
*s = append(*s, &aliasAliasInterfaceInstance{X: c.Int()})
}
}
}
},
func(s *interfaces.Inner, c fuzz.Continue) {
if c.RandBool() {
*s = nil
} else {
*s = &interfacesInnerInstance{X: c.Float64()}
}
},
}
type aliasAliasInterfaceInstance struct {
X int
}
func (i *aliasAliasInterfaceInstance) DeepCopyInterface() aliases.Interface {
if i == nil {
return nil
}
return &aliasAliasInterfaceInstance{X: i.X}
}
func (i *aliasAliasInterfaceInstance) DeepCopyAliasInterface() aliases.AliasInterface {
if i == nil {
return nil
}
return &aliasAliasInterfaceInstance{X: i.X}
}
func (i *aliasAliasInterfaceInstance) DeepCopyAliasAliasInterface() aliases.AliasAliasInterface {
if i == nil {
return nil
}
return &aliasAliasInterfaceInstance{X: i.X}
}
type interfacesInnerInstance struct {
X float64
}
func (i *interfacesInnerInstance) DeepCopyInner() interfaces.Inner {
if i == nil {
return nil
}
return &interfacesInnerInstance{X: i.X}
}
func (i *interfacesInnerInstance) Function() float64 {
return i.X
}

View File

@@ -1,29 +0,0 @@
/*
Copyright 2017 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:deepcopy-gen=package
// This is a test package.
package interfaces
type Inner interface {
Function() float64
DeepCopyInner() Inner
}
type Ttest struct {
I []Inner
}

View File

@@ -1,46 +0,0 @@
// +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 deepcopy-gen. DO NOT EDIT.
package interfaces
// 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
if in.I != nil {
in, out := &in.I, &out.I
*out = make([]Inner, len(*in))
for i := range *in {
if (*in)[i] != nil {
(*out)[i] = (*in)[i].DeepCopyInner()
}
}
}
return
}
// 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
}

View File

@@ -1,43 +0,0 @@
/*
Copyright 2016 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:deepcopy-gen=package
// This is a test package.
package maps
type Ttest struct {
Byte map[string]byte
//Int8 map[string]int8 //TODO: int8 becomes byte in SnippetWriter
Int16 map[string]int16
Int32 map[string]int32
Int64 map[string]int64
Uint8 map[string]uint8
Uint16 map[string]uint16
Uint32 map[string]uint32
Uint64 map[string]uint64
Float32 map[string]float32
Float64 map[string]float64
String map[string]string
StringPtr map[string]*string
StringPtrPtr map[string]**string
Map map[string]map[string]string
MapPtr map[string]*map[string]string
Slice map[string][]string
SlicePtr map[string]*[]string
Struct map[string]Ttest
StructPtr map[string]*Ttest
}

View File

@@ -1,242 +0,0 @@
// +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 deepcopy-gen. DO NOT EDIT.
package maps
// 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
if in.Byte != nil {
in, out := &in.Byte, &out.Byte
*out = make(map[string]byte, len(*in))
for key, val := range *in {
(*out)[key] = val
}
}
if in.Int16 != nil {
in, out := &in.Int16, &out.Int16
*out = make(map[string]int16, len(*in))
for key, val := range *in {
(*out)[key] = val
}
}
if in.Int32 != nil {
in, out := &in.Int32, &out.Int32
*out = make(map[string]int32, len(*in))
for key, val := range *in {
(*out)[key] = val
}
}
if in.Int64 != nil {
in, out := &in.Int64, &out.Int64
*out = make(map[string]int64, len(*in))
for key, val := range *in {
(*out)[key] = val
}
}
if in.Uint8 != nil {
in, out := &in.Uint8, &out.Uint8
*out = make(map[string]byte, len(*in))
for key, val := range *in {
(*out)[key] = val
}
}
if in.Uint16 != nil {
in, out := &in.Uint16, &out.Uint16
*out = make(map[string]uint16, len(*in))
for key, val := range *in {
(*out)[key] = val
}
}
if in.Uint32 != nil {
in, out := &in.Uint32, &out.Uint32
*out = make(map[string]uint32, len(*in))
for key, val := range *in {
(*out)[key] = val
}
}
if in.Uint64 != nil {
in, out := &in.Uint64, &out.Uint64
*out = make(map[string]uint64, len(*in))
for key, val := range *in {
(*out)[key] = val
}
}
if in.Float32 != nil {
in, out := &in.Float32, &out.Float32
*out = make(map[string]float32, len(*in))
for key, val := range *in {
(*out)[key] = val
}
}
if in.Float64 != nil {
in, out := &in.Float64, &out.Float64
*out = make(map[string]float64, len(*in))
for key, val := range *in {
(*out)[key] = val
}
}
if in.String != nil {
in, out := &in.String, &out.String
*out = make(map[string]string, len(*in))
for key, val := range *in {
(*out)[key] = val
}
}
if in.StringPtr != nil {
in, out := &in.StringPtr, &out.StringPtr
*out = make(map[string]*string, len(*in))
for key, val := range *in {
var outVal *string
if val == nil {
(*out)[key] = nil
} else {
in, out := &val, &outVal
*out = new(string)
**out = **in
}
(*out)[key] = outVal
}
}
if in.StringPtrPtr != nil {
in, out := &in.StringPtrPtr, &out.StringPtrPtr
*out = make(map[string]**string, len(*in))
for key, val := range *in {
var outVal **string
if val == nil {
(*out)[key] = nil
} else {
in, out := &val, &outVal
*out = new(*string)
if **in != nil {
in, out := *in, *out
*out = new(string)
**out = **in
}
}
(*out)[key] = outVal
}
}
if in.Map != nil {
in, out := &in.Map, &out.Map
*out = make(map[string]map[string]string, len(*in))
for key, val := range *in {
var outVal map[string]string
if val == nil {
(*out)[key] = nil
} else {
in, out := &val, &outVal
*out = make(map[string]string, len(*in))
for key, val := range *in {
(*out)[key] = val
}
}
(*out)[key] = outVal
}
}
if in.MapPtr != nil {
in, out := &in.MapPtr, &out.MapPtr
*out = make(map[string]*map[string]string, len(*in))
for key, val := range *in {
var outVal *map[string]string
if val == nil {
(*out)[key] = nil
} else {
in, out := &val, &outVal
*out = new(map[string]string)
if **in != nil {
in, out := *in, *out
*out = make(map[string]string, len(*in))
for key, val := range *in {
(*out)[key] = val
}
}
}
(*out)[key] = outVal
}
}
if in.Slice != nil {
in, out := &in.Slice, &out.Slice
*out = make(map[string][]string, len(*in))
for key, val := range *in {
var outVal []string
if val == nil {
(*out)[key] = nil
} else {
in, out := &val, &outVal
*out = make([]string, len(*in))
copy(*out, *in)
}
(*out)[key] = outVal
}
}
if in.SlicePtr != nil {
in, out := &in.SlicePtr, &out.SlicePtr
*out = make(map[string]*[]string, len(*in))
for key, val := range *in {
var outVal *[]string
if val == nil {
(*out)[key] = nil
} else {
in, out := &val, &outVal
*out = new([]string)
if **in != nil {
in, out := *in, *out
*out = make([]string, len(*in))
copy(*out, *in)
}
}
(*out)[key] = outVal
}
}
if in.Struct != nil {
in, out := &in.Struct, &out.Struct
*out = make(map[string]Ttest, len(*in))
for key, val := range *in {
(*out)[key] = *val.DeepCopy()
}
}
if in.StructPtr != nil {
in, out := &in.StructPtr, &out.StructPtr
*out = make(map[string]*Ttest, len(*in))
for key, val := range *in {
var outVal *Ttest
if val == nil {
(*out)[key] = nil
} else {
in, out := &val, &outVal
*out = new(Ttest)
(*in).DeepCopyInto(*out)
}
(*out)[key] = outVal
}
}
return
}
// 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
}

View File

@@ -1,25 +0,0 @@
/*
Copyright 2017 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 otherpkg
type Object interface {
DeepCopyObject() Object
}
type List interface {
DeepCopyList() List
}

View File

@@ -1,157 +0,0 @@
package output_tests
import (
"fmt"
"reflect"
"testing"
"github.com/davecgh/go-spew/spew"
"github.com/google/gofuzz"
"k8s.io/gengo/examples/deepcopy-gen/output_tests/aliases"
"k8s.io/gengo/examples/deepcopy-gen/output_tests/builtins"
"k8s.io/gengo/examples/deepcopy-gen/output_tests/interfaces"
"k8s.io/gengo/examples/deepcopy-gen/output_tests/maps"
"k8s.io/gengo/examples/deepcopy-gen/output_tests/pointer"
"k8s.io/gengo/examples/deepcopy-gen/output_tests/slices"
"k8s.io/gengo/examples/deepcopy-gen/output_tests/structs"
)
func TestWithValueFuzzer(t *testing.T) {
tests := []interface{}{
aliases.Ttest{},
builtins.Ttest{},
interfaces.Ttest{},
maps.Ttest{},
pointer.Ttest{},
slices.Ttest{},
structs.Ttest{},
}
fuzzer := fuzz.New()
fuzzer.NilChance(0.5)
fuzzer.NumElements(0, 2)
fuzzer.Funcs(interfaceFuzzers...)
for _, test := range tests {
t.Run(fmt.Sprintf("%T", test), func(t *testing.T) {
N := 1000
for i := 0; i < N; i++ {
original := reflect.New(reflect.TypeOf(test)).Interface()
fuzzer.Fuzz(original)
reflectCopy := ReflectDeepCopy(original)
if !reflect.DeepEqual(original, reflectCopy) {
t.Errorf("original and reflectCopy are different:\n\n original = %s\n\n jsonCopy = %s", spew.Sdump(original), spew.Sdump(reflectCopy))
}
deepCopy := reflect.ValueOf(original).MethodByName("DeepCopy").Call(nil)[0].Interface()
if !reflect.DeepEqual(original, deepCopy) {
t.Fatalf("original and deepCopy are different:\n\n original = %s\n\n deepCopy() = %s", spew.Sdump(original), spew.Sdump(deepCopy))
}
ValueFuzz(original)
if !reflect.DeepEqual(reflectCopy, deepCopy) {
t.Fatalf("reflectCopy and deepCopy are different:\n\n origin = %s\n\n jsonCopy() = %s", spew.Sdump(original), spew.Sdump(deepCopy))
}
}
})
}
}
func BenchmarkReflectDeepCopy(b *testing.B) {
fourtytwo := "fourtytwo"
fourtytwoPtr := &fourtytwo
var nilMap map[string]string
var nilSlice []string
mapPtr := &map[string]string{"0": "fourtytwo", "1": "fourtytwo"}
slicePtr := &[]string{"fourtytwo", "fourtytwo", "fourtytwo"}
structPtr := &pointer.Ttest{
Builtin: &fourtytwo,
Ptr: &fourtytwoPtr,
}
tests := []interface{}{
maps.Ttest{
Byte: map[string]byte{"0": 42, "1": 42, "3": 42},
Int16: map[string]int16{"0": 42, "1": 42, "3": 42},
Int32: map[string]int32{"0": 42, "1": 42, "3": 42},
Int64: map[string]int64{"0": 42, "1": 42, "3": 42},
Uint8: map[string]uint8{"0": 42, "1": 42, "3": 42},
Uint16: map[string]uint16{"0": 42, "1": 42, "3": 42},
Uint32: map[string]uint32{"0": 42, "1": 42, "3": 42},
Uint64: map[string]uint64{"0": 42, "1": 42, "3": 42},
Float32: map[string]float32{"0": 42.0, "1": 42.0, "3": 42.0},
Float64: map[string]float64{"0": 42, "1": 42, "3": 42},
String: map[string]string{"0": "fourtytwo", "1": "fourtytwo", "3": "fourtytwo"},
StringPtr: map[string]*string{"0": &fourtytwo, "1": &fourtytwo, "3": &fourtytwo},
StringPtrPtr: map[string]**string{"0": &fourtytwoPtr, "1": &fourtytwoPtr, "3": &fourtytwoPtr},
Map: map[string]map[string]string{"0": nil, "1": {"a": fourtytwo, "b": fourtytwo}, "3": {}},
MapPtr: map[string]*map[string]string{"0": nil, "1": {"a": fourtytwo, "b": fourtytwo}, "3": &nilMap},
Slice: map[string][]string{"0": nil, "1": {"a", "b"}, "2": {}},
SlicePtr: map[string]*[]string{"0": nil, "1": {"a", "b"}, "2": &nilSlice},
Struct: map[string]maps.Ttest{"0": {}, "1": {Byte: map[string]byte{"0": 42, "1": 42, "3": 42}}},
StructPtr: map[string]*maps.Ttest{"0": nil, "1": {}, "2": {Byte: map[string]byte{"0": 42, "1": 42, "3": 42}}},
},
slices.Ttest{
Byte: []byte{42, 42, 42},
Int16: []int16{42, 42, 42},
Int32: []int32{42, 42, 42},
Int64: []int64{42, 42, 42},
Uint8: []uint8{42, 42, 42},
Uint16: []uint16{42, 42, 42},
Uint32: []uint32{42, 42, 42},
Uint64: []uint64{42, 42, 42},
Float32: []float32{42.0, 42.0, 42.0},
Float64: []float64{42, 42, 42},
String: []string{"fourtytwo", "fourtytwo", "fourtytwo"},
StringPtr: []*string{&fourtytwo, &fourtytwo, &fourtytwo},
StringPtrPtr: []**string{&fourtytwoPtr, &fourtytwoPtr, &fourtytwoPtr},
Map: []map[string]string{nil, {"a": fourtytwo, "b": fourtytwo}, {}},
MapPtr: []*map[string]string{nil, {"a": fourtytwo, "b": fourtytwo}, &nilMap},
Slice: [][]string{nil, {"a", "b"}, {}},
SlicePtr: []*[]string{nil, {"a", "b"}, &nilSlice},
Struct: []slices.Ttest{{}, {Byte: []byte{42, 42, 42}}},
StructPtr: []*slices.Ttest{nil, {}, {Byte: []byte{42, 42, 42}}},
},
pointer.Ttest{
Builtin: &fourtytwo,
Ptr: &fourtytwoPtr,
Map: &map[string]string{"0": "fourtytwo", "1": "fourtytwo"},
Slice: &[]string{"fourtytwo", "fourtytwo", "fourtytwo"},
MapPtr: &mapPtr,
SlicePtr: &slicePtr,
Struct: &pointer.Ttest{
Builtin: &fourtytwo,
Ptr: &fourtytwoPtr,
},
StructPtr: &structPtr,
},
}
fuzzer := fuzz.New()
fuzzer.NilChance(0.5)
fuzzer.NumElements(0, 2)
fuzzer.Funcs(interfaceFuzzers...)
for _, test := range tests {
b.Run(fmt.Sprintf("%T", test), func(b *testing.B) {
for i := 0; i < b.N; i++ {
switch t := test.(type) {
case maps.Ttest:
t.DeepCopy()
case slices.Ttest:
t.DeepCopy()
case pointer.Ttest:
t.DeepCopy()
default:
b.Fatalf("missing type case in switch for %T", t)
}
}
})
}
}

View File

@@ -1,31 +0,0 @@
/*
Copyright 2017 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:deepcopy-gen=package
// This is a test package.
package pointer
type Ttest struct {
Builtin *string
Ptr **string
Map *map[string]string
Slice *[]string
MapPtr **map[string]string
SlicePtr **[]string
Struct *Ttest
StructPtr **Ttest
}

View File

@@ -1,113 +0,0 @@
// +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 deepcopy-gen. DO NOT EDIT.
package pointer
// 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
if in.Builtin != nil {
in, out := &in.Builtin, &out.Builtin
*out = new(string)
**out = **in
}
if in.Ptr != nil {
in, out := &in.Ptr, &out.Ptr
*out = new(*string)
if **in != nil {
in, out := *in, *out
*out = new(string)
**out = **in
}
}
if in.Map != nil {
in, out := &in.Map, &out.Map
*out = new(map[string]string)
if **in != nil {
in, out := *in, *out
*out = make(map[string]string, len(*in))
for key, val := range *in {
(*out)[key] = val
}
}
}
if in.Slice != nil {
in, out := &in.Slice, &out.Slice
*out = new([]string)
if **in != nil {
in, out := *in, *out
*out = make([]string, len(*in))
copy(*out, *in)
}
}
if in.MapPtr != nil {
in, out := &in.MapPtr, &out.MapPtr
*out = new(*map[string]string)
if **in != nil {
in, out := *in, *out
*out = new(map[string]string)
if **in != nil {
in, out := *in, *out
*out = make(map[string]string, len(*in))
for key, val := range *in {
(*out)[key] = val
}
}
}
}
if in.SlicePtr != nil {
in, out := &in.SlicePtr, &out.SlicePtr
*out = new(*[]string)
if **in != nil {
in, out := *in, *out
*out = new([]string)
if **in != nil {
in, out := *in, *out
*out = make([]string, len(*in))
copy(*out, *in)
}
}
}
if in.Struct != nil {
in, out := &in.Struct, &out.Struct
*out = new(Ttest)
(*in).DeepCopyInto(*out)
}
if in.StructPtr != nil {
in, out := &in.StructPtr, &out.StructPtr
*out = new(*Ttest)
if **in != nil {
in, out := *in, *out
*out = new(Ttest)
(*in).DeepCopyInto(*out)
}
}
return
}
// 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
}

View File

@@ -1,81 +0,0 @@
/*
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 output_tests
import (
"fmt"
"reflect"
)
// ReflectDeepCopy deep copies the object using reflection.
func ReflectDeepCopy(in interface{}) interface{} {
return reflectDeepCopy(reflect.ValueOf(in)).Interface()
}
func reflectDeepCopy(src reflect.Value) reflect.Value {
switch src.Kind() {
case reflect.Interface, reflect.Ptr, reflect.Map, reflect.Slice:
if src.IsNil() {
return src
}
}
switch src.Kind() {
case reflect.Chan, reflect.Func, reflect.UnsafePointer, reflect.Uintptr:
panic(fmt.Sprintf("cannot deep copy kind: %s", src.Kind()))
case reflect.Array:
dst := reflect.New(src.Type())
for i := 0; i < src.Len(); i++ {
dst.Elem().Index(i).Set(reflectDeepCopy(src.Index(i)))
}
return dst.Elem()
case reflect.Interface:
return reflectDeepCopy(src.Elem())
case reflect.Map:
dst := reflect.MakeMap(src.Type())
for _, k := range src.MapKeys() {
dst.SetMapIndex(k, reflectDeepCopy(src.MapIndex(k)))
}
return dst
case reflect.Ptr:
dst := reflect.New(src.Type().Elem())
dst.Elem().Set(reflectDeepCopy(src.Elem()))
return dst
case reflect.Slice:
dst := reflect.MakeSlice(src.Type(), 0, src.Len())
for i := 0; i < src.Len(); i++ {
dst = reflect.Append(dst, reflectDeepCopy(src.Index(i)))
}
return dst
case reflect.Struct:
dst := reflect.New(src.Type())
for i := 0; i < src.NumField(); i++ {
if !dst.Elem().Field(i).CanSet() {
// Can't set private fields. At this point, the
// best we can do is a shallow copy. For
// example, time.Time is a value type with
// private members that can be shallow copied.
return src
}
dst.Elem().Field(i).Set(reflectDeepCopy(src.Field(i)))
}
return dst.Elem()
default:
// Value types like numbers, booleans, and strings.
return src
}
}

View File

@@ -1,43 +0,0 @@
/*
Copyright 2016 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:deepcopy-gen=package
// This is a test package.
package slices
type Ttest struct {
Byte []byte
//Int8 []int8 //TODO: int8 becomes byte in SnippetWriter
Int16 []int16
Int32 []int32
Int64 []int64
Uint8 []uint8
Uint16 []uint16
Uint32 []uint32
Uint64 []uint64
Float32 []float32
Float64 []float64
String []string
StringPtr []*string
StringPtrPtr []**string
Map []map[string]string
MapPtr []*map[string]string
Slice [][]string
SlicePtr []*[]string
Struct []Ttest
StructPtr []*Ttest
}

View File

@@ -1,192 +0,0 @@
// +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 deepcopy-gen. DO NOT EDIT.
package slices
// 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
if in.Byte != nil {
in, out := &in.Byte, &out.Byte
*out = make([]byte, len(*in))
copy(*out, *in)
}
if in.Int16 != nil {
in, out := &in.Int16, &out.Int16
*out = make([]int16, len(*in))
copy(*out, *in)
}
if in.Int32 != nil {
in, out := &in.Int32, &out.Int32
*out = make([]int32, len(*in))
copy(*out, *in)
}
if in.Int64 != nil {
in, out := &in.Int64, &out.Int64
*out = make([]int64, len(*in))
copy(*out, *in)
}
if in.Uint8 != nil {
in, out := &in.Uint8, &out.Uint8
*out = make([]byte, len(*in))
copy(*out, *in)
}
if in.Uint16 != nil {
in, out := &in.Uint16, &out.Uint16
*out = make([]uint16, len(*in))
copy(*out, *in)
}
if in.Uint32 != nil {
in, out := &in.Uint32, &out.Uint32
*out = make([]uint32, len(*in))
copy(*out, *in)
}
if in.Uint64 != nil {
in, out := &in.Uint64, &out.Uint64
*out = make([]uint64, len(*in))
copy(*out, *in)
}
if in.Float32 != nil {
in, out := &in.Float32, &out.Float32
*out = make([]float32, len(*in))
copy(*out, *in)
}
if in.Float64 != nil {
in, out := &in.Float64, &out.Float64
*out = make([]float64, len(*in))
copy(*out, *in)
}
if in.String != nil {
in, out := &in.String, &out.String
*out = make([]string, len(*in))
copy(*out, *in)
}
if in.StringPtr != nil {
in, out := &in.StringPtr, &out.StringPtr
*out = make([]*string, len(*in))
for i := range *in {
if (*in)[i] != nil {
in, out := &(*in)[i], &(*out)[i]
*out = new(string)
**out = **in
}
}
}
if in.StringPtrPtr != nil {
in, out := &in.StringPtrPtr, &out.StringPtrPtr
*out = make([]**string, len(*in))
for i := range *in {
if (*in)[i] != nil {
in, out := &(*in)[i], &(*out)[i]
*out = new(*string)
if **in != nil {
in, out := *in, *out
*out = new(string)
**out = **in
}
}
}
}
if in.Map != nil {
in, out := &in.Map, &out.Map
*out = make([]map[string]string, len(*in))
for i := range *in {
if (*in)[i] != nil {
in, out := &(*in)[i], &(*out)[i]
*out = make(map[string]string, len(*in))
for key, val := range *in {
(*out)[key] = val
}
}
}
}
if in.MapPtr != nil {
in, out := &in.MapPtr, &out.MapPtr
*out = make([]*map[string]string, len(*in))
for i := range *in {
if (*in)[i] != nil {
in, out := &(*in)[i], &(*out)[i]
*out = new(map[string]string)
if **in != nil {
in, out := *in, *out
*out = make(map[string]string, len(*in))
for key, val := range *in {
(*out)[key] = val
}
}
}
}
}
if in.Slice != nil {
in, out := &in.Slice, &out.Slice
*out = make([][]string, len(*in))
for i := range *in {
if (*in)[i] != nil {
in, out := &(*in)[i], &(*out)[i]
*out = make([]string, len(*in))
copy(*out, *in)
}
}
}
if in.SlicePtr != nil {
in, out := &in.SlicePtr, &out.SlicePtr
*out = make([]*[]string, len(*in))
for i := range *in {
if (*in)[i] != nil {
in, out := &(*in)[i], &(*out)[i]
*out = new([]string)
if **in != nil {
in, out := *in, *out
*out = make([]string, len(*in))
copy(*out, *in)
}
}
}
}
if in.Struct != nil {
in, out := &in.Struct, &out.Struct
*out = make([]Ttest, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
if in.StructPtr != nil {
in, out := &in.StructPtr, &out.StructPtr
*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)
}
}
}
return
}
// 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
}

View File

@@ -1,40 +0,0 @@
/*
Copyright 2016 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:deepcopy-gen=package
// This is a test package.
package structs
type Inner struct {
Byte byte
//Int8 int8 //TODO: int8 becomes byte in SnippetWriter
Int16 int16
Int32 int32
Int64 int64
Uint8 uint8
Uint16 uint16
Uint32 uint32
Uint64 uint64
Float32 float32
Float64 float64
String string
}
type Ttest struct {
Inner1 Inner
Inner2 Inner
}

View File

@@ -1,55 +0,0 @@
// +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 deepcopy-gen. DO NOT EDIT.
package structs
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Inner) DeepCopyInto(out *Inner) {
*out = *in
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Inner.
func (in *Inner) DeepCopy() *Inner {
if in == nil {
return nil
}
out := new(Inner)
in.DeepCopyInto(out)
return out
}
// 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.Inner1 = in.Inner1
out.Inner2 = in.Inner2
return
}
// 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
}

View File

@@ -1,86 +0,0 @@
/*
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 output_tests
import (
"reflect"
)
// ValueFuzz recursively changes all basic type values in an object. Any kind of references will not
// be touched, i.e. the addresses of slices, maps, pointers will stay unchanged.
func ValueFuzz(obj interface{}) {
valueFuzz(reflect.ValueOf(obj))
}
func valueFuzz(obj reflect.Value) {
switch obj.Kind() {
case reflect.Array:
for i := 0; i < obj.Len(); i++ {
valueFuzz(obj.Index(i))
}
case reflect.Slice:
if obj.IsNil() {
// TODO: set non-nil value
} else {
for i := 0; i < obj.Len(); i++ {
valueFuzz(obj.Index(i))
}
}
case reflect.Interface, reflect.Ptr:
if obj.IsNil() {
// TODO: set non-nil value
} else {
valueFuzz(obj.Elem())
}
case reflect.Struct:
for i, n := 0, obj.NumField(); i < n; i++ {
valueFuzz(obj.Field(i))
}
case reflect.Map:
if obj.IsNil() {
// TODO: set non-nil value
} else {
for _, k := range obj.MapKeys() {
// map values are not addressable. We need a copy.
v := obj.MapIndex(k)
copy := reflect.New(v.Type())
copy.Elem().Set(v)
valueFuzz(copy.Elem())
obj.SetMapIndex(k, copy.Elem())
}
// TODO: set some new value
}
case reflect.Func: // ignore, we don't have function types in our API
default:
if !obj.CanSet() {
return
}
switch obj.Kind() {
case reflect.String:
obj.SetString(obj.String() + "x")
case reflect.Bool:
obj.SetBool(!obj.Bool())
case reflect.Float32, reflect.Float64:
obj.SetFloat(obj.Float()*2.0 + 1.0)
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
obj.SetInt(obj.Int() + 1)
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
obj.SetUint(obj.Uint() + 1)
default:
}
}
}

View File

@@ -1,171 +0,0 @@
/*
Copyright 2016 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/gengo/examples/deepcopy-gen/output_tests/otherpkg"
// Trivial
type Struct_Empty struct{}
// Only primitives
type Struct_Primitives struct {
BoolField bool
IntField int
StringField string
FloatField float64
}
type Struct_Primitives_Alias Struct_Primitives
type Struct_Embed_Struct_Primitives struct {
Struct_Primitives
}
type Struct_Embed_Int struct {
int
}
type Struct_Struct_Primitives struct {
StructField Struct_Primitives
}
// Manual DeepCopy method
type ManualStruct struct {
StringField string
}
func (m ManualStruct) DeepCopy() ManualStruct {
return m
}
type ManualStruct_Alias ManualStruct
type Struct_Embed_ManualStruct struct {
ManualStruct
}
// Only pointers to primitives
type Struct_PrimitivePointers struct {
BoolPtrField *bool
IntPtrField *int
StringPtrField *string
FloatPtrField *float64
}
type Struct_PrimitivePointers_Alias Struct_PrimitivePointers
type Struct_Embed_Struct_PrimitivePointers struct {
Struct_PrimitivePointers
}
type Struct_Embed_Pointer struct {
*int
}
type Struct_Struct_PrimitivePointers struct {
StructField Struct_PrimitivePointers
}
// Manual DeepCopy method
type ManualSlice []string
func (m ManualSlice) DeepCopy() ManualSlice {
r := make(ManualSlice, len(m))
copy(r, m)
return r
}
// Slices
type Struct_Slices struct {
SliceBoolField []bool
SliceByteField []byte
SliceIntField []int
SliceStringField []string
SliceFloatField []float64
SliceStructPrimitivesField []Struct_Primitives
SliceStructPrimitivesAliasField []Struct_Primitives_Alias
SliceStructPrimitivePointersField []Struct_PrimitivePointers
SliceStructPrimitivePointersAliasField []Struct_PrimitivePointers_Alias
SliceSliceIntField [][]int
SliceManualStructField []ManualStruct
ManualSliceField ManualSlice
}
type Struct_Slices_Alias Struct_Slices
type Struct_Embed_Struct_Slices struct {
Struct_Slices
}
type Struct_Struct_Slices struct {
StructField Struct_Slices
}
// Everything
type Struct_Everything struct {
BoolField bool
IntField int
StringField string
FloatField float64
StructField Struct_Primitives
EmptyStructField Struct_Empty
ManualStructField ManualStruct
ManualStructAliasField ManualStruct_Alias
BoolPtrField *bool
IntPtrField *int
StringPtrField *string
FloatPtrField *float64
PrimitivePointersField Struct_PrimitivePointers
ManualStructPtrField *ManualStruct
ManualStructAliasPtrField *ManualStruct_Alias
SliceBoolField []bool
SliceByteField []byte
SliceIntField []int
SliceStringField []string
SliceFloatField []float64
SlicesField Struct_Slices
SliceManualStructField []ManualStruct
ManualSliceField ManualSlice
}
// An Object
// +k8s:deepcopy-gen:interfaces=k8s.io/gengo/examples/deepcopy-gen/output_tests/otherpkg.Object
type Struct_ExplicitObject struct {
x int
}
// An Object which is used a non-pointer
// +k8s:deepcopy-gen:interfaces=k8s.io/gengo/examples/deepcopy-gen/output_tests/otherpkg.Object
// +k8s:deepcopy-gen:nonpointer-interfaces=true
type Struct_NonPointerExplicitObject struct {
x int
}
// +k8s:deepcopy-gen=false
type Struct_TypeMeta struct {
}
// +k8s:deepcopy-gen:interfaces=k8s.io/gengo/examples/deepcopy-gen/output_tests/otherpkg.Object
// +k8s:deepcopy-gen:interfaces=k8s.io/gengo/examples/deepcopy-gen/output_tests/otherpkg.List
type Struct_ObjectAndList struct {
}
// +k8s:deepcopy-gen:interfaces=k8s.io/gengo/examples/deepcopy-gen/output_tests/otherpkg.Object
// +k8s:deepcopy-gen:interfaces=k8s.io/gengo/examples/deepcopy-gen/output_tests/otherpkg.Object
type Struct_ObjectAndObject struct {
}
// +k8s:deepcopy-gen:interfaces=k8s.io/gengo/examples/deepcopy-gen/output_tests/wholepkg.Selector
// +k8s:deepcopy-gen:interfaces=k8s.io/gengo/examples/deepcopy-gen/output_tests/otherpkg.Object
type Struct_ExplicitSelectorExplicitObject struct {
Struct_TypeMeta
}
type Struct_Interfaces struct {
ObjectField otherpkg.Object
NilObjectField otherpkg.Object
SelectorField Selector
}

View File

@@ -1,20 +0,0 @@
/*
Copyright 2016 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
// Another type in another file.
type Struct_B struct{}

View File

@@ -1,145 +0,0 @@
/*
Copyright 2016 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 (
"reflect"
"testing"
fuzz "github.com/google/gofuzz"
)
func TestDeepCopyPrimitives(t *testing.T) {
x := Struct_Primitives{}
y := Struct_Primitives{}
if !reflect.DeepEqual(&x, &y) {
t.Errorf("objects should be equal to start, but are not")
}
fuzzer := fuzz.New()
fuzzer.Fuzz(&x)
fuzzer.Fuzz(&y)
if reflect.DeepEqual(&x, &y) {
t.Errorf("objects should not be equal, but are")
}
x.DeepCopyInto(&y)
if !reflect.DeepEqual(&x, &y) {
t.Errorf("objects should be equal, but are not")
}
}
func TestDeepCopyInterfaceFields(t *testing.T) {
x := Struct_Interfaces{}
y := Struct_Interfaces{}
if !reflect.DeepEqual(&x, &y) {
t.Errorf("objects should be equal to start, but are not")
}
fuzzer := fuzz.New()
obj := Struct_ExplicitObject{}
fuzzer.Fuzz(&obj)
x.ObjectField = &obj
sel := Struct_ExplicitSelectorExplicitObject{}
fuzzer.Fuzz(&sel)
x.SelectorField = &sel
if reflect.DeepEqual(&x, &y) {
t.Errorf("objects should not be equal, but are")
}
x.DeepCopyInto(&y)
if !reflect.DeepEqual(&x, &y) {
t.Errorf("objects should be equal, but are not")
}
}
func TestNilCopy(t *testing.T) {
var x *Struct_B
y := x.DeepCopy()
if y != nil {
t.Errorf("Expected nil as deepcopy of nil, got %+v", y)
}
}
func assertMethod(t *testing.T, typ reflect.Type, name string) {
if _, found := typ.MethodByName(name); !found {
t.Errorf("Struct_ExplicitObject must have %v method", name)
}
}
func assertNotMethod(t *testing.T, typ reflect.Type, name string) {
if _, found := typ.MethodByName(name); found {
t.Errorf("%v must not have %v method", typ, name)
}
}
func TestInterfaceTypes(t *testing.T) {
explicitObject := reflect.TypeOf(&Struct_ExplicitObject{})
assertMethod(t, explicitObject, "DeepCopyObject")
typeMeta := reflect.TypeOf(&Struct_TypeMeta{})
assertNotMethod(t, typeMeta, "DeepCopy")
objectAndList := reflect.TypeOf(&Struct_ObjectAndList{})
assertMethod(t, objectAndList, "DeepCopyObject")
assertMethod(t, objectAndList, "DeepCopyList")
objectAndObject := reflect.TypeOf(&Struct_ObjectAndObject{})
assertMethod(t, objectAndObject, "DeepCopyObject")
explicitSelectorExplicitObject := reflect.TypeOf(&Struct_ExplicitSelectorExplicitObject{})
assertMethod(t, explicitSelectorExplicitObject, "DeepCopySelector")
assertMethod(t, explicitSelectorExplicitObject, "DeepCopyObject")
}
func TestInterfaceDeepCopy(t *testing.T) {
x := Struct_ExplicitObject{}
fuzzer := fuzz.New()
fuzzer.Fuzz(&x)
y_obj := x.DeepCopyObject()
y, ok := y_obj.(*Struct_ExplicitObject)
if !ok {
t.Fatalf("epxected Struct_ExplicitObject from Struct_ExplicitObject.DeepCopyObject, got: %t", y_obj)
}
if !reflect.DeepEqual(y, &x) {
t.Error("objects should be equal, but are not")
}
}
func TestInterfaceNonPointerDeepCopy(t *testing.T) {
x := Struct_NonPointerExplicitObject{}
fuzzer := fuzz.New()
fuzzer.Fuzz(&x)
y_obj := x.DeepCopyObject()
y, ok := y_obj.(Struct_NonPointerExplicitObject)
if !ok {
t.Fatalf("epxected Struct_NonPointerExplicitObject from Struct_NonPointerExplicitObject.DeepCopyObject, got: %t", y_obj)
}
if !reflect.DeepEqual(y, x) {
t.Error("objects should be equal, but are not")
}
}

View File

@@ -1,20 +0,0 @@
/*
Copyright 2016 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:deepcopy-gen=package
// This is a test package.
package wholepkg

View File

@@ -1,21 +0,0 @@
/*
Copyright 2017 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
type Selector interface {
DeepCopySelector() Selector
}

View File

@@ -1,760 +0,0 @@
// +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 deepcopy-gen. DO NOT EDIT.
package wholepkg
import (
otherpkg "k8s.io/gengo/examples/deepcopy-gen/output_tests/otherpkg"
)
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in ManualSlice) DeepCopyInto(out *ManualSlice) {
{
in := &in
*out = in.DeepCopy()
return
}
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *ManualStruct) DeepCopyInto(out *ManualStruct) {
*out = in.DeepCopy()
return
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *ManualStruct_Alias) DeepCopyInto(out *ManualStruct_Alias) {
*out = *in
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ManualStruct_Alias.
func (in *ManualStruct_Alias) DeepCopy() *ManualStruct_Alias {
if in == nil {
return nil
}
out := new(ManualStruct_Alias)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Struct_B) DeepCopyInto(out *Struct_B) {
*out = *in
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Struct_B.
func (in *Struct_B) DeepCopy() *Struct_B {
if in == nil {
return nil
}
out := new(Struct_B)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Struct_Embed_Int) DeepCopyInto(out *Struct_Embed_Int) {
*out = *in
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Struct_Embed_Int.
func (in *Struct_Embed_Int) DeepCopy() *Struct_Embed_Int {
if in == nil {
return nil
}
out := new(Struct_Embed_Int)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Struct_Embed_ManualStruct) DeepCopyInto(out *Struct_Embed_ManualStruct) {
*out = *in
out.ManualStruct = in.ManualStruct.DeepCopy()
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Struct_Embed_ManualStruct.
func (in *Struct_Embed_ManualStruct) DeepCopy() *Struct_Embed_ManualStruct {
if in == nil {
return nil
}
out := new(Struct_Embed_ManualStruct)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Struct_Embed_Pointer) DeepCopyInto(out *Struct_Embed_Pointer) {
*out = *in
if in.int != nil {
in, out := &in.int, &out.int
*out = new(int)
**out = **in
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Struct_Embed_Pointer.
func (in *Struct_Embed_Pointer) DeepCopy() *Struct_Embed_Pointer {
if in == nil {
return nil
}
out := new(Struct_Embed_Pointer)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Struct_Embed_Struct_PrimitivePointers) DeepCopyInto(out *Struct_Embed_Struct_PrimitivePointers) {
*out = *in
in.Struct_PrimitivePointers.DeepCopyInto(&out.Struct_PrimitivePointers)
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Struct_Embed_Struct_PrimitivePointers.
func (in *Struct_Embed_Struct_PrimitivePointers) DeepCopy() *Struct_Embed_Struct_PrimitivePointers {
if in == nil {
return nil
}
out := new(Struct_Embed_Struct_PrimitivePointers)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Struct_Embed_Struct_Primitives) DeepCopyInto(out *Struct_Embed_Struct_Primitives) {
*out = *in
out.Struct_Primitives = in.Struct_Primitives
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Struct_Embed_Struct_Primitives.
func (in *Struct_Embed_Struct_Primitives) DeepCopy() *Struct_Embed_Struct_Primitives {
if in == nil {
return nil
}
out := new(Struct_Embed_Struct_Primitives)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Struct_Embed_Struct_Slices) DeepCopyInto(out *Struct_Embed_Struct_Slices) {
*out = *in
in.Struct_Slices.DeepCopyInto(&out.Struct_Slices)
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Struct_Embed_Struct_Slices.
func (in *Struct_Embed_Struct_Slices) DeepCopy() *Struct_Embed_Struct_Slices {
if in == nil {
return nil
}
out := new(Struct_Embed_Struct_Slices)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Struct_Empty) DeepCopyInto(out *Struct_Empty) {
*out = *in
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Struct_Empty.
func (in *Struct_Empty) DeepCopy() *Struct_Empty {
if in == nil {
return nil
}
out := new(Struct_Empty)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Struct_Everything) DeepCopyInto(out *Struct_Everything) {
*out = *in
out.StructField = in.StructField
out.EmptyStructField = in.EmptyStructField
out.ManualStructField = in.ManualStructField.DeepCopy()
out.ManualStructAliasField = in.ManualStructAliasField
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.PrimitivePointersField.DeepCopyInto(&out.PrimitivePointersField)
if in.ManualStructPtrField != nil {
in, out := &in.ManualStructPtrField, &out.ManualStructPtrField
x := (*in).DeepCopy()
*out = &x
}
if in.ManualStructAliasPtrField != nil {
in, out := &in.ManualStructAliasPtrField, &out.ManualStructAliasPtrField
*out = new(ManualStruct_Alias)
**out = **in
}
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.SlicesField.DeepCopyInto(&out.SlicesField)
if in.SliceManualStructField != nil {
in, out := &in.SliceManualStructField, &out.SliceManualStructField
*out = make([]ManualStruct, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
out.ManualSliceField = in.ManualSliceField.DeepCopy()
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Struct_Everything.
func (in *Struct_Everything) DeepCopy() *Struct_Everything {
if in == nil {
return nil
}
out := new(Struct_Everything)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Struct_ExplicitObject) DeepCopyInto(out *Struct_ExplicitObject) {
*out = *in
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Struct_ExplicitObject.
func (in *Struct_ExplicitObject) DeepCopy() *Struct_ExplicitObject {
if in == nil {
return nil
}
out := new(Struct_ExplicitObject)
in.DeepCopyInto(out)
return out
}
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new otherpkg.Object.
func (in *Struct_ExplicitObject) DeepCopyObject() otherpkg.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 *Struct_ExplicitSelectorExplicitObject) DeepCopyInto(out *Struct_ExplicitSelectorExplicitObject) {
*out = *in
out.Struct_TypeMeta = in.Struct_TypeMeta
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Struct_ExplicitSelectorExplicitObject.
func (in *Struct_ExplicitSelectorExplicitObject) DeepCopy() *Struct_ExplicitSelectorExplicitObject {
if in == nil {
return nil
}
out := new(Struct_ExplicitSelectorExplicitObject)
in.DeepCopyInto(out)
return out
}
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new otherpkg.Object.
func (in *Struct_ExplicitSelectorExplicitObject) DeepCopyObject() otherpkg.Object {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}
// DeepCopySelector is an autogenerated deepcopy function, copying the receiver, creating a new Selector.
func (in *Struct_ExplicitSelectorExplicitObject) DeepCopySelector() Selector {
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 *Struct_Interfaces) DeepCopyInto(out *Struct_Interfaces) {
*out = *in
if in.ObjectField != nil {
out.ObjectField = in.ObjectField.DeepCopyObject()
}
if in.NilObjectField != nil {
out.NilObjectField = in.NilObjectField.DeepCopyObject()
}
if in.SelectorField != nil {
out.SelectorField = in.SelectorField.DeepCopySelector()
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Struct_Interfaces.
func (in *Struct_Interfaces) DeepCopy() *Struct_Interfaces {
if in == nil {
return nil
}
out := new(Struct_Interfaces)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Struct_NonPointerExplicitObject) DeepCopyInto(out *Struct_NonPointerExplicitObject) {
*out = *in
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Struct_NonPointerExplicitObject.
func (in *Struct_NonPointerExplicitObject) DeepCopy() *Struct_NonPointerExplicitObject {
if in == nil {
return nil
}
out := new(Struct_NonPointerExplicitObject)
in.DeepCopyInto(out)
return out
}
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new otherpkg.Object.
func (in Struct_NonPointerExplicitObject) DeepCopyObject() otherpkg.Object {
return *in.DeepCopy()
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Struct_ObjectAndList) DeepCopyInto(out *Struct_ObjectAndList) {
*out = *in
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Struct_ObjectAndList.
func (in *Struct_ObjectAndList) DeepCopy() *Struct_ObjectAndList {
if in == nil {
return nil
}
out := new(Struct_ObjectAndList)
in.DeepCopyInto(out)
return out
}
// DeepCopyList is an autogenerated deepcopy function, copying the receiver, creating a new otherpkg.List.
func (in *Struct_ObjectAndList) DeepCopyList() otherpkg.List {
if c := in.DeepCopy(); c != nil {
return c
}
return nil
}
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new otherpkg.Object.
func (in *Struct_ObjectAndList) DeepCopyObject() otherpkg.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 *Struct_ObjectAndObject) DeepCopyInto(out *Struct_ObjectAndObject) {
*out = *in
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Struct_ObjectAndObject.
func (in *Struct_ObjectAndObject) DeepCopy() *Struct_ObjectAndObject {
if in == nil {
return nil
}
out := new(Struct_ObjectAndObject)
in.DeepCopyInto(out)
return out
}
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new otherpkg.Object.
func (in *Struct_ObjectAndObject) DeepCopyObject() otherpkg.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 *Struct_PrimitivePointers) DeepCopyInto(out *Struct_PrimitivePointers) {
*out = *in
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
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Struct_PrimitivePointers.
func (in *Struct_PrimitivePointers) DeepCopy() *Struct_PrimitivePointers {
if in == nil {
return nil
}
out := new(Struct_PrimitivePointers)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Struct_PrimitivePointers_Alias) DeepCopyInto(out *Struct_PrimitivePointers_Alias) {
*out = *in
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
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Struct_PrimitivePointers_Alias.
func (in *Struct_PrimitivePointers_Alias) DeepCopy() *Struct_PrimitivePointers_Alias {
if in == nil {
return nil
}
out := new(Struct_PrimitivePointers_Alias)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Struct_Primitives) DeepCopyInto(out *Struct_Primitives) {
*out = *in
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Struct_Primitives.
func (in *Struct_Primitives) DeepCopy() *Struct_Primitives {
if in == nil {
return nil
}
out := new(Struct_Primitives)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Struct_Primitives_Alias) DeepCopyInto(out *Struct_Primitives_Alias) {
*out = *in
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Struct_Primitives_Alias.
func (in *Struct_Primitives_Alias) DeepCopy() *Struct_Primitives_Alias {
if in == nil {
return nil
}
out := new(Struct_Primitives_Alias)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Struct_Slices) DeepCopyInto(out *Struct_Slices) {
*out = *in
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)
}
if in.SliceStructPrimitivesField != nil {
in, out := &in.SliceStructPrimitivesField, &out.SliceStructPrimitivesField
*out = make([]Struct_Primitives, len(*in))
copy(*out, *in)
}
if in.SliceStructPrimitivesAliasField != nil {
in, out := &in.SliceStructPrimitivesAliasField, &out.SliceStructPrimitivesAliasField
*out = make([]Struct_Primitives_Alias, len(*in))
copy(*out, *in)
}
if in.SliceStructPrimitivePointersField != nil {
in, out := &in.SliceStructPrimitivePointersField, &out.SliceStructPrimitivePointersField
*out = make([]Struct_PrimitivePointers, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
if in.SliceStructPrimitivePointersAliasField != nil {
in, out := &in.SliceStructPrimitivePointersAliasField, &out.SliceStructPrimitivePointersAliasField
*out = make([]Struct_PrimitivePointers_Alias, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
if in.SliceSliceIntField != nil {
in, out := &in.SliceSliceIntField, &out.SliceSliceIntField
*out = make([][]int, len(*in))
for i := range *in {
if (*in)[i] != nil {
in, out := &(*in)[i], &(*out)[i]
*out = make([]int, len(*in))
copy(*out, *in)
}
}
}
if in.SliceManualStructField != nil {
in, out := &in.SliceManualStructField, &out.SliceManualStructField
*out = make([]ManualStruct, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
out.ManualSliceField = in.ManualSliceField.DeepCopy()
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Struct_Slices.
func (in *Struct_Slices) DeepCopy() *Struct_Slices {
if in == nil {
return nil
}
out := new(Struct_Slices)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Struct_Slices_Alias) DeepCopyInto(out *Struct_Slices_Alias) {
*out = *in
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)
}
if in.SliceStructPrimitivesField != nil {
in, out := &in.SliceStructPrimitivesField, &out.SliceStructPrimitivesField
*out = make([]Struct_Primitives, len(*in))
copy(*out, *in)
}
if in.SliceStructPrimitivesAliasField != nil {
in, out := &in.SliceStructPrimitivesAliasField, &out.SliceStructPrimitivesAliasField
*out = make([]Struct_Primitives_Alias, len(*in))
copy(*out, *in)
}
if in.SliceStructPrimitivePointersField != nil {
in, out := &in.SliceStructPrimitivePointersField, &out.SliceStructPrimitivePointersField
*out = make([]Struct_PrimitivePointers, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
if in.SliceStructPrimitivePointersAliasField != nil {
in, out := &in.SliceStructPrimitivePointersAliasField, &out.SliceStructPrimitivePointersAliasField
*out = make([]Struct_PrimitivePointers_Alias, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
if in.SliceSliceIntField != nil {
in, out := &in.SliceSliceIntField, &out.SliceSliceIntField
*out = make([][]int, len(*in))
for i := range *in {
if (*in)[i] != nil {
in, out := &(*in)[i], &(*out)[i]
*out = make([]int, len(*in))
copy(*out, *in)
}
}
}
if in.SliceManualStructField != nil {
in, out := &in.SliceManualStructField, &out.SliceManualStructField
*out = make([]ManualStruct, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
out.ManualSliceField = in.ManualSliceField.DeepCopy()
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Struct_Slices_Alias.
func (in *Struct_Slices_Alias) DeepCopy() *Struct_Slices_Alias {
if in == nil {
return nil
}
out := new(Struct_Slices_Alias)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Struct_Struct_PrimitivePointers) DeepCopyInto(out *Struct_Struct_PrimitivePointers) {
*out = *in
in.StructField.DeepCopyInto(&out.StructField)
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Struct_Struct_PrimitivePointers.
func (in *Struct_Struct_PrimitivePointers) DeepCopy() *Struct_Struct_PrimitivePointers {
if in == nil {
return nil
}
out := new(Struct_Struct_PrimitivePointers)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Struct_Struct_Primitives) DeepCopyInto(out *Struct_Struct_Primitives) {
*out = *in
out.StructField = in.StructField
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Struct_Struct_Primitives.
func (in *Struct_Struct_Primitives) DeepCopy() *Struct_Struct_Primitives {
if in == nil {
return nil
}
out := new(Struct_Struct_Primitives)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Struct_Struct_Slices) DeepCopyInto(out *Struct_Struct_Slices) {
*out = *in
in.StructField.DeepCopyInto(&out.StructField)
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Struct_Struct_Slices.
func (in *Struct_Struct_Slices) DeepCopy() *Struct_Struct_Slices {
if in == nil {
return nil
}
out := new(Struct_Struct_Slices)
in.DeepCopyInto(out)
return out
}

View File

@@ -1 +0,0 @@
defaulter-gen

View File

@@ -1,13 +0,0 @@
{
"Rules": [
{
"SelectorRegexp": "k8s[.]io",
"AllowedPrefixes": [
"k8s.io/gengo",
"k8s.io/klog",
"k8s.io/kubernetes/third_party/forked/golang",
"k8s.io/apimachinery/pkg/runtime"
]
}
]
}

View File

@@ -1,15 +0,0 @@
TOOL=defaulter-gen
test:
@if ! git diff --quiet HEAD; then \
echo "FAIL: git client is not clean"; \
false; \
fi
@go build -o /tmp/$(TOOL)
@PKGS=$$(cd _output_tests; go list ./... | paste -sd' ' -); \
/tmp/$(TOOL) --logtostderr --v=4 -i $$(echo $$PKGS | sed 's/ /,/g') -O zz_generated
@if ! git diff --quiet HEAD; then \
echo "FAIL: output files changed"; \
git diff; \
false; \
fi

View File

@@ -1,2 +0,0 @@
approvers:
- smarterclayton

View File

@@ -1,20 +0,0 @@
/*
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/gengo/examples/defaulter-gen/_output_tests/empty"

View File

@@ -1,29 +0,0 @@
/*
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

@@ -1,32 +0,0 @@
// +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

@@ -1,32 +0,0 @@
/*
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"
)
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

@@ -1,20 +0,0 @@
/*
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/gengo/examples/defaulter-gen/_output_tests/pointer"

View File

@@ -1,33 +0,0 @@
/*
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/gengo/examples/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

@@ -1,45 +0,0 @@
// +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

@@ -1,32 +0,0 @@
/*
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"
)
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

@@ -1,20 +0,0 @@
/*
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/gengo/examples/defaulter-gen/_output_tests/slices"

View File

@@ -1,37 +0,0 @@
/*
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/gengo/examples/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

@@ -1,55 +0,0 @@
// +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

@@ -1,32 +0,0 @@
/*
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"
)
func addDefaultingFuncs(scheme *runtime.Scheme) error {
return RegisterDefaults(scheme)
}
func SetDefaults_Struct_Primitives(obj *Struct_Primitives) {
if obj.BoolField == nil {
obj.BoolField = new(bool)
*obj.BoolField = true
}
}

View File

@@ -1,20 +0,0 @@
/*
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/gengo/examples/defaulter-gen/_output_tests/wholepkg"

View File

@@ -1,74 +0,0 @@
/*
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/gengo/examples/defaulter-gen/_output_tests/empty"
)
// Only primitives
type Struct_Primitives struct {
empty.TypeMeta
BoolField *bool
IntField *int
StringField *string
FloatField *float64
}
type Struct_Primitives_Alias Struct_Primitives
type Struct_Struct_Primitives struct {
empty.TypeMeta
StructField Struct_Primitives
}
//Pointer
type Struct_Pointer struct {
empty.TypeMeta
PointerStructPrimitivesField Struct_Primitives
PointerPointerStructPrimitivesField *Struct_Primitives
PointerStructPrimitivesAliasField Struct_Primitives_Alias
PointerPointerStructPrimitivesAliasField Struct_Primitives_Alias
PointerStructStructPrimitives Struct_Struct_Primitives
PointerPointerStructStructPrimitives *Struct_Struct_Primitives
}
// Slices
type Struct_Slices struct {
empty.TypeMeta
SliceStructPrimitivesField []Struct_Primitives
SlicePointerStructPrimitivesField []*Struct_Primitives
SliceStructPrimitivesAliasField []Struct_Primitives_Alias
SlicePointerStructPrimitivesAliasField []*Struct_Primitives_Alias
SliceStructStructPrimitives []Struct_Struct_Primitives
SlicePointerStructStructPrimitives []*Struct_Struct_Primitives
}
// Everything
type Struct_Everything struct {
empty.TypeMeta
BoolPtrField *bool
IntPtrField *int
StringPtrField *string
FloatPtrField *float64
PointerStructField Struct_Pointer
SliceBoolField []bool
SliceByteField []byte
SliceIntField []int
SliceStringField []string
SliceFloatField []float64
SlicesStructField Struct_Slices
}

View File

@@ -1,84 +0,0 @@
// +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(&Struct_Everything{}, func(obj interface{}) { SetObjectDefaults_Struct_Everything(obj.(*Struct_Everything)) })
scheme.AddTypeDefaultingFunc(&Struct_Pointer{}, func(obj interface{}) { SetObjectDefaults_Struct_Pointer(obj.(*Struct_Pointer)) })
scheme.AddTypeDefaultingFunc(&Struct_Primitives{}, func(obj interface{}) { SetObjectDefaults_Struct_Primitives(obj.(*Struct_Primitives)) })
scheme.AddTypeDefaultingFunc(&Struct_Slices{}, func(obj interface{}) { SetObjectDefaults_Struct_Slices(obj.(*Struct_Slices)) })
scheme.AddTypeDefaultingFunc(&Struct_Struct_Primitives{}, func(obj interface{}) { SetObjectDefaults_Struct_Struct_Primitives(obj.(*Struct_Struct_Primitives)) })
return nil
}
func SetObjectDefaults_Struct_Everything(in *Struct_Everything) {
SetObjectDefaults_Struct_Pointer(&in.PointerStructField)
SetObjectDefaults_Struct_Slices(&in.SlicesStructField)
}
func SetObjectDefaults_Struct_Pointer(in *Struct_Pointer) {
SetObjectDefaults_Struct_Primitives(&in.PointerStructPrimitivesField)
if in.PointerPointerStructPrimitivesField != nil {
SetObjectDefaults_Struct_Primitives(in.PointerPointerStructPrimitivesField)
}
SetObjectDefaults_Struct_Struct_Primitives(&in.PointerStructStructPrimitives)
if in.PointerPointerStructStructPrimitives != nil {
SetObjectDefaults_Struct_Struct_Primitives(in.PointerPointerStructStructPrimitives)
}
}
func SetObjectDefaults_Struct_Primitives(in *Struct_Primitives) {
SetDefaults_Struct_Primitives(in)
}
func SetObjectDefaults_Struct_Slices(in *Struct_Slices) {
for i := range in.SliceStructPrimitivesField {
a := &in.SliceStructPrimitivesField[i]
SetObjectDefaults_Struct_Primitives(a)
}
for i := range in.SlicePointerStructPrimitivesField {
a := in.SlicePointerStructPrimitivesField[i]
if a != nil {
SetObjectDefaults_Struct_Primitives(a)
}
}
for i := range in.SliceStructStructPrimitives {
a := &in.SliceStructStructPrimitives[i]
SetObjectDefaults_Struct_Struct_Primitives(a)
}
for i := range in.SlicePointerStructStructPrimitives {
a := in.SlicePointerStructStructPrimitives[i]
if a != nil {
SetObjectDefaults_Struct_Struct_Primitives(a)
}
}
}
func SetObjectDefaults_Struct_Struct_Primitives(in *Struct_Struct_Primitives) {
SetObjectDefaults_Struct_Primitives(&in.StructField)
}

View File

@@ -1,75 +0,0 @@
/*
Copyright 2016 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.
*/
// defaulter-gen is a tool for auto-generating Defaulter functions.
//
// Given a list of input directories, it will scan for top level types
// and generate efficient defaulters for an entire object from the sum
// of the SetDefault_* methods contained in the object tree.
//
// Generation is governed by comment tags in the source. Any package may
// request defaulter generation by including one or more comment tags at
// the package comment level:
//
// // +k8s:defaulter-gen=<field-name-to-flag>
//
// which will create defaulters for any type that contains the provided
// field name (if the type has defaulters). Any type may request explicit
// defaulting by providing the comment tag:
//
// // +k8s:defaulter-gen=true|false
//
// An existing defaulter method (`SetDefaults_TYPE`) can provide the
// comment tag:
//
// // +k8s:defaulter-gen=covers
//
// to indicate that the defaulter does not or should not call any nested
// defaulters.
package main
import (
"k8s.io/gengo/args"
"k8s.io/gengo/examples/defaulter-gen/generators"
"github.com/spf13/pflag"
"k8s.io/klog"
)
func main() {
arguments := args.Default()
// Override defaults.
arguments.OutputFileBaseName = "defaulter_generated"
// Custom args.
customArgs := &generators.CustomArgs{
ExtraPeerDirs: []string{},
}
pflag.CommandLine.StringSliceVar(&customArgs.ExtraPeerDirs, "extra-peer-dirs", customArgs.ExtraPeerDirs,
"Comma-separated list of import paths which are considered, after tag-specified peers, for conversions.")
arguments.CustomArgs = customArgs
// Run it.
if err := arguments.Execute(
generators.NameSystems(),
generators.DefaultNameSystem(),
generators.Packages,
); err != nil {
klog.Fatalf("Error: %v", err)
}
klog.V(2).Info("Completed successfully.")
}

View File

@@ -1 +0,0 @@
import-boss

View File

@@ -1,272 +0,0 @@
/*
Copyright 2016 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 generators has the generators for the import-boss utility.
package generators
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"k8s.io/gengo/args"
"k8s.io/gengo/generator"
"k8s.io/gengo/namer"
"k8s.io/gengo/types"
"k8s.io/klog"
)
const (
importBossFileType = "import-boss"
)
// NameSystems returns the name system used by the generators in this package.
func NameSystems() namer.NameSystems {
return namer.NameSystems{
"raw": namer.NewRawNamer("", nil),
}
}
// DefaultNameSystem returns the default name system for ordering the types to be
// processed by the generators in this package.
func DefaultNameSystem() string {
return "raw"
}
// Packages makes the import-boss package definition.
func Packages(c *generator.Context, arguments *args.GeneratorArgs) generator.Packages {
pkgs := generator.Packages{}
c.FileTypes = map[string]generator.FileType{
importBossFileType: importRuleFile{},
}
for _, p := range c.Universe {
if !arguments.InputIncludes(p) {
// Don't run on e.g. third party dependencies.
continue
}
savedPackage := p
pkgs = append(pkgs, &generator.DefaultPackage{
PackageName: p.Name,
PackagePath: p.Path,
// GeneratorFunc returns a list of generators. Each generator makes a
// single file.
GeneratorFunc: func(c *generator.Context) (generators []generator.Generator) {
return []generator.Generator{&importRules{
myPackage: savedPackage,
}}
},
FilterFunc: func(c *generator.Context, t *types.Type) bool {
return false
},
})
}
return pkgs
}
// A single import restriction rule.
type Rule struct {
// All import paths that match this regexp...
SelectorRegexp string
// ... must have one of these prefixes ...
AllowedPrefixes []string
// ... and must not have one of these prefixes.
ForbiddenPrefixes []string
}
type fileFormat struct {
CurrentImports []string
Rules []Rule
}
func readFile(path string) (*fileFormat, error) {
currentBytes, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("couldn't read %v: %v", path, err)
}
var current fileFormat
err = json.Unmarshal(currentBytes, &current)
if err != nil {
return nil, fmt.Errorf("couldn't unmarshal %v: %v", path, err)
}
return &current, nil
}
func writeFile(path string, ff *fileFormat) error {
raw, err := json.MarshalIndent(ff, "", "\t")
if err != nil {
return fmt.Errorf("couldn't format data for file %v.\n%#v", path, ff)
}
f, err := os.Create(path)
if err != nil {
return fmt.Errorf("couldn't open %v for writing: %v", path, err)
}
defer f.Close()
_, err = f.Write(raw)
return err
}
// This does the actual checking, since it knows the literal destination file.
type importRuleFile struct{}
func (importRuleFile) AssembleFile(f *generator.File, path string) error {
return nil
}
// TODO: make a flag to enable this, or expose this information in some other way.
func (importRuleFile) listEntireImportTree(f *generator.File, path string) error {
// If the file exists, populate its current imports. This is mostly to help
// humans figure out what they need to fix.
if _, err := os.Stat(path); err != nil {
// Ignore packages which haven't opted in by adding an .import-restrictions file.
return nil
}
current, err := readFile(path)
if err != nil {
return err
}
current.CurrentImports = []string{}
for v := range f.Imports {
current.CurrentImports = append(current.CurrentImports, v)
}
sort.Strings(current.CurrentImports)
return writeFile(path, current)
}
// removeLastDir removes the last directory, but leaves the file name
// unchanged. It returns the new path and the removed directory. So:
// "a/b/c/file" -> ("a/b/file", "c")
func removeLastDir(path string) (newPath, removedDir string) {
dir, file := filepath.Split(path)
dir = strings.TrimSuffix(dir, string(filepath.Separator))
return filepath.Join(filepath.Dir(dir), file), filepath.Base(dir)
}
// Keep going up a directory until we find an .import-restrictions file.
func recursiveRead(path string) (*fileFormat, string, error) {
for {
if _, err := os.Stat(path); err == nil {
ff, err := readFile(path)
return ff, path, err
}
nextPath, removedDir := removeLastDir(path)
if nextPath == path || removedDir == "src" {
break
}
path = nextPath
}
return nil, "", nil
}
func (importRuleFile) VerifyFile(f *generator.File, path string) error {
rules, actualPath, err := recursiveRead(path)
if err != nil {
return fmt.Errorf("error finding rules file: %v", err)
}
if rules == nil {
// No restrictions on this directory.
return nil
}
for _, r := range rules.Rules {
re, err := regexp.Compile(r.SelectorRegexp)
if err != nil {
return fmt.Errorf("regexp `%s` in file %q doesn't compile: %v", r.SelectorRegexp, actualPath, err)
}
for v := range f.Imports {
klog.V(4).Infof("Checking %v matches %v: %v\n", r.SelectorRegexp, v, re.MatchString(v))
if !re.MatchString(v) {
continue
}
for _, forbidden := range r.ForbiddenPrefixes {
klog.V(4).Infof("Checking %v against %v\n", v, forbidden)
if strings.HasPrefix(v, forbidden) {
return fmt.Errorf("import %v has forbidden prefix %v", v, forbidden)
}
}
found := false
for _, allowed := range r.AllowedPrefixes {
klog.V(4).Infof("Checking %v against %v\n", v, allowed)
if strings.HasPrefix(v, allowed) {
found = true
break
}
}
if !found {
return fmt.Errorf("import %v did not match any allowed prefix", v)
}
}
}
if len(rules.Rules) > 0 {
klog.V(2).Infof("%v passes rules found in %v\n", path, actualPath)
}
return nil
}
// importRules produces a file with a set for a single type.
type importRules struct {
myPackage *types.Package
imports namer.ImportTracker
}
var (
_ = generator.Generator(&importRules{})
_ = generator.FileType(importRuleFile{})
)
func (r *importRules) Name() string { return "import rules" }
func (r *importRules) Filter(*generator.Context, *types.Type) bool { return false }
func (r *importRules) Namers(*generator.Context) namer.NameSystems { return nil }
func (r *importRules) PackageVars(*generator.Context) []string { return []string{} }
func (r *importRules) PackageConsts(*generator.Context) []string { return []string{} }
func (r *importRules) GenerateType(*generator.Context, *types.Type, io.Writer) error { return nil }
func (r *importRules) Filename() string { return ".import-restrictions" }
func (r *importRules) FileType() string { return importBossFileType }
func (r *importRules) Init(c *generator.Context, w io.Writer) error { return nil }
func (r *importRules) Finalize(*generator.Context, io.Writer) error { return nil }
func dfsImports(dest *[]string, seen map[string]bool, p *types.Package) {
for _, p2 := range p.Imports {
if seen[p2.Path] {
continue
}
seen[p2.Path] = true
dfsImports(dest, seen, p2)
*dest = append(*dest, p2.Path)
}
}
func (r *importRules) Imports(*generator.Context) []string {
all := []string{}
dfsImports(&all, map[string]bool{}, r.myPackage)
return all
}

View File

@@ -1,39 +0,0 @@
/*
Copyright 2016 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 generators
import (
"path/filepath"
"testing"
)
func TestRemoveLastDir(t *testing.T) {
table := map[string]struct{ newPath, removedDir string }{
"a/b/c": {"a/c", "b"},
}
for slashInput, expect := range table {
input := filepath.FromSlash(slashInput)
gotPath, gotRemoved := removeLastDir(input)
if e, a := filepath.FromSlash(expect.newPath), gotPath; e != a {
t.Errorf("%v: wanted %v, got %v", input, e, a)
}
if e, a := filepath.FromSlash(expect.removedDir), gotRemoved; e != a {
t.Errorf("%v: wanted %v, got %v", input, e, a)
}
}
}

View File

@@ -1,78 +0,0 @@
/*
Copyright 2016 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.
*/
// import-boss enforces import restrictions in a given repository.
//
// When a directory is verified, import-boss looks for a file called
// ".import-restrictions". If this file is not found, parent directories will be
// recursively searched.
//
// If an ".import-restrictions" file is found, then all imports of the package
// are checked against each "rule" in the file. A rule consists of three parts:
// * A SelectorRegexp, to select the import paths that the rule applies to.
// * A list of AllowedPrefixes
// * A list of ForbiddenPrefixes
// An import is allowed if it matches at least one allowed prefix and does not
// match any forbidden prefix. An example file looks like this:
//
// {
// "Rules": [
// {
// "SelectorRegexp": "k8s[.]io",
// "AllowedPrefixes": [
// "k8s.io/gengo",
// "k8s.io/kubernetes/third_party"
// ],
// "ForbiddenPrefixes": [
// "k8s.io/kubernetes/pkg/third_party/deprecated"
// ]
// },
// {
// "SelectorRegexp": "^unsafe$",
// "AllowedPrefixes": [
// ],
// "ForbiddenPrefixes": [
// ""
// ]
// }
// ]
// }
//
// Note the secound block explicitly matches the unsafe package, and forbids it
// ("" is a prefix of everything).
package main
import (
"os"
"k8s.io/gengo/args"
"k8s.io/gengo/examples/import-boss/generators"
"k8s.io/klog"
)
func main() {
arguments := args.Default()
if err := arguments.Execute(
generators.NameSystems(),
generators.DefaultNameSystem(),
generators.Packages,
); err != nil {
klog.Errorf("Error: %v", err)
os.Exit(1)
}
klog.V(2).Info("Completed successfully.")
}

View File

@@ -1 +0,0 @@
set-gen

View File

@@ -1,15 +0,0 @@
TOOL=set-gen
test:
@if ! git diff --quiet HEAD; then \
echo "FAIL: git client is not clean"; \
false; \
fi
@go build -o /tmp/$(TOOL)
@PKGS=$$(go list ./sets/types | paste -sd' ' -); \
/tmp/$(TOOL) --logtostderr --v=4 -i $$(echo $$PKGS | sed 's/ /,/g') -O zz_generated -o ./sets
@if ! git diff --quiet HEAD; then \
echo "FAIL: output files changed"; \
git diff; \
false; \
fi

View File

@@ -1,360 +0,0 @@
/*
Copyright 2015 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 generators has the generators for the set-gen utility.
package generators
import (
"io"
"k8s.io/gengo/args"
"k8s.io/gengo/generator"
"k8s.io/gengo/namer"
"k8s.io/gengo/types"
"k8s.io/klog"
)
// NameSystems returns the name system used by the generators in this package.
func NameSystems() namer.NameSystems {
return namer.NameSystems{
"public": namer.NewPublicNamer(0),
"private": namer.NewPrivateNamer(0),
"raw": namer.NewRawNamer("", nil),
}
}
// DefaultNameSystem returns the default name system for ordering the types to be
// processed by the generators in this package.
func DefaultNameSystem() string {
return "public"
}
// Packages makes the sets package definition.
func Packages(_ *generator.Context, arguments *args.GeneratorArgs) generator.Packages {
boilerplate, err := arguments.LoadGoBoilerplate()
if err != nil {
klog.Fatalf("Failed loading boilerplate: %v", err)
}
return generator.Packages{&generator.DefaultPackage{
PackageName: "sets",
PackagePath: arguments.OutputPackagePath,
HeaderText: boilerplate,
PackageDocumentation: []byte(
`// Package sets has auto-generated set types.
`),
// GeneratorFunc returns a list of generators. Each generator makes a
// single file.
GeneratorFunc: func(c *generator.Context) (generators []generator.Generator) {
generators = []generator.Generator{
// Always generate a "doc.go" file.
generator.DefaultGen{OptionalName: "doc"},
// Make a separate file for the Empty type, since it's shared by every type.
generator.DefaultGen{
OptionalName: "empty",
OptionalBody: []byte(emptyTypeDecl),
},
}
// Since we want a file per type that we generate a set for, we
// have to provide a function for this.
for _, t := range c.Order {
generators = append(generators, &genSet{
DefaultGen: generator.DefaultGen{
// Use the privatized version of the
// type name as the file name.
//
// TODO: make a namer that converts
// camelCase to '-' separation for file
// names?
OptionalName: c.Namers["private"].Name(t),
},
outputPackage: arguments.OutputPackagePath,
typeToMatch: t,
imports: generator.NewImportTracker(),
})
}
return generators
},
FilterFunc: func(c *generator.Context, t *types.Type) bool {
// It would be reasonable to filter by the type's package here.
// It might be necessary if your input directory has a big
// import graph.
switch t.Kind {
case types.Map, types.Slice, types.Pointer:
// These types can't be keys in a map.
return false
case types.Builtin:
return true
case types.Struct:
// Only some structs can be keys in a map. This is triggered by the line
// // +genset
// or
// // +genset=true
return extractBoolTagOrDie("genset", t.CommentLines) == true
}
return false
},
}}
}
// genSet produces a file with a set for a single type.
type genSet struct {
generator.DefaultGen
outputPackage string
typeToMatch *types.Type
imports namer.ImportTracker
}
// Filter ignores all but one type because we're making a single file per type.
func (g *genSet) Filter(c *generator.Context, t *types.Type) bool { return t == g.typeToMatch }
func (g *genSet) Namers(c *generator.Context) namer.NameSystems {
return namer.NameSystems{
"raw": namer.NewRawNamer(g.outputPackage, g.imports),
}
}
func (g *genSet) Imports(c *generator.Context) (imports []string) {
return append(g.imports.ImportLines(), "reflect", "sort")
}
// args constructs arguments for templates. Usage:
// g.args(t, "key1", value1, "key2", value2, ...)
//
// 't' is loaded with the key 'type'.
//
// We could use t directly as the argument, but doing it this way makes it easy
// to mix in additional parameters. This feature is not used in this set
// generator, but is present as an example.
func (g *genSet) args(t *types.Type, kv ...interface{}) interface{} {
m := map[interface{}]interface{}{"type": t}
for i := 0; i < len(kv)/2; i++ {
m[kv[i*2]] = kv[i*2+1]
}
return m
}
// GenerateType makes the body of a file implementing a set for type t.
func (g *genSet) GenerateType(c *generator.Context, t *types.Type, w io.Writer) error {
sw := generator.NewSnippetWriter(w, c, "$", "$")
sw.Do(setCode, g.args(t))
sw.Do("func less$.type|public$(lhs, rhs $.type|raw$) bool {\n", g.args(t))
g.lessBody(sw, t)
sw.Do("}\n", g.args(t))
return sw.Error()
}
func (g *genSet) lessBody(sw *generator.SnippetWriter, t *types.Type) {
// TODO: make this recursive, handle pointers and multiple nested structs...
switch t.Kind {
case types.Struct:
for _, m := range types.FlattenMembers(t.Members) {
sw.Do("if lhs.$.Name$ < rhs.$.Name$ { return true }\n", m)
sw.Do("if lhs.$.Name$ > rhs.$.Name$ { return false }\n", m)
}
sw.Do("return false\n", nil)
default:
sw.Do("return lhs < rhs\n", nil)
}
}
// written to the "empty.go" file.
var emptyTypeDecl = `
// Empty is public since it is used by some internal API objects for conversions between external
// string arrays and internal sets, and conversion logic requires public types today.
type Empty struct{}
`
// Written for every type. If you've never used text/template before:
// $.type$ refers to the source type; |public means to
// call the function giving the public name, |raw the raw type name.
var setCode = `// sets.$.type|public$ is a set of $.type|raw$s, implemented via map[$.type|raw$]struct{} for minimal memory consumption.
type $.type|public$ map[$.type|raw$]Empty
// New$.type|public$ creates a $.type|public$ from a list of values.
func New$.type|public$(items ...$.type|raw$) $.type|public$ {
ss := $.type|public${}
ss.Insert(items...)
return ss
}
// $.type|public$KeySet creates a $.type|public$ from a keys of a map[$.type|raw$](? extends interface{}).
// If the value passed in is not actually a map, this will panic.
func $.type|public$KeySet(theMap interface{}) $.type|public$ {
v := reflect.ValueOf(theMap)
ret := $.type|public${}
for _, keyValue := range v.MapKeys() {
ret.Insert(keyValue.Interface().($.type|raw$))
}
return ret
}
// Insert adds items to the set.
func (s $.type|public$) Insert(items ...$.type|raw$) {
for _, item := range items {
s[item] = Empty{}
}
}
// Delete removes all items from the set.
func (s $.type|public$) Delete(items ...$.type|raw$) {
for _, item := range items {
delete(s, item)
}
}
// Has returns true if and only if item is contained in the set.
func (s $.type|public$) Has(item $.type|raw$) bool {
_, contained := s[item]
return contained
}
// HasAll returns true if and only if all items are contained in the set.
func (s $.type|public$) HasAll(items ...$.type|raw$) bool {
for _, item := range items {
if !s.Has(item) {
return false
}
}
return true
}
// HasAny returns true if any items are contained in the set.
func (s $.type|public$) HasAny(items ...$.type|raw$) bool {
for _, item := range items {
if s.Has(item) {
return true
}
}
return false
}
// Difference returns a set of objects that are not in s2
// For example:
// s1 = {a1, a2, a3}
// s2 = {a1, a2, a4, a5}
// s1.Difference(s2) = {a3}
// s2.Difference(s1) = {a4, a5}
func (s $.type|public$) Difference(s2 $.type|public$) $.type|public$ {
result := New$.type|public$()
for key := range s {
if !s2.Has(key) {
result.Insert(key)
}
}
return result
}
// Union returns a new set which includes items in either s1 or s2.
// For example:
// s1 = {a1, a2}
// s2 = {a3, a4}
// s1.Union(s2) = {a1, a2, a3, a4}
// s2.Union(s1) = {a1, a2, a3, a4}
func (s1 $.type|public$) Union(s2 $.type|public$) $.type|public$ {
result := New$.type|public$()
for key := range s1 {
result.Insert(key)
}
for key := range s2 {
result.Insert(key)
}
return result
}
// Intersection returns a new set which includes the item in BOTH s1 and s2
// For example:
// s1 = {a1, a2}
// s2 = {a2, a3}
// s1.Intersection(s2) = {a2}
func (s1 $.type|public$) Intersection(s2 $.type|public$) $.type|public$ {
var walk, other $.type|public$
result := New$.type|public$()
if s1.Len() < s2.Len() {
walk = s1
other = s2
} else {
walk = s2
other = s1
}
for key := range walk {
if other.Has(key) {
result.Insert(key)
}
}
return result
}
// IsSuperset returns true if and only if s1 is a superset of s2.
func (s1 $.type|public$) IsSuperset(s2 $.type|public$) bool {
for item := range s2 {
if !s1.Has(item) {
return false
}
}
return true
}
// Equal returns true if and only if s1 is equal (as a set) to s2.
// Two sets are equal if their membership is identical.
// (In practice, this means same elements, order doesn't matter)
func (s1 $.type|public$) Equal(s2 $.type|public$) bool {
return len(s1) == len(s2) && s1.IsSuperset(s2)
}
type sortableSliceOf$.type|public$ []$.type|raw$
func (s sortableSliceOf$.type|public$) Len() int { return len(s) }
func (s sortableSliceOf$.type|public$) Less(i, j int) bool { return less$.type|public$(s[i], s[j]) }
func (s sortableSliceOf$.type|public$) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// List returns the contents as a sorted $.type|raw$ slice.
func (s $.type|public$) List() []$.type|raw$ {
res := make(sortableSliceOf$.type|public$, 0, len(s))
for key := range s {
res = append(res, key)
}
sort.Sort(res)
return []$.type|raw$(res)
}
// UnsortedList returns the slice with contents in random order.
func (s $.type|public$) UnsortedList() []$.type|raw$ {
res :=make([]$.type|raw$, 0, len(s))
for key := range s {
res = append(res, key)
}
return res
}
// Returns a single element from the set.
func (s $.type|public$) PopAny() ($.type|raw$, bool) {
for key := range s {
s.Delete(key)
return key, true
}
var zeroValue $.type|raw$
return zeroValue, false
}
// Len returns the size of the set.
func (s $.type|public$) Len() int {
return len(s)
}
`

View File

@@ -1,33 +0,0 @@
/*
Copyright 2016 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 generators
import (
"k8s.io/gengo/types"
"k8s.io/klog"
)
// extractBoolTagOrDie gets the comment-tags for the key and asserts that, if
// it exists, the value is boolean. If the tag did not exist, it returns
// false.
func extractBoolTagOrDie(key string, lines []string) bool {
val, err := types.ExtractSingleBoolCommentTag("+", key, false, lines)
if err != nil {
klog.Fatalf(err.Error())
}
return val
}

View File

@@ -1,47 +0,0 @@
/*
Copyright 2015 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.
*/
// set-gen is an example usage of go2idl.
//
// Structs in the input directories with the below line in their comments will
// have sets generated for them.
// // +genset
//
// Any builtin type referenced anywhere in the input directories will have a
// set generated for it.
package main
import (
"os"
"k8s.io/gengo/args"
"k8s.io/gengo/examples/set-gen/generators"
"k8s.io/klog"
)
func main() {
arguments := args.Default()
if err := arguments.Execute(
generators.NameSystems(),
generators.DefaultNameSystem(),
generators.Packages,
); err != nil {
klog.Errorf("Error: %v", err)
os.Exit(1)
}
klog.V(2).Info("Completed successfully.")
}

View File

@@ -1,270 +0,0 @@
/*
Copyright 2014 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 sets
import (
"reflect"
"testing"
)
func TestStringSet(t *testing.T) {
s := String{}
s2 := String{}
if len(s) != 0 {
t.Errorf("Expected len=0: %d", len(s))
}
s.Insert("a", "b")
if len(s) != 2 {
t.Errorf("Expected len=2: %d", len(s))
}
s.Insert("c")
if s.Has("d") {
t.Errorf("Unexpected contents: %#v", s)
}
if !s.Has("a") {
t.Errorf("Missing contents: %#v", s)
}
s.Delete("a")
if s.Has("a") {
t.Errorf("Unexpected contents: %#v", s)
}
s.Insert("a")
if s.HasAll("a", "b", "d") {
t.Errorf("Unexpected contents: %#v", s)
}
if !s.HasAll("a", "b") {
t.Errorf("Missing contents: %#v", s)
}
s2.Insert("a", "b", "d")
if s.IsSuperset(s2) {
t.Errorf("Unexpected contents: %#v", s)
}
s2.Delete("d")
if !s.IsSuperset(s2) {
t.Errorf("Missing contents: %#v", s)
}
}
func TestStringSetDeleteMultiples(t *testing.T) {
s := String{}
s.Insert("a", "b", "c")
if len(s) != 3 {
t.Errorf("Expected len=3: %d", len(s))
}
s.Delete("a", "c")
if len(s) != 1 {
t.Errorf("Expected len=1: %d", len(s))
}
if s.Has("a") {
t.Errorf("Unexpected contents: %#v", s)
}
if s.Has("c") {
t.Errorf("Unexpected contents: %#v", s)
}
if !s.Has("b") {
t.Errorf("Missing contents: %#v", s)
}
}
func TestNewStringSet(t *testing.T) {
s := NewString("a", "b", "c")
if len(s) != 3 {
t.Errorf("Expected len=3: %d", len(s))
}
if !s.Has("a") || !s.Has("b") || !s.Has("c") {
t.Errorf("Unexpected contents: %#v", s)
}
}
func TestStringSetList(t *testing.T) {
s := NewString("z", "y", "x", "a")
if !reflect.DeepEqual(s.List(), []string{"a", "x", "y", "z"}) {
t.Errorf("List gave unexpected result: %#v", s.List())
}
}
func TestStringSetDifference(t *testing.T) {
a := NewString("1", "2", "3")
b := NewString("1", "2", "4", "5")
c := a.Difference(b)
d := b.Difference(a)
if len(c) != 1 {
t.Errorf("Expected len=1: %d", len(c))
}
if !c.Has("3") {
t.Errorf("Unexpected contents: %#v", c.List())
}
if len(d) != 2 {
t.Errorf("Expected len=2: %d", len(d))
}
if !d.Has("4") || !d.Has("5") {
t.Errorf("Unexpected contents: %#v", d.List())
}
}
func TestStringSetHasAny(t *testing.T) {
a := NewString("1", "2", "3")
if !a.HasAny("1", "4") {
t.Errorf("expected true, got false")
}
if a.HasAny("0", "4") {
t.Errorf("expected false, got true")
}
}
func TestStringSetEquals(t *testing.T) {
// Simple case (order doesn't matter)
a := NewString("1", "2")
b := NewString("2", "1")
if !a.Equal(b) {
t.Errorf("Expected to be equal: %v vs %v", a, b)
}
// It is a set; duplicates are ignored
b = NewString("2", "2", "1")
if !a.Equal(b) {
t.Errorf("Expected to be equal: %v vs %v", a, b)
}
// Edge cases around empty sets / empty strings
a = NewString()
b = NewString()
if !a.Equal(b) {
t.Errorf("Expected to be equal: %v vs %v", a, b)
}
b = NewString("1", "2", "3")
if a.Equal(b) {
t.Errorf("Expected to be not-equal: %v vs %v", a, b)
}
b = NewString("1", "2", "")
if a.Equal(b) {
t.Errorf("Expected to be not-equal: %v vs %v", a, b)
}
// Check for equality after mutation
a = NewString()
a.Insert("1")
if a.Equal(b) {
t.Errorf("Expected to be not-equal: %v vs %v", a, b)
}
a.Insert("2")
if a.Equal(b) {
t.Errorf("Expected to be not-equal: %v vs %v", a, b)
}
a.Insert("")
if !a.Equal(b) {
t.Errorf("Expected to be equal: %v vs %v", a, b)
}
a.Delete("")
if a.Equal(b) {
t.Errorf("Expected to be not-equal: %v vs %v", a, b)
}
}
func TestStringUnion(t *testing.T) {
tests := []struct {
s1 String
s2 String
expected String
}{
{
NewString("1", "2", "3", "4"),
NewString("3", "4", "5", "6"),
NewString("1", "2", "3", "4", "5", "6"),
},
{
NewString("1", "2", "3", "4"),
NewString(),
NewString("1", "2", "3", "4"),
},
{
NewString(),
NewString("1", "2", "3", "4"),
NewString("1", "2", "3", "4"),
},
{
NewString(),
NewString(),
NewString(),
},
}
for _, test := range tests {
union := test.s1.Union(test.s2)
if union.Len() != test.expected.Len() {
t.Errorf("Expected union.Len()=%d but got %d", test.expected.Len(), union.Len())
}
if !union.Equal(test.expected) {
t.Errorf("Expected union.Equal(expected) but not true. union:%v expected:%v", union.List(), test.expected.List())
}
}
}
func TestStringIntersection(t *testing.T) {
tests := []struct {
s1 String
s2 String
expected String
}{
{
NewString("1", "2", "3", "4"),
NewString("3", "4", "5", "6"),
NewString("3", "4"),
},
{
NewString("1", "2", "3", "4"),
NewString("1", "2", "3", "4"),
NewString("1", "2", "3", "4"),
},
{
NewString("1", "2", "3", "4"),
NewString(),
NewString(),
},
{
NewString(),
NewString("1", "2", "3", "4"),
NewString(),
},
{
NewString(),
NewString(),
NewString(),
},
}
for _, test := range tests {
intersection := test.s1.Intersection(test.s2)
if intersection.Len() != test.expected.Len() {
t.Errorf("Expected intersection.Len()=%d but got %d", test.expected.Len(), intersection.Len())
}
if !intersection.Equal(test.expected) {
t.Errorf("Expected intersection.Equal(expected) but not true. intersection:%v expected:%v", intersection.List(), test.expected.List())
}
}
}

View File

@@ -1,30 +0,0 @@
/*
Copyright 2015 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 types just provides input types to the set generator. It also
// contains a "go generate" block.
// (You must first `go install k8s.io/gengo/examples/set-gen`)
package types
//go:generate set-gen -i k8s.io/gengo/examples/set-gen/sets/types -o k8s.io/gengo/examples/set-gen/sets
type ReferenceSetTypes struct {
// These types all cause files to be generated
a int64
b int
c byte
d string
}

View File

@@ -1,88 +0,0 @@
/*
Copyright 2015 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 generator_test
import (
"bytes"
"strings"
"testing"
"k8s.io/gengo/generator"
"k8s.io/gengo/namer"
"k8s.io/gengo/parser"
)
func construct(t *testing.T, files map[string]string) *generator.Context {
b := parser.New()
for name, src := range files {
if err := b.AddFileForTest("/tmp/"+name, name, []byte(src)); err != nil {
t.Fatal(err)
}
}
c, err := generator.NewContext(b, namer.NameSystems{
"public": namer.NewPublicNamer(0),
"private": namer.NewPrivateNamer(0),
}, "public")
if err != nil {
t.Fatal(err)
}
return c
}
func TestSnippetWriter(t *testing.T) {
var structTest = map[string]string{
"base/foo/proto/foo.go": `
package foo
// Blah is a test.
// A test, I tell you.
type Blah struct {
// A is the first field.
A int64 ` + "`" + `json:"a"` + "`" + `
// B is the second field.
// Multiline comments work.
B string ` + "`" + `json:"b"` + "`" + `
}
`,
}
c := construct(t, structTest)
b := &bytes.Buffer{}
err := generator.NewSnippetWriter(b, c, "$", "$").
Do("$.|public$$.|private$", c.Order[0]).
Error()
if err != nil {
t.Errorf("Unexpected error %v", err)
}
if e, a := "Blahblah", b.String(); e != a {
t.Errorf("Expected %q, got %q", e, a)
}
err = generator.NewSnippetWriter(b, c, "$", "$").
Do("$.|public", c.Order[0]).
Error()
if err == nil {
t.Errorf("expected error on invalid template")
} else {
// Dear reader, I apologize for making the worst change
// detection test in the history of ever.
if e, a := "snippet_writer_test.go", err.Error(); !strings.Contains(a, e) {
t.Errorf("Expected %q but didn't find it in %q", e, a)
}
}
}

View File

@@ -1,100 +0,0 @@
/*
Copyright 2015 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 namer
import (
"reflect"
"testing"
"k8s.io/gengo/types"
)
func TestNameStrategy(t *testing.T) {
u := types.Universe{}
// Add some types.
base := u.Type(types.Name{Package: "foo/bar", Name: "Baz"})
base.Kind = types.Struct
tmp := u.Type(types.Name{Package: "", Name: "[]bar.Baz"})
tmp.Kind = types.Slice
tmp.Elem = base
tmp = u.Type(types.Name{Package: "", Name: "map[string]bar.Baz"})
tmp.Kind = types.Map
tmp.Key = types.String
tmp.Elem = base
tmp = u.Type(types.Name{Package: "foo/other", Name: "Baz"})
tmp.Kind = types.Struct
tmp.Members = []types.Member{{
Embedded: true,
Type: base,
}}
tmp = u.Type(types.Name{Package: "", Name: "chan Baz"})
tmp.Kind = types.Chan
tmp.Elem = base
u.Type(types.Name{Package: "", Name: "string"})
o := Orderer{NewPublicNamer(0)}
order := o.OrderUniverse(u)
orderedNames := make([]string, len(order))
for i, t := range order {
orderedNames[i] = o.Name(t)
}
expect := []string{"Baz", "Baz", "ChanBaz", "MapStringToBaz", "SliceBaz", "String"}
if e, a := expect, orderedNames; !reflect.DeepEqual(e, a) {
t.Errorf("Wanted %#v, got %#v", e, a)
}
o = Orderer{NewRawNamer("my/package", nil)}
order = o.OrderUniverse(u)
orderedNames = make([]string, len(order))
for i, t := range order {
orderedNames[i] = o.Name(t)
}
expect = []string{"[]bar.Baz", "bar.Baz", "chan bar.Baz", "map[string]bar.Baz", "other.Baz", "string"}
if e, a := expect, orderedNames; !reflect.DeepEqual(e, a) {
t.Errorf("Wanted %#v, got %#v", e, a)
}
o = Orderer{NewRawNamer("foo/bar", nil)}
order = o.OrderUniverse(u)
orderedNames = make([]string, len(order))
for i, t := range order {
orderedNames[i] = o.Name(t)
}
expect = []string{"Baz", "[]Baz", "chan Baz", "map[string]Baz", "other.Baz", "string"}
if e, a := expect, orderedNames; !reflect.DeepEqual(e, a) {
t.Errorf("Wanted %#v, got %#v", e, a)
}
o = Orderer{NewPublicNamer(1)}
order = o.OrderUniverse(u)
orderedNames = make([]string, len(order))
for i, t := range order {
orderedNames[i] = o.Name(t)
}
expect = []string{"BarBaz", "ChanBarBaz", "MapStringToBarBaz", "OtherBaz", "SliceBarBaz", "String"}
if e, a := expect, orderedNames; !reflect.DeepEqual(e, a) {
t.Errorf("Wanted %#v, got %#v", e, a)
}
}

View File

@@ -1,123 +0,0 @@
/*
Copyright 2015 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 namer
import (
"testing"
"k8s.io/gengo/types"
)
func TestPluralNamer(t *testing.T) {
exceptions := map[string]string{
// The type name is already in the plural form
"Endpoints": "endpoints",
}
public := NewPublicPluralNamer(exceptions)
private := NewPrivatePluralNamer(exceptions)
cases := []struct {
typeName string
expectedPrivate string
expectedPublic string
}{
{
"I",
"i",
"I",
},
{
"Pod",
"pods",
"Pods",
},
{
"Entry",
"entries",
"Entries",
},
{
"Endpoints",
"endpoints",
"Endpoints",
},
{
"Bus",
"buses",
"Buses",
},
{
"Fizz",
"fizzes",
"Fizzes",
},
{
"Search",
"searches",
"Searches",
},
{
"Autograph",
"autographs",
"Autographs",
},
{
"Dispatch",
"dispatches",
"Dispatches",
},
{
"Earth",
"earths",
"Earths",
},
{
"City",
"cities",
"Cities",
},
{
"Ray",
"rays",
"Rays",
},
{
"Fountain",
"fountains",
"Fountains",
},
{
"Life",
"lives",
"Lives",
},
{
"Leaf",
"leaves",
"Leaves",
},
}
for _, c := range cases {
testType := &types.Type{Name: types.Name{Name: c.typeName}}
if e, a := c.expectedPrivate, private.Name(testType); e != a {
t.Errorf("Unexpected result from private plural namer. Expected: %s, Got: %s", e, a)
}
if e, a := c.expectedPublic, public.Name(testType); e != a {
t.Errorf("Unexpected result from public plural namer. Expected: %s, Got: %s", e, a)
}
}
}

View File

@@ -1,68 +0,0 @@
/*
Copyright 2017 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 parser
import (
"testing"
)
func TestImportBuildPackage(t *testing.T) {
b := New()
if _, err := b.importBuildPackage("fake/dep"); err != nil {
t.Fatal(err)
}
if _, ok := b.buildPackages["fake/dep"]; !ok {
t.Errorf("missing expected, but got %v", b.buildPackages)
}
if len(b.buildPackages) > 1 {
// this would happen if the canonicalization failed to normalize the path
// you'd get a k8s.io/gengo/vendor/fake/dep key too
t.Errorf("missing one, but got %v", b.buildPackages)
}
}
func TestCanonicalizeImportPath(t *testing.T) {
tcs := []struct {
name string
input string
output string
}{
{
name: "passthrough",
input: "github.com/foo/bar",
output: "github.com/foo/bar",
},
{
name: "simple",
input: "github.com/foo/vendor/k8s.io/kubernetes/pkg/api",
output: "k8s.io/kubernetes/pkg/api",
},
{
name: "deeper",
input: "github.com/foo/bar/vendor/k8s.io/kubernetes/pkg/api",
output: "k8s.io/kubernetes/pkg/api",
},
}
for _, tc := range tcs {
actual := canonicalizeImportPath(tc.input)
if string(actual) != tc.output {
t.Errorf("%v: expected %q got %q", tc.name, tc.output, actual)
}
}
}

View File

@@ -1,459 +0,0 @@
/*
Copyright 2015 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 parser_test
import (
"bytes"
"path"
"path/filepath"
"reflect"
"testing"
"text/template"
"k8s.io/gengo/args"
"k8s.io/gengo/namer"
"k8s.io/gengo/parser"
"k8s.io/gengo/types"
)
func TestRecursive(t *testing.T) {
d := args.Default()
d.InputDirs = []string{"k8s.io/gengo/testdata/a/..."}
b, err := d.NewBuilder()
if err != nil {
t.Fatalf("Fail making builder: %v", err)
}
_, err = b.FindTypes()
if err != nil {
t.Fatalf("Fail finding types: %v", err)
}
foundB := false
for _, p := range b.FindPackages() {
t.Logf("Package: %v", p)
if p == "k8s.io/gengo/testdata/a/b" {
foundB = true
}
}
if !foundB {
t.Errorf("Expected to find packages a and b")
}
}
type file struct {
path string
contents string
}
// Pass files in topological order - deps first!
func construct(t *testing.T, files []file, testNamer namer.Namer) (*parser.Builder, types.Universe, []*types.Type) {
b := parser.New()
for _, f := range files {
if err := b.AddFileForTest(path.Dir(f.path), filepath.FromSlash(f.path), []byte(f.contents)); err != nil {
t.Fatal(err)
}
}
u, err := b.FindTypes()
if err != nil {
t.Fatal(err)
}
orderer := namer.Orderer{Namer: testNamer}
o := orderer.OrderUniverse(u)
return b, u, o
}
func TestBuilder(t *testing.T) {
var testFiles = []file{
{
path: "base/common/proto/common.go", contents: `
package common
type Object struct {
ID int64
}
`,
}, {
path: "base/foo/proto/foo.go", contents: `
package foo
import (
"base/common/proto"
)
type Blah struct {
common.Object
Count int64
Frobbers map[string]*Frobber
Baz []Object
Nickname *string
NumberIsAFavorite map[int]bool
}
type Frobber struct {
Name string
Amount int64
}
type Object struct {
common.Object
}
func AFunc(obj1 common.Object, obj2 Object) Frobber {
}
var AVar Frobber
var (
AnotherVar = Frobber{}
)
`,
},
}
var tmplText = `
package o
{{define "Struct"}}type {{Name .}} interface { {{range $m := .Members}}{{$n := Name $m.Type}}
{{if $m.Embedded}}{{$n}}{{else}}{{$m.Name}}() {{$n}}{{if $m.Type.Elem}}{{else}}
Set{{$m.Name}}({{$n}}){{end}}{{end}}{{end}}
}
{{end}}
{{define "Func"}}{{$s := .Underlying.Signature}}var {{Name .}} func({{range $index,$elem := $s.Parameters}}{{if $index}}, {{end}}{{Raw $elem}}{{end}}) {{if $s.Results|len |gt 1}}({{end}}{{range $index,$elem := $s.Results}}{{if $index}}, {{end}}{{Raw .}}{{end}}{{if $s.Results|len |gt 1}}){{end}} = {{Raw .}}
{{end}}
{{define "Var"}}{{$t := .Underlying}}var {{Name .}} {{Raw $t}} = {{Raw .}}
{{end}}
{{range $t := .}}{{if eq $t.Kind "Struct"}}{{template "Struct" $t}}{{end}}{{end}}
{{range $t := .}}{{if eq $t.Kind "DeclarationOf"}}{{if eq $t.Underlying.Kind "Func"}}{{template "Func" $t}}{{end}}{{end}}{{end}}
{{range $t := .}}{{if eq $t.Kind "DeclarationOf"}}{{if ne $t.Underlying.Kind "Func"}}{{template "Var" $t}}{{end}}{{end}}{{end}}`
var expect = `
package o
type CommonObject interface {
ID() Int64
SetID(Int64)
}
type FooBlah interface {
CommonObject
Count() Int64
SetCount(Int64)
Frobbers() MapStringToPointerFooFrobber
Baz() SliceFooObject
Nickname() PointerString
NumberIsAFavorite() MapIntToBool
}
type FooFrobber interface {
Name() String
SetName(String)
Amount() Int64
SetAmount(Int64)
}
type FooObject interface {
CommonObject
}
var FooAFunc func(proto.Object, proto.Object) proto.Frobber = proto.AFunc
var FooAVar proto.Frobber = proto.AVar
var FooAnotherVar proto.Frobber = proto.AnotherVar
`
testNamer := namer.NewPublicNamer(1, "proto")
rawNamer := namer.NewRawNamer("o", nil)
_, u, o := construct(t, testFiles, testNamer)
t.Logf("\n%v\n\n", o)
args := map[string]interface{}{
"Name": testNamer.Name,
"Raw": rawNamer.Name,
}
tmpl := template.Must(
template.New("").
Funcs(args).
Parse(tmplText),
)
buf := &bytes.Buffer{}
tmpl.Execute(buf, o)
if e, a := expect, buf.String(); e != a {
t.Errorf("Wanted, got:\n%v\n-----\n%v\n", e, a)
}
if p := u.Package("base/foo/proto"); !p.HasImport("base/common/proto") {
t.Errorf("Unexpected lack of import line: %s", p.Imports)
}
}
func TestStructParse(t *testing.T) {
var structTest = file{
path: "base/foo/proto/foo.go",
contents: `
package foo
// Blah is a test.
// A test, I tell you.
type Blah struct {
// A is the first field.
A int64 ` + "`" + `json:"a"` + "`" + `
// B is the second field.
// Multiline comments work.
B string ` + "`" + `json:"b"` + "`" + `
}
`,
}
_, u, o := construct(t, []file{structTest}, namer.NewPublicNamer(0))
t.Logf("%#v", o)
blahT := u.Type(types.Name{Package: "base/foo/proto", Name: "Blah"})
if blahT == nil {
t.Fatal("type not found")
}
if e, a := types.Struct, blahT.Kind; e != a {
t.Errorf("struct kind wrong, wanted %v, got %v", e, a)
}
if e, a := []string{"Blah is a test.", "A test, I tell you."}, blahT.CommentLines; !reflect.DeepEqual(e, a) {
t.Errorf("struct comment wrong, wanted %q, got %q", e, a)
}
m := types.Member{
Name: "B",
Embedded: false,
CommentLines: []string{"B is the second field.", "Multiline comments work."},
Tags: `json:"b"`,
Type: types.String,
}
if e, a := m, blahT.Members[1]; !reflect.DeepEqual(e, a) {
t.Errorf("wanted, got:\n%#v\n%#v", e, a)
}
}
func TestParseSecondClosestCommentLines(t *testing.T) {
const fileName = "base/foo/proto/foo.go"
testCases := []struct {
testFile file
expected []string
}{
{
testFile: file{
path: fileName, contents: `
package foo
// Blah's SecondClosestCommentLines.
// Another line.
// Blah is a test.
// A test, I tell you.
type Blah struct {
a int
}
`},
expected: []string{"Blah's SecondClosestCommentLines.", "Another line."},
},
{
testFile: file{
path: fileName, contents: `
package foo
// Blah's SecondClosestCommentLines.
// Another line.
type Blah struct {
a int
}
`},
expected: []string{"Blah's SecondClosestCommentLines.", "Another line."},
},
}
for _, test := range testCases {
_, u, o := construct(t, []file{test.testFile}, namer.NewPublicNamer(0))
t.Logf("%#v", o)
blahT := u.Type(types.Name{Package: "base/foo/proto", Name: "Blah"})
if e, a := test.expected, blahT.SecondClosestCommentLines; !reflect.DeepEqual(e, a) {
t.Errorf("struct second closest comment wrong, wanted %q, got %q", e, a)
}
}
}
func TestTypeKindParse(t *testing.T) {
var testFiles = []file{
{path: "a/foo.go", contents: "package a\ntype Test string\n"},
{path: "b/foo.go", contents: "package b\ntype Test map[int]string\n"},
{path: "c/foo.go", contents: "package c\ntype Test []string\n"},
{path: "d/foo.go", contents: "package d\ntype Test struct{a int; b struct{a int}; c map[int]string; d *string}\n"},
{path: "e/foo.go", contents: "package e\ntype Test *string\n"},
{path: "f/foo.go", contents: `
package f
import (
"a"
"b"
)
type Test []a.Test
type Test2 *a.Test
type Test3 map[a.Test]b.Test
type Test4 struct {
a struct {a a.Test; b b.Test}
b map[a.Test]b.Test
c *a.Test
d []a.Test
e []string
}
`},
{path: "g/foo.go", contents: `
package g
type Test func(a, b string) (c, d string)
func (t Test) Method(a, b string) (c, d string) { return t(a, b) }
type Interface interface{Method(a, b string) (c, d string)}
`},
}
// Check that the right types are found, and the namers give the expected names.
assertions := []struct {
Package, Name string
k types.Kind
names []string
}{
{
Package: "a", Name: "Test", k: types.Alias,
names: []string{"Test", "ATest", "test", "aTest", "a.Test"},
},
{
Package: "b", Name: "Test", k: types.Map,
names: []string{"Test", "BTest", "test", "bTest", "b.Test"},
},
{
Package: "c", Name: "Test", k: types.Slice,
names: []string{"Test", "CTest", "test", "cTest", "c.Test"},
},
{
Package: "d", Name: "Test", k: types.Struct,
names: []string{"Test", "DTest", "test", "dTest", "d.Test"},
},
{
Package: "e", Name: "Test", k: types.Pointer,
names: []string{"Test", "ETest", "test", "eTest", "e.Test"},
},
{
Package: "f", Name: "Test", k: types.Slice,
names: []string{"Test", "FTest", "test", "fTest", "f.Test"},
},
{
Package: "g", Name: "Test", k: types.Func,
names: []string{"Test", "GTest", "test", "gTest", "g.Test"},
},
{
Package: "g", Name: "Interface", k: types.Interface,
names: []string{"Interface", "GInterface", "interface", "gInterface", "g.Interface"},
},
{
Package: "", Name: "string", k: types.Builtin,
names: []string{"String", "String", "string", "string", "string"},
},
{
Package: "", Name: "int", k: types.Builtin,
names: []string{"Int", "Int", "int", "int", "int"},
},
{
Package: "", Name: "struct{a int}", k: types.Struct,
names: []string{"StructInt", "StructInt", "structInt", "structInt", "struct{a int}"},
},
{
Package: "", Name: "struct{a a.Test; b b.Test}", k: types.Struct,
names: []string{"StructTestTest", "StructATestBTest", "structTestTest", "structATestBTest", "struct{a a.Test; b b.Test}"},
},
{
Package: "", Name: "map[int]string", k: types.Map,
names: []string{"MapIntToString", "MapIntToString", "mapIntToString", "mapIntToString", "map[int]string"},
},
{
Package: "", Name: "map[a.Test]b.Test", k: types.Map,
names: []string{"MapTestToTest", "MapATestToBTest", "mapTestToTest", "mapATestToBTest", "map[a.Test]b.Test"},
},
{
Package: "", Name: "[]string", k: types.Slice,
names: []string{"SliceString", "SliceString", "sliceString", "sliceString", "[]string"},
},
{
Package: "", Name: "[]a.Test", k: types.Slice,
names: []string{"SliceTest", "SliceATest", "sliceTest", "sliceATest", "[]a.Test"},
},
{
Package: "", Name: "*string", k: types.Pointer,
names: []string{"PointerString", "PointerString", "pointerString", "pointerString", "*string"},
},
{
Package: "", Name: "*a.Test", k: types.Pointer,
names: []string{"PointerTest", "PointerATest", "pointerTest", "pointerATest", "*a.Test"},
},
}
namers := []namer.Namer{
namer.NewPublicNamer(0),
namer.NewPublicNamer(1),
namer.NewPrivateNamer(0),
namer.NewPrivateNamer(1),
namer.NewRawNamer("", nil),
}
for nameIndex, namer := range namers {
_, u, _ := construct(t, testFiles, namer)
t.Logf("Found types:\n")
for pkgName, pkg := range u {
for typeName, cur := range pkg.Types {
t.Logf("%q-%q: %s %s", pkgName, typeName, cur.Name, cur.Kind)
}
}
t.Logf("\n\n")
for _, item := range assertions {
n := types.Name{Package: item.Package, Name: item.Name}
thisType := u.Type(n)
if thisType == nil {
t.Errorf("type %s not found", n)
continue
}
underlyingType := thisType
if item.k != types.Alias && thisType.Kind == types.Alias {
underlyingType = thisType.Underlying
if underlyingType == nil {
t.Errorf("underlying type %s not found", n)
continue
}
}
if e, a := item.k, underlyingType.Kind; e != a {
t.Errorf("%v-%s: type kind wrong, wanted %v, got %v (%#v)", nameIndex, n, e, a, underlyingType)
}
if e, a := item.names[nameIndex], namer.Name(thisType); e != a {
t.Errorf("%v-%s: Expected %q, got %q", nameIndex, n, e, a)
}
}
// Also do some one-off checks
gtest := u.Type(types.Name{Package: "g", Name: "Test"})
if e, a := 1, len(gtest.Methods); e != a {
t.Errorf("expected %v but found %v methods: %#v", e, a, gtest)
}
iface := u.Type(types.Name{Package: "g", Name: "Interface"})
if e, a := 1, len(iface.Methods); e != a {
t.Errorf("expected %v but found %v methods: %#v", e, a, iface)
}
}
}

20
vendor/k8s.io/gengo/testdata/a/a.go generated vendored
View File

@@ -1,20 +0,0 @@
/*
Copyright YEAR 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 a
// A is a type for testing.
type A string

View File

@@ -1,20 +0,0 @@
/*
Copyright YEAR 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 b
// B is a type for testing
type B string

View File

@@ -1,86 +0,0 @@
/*
Copyright 2015 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 types
import (
"reflect"
"strings"
"testing"
)
func TestExtractCommentTags(t *testing.T) {
commentLines := []string{
"Human comment that is ignored.",
"+foo=value1",
"+bar",
"+foo=value2",
"+baz=qux,zrb=true",
}
a := ExtractCommentTags("+", commentLines)
e := map[string][]string{
"foo": {"value1", "value2"},
"bar": {""},
"baz": {"qux,zrb=true"},
}
if !reflect.DeepEqual(e, a) {
t.Errorf("Wanted %q, got %q", e, a)
}
}
func TestExtractSingleBoolCommentTag(t *testing.T) {
commentLines := []string{
"Human comment that is ignored.",
"+TRUE=true",
"+FALSE=false",
"+MULTI=true",
"+MULTI=false",
"+MULTI=multi",
"+NOTBOOL=blue",
"+EMPTY",
}
testCases := []struct {
key string
def bool
exp bool
err string // if set, ignore exp.
}{
{"TRUE", false, true, ""},
{"FALSE", true, false, ""},
{"MULTI", false, true, ""},
{"NOTBOOL", false, true, "is not boolean"},
{"EMPTY", false, true, "is not boolean"},
{"ABSENT", true, true, ""},
{"ABSENT", false, false, ""},
}
for i, tc := range testCases {
v, err := ExtractSingleBoolCommentTag("+", tc.key, tc.def, commentLines)
if err != nil && tc.err == "" {
t.Errorf("[%d]: unexpected failure: %v", i, err)
} else if err == nil && tc.err != "" {
t.Errorf("[%d]: expected failure: %v", i, tc.err)
} else if err != nil {
if !strings.Contains(err.Error(), tc.err) {
t.Errorf("[%d]: unexpected error: expected %q, got %q", i, tc.err, err)
}
} else if v != tc.exp {
t.Errorf("[%d]: unexpected value: expected %t, got %t", i, tc.exp, v)
}
}
}

View File

@@ -1,68 +0,0 @@
/*
Copyright 2015 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 types
import (
"reflect"
"testing"
)
func TestFlatten(t *testing.T) {
mapType := &Type{
Name: Name{Package: "", Name: "map[string]string"},
Kind: Map,
Key: String,
Elem: String,
}
m := []Member{
{
Name: "Baz",
Embedded: true,
Type: &Type{
Name: Name{Package: "pkg", Name: "Baz"},
Kind: Struct,
Members: []Member{
{Name: "Foo", Type: String},
{
Name: "Qux",
Embedded: true,
Type: &Type{
Name: Name{Package: "pkg", Name: "Qux"},
Kind: Struct,
Members: []Member{{Name: "Zot", Type: String}},
},
},
},
},
},
{Name: "Bar", Type: String},
{
Name: "NotSureIfLegal",
Embedded: true,
Type: mapType,
},
}
e := []Member{
{Name: "Bar", Type: String},
{Name: "NotSureIfLegal", Type: mapType, Embedded: true},
{Name: "Foo", Type: String},
{Name: "Zot", Type: String},
}
if a := FlattenMembers(m); !reflect.DeepEqual(e, a) {
t.Errorf("Expected \n%#v\n, got \n%#v\n", e, a)
}
}

View File

@@ -1,210 +0,0 @@
/*
Copyright 2015 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 types
import (
"testing"
)
func TestGetBuiltin(t *testing.T) {
u := Universe{}
if builtinPkg := u.Package(""); builtinPkg.Has("string") {
t.Errorf("Expected builtin package to not have builtins until they're asked for explicitly. %#v", builtinPkg)
}
s := u.Type(Name{Package: "", Name: "string"})
if s != String {
t.Errorf("Expected canonical string type.")
}
if builtinPkg := u.Package(""); !builtinPkg.Has("string") {
t.Errorf("Expected builtin package to exist and have builtins by default. %#v", builtinPkg)
}
if builtinPkg := u.Package(""); len(builtinPkg.Types) != 1 {
t.Errorf("Expected builtin package to not have builtins until they're asked for explicitly. %#v", builtinPkg)
}
}
func TestGetMarker(t *testing.T) {
u := Universe{}
n := Name{Package: "path/to/package", Name: "Foo"}
f := u.Type(n)
if f == nil || f.Name != n {
t.Errorf("Expected marker type.")
}
}
func Test_Type_IsPrimitive(t *testing.T) {
testCases := []struct {
typ Type
expect bool
}{
{
typ: Type{
Name: Name{Package: "pkgname", Name: "typename"},
Kind: Builtin,
},
expect: true,
},
{
typ: Type{
Name: Name{Package: "pkgname", Name: "typename"},
Kind: Alias,
Underlying: &Type{
Name: Name{Package: "pkgname", Name: "underlying"},
Kind: Builtin,
},
},
expect: true,
},
{
typ: Type{
Name: Name{Package: "pkgname", Name: "typename"},
Kind: Pointer,
Elem: &Type{
Name: Name{Package: "pkgname", Name: "pointee"},
Kind: Builtin,
},
},
expect: false,
},
{
typ: Type{
Name: Name{Package: "pkgname", Name: "typename"},
Kind: Struct,
},
expect: false,
},
}
for i, tc := range testCases {
r := tc.typ.IsPrimitive()
if r != tc.expect {
t.Errorf("case[%d]: expected %t, got %t", i, tc.expect, r)
}
}
}
func Test_Type_IsAssignable(t *testing.T) {
testCases := []struct {
typ Type
expect bool
}{
{
typ: Type{
Name: Name{Package: "pkgname", Name: "typename"},
Kind: Builtin,
},
expect: true,
},
{
typ: Type{
Name: Name{Package: "pkgname", Name: "typename"},
Kind: Alias,
Underlying: &Type{
Name: Name{Package: "pkgname", Name: "underlying"},
Kind: Builtin,
},
},
expect: true,
},
{
typ: Type{
Name: Name{Package: "pkgname", Name: "typename"},
Kind: Pointer,
Elem: &Type{
Name: Name{Package: "pkgname", Name: "pointee"},
Kind: Builtin,
},
},
expect: false,
},
{
typ: Type{
Name: Name{Package: "pkgname", Name: "typename"},
Kind: Struct, // Empty struct
},
expect: true,
},
{
typ: Type{
Name: Name{Package: "pkgname", Name: "typename"},
Kind: Struct,
Members: []Member{
{
Name: "m1",
Type: &Type{
Name: Name{Package: "pkgname", Name: "typename"},
Kind: Builtin,
},
},
{
Name: "m2",
Type: &Type{
Name: Name{Package: "pkgname", Name: "typename"},
Kind: Alias,
Underlying: &Type{
Name: Name{Package: "pkgname", Name: "underlying"},
Kind: Builtin,
},
},
},
{
Name: "m3",
Type: &Type{
Name: Name{Package: "pkgname", Name: "typename"},
Kind: Struct, // Empty struct
},
},
},
},
expect: true,
},
{
typ: Type{
Name: Name{Package: "pkgname", Name: "typename"},
Kind: Struct,
Members: []Member{
{
Name: "m1",
Type: &Type{
Name: Name{Package: "pkgname", Name: "typename"},
Kind: Builtin,
},
},
{
Name: "m2",
Type: &Type{
Name: Name{Package: "pkgname", Name: "typename"},
Kind: Pointer,
Elem: &Type{
Name: Name{Package: "pkgname", Name: "pointee"},
Kind: Builtin,
},
},
},
},
},
expect: false,
},
}
for i, tc := range testCases {
r := tc.typ.IsAssignable()
if r != tc.expect {
t.Errorf("case[%d]: expected %t, got %t", i, tc.expect, r)
}
}
}