diff --git a/pkg/dyff/core.go b/pkg/dyff/core.go index e3d17c5..40c0631 100644 --- a/pkg/dyff/core.go +++ b/pkg/dyff/core.go @@ -38,6 +38,14 @@ import ( const defaultFallbackTerminalWidth = 80 +// Internal string constants for type names and type decisions +const ( + typeMap = "map" + typeSimpleList = "list" + typeComplexList = "complex-list" + typeString = "string" +) + // FixedTerminalWidth disables terminal width detection and reset it with a fixed given value var FixedTerminalWidth = -1 @@ -656,7 +664,7 @@ func listNamesOfNamedList(list []interface{}, identifier string) ([]string, erro result[i] = value.(string) default: - return nil, fmt.Errorf("unable to list names of a names list, because list entry #%d is not a YAML map but %s", i, typeToName(entry)) + return nil, fmt.Errorf("unable to list names of a names list, because list entry #%d is not a YAML map but %s", i, getType(entry)) } } @@ -774,7 +782,7 @@ func Grab(obj interface{}, pathString string) (interface{}, error) { for _, element := range path.PathElements { if element.Key != "" { // List if !isList(pointer) { - return nil, fmt.Errorf("failed to traverse tree, expected a list but found type %s at %s", typeToName(pointer), pointerPath.ToGoPatchStyle(false)) + return nil, fmt.Errorf("failed to traverse tree, expected a %s but found type %s at %s", typeSimpleList, getType(pointer), pointerPath.ToGoPatchStyle(false)) } entry, ok := getEntryFromNamedList(pointer.([]interface{}), element.Key, element.Name) @@ -786,19 +794,19 @@ func Grab(obj interface{}, pathString string) (interface{}, error) { } else if id, err := strconv.Atoi(element.Name); err == nil { // List (entry referenced by its index) if !isList(pointer) { - return nil, fmt.Errorf("failed to traverse tree, expected a list but found type %s at %s", typeToName(pointer), pointerPath.ToGoPatchStyle(false)) + return nil, fmt.Errorf("failed to traverse tree, expected a %s but found type %s at %s", typeSimpleList, getType(pointer), pointerPath.ToGoPatchStyle(false)) } list := pointer.([]interface{}) if id < 0 || id >= len(list) { - return nil, fmt.Errorf("failed to traverse tree, provided list index %d is not in range: 0..%d", id, len(list)-1) + return nil, fmt.Errorf("failed to traverse tree, provided %s index %d is not in range: 0..%d", typeSimpleList, id, len(list)-1) } pointer = list[id] } else { // Map if !isMapSlice(pointer) { - return nil, fmt.Errorf("failed to traverse tree, expected a YAML map but found type %s at %s", typeToName(pointer), pointerPath.ToGoPatchStyle(false)) + return nil, fmt.Errorf("failed to traverse tree, expected a %s but found type %s at %s", typeMap, getType(pointer), pointerPath.ToGoPatchStyle(false)) } entry, err := getValueByKey(pointer.(yaml.MapSlice), element.Name) @@ -849,15 +857,25 @@ func ChangeRoot(inputFile *InputFile, path string, translateListToDocuments bool return nil } -func typeToName(obj interface{}) string { - switch obj.(type) { +func getType(value interface{}) string { + switch value.(type) { case yaml.MapSlice: - return "YAML map" + return typeMap + + case []interface{}: + if isComplexSlice(value.([]interface{})) { + return typeComplexList + } + + return typeSimpleList - case []yaml.MapSlice, []interface{}: - return "YAML list" + case []yaml.MapSlice: + return typeComplexList + + case string: + return typeString default: - return reflect.TypeOf(obj).Kind().String() + return reflect.TypeOf(value).Kind().String() } } diff --git a/pkg/dyff/core_test.go b/pkg/dyff/core_test.go index ac92b30..13511a5 100644 --- a/pkg/dyff/core_test.go +++ b/pkg/dyff/core_test.go @@ -206,9 +206,9 @@ list: Expect(grabError(example, "/yaml/simple-list/-1")).To(BeEquivalentTo("failed to traverse tree, provided list index -1 is not in range: 0..4")) Expect(grabError(example, "/yaml/does-not-exist")).To(BeEquivalentTo("no key 'does-not-exist' found in map, available keys are: map, simple-list, named-entry-list-using-name, named-entry-list-using-key, named-entry-list-using-id")) - Expect(grabError(example, "/yaml/0")).To(BeEquivalentTo("failed to traverse tree, expected a list but found type YAML map at /yaml")) - Expect(grabError(example, "/yaml/simple-list/foobar")).To(BeEquivalentTo("failed to traverse tree, expected a YAML map but found type YAML list at /yaml/simple-list")) - Expect(grabError(example, "/yaml/map/foobar=0")).To(BeEquivalentTo("failed to traverse tree, expected a list but found type YAML map at /yaml/map")) + Expect(grabError(example, "/yaml/0")).To(BeEquivalentTo("failed to traverse tree, expected a list but found type map at /yaml")) + Expect(grabError(example, "/yaml/simple-list/foobar")).To(BeEquivalentTo("failed to traverse tree, expected a map but found type list at /yaml/simple-list")) + Expect(grabError(example, "/yaml/map/foobar=0")).To(BeEquivalentTo("failed to traverse tree, expected a list but found type map at /yaml/map")) Expect(grabError(example, "/yaml/named-entry-list-using-id/id=0")).To(BeEquivalentTo("there is no entry id: 0 in the list")) }) }) diff --git a/pkg/dyff/input.go b/pkg/dyff/input.go index 7c93c44..ae93617 100644 --- a/pkg/dyff/input.go +++ b/pkg/dyff/input.go @@ -30,7 +30,6 @@ import ( "net/url" "os" "path/filepath" - "reflect" "sort" "strings" "time" @@ -42,14 +41,6 @@ import ( ordered "github.com/virtuald/go-ordered-json" ) -// Internal string constants for type names and type decisions -const ( - typeMap = "map" - typeSimpleList = "slice" - typeComplexList = "complex-slice" - typeString = "string" -) - // PreserveKeyOrderInJSON specifies whether a special library is used to decode // JSON input to preserve the order of keys in maps even though that is not part // of the JSON specification. @@ -376,18 +367,6 @@ func mapSlicify(obj interface{}) interface{} { } } -func getType(value interface{}) string { - valueType := reflect.TypeOf(value).Kind() - switch valueType { - case reflect.Slice: - if isComplexSlice(value.([]interface{})) { - return typeComplexList - } - } - - return valueType.String() -} - func getBytesFromLocation(location string) ([]byte, error) { var data []byte var err error