2023-06-23 17:51:55 +00:00
|
|
|
package d2ir
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"strings"
|
2023-07-27 09:36:33 +00:00
|
|
|
|
2023-07-30 20:52:42 +00:00
|
|
|
"oss.terrastruct.com/d2/d2ast"
|
2023-07-27 09:36:33 +00:00
|
|
|
"oss.terrastruct.com/d2/d2graph"
|
2023-06-23 17:51:55 +00:00
|
|
|
)
|
|
|
|
|
|
2023-07-30 20:52:42 +00:00
|
|
|
func (m *Map) multiGlob(pattern []string) ([]*Field, bool) {
|
2023-07-27 09:36:33 +00:00
|
|
|
var fa []*Field
|
2023-07-30 20:52:42 +00:00
|
|
|
if d2ast.IsDoubleGlob(pattern) {
|
|
|
|
|
m._doubleGlob(&fa)
|
|
|
|
|
return fa, true
|
|
|
|
|
}
|
|
|
|
|
if d2ast.IsTripleGlob(pattern) {
|
|
|
|
|
m._tripleGlob(&fa)
|
|
|
|
|
return fa, true
|
|
|
|
|
}
|
|
|
|
|
return nil, false
|
2023-07-27 09:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *Map) _doubleGlob(fa *[]*Field) {
|
|
|
|
|
for _, f := range m.Fields {
|
|
|
|
|
if _, ok := d2graph.ReservedKeywords[f.Name]; ok {
|
2023-07-30 20:52:42 +00:00
|
|
|
if f.Name == "layers" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2023-07-30 10:15:33 +00:00
|
|
|
if _, ok := d2graph.BoardKeywords[f.Name]; !ok {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2023-07-27 09:36:33 +00:00
|
|
|
}
|
|
|
|
|
*fa = append(*fa, f)
|
|
|
|
|
if f.Map() != nil {
|
|
|
|
|
f.Map()._doubleGlob(fa)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-30 20:52:42 +00:00
|
|
|
func (m *Map) _tripleGlob(fa *[]*Field) {
|
|
|
|
|
for _, f := range m.Fields {
|
|
|
|
|
if _, ok := d2graph.ReservedKeywords[f.Name]; ok {
|
|
|
|
|
if _, ok := d2graph.BoardKeywords[f.Name]; !ok {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2023-08-16 06:33:50 +00:00
|
|
|
// We don't ever want to append layers, scenarios or steps directly.
|
|
|
|
|
if f.Map() != nil {
|
|
|
|
|
f.Map()._tripleGlob(fa)
|
|
|
|
|
}
|
|
|
|
|
continue
|
2023-07-30 20:52:42 +00:00
|
|
|
}
|
|
|
|
|
*fa = append(*fa, f)
|
|
|
|
|
if f.Map() != nil {
|
|
|
|
|
f.Map()._tripleGlob(fa)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-23 17:51:55 +00:00
|
|
|
func matchPattern(s string, pattern []string) bool {
|
|
|
|
|
if len(pattern) == 0 {
|
|
|
|
|
return true
|
|
|
|
|
}
|
2023-07-29 23:54:44 +00:00
|
|
|
if _, ok := d2graph.ReservedKeywords[s]; ok {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2023-06-23 17:51:55 +00:00
|
|
|
|
|
|
|
|
for i := 0; i < len(pattern); i++ {
|
|
|
|
|
if pattern[i] == "*" {
|
|
|
|
|
// * so match next.
|
|
|
|
|
if i != len(pattern)-1 {
|
2023-07-30 04:33:41 +00:00
|
|
|
j := strings.Index(strings.ToLower(s), strings.ToLower(pattern[i+1]))
|
2023-06-23 17:51:55 +00:00
|
|
|
if j == -1 {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
s = s[j+len(pattern[i+1]):]
|
|
|
|
|
i++
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2023-07-29 23:43:22 +00:00
|
|
|
if !strings.HasPrefix(strings.ToLower(s), strings.ToLower(pattern[i])) {
|
2023-06-23 17:51:55 +00:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
s = s[len(pattern[i]):]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|