Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Strategic Merge Patch #43880

Merged
merged 1 commit into from
Apr 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 17 additions & 3 deletions staging/src/k8s.io/apimachinery/pkg/util/mergepatch/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,23 @@ import (
"fmt"
)

var ErrBadJSONDoc = errors.New("Invalid JSON document")
var ErrNoListOfLists = errors.New("Lists of lists are not supported")
var ErrBadPatchFormatForPrimitiveList = errors.New("Invalid patch format of primitive list")
var (
ErrBadJSONDoc = errors.New("Invalid JSON document")
ErrNoListOfLists = errors.New("Lists of lists are not supported")
ErrBadPatchFormatForPrimitiveList = errors.New("Invalid patch format of primitive list")
)

func ErrNoMergeKey(m map[string]interface{}, k string) error {
return fmt.Errorf("map: %v does not contain declared merge key: %s", m, k)
}

func ErrBadArgType(expected, actual string) error {
return fmt.Errorf("expected a %s, but received a %s", expected, actual)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would simplify the code to take expected, actual interface{} and the print the type with %T (instead of using reflect.TypeOf(obj).Kind().String()).

Copy link
Member Author

@mengqiy mengqiy Apr 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

%T gives a type that is too concrete for us. Return a very concrete type may confuse the users sometimes.
But we can use reflect control which level of type we want to return to the users.

x := []byte("foo")
fmt.Printf("%T\n", x)
fmt.Printf(reflect.TypeOf(x).String())
fmt.Printf(reflect.TypeOf(x).Kind().String())

[]uint8
[]uint8
slice

Copy link
Member

@apelisse apelisse Apr 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, thanks for pointing this out. TIL what typeOf(obj).Kind() does :-)

}

func ErrBadPatchType(t interface{}, m map[string]interface{}) error {
return fmt.Errorf("unknown patch type: %s in map: %v", t, m)
}

// IsPreconditionFailed returns true if the provided error indicates
// a precondition failed.
Expand Down