Skip to content

Commit

Permalink
Remove duplicate code
Browse files Browse the repository at this point in the history
Combine code in core and input that both covers getting a readable
name of a type of an object. Fix test cases and errors around the
names, too.
  • Loading branch information
HeavyWombat committed May 24, 2018
1 parent 28b3f31 commit f3f23a0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 35 deletions.
40 changes: 29 additions & 11 deletions pkg/dyff/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))
}
}

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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()
}
}
6 changes: 3 additions & 3 deletions pkg/dyff/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
})
})
Expand Down
21 changes: 0 additions & 21 deletions pkg/dyff/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"net/url"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"time"
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f3f23a0

Please sign in to comment.