-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver.go
168 lines (142 loc) · 3.44 KB
/
resolver.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2019 Seamia Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"math/rand"
"sort"
"strings"
)
func resolveAny(src interface{}, key string) (bool, string) {
if src == nil {
return notFound() // todo: could it be a legit return value
}
switch actual := src.(type) {
case msi:
return resolveMap(actual, key)
case slice:
return resolveSlice(actual, key)
case string:
if len(key) == 0 {
return true, actual
} else {
quit("stil have non empty path [%s] for terminal value [%s]", key, actual)
}
default:
quit("unhandled type: %v", actual)
}
return notFound()
}
func breakPath(src string) (string, string) {
bits := strings.Split(src, itemsSeparator)
first := bits[0]
remainder := strings.Join(bits[1:], itemsSeparator)
return first, remainder
}
func breakParam(src string) (string, string) {
bits := strings.Split(src, ":")
switch len(bits) {
case 1:
return bits[0], ""
case 2:
return bits[0], bits[1]
default:
quit("too many parts in [%s]", src)
}
return "", ""
}
func resolveMap(src msi, key string) (bool, string) {
first, remainder := breakPath(key)
if data, found := src[first]; found {
/*
if txt, okay := data.(string); okay {
return true, txt
}*/
return resolveAny(data, remainder)
}
return notFound()
}
func resolveSlice(src slice, key string) (bool, string) {
if len(src) == 0 {
return notFound()
}
cmds := strings.Split(key, ";")
if len(cmds) > 1 {
exact := make([]string, 0)
inexact := make([]string, 0)
for _, cmd := range cmds {
if strings.Contains(cmd, "=") {
exact = append(exact, cmd)
} else if strings.Contains(cmd, ":") {
inexact = append(inexact, cmd)
} else {
quit("illegal value [%s], which a part of [%s]", cmd, key)
}
}
if len(inexact) > 1 {
quit("more than one selectors: [%s], which is a part of [%s]", strings.Join(inexact, "; "), key)
}
for _, one := range exact {
src = reduceSlice(src, one)
}
if len(inexact) == 1 {
return resolveSlice(src, inexact[0])
} else {
quit("cannot resolve the condition [%s]", key)
}
return notFound()
}
first, remainder := breakPath(key)
name, options := breakParam(first)
index := indexInvalid
switch options {
case "first":
index = 0
case "last":
index = len(src) - 1
case "random":
index = rand.Intn(len(src))
}
if index == indexInvalid {
selector := make(map[string]int)
for index, item := range src {
if success, value := resolveAny(item, name); success {
selector[value] = index
} else {
// ?????
}
}
var keys []string
for k := range selector {
keys = append(keys, k)
}
sort.Strings(keys)
index = indexInvalid
switch options {
case "latest":
index = selector[keys[len(keys)-1]]
case "earliest":
index = selector[keys[0]]
}
}
if index != indexInvalid {
return resolveAny(src[index], remainder)
} else {
}
comment(true, "********************* %s; %s; %s;", name, options, remainder)
return notFound()
}
func reduceSlice(src slice, key string) slice {
field, value, evaluator := findEvaluator(key)
result := make(slice, 0, len(src))
for _, entry := range src {
if hasFieldValue(entry, field, value, evaluator) {
result = append(result, entry)
}
}
debug("-- reduced slice from %v to %v using [%s] condition", len(src), len(result), key)
return result
}
func notFound() (bool, string) {
return false, ""
}