This repository has been archived by the owner on Aug 14, 2023. It is now read-only.
/
query.go
138 lines (121 loc) · 2.62 KB
/
query.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
134
135
136
137
138
// The query package provides the functionality to query records.
// By using reflections the package is structure-agnostic.
package query
import (
"fmt"
"reflect"
"strings"
"github.com/logrusorgru/aurora"
)
type Queryables []Queryable
func (q Queryables) Types() []string {
result := make([]string, len(q))
for i := range q {
result[i] = q[i].Name
}
return result
}
func (q Queryables) PPTypes() {
typ := q.Types()
fmt.Println(aurora.BrightMagenta("Types:"))
for i := range typ {
fmt.Println(aurora.Green(fmt.Sprintf("- %s", typ[i])))
}
}
func (q Queryables) keys() Keys {
var rsl Keys
for i := range q {
keys := q[i].keys()
for j := range keys {
rsl = rsl.Add(keys[j])
}
}
return rsl
}
func (q Queryables) PPKeys(mode OutputMode) {
kys := q.keys()
fmt.Println(fmt.Sprintf("%s%s%s", aurora.BrightMagenta(aurora.Bold("Keys")), aurora.BrightMagenta(" for "), aurora.BrightMagenta(q.String())))
out := Output{
Header: []string{"Name Key", "Element Type(s)"},
Element: kys.Data(),
}
out.PPKeyValue(nil, mode, false)
}
func (q Queryables) QueryablesFromUserInput(input string) (Queryables, error) {
if input == "" {
return q, nil
}
inputs := strings.Split(input, ",")
var rsl Queryables
for i := range inputs {
inp := strings.TrimPrefix(inputs[i], " ")
inp = strings.TrimSuffix(inp, " ")
for j := range q {
if q[j].matchTypeFromUserInput(inp) {
rsl = append(rsl, q[j])
}
}
}
if len(rsl) == 0 {
return rsl, fmt.Errorf("no types for \"%s\" found", q.String())
}
return rsl, nil
}
func (q Queryables) String() string {
if len(q) == 0 {
return ""
}
rsl := q[0].Name
for i := 1; i < len(q); i++ {
rsl = fmt.Sprint(rsl, ", ", q[i].Name)
}
return rsl
}
type Keys []Key
func (k Keys) Add(key Key) Keys {
for i := range k {
if k[i].Name == key.Name {
k[i].Queryables = append(k[i].Queryables, key.Queryables...)
return k
}
}
return append(k, key)
}
func (k Keys) Data() []KeyValue {
rsl := make([]KeyValue, len(k))
for i := range k {
rsl[i] = KeyValue{
Key: k[i].Name,
Value: k[i].Queryables.String()}
}
return rsl
}
type Key struct {
Name string
Queryables Queryables
}
type Queryable struct {
Name string
Type interface{}
}
func (q Queryable) matchTypeFromUserInput(input string) bool {
input = strings.ToLower(input)
if input == q.Name {
return true
}
if q.Name == strings.TrimSuffix(input, "s") {
return true
}
return false
}
func (q Queryable) keys() Keys {
var rsl Keys
v := reflect.ValueOf(q.Type)
for i := 0; i < v.NumField(); i++ {
rsl = rsl.Add(Key{
Name: v.Type().Field(i).Name,
Queryables: Queryables{q},
})
}
return rsl
}