Skip to content

Commit

Permalink
d2ir: Fix implementation of Map.createEdge
Browse files Browse the repository at this point in the history
  • Loading branch information
nhooyr committed Jul 29, 2023
1 parent 4a99d66 commit c349657
Show file tree
Hide file tree
Showing 20 changed files with 571 additions and 6,760 deletions.
2 changes: 1 addition & 1 deletion d2ir/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,8 +685,8 @@ func (c *compiler) _compileEdges(refctx *RefContext) {
e.Map_ = &Map{
parent: e,
}
c.compileField(e.Map_, refctx.Key.EdgeKey, refctx)
}
c.compileField(e.Map_, refctx.Key.EdgeKey, refctx)
} else {
if refctx.Key.Primary.Unbox() != nil {
e.Primary_ = &Scalar{
Expand Down
50 changes: 20 additions & 30 deletions d2ir/d2ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -922,13 +922,6 @@ func (m *Map) createEdge(eid *EdgeID, refctx *RefContext, ea *[]*Edge) error {
return d2parser.Errorf(refctx.Edge.Src.Path[ij].Unbox(), "edge with board keyword alone doesn't make sense")
}

srcStart := len(refctx.Edge.Src.Path) - len(eid.SrcPath)
if srcStart < 0 {
srcStart = 0
}
srcKP := refctx.Edge.Src.Copy()
srcKP.Path = srcKP.Path[srcStart:]

ij = findProhibitedEdgeKeyword(eid.DstPath...)
if ij != -1 {
return d2parser.Errorf(refctx.Edge.Dst.Path[ij].Unbox(), "reserved keywords are prohibited in edges")
Expand All @@ -938,37 +931,34 @@ func (m *Map) createEdge(eid *EdgeID, refctx *RefContext, ea *[]*Edge) error {
return d2parser.Errorf(refctx.Edge.Dst.Path[ij].Unbox(), "edge with board keyword alone doesn't make sense")
}

dstStart := len(refctx.Edge.Dst.Path) - len(eid.DstPath)
if dstStart < 0 {
dstStart = 0
}
dstKP := refctx.Edge.Dst.Copy()
dstKP.Path = dstKP.Path[dstStart:]

underscoresCountSrc := 0
underscoresCountDst := 0
for _, el := range refctx.Edge.Src.Path {
if el.ScalarString() == "_" {
underscoresCountSrc++
srcKP := d2ast.MakeKeyPath(eid.SrcPath)
lastMatch := 0
for i, el := range srcKP.Path {
for j := lastMatch; j < len(refctx.Edge.Src.Path); j++ {
realEl := refctx.Edge.Src.Path[j]
if el.ScalarString() == realEl.ScalarString() {
srcKP.Path[i] = realEl
lastMatch += j + 1
}
}
}
for _, el := range refctx.Edge.Dst.Path {
if el.ScalarString() == "_" {
underscoresCountDst++
dstKP := d2ast.MakeKeyPath(eid.DstPath)
lastMatch = 0
for i, el := range dstKP.Path {
for j := lastMatch; j < len(refctx.Edge.Dst.Path); j++ {
realEl := refctx.Edge.Dst.Path[j]
if el.ScalarString() == realEl.ScalarString() {
dstKP.Path[i] = realEl
lastMatch += j + 1
}
}
}
for i := 0; i < underscoresCountDst; i++ {
srcKP.Path = append([]*d2ast.StringBox{d2ast.RawStringBox(eid.SrcPath[i], true)}, srcKP.Path...)
}
for i := 0; i < underscoresCountSrc; i++ {
dstKP.Path = append([]*d2ast.StringBox{d2ast.RawStringBox(eid.DstPath[i], true)}, dstKP.Path...)
}

srcFA, err := m.EnsureField(srcKP, refctx)
srcFA, err := m.EnsureField(srcKP, nil)
if err != nil {
return err
}
dstFA, err := m.EnsureField(dstKP, refctx)
dstFA, err := m.EnsureField(dstKP, nil)
if err != nil {
return err
}
Expand Down
42 changes: 42 additions & 0 deletions testdata/d2compiler/TestCompile2/nulls/basic/attribute.exp.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c349657

Please sign in to comment.