mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-01 07:47:56 +00:00
Merge pull request #1693 from smarterclayton/move_diff
Minor moves of functions into their own files
This commit is contained in:
commit
01083416dd
@ -25,7 +25,6 @@ import (
|
|||||||
internal "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
internal "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
_ "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1"
|
_ "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1"
|
||||||
_ "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta2"
|
_ "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta2"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||||
"github.com/fsouza/go-dockerclient"
|
"github.com/fsouza/go-dockerclient"
|
||||||
"github.com/google/gofuzz"
|
"github.com/google/gofuzz"
|
||||||
@ -128,7 +127,7 @@ func TestInternalRoundTrip(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !reflect.DeepEqual(obj, actual) {
|
if !reflect.DeepEqual(obj, actual) {
|
||||||
t.Errorf("%s: diff %s", k, runtime.ObjectDiff(obj, actual))
|
t.Errorf("%s: diff %s", k, util.ObjectDiff(obj, actual))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -130,7 +130,7 @@ func runTest(t *testing.T, codec runtime.Codec, source runtime.Object) {
|
|||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
if !reflect.DeepEqual(source, obj2) {
|
if !reflect.DeepEqual(source, obj2) {
|
||||||
t.Errorf("1: %v: diff: %v", name, runtime.ObjectDiff(source, obj2))
|
t.Errorf("1: %v: diff: %v", name, util.ObjectDiff(source, obj2))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -141,7 +141,7 @@ func runTest(t *testing.T, codec runtime.Codec, source runtime.Object) {
|
|||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
if !reflect.DeepEqual(source, obj3) {
|
if !reflect.DeepEqual(source, obj3) {
|
||||||
t.Errorf("3: %v: diff: %v", name, runtime.ObjectDiff(source, obj3))
|
t.Errorf("3: %v: diff: %v", name, util.ObjectDiff(source, obj3))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
43
pkg/runtime/codec.go
Normal file
43
pkg/runtime/codec.go
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2014 Google Inc. All rights reserved.
|
||||||
|
|
||||||
|
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 runtime
|
||||||
|
|
||||||
|
// CodecFor returns a Codec that invokes Encode with the provided version.
|
||||||
|
func CodecFor(scheme *Scheme, version string) Codec {
|
||||||
|
return &codecWrapper{scheme, version}
|
||||||
|
}
|
||||||
|
|
||||||
|
// EncodeOrDie is a version of Encode which will panic instead of returning an error. For tests.
|
||||||
|
func EncodeOrDie(codec Codec, obj Object) string {
|
||||||
|
bytes, err := codec.Encode(obj)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return string(bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
// codecWrapper implements encoding to an alternative
|
||||||
|
// default version for a scheme.
|
||||||
|
type codecWrapper struct {
|
||||||
|
*Scheme
|
||||||
|
version string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encode implements Codec
|
||||||
|
func (c *codecWrapper) Encode(obj Object) ([]byte, error) {
|
||||||
|
return c.Scheme.EncodeToVersion(obj, c.version)
|
||||||
|
}
|
@ -17,41 +17,13 @@ limitations under the License.
|
|||||||
package runtime
|
package runtime
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/conversion"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/conversion"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
|
||||||
"gopkg.in/v1/yaml"
|
"gopkg.in/v1/yaml"
|
||||||
)
|
)
|
||||||
|
|
||||||
// codecWrapper implements encoding to an alternative
|
|
||||||
// default version for a scheme.
|
|
||||||
type codecWrapper struct {
|
|
||||||
*Scheme
|
|
||||||
version string
|
|
||||||
}
|
|
||||||
|
|
||||||
// Encode implements Codec
|
|
||||||
func (c *codecWrapper) Encode(obj Object) ([]byte, error) {
|
|
||||||
return c.Scheme.EncodeToVersion(obj, c.version)
|
|
||||||
}
|
|
||||||
|
|
||||||
// CodecFor returns a Codec that invokes Encode with the provided version.
|
|
||||||
func CodecFor(scheme *Scheme, version string) Codec {
|
|
||||||
return &codecWrapper{scheme, version}
|
|
||||||
}
|
|
||||||
|
|
||||||
// EncodeOrDie is a version of Encode which will panic instead of returning an error. For tests.
|
|
||||||
func EncodeOrDie(codec Codec, obj Object) string {
|
|
||||||
bytes, err := codec.Encode(obj)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return string(bytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Scheme defines methods for serializing and deserializing API objects. It
|
// Scheme defines methods for serializing and deserializing API objects. It
|
||||||
// is an adaptation of conversion's Scheme for our API objects.
|
// is an adaptation of conversion's Scheme for our API objects.
|
||||||
type Scheme struct {
|
type Scheme struct {
|
||||||
@ -368,32 +340,6 @@ func (s *Scheme) CopyOrDie(obj Object) Object {
|
|||||||
return newObj
|
return newObj
|
||||||
}
|
}
|
||||||
|
|
||||||
// ObjectDiff writes the two objects out as JSON and prints out the identical part of
|
|
||||||
// the objects followed by the remaining part of 'a' and finally the remaining part of 'b'.
|
|
||||||
// For debugging tests.
|
|
||||||
func ObjectDiff(a, b Object) string {
|
|
||||||
ab, err := json.Marshal(a)
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Sprintf("a: %v", err))
|
|
||||||
}
|
|
||||||
bb, err := json.Marshal(b)
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Sprintf("b: %v", err))
|
|
||||||
}
|
|
||||||
return util.StringDiff(string(ab), string(bb))
|
|
||||||
}
|
|
||||||
|
|
||||||
// ObjectGoPrintDiff is like ObjectDiff, but uses go's %#v formatter to print the
|
|
||||||
// objects, in case json isn't showing you the difference. (reflect.DeepEqual makes
|
|
||||||
// a distinction between nil and empty slices, for example, even though nothing else
|
|
||||||
// really does.)
|
|
||||||
func ObjectGoPrintDiff(a, b Object) string {
|
|
||||||
return util.StringDiff(
|
|
||||||
fmt.Sprintf("%#v", a),
|
|
||||||
fmt.Sprintf("%#v", b),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// metaInsertion implements conversion.MetaInsertionFactory, which lets the conversion
|
// metaInsertion implements conversion.MetaInsertionFactory, which lets the conversion
|
||||||
// package figure out how to encode our object's types and versions. These fields are
|
// package figure out how to encode our object's types and versions. These fields are
|
||||||
// located in our TypeMeta.
|
// located in our TypeMeta.
|
||||||
|
68
pkg/util/diff.go
Normal file
68
pkg/util/diff.go
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2014 Google Inc. All rights reserved.
|
||||||
|
|
||||||
|
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 util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// StringDiff diffs a and b and returns a human readable diff.
|
||||||
|
func StringDiff(a, b string) string {
|
||||||
|
ba := []byte(a)
|
||||||
|
bb := []byte(b)
|
||||||
|
out := []byte{}
|
||||||
|
i := 0
|
||||||
|
for ; i < len(ba) && i < len(bb); i++ {
|
||||||
|
if ba[i] != bb[i] {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
out = append(out, ba[i])
|
||||||
|
}
|
||||||
|
out = append(out, []byte("\n\nA: ")...)
|
||||||
|
out = append(out, ba[i:]...)
|
||||||
|
out = append(out, []byte("\n\nB: ")...)
|
||||||
|
out = append(out, bb[i:]...)
|
||||||
|
out = append(out, []byte("\n\n")...)
|
||||||
|
return string(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ObjectDiff writes the two objects out as JSON and prints out the identical part of
|
||||||
|
// the objects followed by the remaining part of 'a' and finally the remaining part of 'b'.
|
||||||
|
// For debugging tests.
|
||||||
|
func ObjectDiff(a, b interface{}) string {
|
||||||
|
ab, err := json.Marshal(a)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("a: %v", err))
|
||||||
|
}
|
||||||
|
bb, err := json.Marshal(b)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("b: %v", err))
|
||||||
|
}
|
||||||
|
return StringDiff(string(ab), string(bb))
|
||||||
|
}
|
||||||
|
|
||||||
|
// ObjectGoPrintDiff is like ObjectDiff, but uses go's %#v formatter to print the
|
||||||
|
// objects, in case json isn't showing you the difference. (reflect.DeepEqual makes
|
||||||
|
// a distinction between nil and empty slices, for example, even though nothing else
|
||||||
|
// really does.)
|
||||||
|
func ObjectGoPrintDiff(a, b interface{}) string {
|
||||||
|
return StringDiff(
|
||||||
|
fmt.Sprintf("%#v", a),
|
||||||
|
fmt.Sprintf("%#v", b),
|
||||||
|
)
|
||||||
|
}
|
@ -144,26 +144,6 @@ func (intstr IntOrString) MarshalJSON() ([]byte, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// StringDiff diffs a and b and returns a human readable diff.
|
|
||||||
func StringDiff(a, b string) string {
|
|
||||||
ba := []byte(a)
|
|
||||||
bb := []byte(b)
|
|
||||||
out := []byte{}
|
|
||||||
i := 0
|
|
||||||
for ; i < len(ba) && i < len(bb); i++ {
|
|
||||||
if ba[i] != bb[i] {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
out = append(out, ba[i])
|
|
||||||
}
|
|
||||||
out = append(out, []byte("\n\nA: ")...)
|
|
||||||
out = append(out, ba[i:]...)
|
|
||||||
out = append(out, []byte("\n\nB: ")...)
|
|
||||||
out = append(out, bb[i:]...)
|
|
||||||
out = append(out, []byte("\n\n")...)
|
|
||||||
return string(out)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Takes a list of strings and compiles them into a list of regular expressions
|
// Takes a list of strings and compiles them into a list of regular expressions
|
||||||
func CompileRegexps(regexpStrings []string) ([]*regexp.Regexp, error) {
|
func CompileRegexps(regexpStrings []string) ([]*regexp.Regexp, error) {
|
||||||
regexps := []*regexp.Regexp{}
|
regexps := []*regexp.Regexp{}
|
||||||
|
Loading…
Reference in New Issue
Block a user