Skip to content

Commit

Permalink
guard against unexpected type during config manipulation
Browse files Browse the repository at this point in the history
  • Loading branch information
deads2k committed Feb 13, 2015
1 parent 2dcaab7 commit 9414610
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions pkg/kubectl/cmd/config/navigation_step_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ func newNavigationSteps(path string) (*navigationSteps, error) {
// This set of reflective code pulls the type of the map values, uses that type to look up the set of legal tags. Those legal tags are used to
// walk the list of remaining parts until we find a match to a legal tag or the end of the string. That name is used to burn all the used parts.
mapValueType := currType.Elem()
mapValueOptions := getPotentialTypeValues(mapValueType)
mapValueOptions, err := getPotentialTypeValues(mapValueType)
if err != nil {
return nil, err
}
nextPart := findNameStep(individualParts[currPartIndex:], util.KeySet(reflect.ValueOf(mapValueOptions)))

steps = append(steps, navigationStep{nextPart, mapValueType})
Expand All @@ -61,7 +64,10 @@ func newNavigationSteps(path string) (*navigationSteps, error) {
case reflect.Struct:
nextPart := individualParts[currPartIndex]

options := getPotentialTypeValues(currType)
options, err := getPotentialTypeValues(currType)
if err != nil {
return nil, err
}
fieldType, exists := options[nextPart]
if !exists {
return nil, fmt.Errorf("unable to parse %v after %v at %v", path, steps, currType)
Expand Down Expand Up @@ -113,7 +119,11 @@ func findNameStep(parts []string, typeOptions util.StringSet) string {
}

// getPotentialTypeValues takes a type and looks up the tags used to represent its fields when serialized.
func getPotentialTypeValues(typeValue reflect.Type) map[string]reflect.Type {
func getPotentialTypeValues(typeValue reflect.Type) (map[string]reflect.Type, error) {
if typeValue.Kind() != reflect.Struct {
return nil, fmt.Errorf("%v is not of type struct", typeValue)
}

ret := make(map[string]reflect.Type)

for fieldIndex := 0; fieldIndex < typeValue.NumField(); fieldIndex++ {
Expand All @@ -124,7 +134,7 @@ func getPotentialTypeValues(typeValue reflect.Type) map[string]reflect.Type {
ret[yamlTagName] = fieldType.Type
}

return ret
return ret, nil
}

func findKnownValue(parts []string, valueOptions util.StringSet) int {
Expand Down

0 comments on commit 9414610

Please sign in to comment.