Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

d2ir: Single Level Ampersand Filters #1509

Merged
merged 23 commits into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
359976e
d2ir: Add single level field glob patterns
nhooyr Jun 23, 2023
210816a
d2ir: Fix _ with null
nhooyr Jul 14, 2023
1217ff3
d2ir: Add single glob matching to edges and make it work fully recurs…
nhooyr Jun 24, 2023
d9b4b95
d2ir: Implement double globs
nhooyr Jul 27, 2023
0d2b0aa
d2ir: Implement edge index globs
nhooyr Jul 27, 2023
b119174
d2ir: Fix implementation of Map.createEdge
nhooyr Jul 29, 2023
9566775
d2ir: Fix and add test for glob-edge-glob-index
nhooyr Jul 29, 2023
ff47a00
d2ir: Make globs case insensitive to match the rest of the language
nhooyr Jul 29, 2023
82663f0
d2ir: Explain EnsureField misnomer
nhooyr Jul 29, 2023
63efa12
d2ir: Fix globs to not match reserved
nhooyr Jul 29, 2023
bd2c94f
d2ir: Make suffix globs case insensitive too
nhooyr Jul 30, 2023
73e4e68
changelogs/next.md: Update
nhooyr Jul 30, 2023
9c37d6d
d2ir: Make globs more ergonomic in two specific edge cases
nhooyr Jul 30, 2023
6fdf4b0
d2ir: Make double globs work sanely across boards
nhooyr Jul 30, 2023
6ca36e6
d2ir: Glob review fixes
nhooyr Jul 30, 2023
0002817
d2ir: Implement ampersand filters
nhooyr Jul 29, 2023
4316611
d2ir: Add single field filtering
nhooyr Jul 29, 2023
6b73a61
d2ir: Add filter error tests
nhooyr Jul 30, 2023
60a19cf
e2etests: Fix ampersand-escape test
nhooyr Jul 30, 2023
0f45d2b
d2ast: Fix RawString test with ampersand
nhooyr Jul 30, 2023
7ae2d78
d2ir: Add filtering on class arrays
nhooyr Jul 30, 2023
6358a44
changelogs/next.md: Update
nhooyr Jul 30, 2023
d0d3ebe
d2ir: Fix filters on nested fields
nhooyr Jul 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Layout capability also takes a subtle but important step forward: you can now cu
- Scale renders and disable fit to screen with `--scale` flag [#1413](https://github.com/terrastruct/d2/pull/1413)
- `null` keyword can be used to un-declare. See [docs](https://d2lang.com/tour/overrides#null) [#1446](https://github.com/terrastruct/d2/pull/1446)
- Develop multi-board diagrams in watch mode (links to layers/scenarios/steps work in `--watch`) [#1503](https://github.com/terrastruct/d2/pull/1503)
- Glob patterns have been implemented. See [docs](https://d2lang.com/tour/globs). [#1479](https://github.com/terrastruct/d2/pull/1479)
- Ampersand filters have been implemented. See [docs](https://d2lang.com/tour/filters). [#1509](https://github.com/terrastruct/d2/pull/1509)

#### Improvements 🧹

Expand Down
59 changes: 52 additions & 7 deletions d2ast/d2ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,8 @@ type Number struct {
type UnquotedString struct {
Range Range `json:"range"`
Value []InterpolationBox `json:"value"`
// Pattern holds the parsed glob pattern if in a key and the unquoted string represents a valid pattern.
Pattern []string `json:"pattern,omitempty"`
}

func (s *UnquotedString) Coalesce() {
Expand Down Expand Up @@ -608,7 +610,7 @@ func (m *Map) IsFileMap() bool {
type Key struct {
Range Range `json:"range"`

// Indicates this MapKey is an override selector.
// Indicates this MapKey is a filter selector.
Ampersand bool `json:"ampersand,omitempty"`

// At least one of Key and Edges will be set but all four can also be set.
Expand Down Expand Up @@ -718,6 +720,19 @@ func (mk *Key) SetScalar(scalar ScalarBox) {
}
}

func (mk *Key) HasQueryGlob() bool {
if mk.Key.HasGlob() && len(mk.Edges) == 0 {
return true
}
if mk.EdgeIndex != nil && mk.EdgeIndex.Glob && mk.EdgeKey == nil {
return true
}
if mk.EdgeKey.HasGlob() {
return true
}
return false
}

type KeyPath struct {
Range Range `json:"range"`
Path []*StringBox `json:"path"`
Expand All @@ -738,6 +753,37 @@ func (kp *KeyPath) IDA() (ida []string) {
return ida
}

func (kp *KeyPath) Copy() *KeyPath {
kp2 := *kp
kp2.Path = nil
kp2.Path = append(kp2.Path, kp.Path...)
return &kp2
}

func (kp *KeyPath) HasDoubleGlob() bool {
if kp == nil {
return false
}
for _, el := range kp.Path {
if el.UnquotedString != nil && el.ScalarString() == "**" {
return true
}
}
return false
}

func (kp *KeyPath) HasGlob() bool {
if kp == nil {
return false
}
for _, el := range kp.Path {
if el.UnquotedString != nil && len(el.UnquotedString.Pattern) > 0 {
return true
}
}
return false
}

type Edge struct {
Range Range `json:"range"`

Expand Down Expand Up @@ -1056,6 +1102,10 @@ func (sb *StringBox) Unbox() String {
}
}

func (sb *StringBox) ScalarString() string {
return sb.Unbox().ScalarString()
}

// InterpolationBox is used to select between strings and substitutions in unquoted and
// double quoted strings. There is no corresponding interface to avoid unnecessary
// abstraction.
Expand All @@ -1068,12 +1118,11 @@ type InterpolationBox struct {
// & is only special if it begins a key.
// - is only special if followed by another - in a key.
// ' " and | are only special if they begin an unquoted key or value.
var UnquotedKeySpecials = string([]rune{'#', ';', '\n', '\\', '{', '}', '[', ']', '\'', '"', '|', ':', '.', '-', '<', '>', '*', '&', '(', ')', '@'})
var UnquotedKeySpecials = string([]rune{'#', ';', '\n', '\\', '{', '}', '[', ']', '\'', '"', '|', ':', '.', '-', '<', '>', '*', '&', '(', ')', '@', '&'})
var UnquotedValueSpecials = string([]rune{'#', ';', '\n', '\\', '{', '}', '[', ']', '\'', '"', '|', '$', '@'})

// RawString returns s in a AST String node that can format s in the most aesthetically
// pleasing way.
// TODO: Return StringBox
func RawString(s string, inKey bool) String {
if s == "" {
return FlatDoubleQuotedString(s)
Expand All @@ -1086,10 +1135,6 @@ func RawString(s string, inKey bool) String {
if i+1 < len(s) && s[i+1] != '-' {
continue
}
case '&':
if i > 0 {
continue
}
}
if strings.ContainsRune(UnquotedKeySpecials, r) {
if !strings.ContainsRune(s, '"') {
Expand Down
Loading