func getTypeForPath()

in pkg/confidence/utils.go [43:82]


func getTypeForPath(schema map[string]interface{}, path string) (reflect.Kind, error) {
	if path == "" {
		return reflect.Map, nil
	}

	firstPartAndRest := strings.SplitN(path, ".", 2)
	if len(firstPartAndRest) == 1 {
		value, ok := schema[firstPartAndRest[0]].(map[string]interface{})
		if !ok {
			return 0, fmt.Errorf("schema was not in the expected format")
		}

		if _, isBool := value["boolSchema"]; isBool {
			return reflect.Bool, nil
		} else if _, isString := value["stringSchema"]; isString {
			return reflect.String, nil
		} else if _, isInt := value["intSchema"]; isInt {
			return reflect.Int64, nil
		} else if _, isFloat := value["doubleSchema"]; isFloat {
			return reflect.Float64, nil
		} else if _, isMap := value["structSchema"]; isMap {
			return reflect.Map, nil
		}

		return 0, fmt.Errorf("unable to find property type in schema %s", path)
	}

	// If we are here, the property path contains multiple entries -> this must be a struct -> recurse down the tree.
	childMap, ok := schema[firstPartAndRest[0]].(map[string]interface{})
	if !ok {
		return 0, fmt.Errorf("unexpected error when parsing resolve response schema")
	}

	if structMap, isStruct := childMap["structSchema"]; isStruct {
		structSchema, _ := structMap.(map[string]interface{})["schema"].(map[string]interface{})
		return getTypeForPath(structSchema, firstPartAndRest[1])
	}

	return 0, fmt.Errorf("unable to find property in schema %s", path)
}