-
Notifications
You must be signed in to change notification settings - Fork 4
/
tag.go
208 lines (188 loc) · 4.9 KB
/
tag.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package neatly
import (
"github.com/viant/toolbox"
"github.com/viant/toolbox/data"
"github.com/viant/toolbox/storage"
"github.com/viant/toolbox/url"
"path"
"strings"
"unicode"
)
//Tag represents a nearly tag
type Tag struct {
OwnerSource *url.Resource
OwnerName string
Name string
Group string
IsArray bool
Iterator *TagIterator
LineNumber int
Subpath string
PathMatch string
tagIdPrefix string
}
//HasActiveIterator returns true if tag has active iterator
func (t *Tag) HasActiveIterator() bool {
if t == nil {
return false
}
return t.Iterator != nil && t.Iterator.Has()
}
func (t *Tag) setTagObject(context *tagContext, record map[string]interface{}, includeMeta bool) data.Map {
var result data.Map
if t.IsArray {
result = data.NewMap()
context.objectContainer.GetCollection(t.Name).Push(result)
} else {
result = context.objectContainer.GetMap(t.Name)
}
context.tagName = t.Name
if t.HasActiveIterator() {
context.tagIndex = t.Iterator.Index()
}
value, has := record["Subpath"]
if has {
t.Subpath, t.PathMatch = t.expandPathIfNeeded(toolbox.AsString(value))
}
if t.Subpath != "" {
context.Subpath = t.Subpath
}
context.tagID = t.TagID()
if includeMeta {
t.setMeta(result, record)
}
context.tagObject = result
return result
}
func (t *Tag) expandPathIfNeeded(subpath string) (string, string) {
if !strings.HasSuffix(subpath, "*") {
return subpath, ""
}
parentURL, _ := toolbox.URLSplit(t.OwnerSource.URL)
var leafDirectory = ""
var subPathParent = ""
subPathElements := strings.Split(subpath, "/")
for _, candidate := range subPathElements {
if strings.Contains(candidate, "*") {
leafDirectory = strings.Replace(candidate, "*", "", 1)
break
}
subPathParent = path.Join(subPathParent, candidate)
parentURL = toolbox.URLPathJoin(parentURL, candidate)
}
storageService, err := storage.NewServiceForURL(parentURL, t.OwnerSource.Credentials)
if err == nil {
candidates, err := storageService.List(parentURL)
if err == nil {
for _, candidate := range candidates {
if candidate.URL() == parentURL {
continue
}
_, candidateName := toolbox.URLSplit(candidate.URL())
if strings.HasPrefix(candidateName, leafDirectory) {
if subPathParent != "" {
return path.Join(subPathParent, candidateName), string(candidateName[len(leafDirectory):])
}
return candidateName, string(candidateName[len(leafDirectory):])
}
}
}
}
return subpath, ""
}
//setMeta sets Tag, optionally TagIndex and Subpath to the provided object
func (t *Tag) setMeta(object data.Map, record map[string]interface{}) {
object["Tag"] = t.Name
if t.HasActiveIterator() {
object["TagIndex"] = t.Iterator.Index()
}
value, has := record["Subpath"]
if has {
t.SetSubPath(toolbox.AsString(value))
}
if t.Subpath != "" {
object["Subpath"] = t.Subpath
}
if t.PathMatch != "" {
object["PathMatch"] = t.PathMatch
}
if value, has := record["Group"]; has {
t.Group = toolbox.AsString(value)
}
object["TagID"] = t.TagID()
}
func (t *Tag) Expand(text string) string {
var aMap = data.NewMap()
aMap.Put("pathMatch", t.PathMatch)
aMap.Put("subPath", t.Subpath)
if t.HasActiveIterator() {
aMap.Put("index", t.Iterator.Index())
aMap.Put("idx", toolbox.AsInt(t.Iterator.Index()))
}
return aMap.ExpandAsText(text)
}
//SetSubPath set subpath for the tag
func (t *Tag) SetSubPath(subpath string) {
t.Subpath, t.PathMatch = t.expandPathIfNeeded(subpath)
}
//TagID returns tag ID
func (t *Tag) TagID() string {
var index = ""
if t.HasActiveIterator() {
index = t.Iterator.Index()
}
var subPath = t.Subpath
if subPath != "" {
if strings.Contains(subPath, index) {
index = ""
}
}
if strings.Contains(t.Name, "$") {
expandedName := t.Expand(t.Name)
if strings.Contains(subPath, expandedName) {
subPath = ""
}
}
var tagIdPostfix = index + subPath
if tagIdPostfix != "" && t.tagIdPrefix != "" {
tagIdPostfix = " " + tagIdPostfix
}
value := t.Expand(t.tagIdPrefix + tagIdPostfix)
var result = make([]byte, 0)
for _, r := range value {
if r == ' ' {
result = append(result, '_')
continue
}
if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_' {
result = append(result, byte(r))
}
}
group := ""
if t.Group != "" {
group = t.Group + "_"
}
return group + string(result)
}
//NewTag creates a new neatly tag
func NewTag(ownerName string, ownerSource *url.Resource, key string, lineNumber int) *Tag {
var result = &Tag{
OwnerName: ownerName,
OwnerSource: ownerSource,
Name: key,
LineNumber: lineNumber,
}
key = decodeIteratorIfPresent(key, result)
if len(key) > 2 && string(key[0:2]) == "[]" {
result.Name = string(key[2:])
result.IsArray = true
}
if rangeIndex := strings.LastIndex(result.Name, "{"); rangeIndex != -1 {
result.Name = string(result.Name[:rangeIndex])
}
if ownerName != "" {
ownerName = ownerName + "_"
}
result.tagIdPrefix = ownerName + result.Name
return result
}