-
Notifications
You must be signed in to change notification settings - Fork 33
/
tag.go
144 lines (132 loc) · 3.19 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
package model
import (
"github.com/viant/afs/file"
"github.com/viant/afs/url"
"github.com/viant/endly/model/location"
"github.com/viant/toolbox"
"github.com/viant/toolbox/data"
"strings"
"unicode"
)
// Tag represents a tag
type Tag struct {
OwnerSource *location.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) expandPathIfNeeded(subpath string) (string, string) {
if !strings.HasSuffix(subpath, "*") {
return subpath, ""
}
return url.Split(t.OwnerSource.URL, file.Scheme)
}
//
//// setMeta sets Tag, optionally Instance 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["Instance"] = 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)
}
// 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 *location.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
}