diff --git a/internal/stash/filter/criterion.go b/internal/stash/filter/criterion.go index 380f1c2..5b8e2c9 100644 --- a/internal/stash/filter/criterion.go +++ b/internal/stash/filter/criterion.go @@ -52,15 +52,13 @@ func parseHierarchicalMultiCriterionInput(c map[string]any) *gql.HierarchicalMul out.Value = make([]string, len(items)) for i := range items { - id, _ := get[float64](items[i].(map[string]any), "id") - out.Value[i] = strconv.Itoa(int(id)) + out.Value[i], _ = getAsString(items[i].(map[string]any), "id") } excluded, _ := get[[]any](c, "value.excluded") out.Excludes = make([]string, len(excluded)) for i := range excluded { - id, _ := get[float64](excluded[i].(map[string]any), "id") - out.Excludes[i] = strconv.Itoa(int(id)) + out.Excludes[i], _ = getAsString(excluded[i].(map[string]any), "id") } return &out } @@ -70,29 +68,20 @@ func parseMultiCriterionInput(c map[string]any) *gql.MultiCriterionInput { Modifier: modifier(c), } + excluded, _ := get[[]any](c, "value.excluded") + out.Excludes = make([]string, len(excluded)) + for i := range excluded { + out.Excludes[i], _ = getAsString(excluded[i].(map[string]any), "id") + } + items, err := get[[]any](c, "value.items") + if err != nil { + items, err = get[[]any](c, "value") + } if err == nil { out.Value = make([]string, len(items)) for i := range items { - id, _ := get[float64](items[i].(map[string]any), "id") - out.Value[i] = strconv.Itoa(int(id)) - } - - excluded, _ := get[[]any](c, "value.excluded") - out.Excludes = make([]string, len(excluded)) - for i := range excluded { - id, _ := get[float64](items[i].(map[string]any), "id") - out.Excludes[i] = strconv.Itoa(int(id)) - } - return &out - } - - arr, err := get[[]any](c, "value") - if err == nil { - out.Value = make([]string, len(arr)) - for i := range arr { - id, _ := get[float64](arr[i].(map[string]any), "id") - out.Value[i] = strconv.Itoa(int(id)) + out.Value[i], _ = getAsString(items[i].(map[string]any), "id") } return &out } diff --git a/internal/stash/filter/map.go b/internal/stash/filter/map.go index cce4bde..85bf6f2 100644 --- a/internal/stash/filter/map.go +++ b/internal/stash/filter/map.go @@ -14,33 +14,43 @@ func (e errKeyNotFound) Error() string { return fmt.Sprintf("required key '%s' not found in '%v'", e.keys, e.root) } +func getAsString(root map[string]any, keyPath string) (string, error) { + s, err := get[string](root, keyPath) + if err != nil { + a, err := get[any](root, keyPath) + if err != nil { + return "", err + } + s = fmt.Sprintf("%v", a) + } + return s, nil +} + func get[T any](root map[string]any, keyPath string) (T, error) { - keys := strings.Split(keyPath, ".") var t T - m := root - for i, key := range keys { - v, ok := m[key] + keys := strings.Split(keyPath, ".") + key := keys[0] + if len(keys) > 1 { + m, ok := root[key].(map[string]any) if !ok { - break - } - if i == len(keys)-1 { - //reached last key, return value - if v == nil { - return t, nil - } - r, ok := v.(T) - if !ok { - return t, fmt.Errorf("expected '%T' but found '%T' for path '%v' in '%v'", t, v, keyPath, root) + return t, errKeyNotFound{ + keys: keys, + root: root, } - return r, nil } - m, ok = v.(map[string]any) - if !ok { - return t, fmt.Errorf("expected 'any' but found '%T' for '%v' in path '%v' in '%v'", v, key, keyPath, m) + restPath, _ := strings.CutPrefix(keyPath, key+".") + return get[T](m, restPath) + } + v, ok := root[key] + if !ok { + return t, errKeyNotFound{ + keys: keys, + root: root, } } - return t, errKeyNotFound{ - keys: keys, - root: root, + r, ok := v.(T) + if !ok { + return t, fmt.Errorf("expected '%T' but found '%T' for path '%v' in '%v'", t, v, keyPath, root) } + return r, nil }