Add glide.yaml and vendor deps
This commit is contained in:
parent
db918f12ad
commit
5b3d5e81bd
18880 changed files with 5166045 additions and 1 deletions
40
vendor/k8s.io/kubernetes/pkg/conversion/BUILD
generated
vendored
Normal file
40
vendor/k8s.io/kubernetes/pkg/conversion/BUILD
generated
vendored
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
licenses(["notice"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_binary",
|
||||
"go_library",
|
||||
"go_test",
|
||||
"cgo_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"cloner.go",
|
||||
"converter.go",
|
||||
"deep_equal.go",
|
||||
"doc.go",
|
||||
"helper.go",
|
||||
],
|
||||
tags = ["automanaged"],
|
||||
deps = ["//third_party/forked/golang/reflect:go_default_library"],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = [
|
||||
"converter_test.go",
|
||||
"deep_copy_test.go",
|
||||
"helper_test.go",
|
||||
],
|
||||
library = "go_default_library",
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//pkg/util/diff:go_default_library",
|
||||
"//vendor:github.com/google/gofuzz",
|
||||
"//vendor:github.com/spf13/pflag",
|
||||
],
|
||||
)
|
||||
5
vendor/k8s.io/kubernetes/pkg/conversion/OWNERS
generated
vendored
Normal file
5
vendor/k8s.io/kubernetes/pkg/conversion/OWNERS
generated
vendored
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
assignees:
|
||||
- derekwaynecarr
|
||||
- lavalamp
|
||||
- smarterclayton
|
||||
- wojtek-t
|
||||
249
vendor/k8s.io/kubernetes/pkg/conversion/cloner.go
generated
vendored
Normal file
249
vendor/k8s.io/kubernetes/pkg/conversion/cloner.go
generated
vendored
Normal file
|
|
@ -0,0 +1,249 @@
|
|||
/*
|
||||
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 conversion
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// Cloner knows how to copy one type to another.
|
||||
type Cloner struct {
|
||||
// Map from the type to a function which can do the deep copy.
|
||||
deepCopyFuncs map[reflect.Type]reflect.Value
|
||||
generatedDeepCopyFuncs map[reflect.Type]func(in interface{}, out interface{}, c *Cloner) error
|
||||
}
|
||||
|
||||
// NewCloner creates a new Cloner object.
|
||||
func NewCloner() *Cloner {
|
||||
c := &Cloner{
|
||||
deepCopyFuncs: map[reflect.Type]reflect.Value{},
|
||||
generatedDeepCopyFuncs: map[reflect.Type]func(in interface{}, out interface{}, c *Cloner) error{},
|
||||
}
|
||||
if err := c.RegisterDeepCopyFunc(byteSliceDeepCopy); err != nil {
|
||||
// If one of the deep-copy functions is malformed, detect it immediately.
|
||||
panic(err)
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
// Prevent recursing into every byte...
|
||||
func byteSliceDeepCopy(in *[]byte, out *[]byte, c *Cloner) error {
|
||||
if *in != nil {
|
||||
*out = make([]byte, len(*in))
|
||||
copy(*out, *in)
|
||||
} else {
|
||||
*out = nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Verifies whether a deep-copy function has a correct signature.
|
||||
func verifyDeepCopyFunctionSignature(ft reflect.Type) error {
|
||||
if ft.Kind() != reflect.Func {
|
||||
return fmt.Errorf("expected func, got: %v", ft)
|
||||
}
|
||||
if ft.NumIn() != 3 {
|
||||
return fmt.Errorf("expected three 'in' params, got %v", ft)
|
||||
}
|
||||
if ft.NumOut() != 1 {
|
||||
return fmt.Errorf("expected one 'out' param, got %v", ft)
|
||||
}
|
||||
if ft.In(0).Kind() != reflect.Ptr {
|
||||
return fmt.Errorf("expected pointer arg for 'in' param 0, got: %v", ft)
|
||||
}
|
||||
if ft.In(1) != ft.In(0) {
|
||||
return fmt.Errorf("expected 'in' param 0 the same as param 1, got: %v", ft)
|
||||
}
|
||||
var forClonerType Cloner
|
||||
if expected := reflect.TypeOf(&forClonerType); ft.In(2) != expected {
|
||||
return fmt.Errorf("expected '%v' arg for 'in' param 2, got: '%v'", expected, ft.In(2))
|
||||
}
|
||||
var forErrorType error
|
||||
// This convolution is necessary, otherwise TypeOf picks up on the fact
|
||||
// that forErrorType is nil
|
||||
errorType := reflect.TypeOf(&forErrorType).Elem()
|
||||
if ft.Out(0) != errorType {
|
||||
return fmt.Errorf("expected error return, got: %v", ft)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RegisterGeneratedDeepCopyFunc registers a copying func with the Cloner.
|
||||
// deepCopyFunc must take three parameters: a type input, a pointer to a
|
||||
// type output, and a pointer to Cloner. It should return an error.
|
||||
//
|
||||
// Example:
|
||||
// c.RegisterGeneratedDeepCopyFunc(
|
||||
// func(in Pod, out *Pod, c *Cloner) error {
|
||||
// // deep copy logic...
|
||||
// return nil
|
||||
// })
|
||||
func (c *Cloner) RegisterDeepCopyFunc(deepCopyFunc interface{}) error {
|
||||
fv := reflect.ValueOf(deepCopyFunc)
|
||||
ft := fv.Type()
|
||||
if err := verifyDeepCopyFunctionSignature(ft); err != nil {
|
||||
return err
|
||||
}
|
||||
c.deepCopyFuncs[ft.In(0)] = fv
|
||||
return nil
|
||||
}
|
||||
|
||||
// GeneratedDeepCopyFunc bundles an untyped generated deep-copy function of a type
|
||||
// with a reflection type object used as a key to lookup the deep-copy function.
|
||||
type GeneratedDeepCopyFunc struct {
|
||||
Fn func(in interface{}, out interface{}, c *Cloner) error
|
||||
InType reflect.Type
|
||||
}
|
||||
|
||||
// Similar to RegisterDeepCopyFunc, but registers deep copy function that were
|
||||
// automatically generated.
|
||||
func (c *Cloner) RegisterGeneratedDeepCopyFunc(fn GeneratedDeepCopyFunc) error {
|
||||
c.generatedDeepCopyFuncs[fn.InType] = fn.Fn
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeepCopy will perform a deep copy of a given object.
|
||||
func (c *Cloner) DeepCopy(in interface{}) (interface{}, error) {
|
||||
// Can be invalid if we run DeepCopy(X) where X is a nil interface type.
|
||||
// For example, we get an invalid value when someone tries to deep-copy
|
||||
// a nil labels.Selector.
|
||||
// This does not occur if X is nil and is a pointer to a concrete type.
|
||||
if in == nil {
|
||||
return nil, nil
|
||||
}
|
||||
inValue := reflect.ValueOf(in)
|
||||
outValue, err := c.deepCopy(inValue)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return outValue.Interface(), nil
|
||||
}
|
||||
|
||||
func (c *Cloner) deepCopy(src reflect.Value) (reflect.Value, error) {
|
||||
inType := src.Type()
|
||||
|
||||
switch src.Kind() {
|
||||
case reflect.Interface, reflect.Ptr, reflect.Map, reflect.Slice:
|
||||
if src.IsNil() {
|
||||
return src, nil
|
||||
}
|
||||
}
|
||||
|
||||
if fv, ok := c.deepCopyFuncs[inType]; ok {
|
||||
return c.customDeepCopy(src, fv)
|
||||
}
|
||||
if fv, ok := c.generatedDeepCopyFuncs[inType]; ok {
|
||||
var outValue reflect.Value
|
||||
outValue = reflect.New(inType.Elem())
|
||||
err := fv(src.Interface(), outValue.Interface(), c)
|
||||
return outValue, err
|
||||
}
|
||||
return c.defaultDeepCopy(src)
|
||||
}
|
||||
|
||||
func (c *Cloner) customDeepCopy(src, fv reflect.Value) (reflect.Value, error) {
|
||||
outValue := reflect.New(src.Type().Elem())
|
||||
args := []reflect.Value{src, outValue, reflect.ValueOf(c)}
|
||||
result := fv.Call(args)[0].Interface()
|
||||
// This convolution is necessary because nil interfaces won't convert
|
||||
// to error.
|
||||
if result == nil {
|
||||
return outValue, nil
|
||||
}
|
||||
return outValue, result.(error)
|
||||
}
|
||||
|
||||
func (c *Cloner) defaultDeepCopy(src reflect.Value) (reflect.Value, error) {
|
||||
switch src.Kind() {
|
||||
case reflect.Chan, reflect.Func, reflect.UnsafePointer, reflect.Uintptr:
|
||||
return src, fmt.Errorf("cannot deep copy kind: %s", src.Kind())
|
||||
case reflect.Array:
|
||||
dst := reflect.New(src.Type())
|
||||
for i := 0; i < src.Len(); i++ {
|
||||
copyVal, err := c.deepCopy(src.Index(i))
|
||||
if err != nil {
|
||||
return src, err
|
||||
}
|
||||
dst.Elem().Index(i).Set(copyVal)
|
||||
}
|
||||
return dst.Elem(), nil
|
||||
case reflect.Interface:
|
||||
if src.IsNil() {
|
||||
return src, nil
|
||||
}
|
||||
return c.deepCopy(src.Elem())
|
||||
case reflect.Map:
|
||||
if src.IsNil() {
|
||||
return src, nil
|
||||
}
|
||||
dst := reflect.MakeMap(src.Type())
|
||||
for _, k := range src.MapKeys() {
|
||||
copyVal, err := c.deepCopy(src.MapIndex(k))
|
||||
if err != nil {
|
||||
return src, err
|
||||
}
|
||||
dst.SetMapIndex(k, copyVal)
|
||||
}
|
||||
return dst, nil
|
||||
case reflect.Ptr:
|
||||
if src.IsNil() {
|
||||
return src, nil
|
||||
}
|
||||
dst := reflect.New(src.Type().Elem())
|
||||
copyVal, err := c.deepCopy(src.Elem())
|
||||
if err != nil {
|
||||
return src, err
|
||||
}
|
||||
dst.Elem().Set(copyVal)
|
||||
return dst, nil
|
||||
case reflect.Slice:
|
||||
if src.IsNil() {
|
||||
return src, nil
|
||||
}
|
||||
dst := reflect.MakeSlice(src.Type(), 0, src.Len())
|
||||
for i := 0; i < src.Len(); i++ {
|
||||
copyVal, err := c.deepCopy(src.Index(i))
|
||||
if err != nil {
|
||||
return src, err
|
||||
}
|
||||
dst = reflect.Append(dst, copyVal)
|
||||
}
|
||||
return dst, nil
|
||||
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, nil
|
||||
}
|
||||
copyVal, err := c.deepCopy(src.Field(i))
|
||||
if err != nil {
|
||||
return src, err
|
||||
}
|
||||
dst.Elem().Field(i).Set(copyVal)
|
||||
}
|
||||
return dst.Elem(), nil
|
||||
|
||||
default:
|
||||
// Value types like numbers, booleans, and strings.
|
||||
return src, nil
|
||||
}
|
||||
}
|
||||
953
vendor/k8s.io/kubernetes/pkg/conversion/converter.go
generated
vendored
Normal file
953
vendor/k8s.io/kubernetes/pkg/conversion/converter.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load diff
826
vendor/k8s.io/kubernetes/pkg/conversion/converter_test.go
generated
vendored
Normal file
826
vendor/k8s.io/kubernetes/pkg/conversion/converter_test.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load diff
161
vendor/k8s.io/kubernetes/pkg/conversion/deep_copy_test.go
generated
vendored
Normal file
161
vendor/k8s.io/kubernetes/pkg/conversion/deep_copy_test.go
generated
vendored
Normal file
|
|
@ -0,0 +1,161 @@
|
|||
/*
|
||||
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 conversion
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/google/gofuzz"
|
||||
)
|
||||
|
||||
func TestDeepCopy(t *testing.T) {
|
||||
semantic := EqualitiesOrDie()
|
||||
f := fuzz.New().NilChance(.5).NumElements(0, 100)
|
||||
table := []interface{}{
|
||||
map[string]string{},
|
||||
int(5),
|
||||
"hello world",
|
||||
struct {
|
||||
A, B, C struct {
|
||||
D map[string]int
|
||||
}
|
||||
X []int
|
||||
Y []byte
|
||||
}{},
|
||||
}
|
||||
for _, obj := range table {
|
||||
obj2, err := NewCloner().DeepCopy(obj)
|
||||
if err != nil {
|
||||
t.Errorf("Error: couldn't copy %#v", obj)
|
||||
continue
|
||||
}
|
||||
if e, a := obj, obj2; !semantic.DeepEqual(e, a) {
|
||||
t.Errorf("expected %#v\ngot %#v", e, a)
|
||||
}
|
||||
|
||||
obj3 := reflect.New(reflect.TypeOf(obj)).Interface()
|
||||
f.Fuzz(obj3)
|
||||
obj4, err := NewCloner().DeepCopy(obj3)
|
||||
if err != nil {
|
||||
t.Errorf("Error: couldn't copy %#v", obj)
|
||||
continue
|
||||
}
|
||||
if e, a := obj3, obj4; !semantic.DeepEqual(e, a) {
|
||||
t.Errorf("expected %#v\ngot %#v", e, a)
|
||||
}
|
||||
f.Fuzz(obj3)
|
||||
}
|
||||
}
|
||||
|
||||
func copyOrDie(t *testing.T, in interface{}) interface{} {
|
||||
out, err := NewCloner().DeepCopy(in)
|
||||
if err != nil {
|
||||
t.Fatalf("DeepCopy failed: %#q: %v", in, err)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestDeepCopySliceSeparate(t *testing.T) {
|
||||
x := []int{5}
|
||||
y := copyOrDie(t, x).([]int)
|
||||
x[0] = 3
|
||||
if y[0] == 3 {
|
||||
t.Errorf("deep copy wasn't deep: %#q %#q", x, y)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeepCopyArraySeparate(t *testing.T) {
|
||||
x := [1]int{5}
|
||||
y := copyOrDie(t, x).([1]int)
|
||||
x[0] = 3
|
||||
if y[0] == 3 {
|
||||
t.Errorf("deep copy wasn't deep: %#q %#q", x, y)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeepCopyMapSeparate(t *testing.T) {
|
||||
x := map[string]int{"foo": 5}
|
||||
y := copyOrDie(t, x).(map[string]int)
|
||||
x["foo"] = 3
|
||||
if y["foo"] == 3 {
|
||||
t.Errorf("deep copy wasn't deep: %#q %#q", x, y)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeepCopyPointerSeparate(t *testing.T) {
|
||||
z := 5
|
||||
x := &z
|
||||
y := copyOrDie(t, x).(*int)
|
||||
*x = 3
|
||||
if *y == 3 {
|
||||
t.Errorf("deep copy wasn't deep: %#q %#q", x, y)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeepCopyStruct(t *testing.T) {
|
||||
type Foo struct {
|
||||
A int
|
||||
}
|
||||
type Bar struct {
|
||||
Foo
|
||||
F *Foo
|
||||
}
|
||||
a := &Bar{Foo{1}, &Foo{2}}
|
||||
b := copyOrDie(t, a).(*Bar)
|
||||
a.A = 3
|
||||
a.F.A = 4
|
||||
|
||||
if b.A != 1 || b.F.A != 2 {
|
||||
t.Errorf("deep copy wasn't deep: %#v, %#v", a, b)
|
||||
}
|
||||
}
|
||||
|
||||
var result interface{}
|
||||
|
||||
func BenchmarkDeepCopy(b *testing.B) {
|
||||
table := []interface{}{
|
||||
map[string]string{},
|
||||
int(5),
|
||||
"hello world",
|
||||
struct {
|
||||
A, B, C struct {
|
||||
D map[string]int
|
||||
}
|
||||
X []int
|
||||
Y []byte
|
||||
}{},
|
||||
}
|
||||
|
||||
f := fuzz.New().RandSource(rand.NewSource(1)).NilChance(.5).NumElements(0, 100)
|
||||
for i := range table {
|
||||
out := table[i]
|
||||
obj := reflect.New(reflect.TypeOf(out)).Interface()
|
||||
f.Fuzz(obj)
|
||||
table[i] = obj
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
var r interface{}
|
||||
for i := 0; i < b.N; i++ {
|
||||
for j := range table {
|
||||
r, _ = NewCloner().DeepCopy(table[j])
|
||||
}
|
||||
}
|
||||
result = r
|
||||
}
|
||||
36
vendor/k8s.io/kubernetes/pkg/conversion/deep_equal.go
generated
vendored
Normal file
36
vendor/k8s.io/kubernetes/pkg/conversion/deep_equal.go
generated
vendored
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
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 conversion
|
||||
|
||||
import (
|
||||
"k8s.io/kubernetes/third_party/forked/golang/reflect"
|
||||
)
|
||||
|
||||
// The code for this type must be located in third_party, since it forks from
|
||||
// go std lib. But for convenience, we expose the type here, too.
|
||||
type Equalities struct {
|
||||
reflect.Equalities
|
||||
}
|
||||
|
||||
// For convenience, panics on errors
|
||||
func EqualitiesOrDie(funcs ...interface{}) Equalities {
|
||||
e := Equalities{reflect.Equalities{}}
|
||||
if err := e.AddFuncs(funcs...); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return e
|
||||
}
|
||||
24
vendor/k8s.io/kubernetes/pkg/conversion/doc.go
generated
vendored
Normal file
24
vendor/k8s.io/kubernetes/pkg/conversion/doc.go
generated
vendored
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
/*
|
||||
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 conversion provides go object versioning.
|
||||
//
|
||||
// Specifically, conversion provides a way for you to define multiple versions
|
||||
// of the same object. You may write functions which implement conversion logic,
|
||||
// but for the fields which did not change, copying is automated. This makes it
|
||||
// easy to modify the structures you use in memory without affecting the format
|
||||
// you store on disk or respond to in your external API calls.
|
||||
package conversion // import "k8s.io/kubernetes/pkg/conversion"
|
||||
39
vendor/k8s.io/kubernetes/pkg/conversion/helper.go
generated
vendored
Normal file
39
vendor/k8s.io/kubernetes/pkg/conversion/helper.go
generated
vendored
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
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 conversion
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// EnforcePtr ensures that obj is a pointer of some sort. Returns a reflect.Value
|
||||
// of the dereferenced pointer, ensuring that it is settable/addressable.
|
||||
// Returns an error if this is not possible.
|
||||
func EnforcePtr(obj interface{}) (reflect.Value, error) {
|
||||
v := reflect.ValueOf(obj)
|
||||
if v.Kind() != reflect.Ptr {
|
||||
if v.Kind() == reflect.Invalid {
|
||||
return reflect.Value{}, fmt.Errorf("expected pointer, but got invalid kind")
|
||||
}
|
||||
return reflect.Value{}, fmt.Errorf("expected pointer, but got %v type", v.Type())
|
||||
}
|
||||
if v.IsNil() {
|
||||
return reflect.Value{}, fmt.Errorf("expected pointer, but got nil")
|
||||
}
|
||||
return v.Elem(), nil
|
||||
}
|
||||
38
vendor/k8s.io/kubernetes/pkg/conversion/helper_test.go
generated
vendored
Normal file
38
vendor/k8s.io/kubernetes/pkg/conversion/helper_test.go
generated
vendored
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
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 conversion
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestInvalidPtrValueKind(t *testing.T) {
|
||||
var simple interface{}
|
||||
switch obj := simple.(type) {
|
||||
default:
|
||||
_, err := EnforcePtr(obj)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error on invalid kind")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnforceNilPtr(t *testing.T) {
|
||||
var nilPtr *struct{}
|
||||
_, err := EnforcePtr(nilPtr)
|
||||
if err == nil {
|
||||
t.Errorf("Expected error on nil pointer")
|
||||
}
|
||||
}
|
||||
31
vendor/k8s.io/kubernetes/pkg/conversion/queryparams/BUILD
generated
vendored
Normal file
31
vendor/k8s.io/kubernetes/pkg/conversion/queryparams/BUILD
generated
vendored
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
licenses(["notice"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_binary",
|
||||
"go_library",
|
||||
"go_test",
|
||||
"cgo_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"convert.go",
|
||||
"doc.go",
|
||||
],
|
||||
tags = ["automanaged"],
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_xtest",
|
||||
srcs = ["convert_test.go"],
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//pkg/apis/meta/v1:go_default_library",
|
||||
"//pkg/conversion/queryparams:go_default_library",
|
||||
"//pkg/runtime/schema:go_default_library",
|
||||
],
|
||||
)
|
||||
188
vendor/k8s.io/kubernetes/pkg/conversion/queryparams/convert.go
generated
vendored
Normal file
188
vendor/k8s.io/kubernetes/pkg/conversion/queryparams/convert.go
generated
vendored
Normal file
|
|
@ -0,0 +1,188 @@
|
|||
/*
|
||||
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 queryparams
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Marshaler converts an object to a query parameter string representation
|
||||
type Marshaler interface {
|
||||
MarshalQueryParameter() (string, error)
|
||||
}
|
||||
|
||||
// Unmarshaler converts a string representation to an object
|
||||
type Unmarshaler interface {
|
||||
UnmarshalQueryParameter(string) error
|
||||
}
|
||||
|
||||
func jsonTag(field reflect.StructField) (string, bool) {
|
||||
structTag := field.Tag.Get("json")
|
||||
if len(structTag) == 0 {
|
||||
return "", false
|
||||
}
|
||||
parts := strings.Split(structTag, ",")
|
||||
tag := parts[0]
|
||||
if tag == "-" {
|
||||
tag = ""
|
||||
}
|
||||
omitempty := false
|
||||
parts = parts[1:]
|
||||
for _, part := range parts {
|
||||
if part == "omitempty" {
|
||||
omitempty = true
|
||||
break
|
||||
}
|
||||
}
|
||||
return tag, omitempty
|
||||
}
|
||||
|
||||
func formatValue(value interface{}) string {
|
||||
return fmt.Sprintf("%v", value)
|
||||
}
|
||||
|
||||
func isPointerKind(kind reflect.Kind) bool {
|
||||
return kind == reflect.Ptr
|
||||
}
|
||||
|
||||
func isStructKind(kind reflect.Kind) bool {
|
||||
return kind == reflect.Struct
|
||||
}
|
||||
|
||||
func isValueKind(kind reflect.Kind) bool {
|
||||
switch kind {
|
||||
case reflect.String, reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16,
|
||||
reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8,
|
||||
reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32,
|
||||
reflect.Float64, reflect.Complex64, reflect.Complex128:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func zeroValue(value reflect.Value) bool {
|
||||
return reflect.DeepEqual(reflect.Zero(value.Type()).Interface(), value.Interface())
|
||||
}
|
||||
|
||||
func customMarshalValue(value reflect.Value) (reflect.Value, bool) {
|
||||
// Return unless we implement a custom query marshaler
|
||||
if !value.CanInterface() {
|
||||
return reflect.Value{}, false
|
||||
}
|
||||
|
||||
marshaler, ok := value.Interface().(Marshaler)
|
||||
if !ok {
|
||||
return reflect.Value{}, false
|
||||
}
|
||||
|
||||
// Don't invoke functions on nil pointers
|
||||
// If the type implements MarshalQueryParameter, AND the tag is not omitempty, AND the value is a nil pointer, "" seems like a reasonable response
|
||||
if isPointerKind(value.Kind()) && zeroValue(value) {
|
||||
return reflect.ValueOf(""), true
|
||||
}
|
||||
|
||||
// Get the custom marshalled value
|
||||
v, err := marshaler.MarshalQueryParameter()
|
||||
if err != nil {
|
||||
return reflect.Value{}, false
|
||||
}
|
||||
return reflect.ValueOf(v), true
|
||||
}
|
||||
|
||||
func addParam(values url.Values, tag string, omitempty bool, value reflect.Value) {
|
||||
if omitempty && zeroValue(value) {
|
||||
return
|
||||
}
|
||||
val := ""
|
||||
iValue := fmt.Sprintf("%v", value.Interface())
|
||||
|
||||
if iValue != "<nil>" {
|
||||
val = iValue
|
||||
}
|
||||
values.Add(tag, val)
|
||||
}
|
||||
|
||||
func addListOfParams(values url.Values, tag string, omitempty bool, list reflect.Value) {
|
||||
for i := 0; i < list.Len(); i++ {
|
||||
addParam(values, tag, omitempty, list.Index(i))
|
||||
}
|
||||
}
|
||||
|
||||
// Convert takes an object and converts it to a url.Values object using JSON tags as
|
||||
// parameter names. Only top-level simple values, arrays, and slices are serialized.
|
||||
// Embedded structs, maps, etc. will not be serialized.
|
||||
func Convert(obj interface{}) (url.Values, error) {
|
||||
result := url.Values{}
|
||||
if obj == nil {
|
||||
return result, nil
|
||||
}
|
||||
var sv reflect.Value
|
||||
switch reflect.TypeOf(obj).Kind() {
|
||||
case reflect.Ptr, reflect.Interface:
|
||||
sv = reflect.ValueOf(obj).Elem()
|
||||
default:
|
||||
return nil, fmt.Errorf("expecting a pointer or interface")
|
||||
}
|
||||
st := sv.Type()
|
||||
if !isStructKind(st.Kind()) {
|
||||
return nil, fmt.Errorf("expecting a pointer to a struct")
|
||||
}
|
||||
|
||||
// Check all object fields
|
||||
convertStruct(result, st, sv)
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func convertStruct(result url.Values, st reflect.Type, sv reflect.Value) {
|
||||
for i := 0; i < st.NumField(); i++ {
|
||||
field := sv.Field(i)
|
||||
tag, omitempty := jsonTag(st.Field(i))
|
||||
if len(tag) == 0 {
|
||||
continue
|
||||
}
|
||||
ft := field.Type()
|
||||
|
||||
kind := ft.Kind()
|
||||
if isPointerKind(kind) {
|
||||
ft = ft.Elem()
|
||||
kind = ft.Kind()
|
||||
if !field.IsNil() {
|
||||
field = reflect.Indirect(field)
|
||||
}
|
||||
}
|
||||
|
||||
switch {
|
||||
case isValueKind(kind):
|
||||
addParam(result, tag, omitempty, field)
|
||||
case kind == reflect.Array || kind == reflect.Slice:
|
||||
if isValueKind(ft.Elem().Kind()) {
|
||||
addListOfParams(result, tag, omitempty, field)
|
||||
}
|
||||
case isStructKind(kind) && !(zeroValue(field) && omitempty):
|
||||
if marshalValue, ok := customMarshalValue(field); ok {
|
||||
addParam(result, tag, omitempty, marshalValue)
|
||||
} else {
|
||||
convertStruct(result, ft, field)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
212
vendor/k8s.io/kubernetes/pkg/conversion/queryparams/convert_test.go
generated
vendored
Normal file
212
vendor/k8s.io/kubernetes/pkg/conversion/queryparams/convert_test.go
generated
vendored
Normal file
|
|
@ -0,0 +1,212 @@
|
|||
/*
|
||||
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 queryparams_test
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
metav1 "k8s.io/kubernetes/pkg/apis/meta/v1"
|
||||
"k8s.io/kubernetes/pkg/conversion/queryparams"
|
||||
"k8s.io/kubernetes/pkg/runtime/schema"
|
||||
)
|
||||
|
||||
type namedString string
|
||||
type namedBool bool
|
||||
|
||||
type bar struct {
|
||||
Float1 float32 `json:"float1"`
|
||||
Float2 float64 `json:"float2"`
|
||||
Int1 int64 `json:"int1,omitempty"`
|
||||
Int2 int32 `json:"int2,omitempty"`
|
||||
Int3 int16 `json:"int3,omitempty"`
|
||||
Str1 string `json:"str1,omitempty"`
|
||||
Ignored int
|
||||
Ignored2 string
|
||||
}
|
||||
|
||||
func (obj *bar) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }
|
||||
|
||||
type foo struct {
|
||||
Str string `json:"str"`
|
||||
Integer int `json:"integer,omitempty"`
|
||||
Slice []string `json:"slice,omitempty"`
|
||||
Boolean bool `json:"boolean,omitempty"`
|
||||
NamedStr namedString `json:"namedStr,omitempty"`
|
||||
NamedBool namedBool `json:"namedBool,omitempty"`
|
||||
Foobar bar `json:"foobar,omitempty"`
|
||||
Testmap map[string]string `json:"testmap,omitempty"`
|
||||
}
|
||||
|
||||
func (obj *foo) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }
|
||||
|
||||
type baz struct {
|
||||
Ptr *int `json:"ptr"`
|
||||
Bptr *bool `json:"bptr,omitempty"`
|
||||
}
|
||||
|
||||
func (obj *baz) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }
|
||||
|
||||
// childStructs tests some of the types we serialize to query params for log API calls
|
||||
// notably, the nested time struct
|
||||
type childStructs struct {
|
||||
Container string `json:"container,omitempty"`
|
||||
Follow bool `json:"follow,omitempty"`
|
||||
Previous bool `json:"previous,omitempty"`
|
||||
SinceSeconds *int64 `json:"sinceSeconds,omitempty"`
|
||||
SinceTime *metav1.Time `json:"sinceTime,omitempty"`
|
||||
EmptyTime *metav1.Time `json:"emptyTime"`
|
||||
}
|
||||
|
||||
func (obj *childStructs) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }
|
||||
|
||||
func validateResult(t *testing.T, input interface{}, actual, expected url.Values) {
|
||||
local := url.Values{}
|
||||
for k, v := range expected {
|
||||
local[k] = v
|
||||
}
|
||||
for k, v := range actual {
|
||||
if ev, ok := local[k]; !ok || !reflect.DeepEqual(ev, v) {
|
||||
if !ok {
|
||||
t.Errorf("%#v: actual value key %s not found in expected map", input, k)
|
||||
} else {
|
||||
t.Errorf("%#v: values don't match: actual: %#v, expected: %#v", input, v, ev)
|
||||
}
|
||||
}
|
||||
delete(local, k)
|
||||
}
|
||||
if len(local) > 0 {
|
||||
t.Errorf("%#v: expected map has keys that were not found in actual map: %#v", input, local)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvert(t *testing.T) {
|
||||
sinceSeconds := int64(123)
|
||||
sinceTime := metav1.Date(2000, 1, 1, 12, 34, 56, 0, time.UTC)
|
||||
|
||||
tests := []struct {
|
||||
input interface{}
|
||||
expected url.Values
|
||||
}{
|
||||
{
|
||||
input: &foo{
|
||||
Str: "hello",
|
||||
},
|
||||
expected: url.Values{"str": {"hello"}},
|
||||
},
|
||||
{
|
||||
input: &foo{
|
||||
Str: "test string",
|
||||
Slice: []string{"one", "two", "three"},
|
||||
Integer: 234,
|
||||
Boolean: true,
|
||||
},
|
||||
expected: url.Values{"str": {"test string"}, "slice": {"one", "two", "three"}, "integer": {"234"}, "boolean": {"true"}},
|
||||
},
|
||||
{
|
||||
input: &foo{
|
||||
Str: "named types",
|
||||
NamedStr: "value1",
|
||||
NamedBool: true,
|
||||
},
|
||||
expected: url.Values{"str": {"named types"}, "namedStr": {"value1"}, "namedBool": {"true"}},
|
||||
},
|
||||
{
|
||||
input: &foo{
|
||||
Str: "don't ignore embedded struct",
|
||||
Foobar: bar{
|
||||
Float1: 5.0,
|
||||
},
|
||||
},
|
||||
expected: url.Values{"str": {"don't ignore embedded struct"}, "float1": {"5"}, "float2": {"0"}},
|
||||
},
|
||||
{
|
||||
// Ignore untagged fields
|
||||
input: &bar{
|
||||
Float1: 23.5,
|
||||
Float2: 100.7,
|
||||
Int1: 1,
|
||||
Int2: 2,
|
||||
Int3: 3,
|
||||
Ignored: 1,
|
||||
Ignored2: "ignored",
|
||||
},
|
||||
expected: url.Values{"float1": {"23.5"}, "float2": {"100.7"}, "int1": {"1"}, "int2": {"2"}, "int3": {"3"}},
|
||||
},
|
||||
{
|
||||
// include fields that are not tagged omitempty
|
||||
input: &foo{
|
||||
NamedStr: "named str",
|
||||
},
|
||||
expected: url.Values{"str": {""}, "namedStr": {"named str"}},
|
||||
},
|
||||
{
|
||||
input: &baz{
|
||||
Ptr: intp(5),
|
||||
Bptr: boolp(true),
|
||||
},
|
||||
expected: url.Values{"ptr": {"5"}, "bptr": {"true"}},
|
||||
},
|
||||
{
|
||||
input: &baz{
|
||||
Bptr: boolp(true),
|
||||
},
|
||||
expected: url.Values{"ptr": {""}, "bptr": {"true"}},
|
||||
},
|
||||
{
|
||||
input: &baz{
|
||||
Ptr: intp(5),
|
||||
},
|
||||
expected: url.Values{"ptr": {"5"}},
|
||||
},
|
||||
{
|
||||
input: &childStructs{
|
||||
Container: "mycontainer",
|
||||
Follow: true,
|
||||
Previous: true,
|
||||
SinceSeconds: &sinceSeconds,
|
||||
SinceTime: &sinceTime, // test a custom marshaller
|
||||
EmptyTime: nil, // test a nil custom marshaller without omitempty
|
||||
},
|
||||
expected: url.Values{"container": {"mycontainer"}, "follow": {"true"}, "previous": {"true"}, "sinceSeconds": {"123"}, "sinceTime": {"2000-01-01T12:34:56Z"}, "emptyTime": {""}},
|
||||
},
|
||||
{
|
||||
input: &childStructs{
|
||||
Container: "mycontainer",
|
||||
Follow: true,
|
||||
Previous: true,
|
||||
SinceSeconds: &sinceSeconds,
|
||||
SinceTime: nil, // test a nil custom marshaller with omitempty
|
||||
},
|
||||
expected: url.Values{"container": {"mycontainer"}, "follow": {"true"}, "previous": {"true"}, "sinceSeconds": {"123"}, "emptyTime": {""}},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
result, err := queryparams.Convert(test.input)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error while converting %#v: %v", test.input, err)
|
||||
}
|
||||
validateResult(t, test.input, result, test.expected)
|
||||
}
|
||||
}
|
||||
|
||||
func intp(n int) *int { return &n }
|
||||
|
||||
func boolp(b bool) *bool { return &b }
|
||||
19
vendor/k8s.io/kubernetes/pkg/conversion/queryparams/doc.go
generated
vendored
Normal file
19
vendor/k8s.io/kubernetes/pkg/conversion/queryparams/doc.go
generated
vendored
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
/*
|
||||
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 queryparams provides conversion from versioned
|
||||
// runtime objects to URL query values
|
||||
package queryparams // import "k8s.io/kubernetes/pkg/conversion/queryparams"
|
||||
Loading…
Add table
Add a link
Reference in a new issue