Skip to content

Commit

Permalink
Merge pull request #1090 from alixander/scenarios-bug
Browse files Browse the repository at this point in the history
fix scenarios/steps inheritence
  • Loading branch information
alixander authored Apr 7, 2023
2 parents 2d7cdc5 + 754e0ec commit ad880fa
Show file tree
Hide file tree
Showing 14 changed files with 4,506 additions and 577 deletions.
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#### Bugfixes ⛑️

- Fix inheritence in scenarios/steps [#1090](https://github.com/terrastruct/d2/pull/1090)
- Fix a bug in 32bit builds [#1115](https://github.com/terrastruct/d2/issues/1115)
- Fix a bug in ID parsing [#322](https://github.com/terrastruct/d2/issues/322)
- Fix a bug in watch mode parsing SVG [#1119](https://github.com/terrastruct/d2/issues/1119)
Expand Down
13 changes: 13 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2456,6 +2456,19 @@ layers: {
assert.False(t, g.Layers[1].Scenarios[1].IsFolderOnly)
},
},
{
name: "scenarios_edge_index",
run: func(t *testing.T) {
assertCompile(t, `a -> x
scenarios: {
1: {
(a -> x)[0].style.opacity: 0.1
}
}
`, "")
},
},
{
name: "errs/duplicate_board",
run: func(t *testing.T) {
Expand Down
70 changes: 22 additions & 48 deletions d2ir/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,62 +22,20 @@ func Compile(ast *d2ast.Map) (*Map, error) {
m.initRoot()
m.parent.(*Field).References[0].Context.Scope = ast
c.compileMap(m, ast)
c.compileScenarios(m)
c.compileSteps(m)
if !c.err.Empty() {
return nil, c.err
}
return m, nil
}

func (c *compiler) compileScenarios(m *Map) {
scenariosf := m.GetField("scenarios")
if scenariosf == nil {
func (c *compiler) overlay(base *Map, f *Field) {
if f.Map() == nil || f.Primary() != nil {
c.errorf(f.References[0].Context.Key, "invalid %s", NodeBoardKind(f))
return
}
scenarios := scenariosf.Map()
if scenarios == nil {
return
}

for _, sf := range scenarios.Fields {
if sf.Map() == nil || sf.Primary() != nil {
c.errorf(sf.References[0].Context.Key, "invalid scenario")
continue
}
base := m.CopyBase(sf)
OverlayMap(base, sf.Map())
sf.Composite = base
c.compileScenarios(sf.Map())
c.compileSteps(sf.Map())
}
}

func (c *compiler) compileSteps(m *Map) {
stepsf := m.GetField("steps")
if stepsf == nil {
return
}
steps := stepsf.Map()
if steps == nil {
return
}
for i, sf := range steps.Fields {
if sf.Map() == nil || sf.Primary() != nil {
c.errorf(sf.References[0].Context.Key, "invalid step")
break
}
var base *Map
if i == 0 {
base = m.CopyBase(sf)
} else {
base = steps.Fields[i-1].Map().CopyBase(sf)
}
OverlayMap(base, sf.Map())
sf.Composite = base
c.compileScenarios(sf.Map())
c.compileSteps(sf.Map())
}
base = base.CopyBase(f)
OverlayMap(base, f.Map())
f.Composite = base
}

func (c *compiler) compileMap(dst *Map, ast *d2ast.Map) {
Expand Down Expand Up @@ -128,6 +86,22 @@ func (c *compiler) compileField(dst *Map, kp *d2ast.KeyPath, refctx *RefContext)
parent: f,
}
}
switch NodeBoardKind(f) {
case BoardScenario:
c.overlay(ParentBoard(f).Map(), f)
case BoardStep:
stepsMap := ParentMap(f)
for i := range stepsMap.Fields {
if stepsMap.Fields[i] == f {
if i == 0 {
c.overlay(ParentBoard(f).Map(), f)
} else {
c.overlay(stepsMap.Fields[i-1].Map(), f)
}
break
}
}
}
c.compileMap(f.Map(), refctx.Key.Value.Map)
} else if refctx.Key.Value.ScalarBox().Unbox() != nil {
// If the link is a board, we need to transform it into an absolute path.
Expand Down
19 changes: 16 additions & 3 deletions d2ir/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,20 @@ scenarios: {
assertQuery(t, m, 0, 0, nil, "scenarios.nuclear.quiche")
},
},
{
name: "edge",
run: func(t testing.TB) {
m, err := compile(t, `a -> b
scenarios: {
1: {
(a -> b)[0].style.opacity: 0.1
}
}`)
assert.Success(t, err)

assertQuery(t, m, 0, 0, nil, "(a -> b)[0]")
},
},
}
runa(t, tca)
}
Expand Down Expand Up @@ -431,9 +445,8 @@ scenarios: {
shape: sql_table
hey: int {constraint: primary_key}
}`)
assert.ErrorString(t, err, `TestCompile/steps/steps_panic.d2:6:3: invalid scenario
TestCompile/steps/steps_panic.d2:7:3: invalid scenario
TestCompile/steps/steps_panic.d2:2:3: invalid step`)
assert.ErrorString(t, err, `TestCompile/steps/steps_panic.d2:3:3: invalid step
TestCompile/steps/steps_panic.d2:7:3: invalid scenario`)
},
},
{
Expand Down
152 changes: 76 additions & 76 deletions d2renderers/d2svg/appendix/testdata/internal-links/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified e2etests-cli/testdata/TestCLI_E2E/internal_linked_pdf.exp.pdf
Binary file not shown.
Loading

0 comments on commit ad880fa

Please sign in to comment.