Skip to content

Commit

Permalink
fix for stash filter refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
o-fl0w committed Nov 15, 2023
1 parent d182c2c commit 7bc7d5e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 44 deletions.
35 changes: 12 additions & 23 deletions internal/stash/filter/criterion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
52 changes: 31 additions & 21 deletions internal/stash/filter/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 7bc7d5e

Please sign in to comment.