-
Notifications
You must be signed in to change notification settings - Fork 1
/
id_set.go
133 lines (110 loc) · 2.6 KB
/
id_set.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package vangogh_local_data
import (
"errors"
"fmt"
"github.com/boggydigital/kevlar"
"golang.org/x/exp/maps"
"sort"
"strings"
)
const (
DefaultSort = TitleProperty
DefaultDesc = false
)
func idsFromSlugs(slugs []string, rdx kevlar.ReadableRedux) ([]string, error) {
var err error
if rdx == nil && len(slugs) > 0 {
rdx, err = NewReduxReader(SlugProperty)
if err != nil {
return nil, err
}
}
if rdx == nil {
return nil, errors.New("converting slugs to ids requires redux")
}
if err := rdx.MustHave(SlugProperty); err != nil {
return nil, err
}
var ids []string
for _, slug := range slugs {
if slug != "" {
matchedIds := rdx.Match(map[string][]string{SlugProperty: {slug}}, kevlar.FullMatch)
ids = append(ids, matchedIds...)
}
}
return ids, nil
}
func PropertyListsFromIdSet(
ids []string,
propertyFilter map[string][]string,
properties []string,
rdx kevlar.ReadableRedux) (map[string][]string, error) {
propSet := make(map[string]bool)
for _, p := range properties {
propSet[p] = true
}
propSet[TitleProperty] = true
if rdx == nil {
var err error
rdx, err = NewReduxReader(maps.Keys(propSet)...)
if err != nil {
return nil, err
}
}
itps := make(map[string][]string)
for _, id := range ids {
itp, err := propertyListFromId(id, propertyFilter, maps.Keys(propSet), rdx)
if err != nil {
return itps, err
}
for idTitle, props := range itp {
itps[idTitle] = props
}
}
return itps, nil
}
func propertyListFromId(
id string,
propertyFilter map[string][]string,
properties []string,
rdx kevlar.ReadableRedux) (map[string][]string, error) {
if err := rdx.MustHave(properties...); err != nil {
return nil, err
}
title, ok := rdx.GetLastVal(TitleProperty, id)
if !ok {
return nil, nil
}
itp := make(map[string][]string)
idTitle := fmt.Sprintf("%s %s", id, title)
itp[idTitle] = make([]string, 0)
sort.Strings(properties)
for _, prop := range properties {
if prop == IdProperty ||
prop == TitleProperty {
continue
}
values, ok := rdx.GetAllValues(prop, id)
if !ok || len(values) == 0 {
continue
}
filterValues := propertyFilter[prop]
for _, val := range values {
if isPropertyValueFiltered(val, filterValues) {
continue
}
itp[idTitle] = append(itp[idTitle], fmt.Sprintf("%s:%s", prop, val))
}
}
return itp, nil
}
func isPropertyValueFiltered(value string, filterValues []string) bool {
value = strings.ToLower(value)
for _, fv := range filterValues {
if strings.Contains(value, fv) {
return false
}
}
// this makes sure we don't filter values if there is no filter
return len(filterValues) > 0
}