Skip to content

Commit

Permalink
Merge pull request #1797 from alixander/d2oracle-set-imports
Browse files Browse the repository at this point in the history
d2oracle set imports
  • Loading branch information
alixander authored Jan 2, 2024
2 parents 0b99371 + 99c4215 commit d15d5b1
Show file tree
Hide file tree
Showing 9 changed files with 1,170 additions and 107 deletions.
3 changes: 3 additions & 0 deletions d2oracle/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,9 @@ func _set(g *d2graph.Graph, baseAST *d2ast.Map, key string, tag, value *string)
maybeNewScope = ref.MapKey.Value.Map
}
}
} else if IsImported(g, obj) {
appendMapKey(scope, mk)
return nil
} else {
maybeNewScope = obj.Map
}
Expand Down
112 changes: 65 additions & 47 deletions d2oracle/edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package d2oracle_test

import (
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strconv"
"strings"
Expand All @@ -13,6 +10,7 @@ import (
"oss.terrastruct.com/util-go/assert"
"oss.terrastruct.com/util-go/diff"
"oss.terrastruct.com/util-go/go2"
"oss.terrastruct.com/util-go/mapfs"
"oss.terrastruct.com/util-go/xjson"

"oss.terrastruct.com/d2/d2compiler"
Expand Down Expand Up @@ -1999,7 +1997,7 @@ scenarios: {
}
`,
fsTexts: map[string]string{
"meow": `x: {
"meow.d2": `x: {
style.fill: blue
}
`,
Expand All @@ -2022,7 +2020,7 @@ scenarios: {
}
`,
fsTexts: map[string]string{
"meow": `x: {
"meow.d2": `x: {
style.fill: blue
}
`,
Expand All @@ -2046,7 +2044,7 @@ scenarios: {
}
`,
fsTexts: map[string]string{
"meow": `x: {
"meow.d2": `x: {
style.fill: blue
}
`,
Expand All @@ -2058,6 +2056,55 @@ scenarios: {
y
style.fill: yellow
}
`,
},
{
name: "import/4",

text: `...@yo
a`,
fsTexts: map[string]string{
"yo.d2": `b`,
},
key: `b.style.fill`,
value: go2.Pointer(`red`),
exp: `...@yo
a
b.style.fill: red
`,
},
{
name: "import/5",

text: `a
x: {
...@yo
}`,
fsTexts: map[string]string{
"yo.d2": `b`,
},
key: `x.b.style.fill`,
value: go2.Pointer(`red`),
exp: `a
x: {
...@yo
b.style.fill: red
}
`,
},
{
name: "import/6",

text: `a
x: @yo`,
fsTexts: map[string]string{
"yo.d2": `b`,
},
key: `x.b.style.fill`,
value: go2.Pointer(`red`),
exp: `a
x: @yo
x.b.style.fill: red
`,
},
}
Expand Down Expand Up @@ -7081,17 +7128,23 @@ type editTest struct {
}

func (tc editTest) run(t *testing.T) {
var tfs *mapfs.FS
d2Path := fmt.Sprintf("d2/testdata/d2oracle/%v.d2", t.Name())
tfs := testFS(make(map[string]*testF))
for name, text := range tc.fsTexts {
tfs[name] = &testF{content: text}
if tc.fsTexts != nil {
tc.fsTexts["index.d2"] = tc.text
d2Path = "index.d2"
var err error
tfs, err = mapfs.New(tc.fsTexts)
assert.Success(t, err)
t.Cleanup(func() {
assert.Success(t, tfs.Close())
})
}

g, _, err := d2compiler.Compile(d2Path, strings.NewReader(tc.text), &d2compiler.CompileOptions{
FS: tfs,
})
if err != nil {
t.Fatal(err)
}
assert.Success(t, err)

g, err = tc.testFunc(g)
if tc.expErr != "" {
Expand Down Expand Up @@ -8446,38 +8499,3 @@ scenarios: {
})
}
}

type testF struct {
content string
readIndex int
}

func (tf *testF) Close() error {
return nil
}

func (tf *testF) Read(p []byte) (int, error) {
data := []byte(tf.content)
if tf.readIndex >= len(data) {
tf.readIndex = 0
return 0, io.EOF
}
readBytes := copy(p, data[tf.readIndex:])
tf.readIndex += readBytes
return readBytes, nil
}

func (tf *testF) Stat() (os.FileInfo, error) {
return nil, nil
}

type testFS map[string]*testF

func (tfs testFS) Open(name string) (fs.File, error) {
for k := range tfs {
if strings.HasSuffix(name[:len(name)-3], k) {
return tfs[k], nil
}
}
return nil, fs.ErrNotExist
}
10 changes: 10 additions & 0 deletions d2oracle/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ func GetParentID(g *d2graph.Graph, boardPath []string, absID string) (string, er
return obj.Parent.AbsID(), nil
}

func IsImported(g *d2graph.Graph, obj *d2graph.Object) bool {
for _, ref := range obj.References {
if ref.MapKey.Range.Path == g.AST.Range.Path {
return false
}
}

return true
}

func GetObj(g *d2graph.Graph, boardPath []string, absID string) *d2graph.Object {
g = GetBoardGraph(g, boardPath)
if g == nil {
Expand Down
40 changes: 20 additions & 20 deletions testdata/d2oracle/TestSet/import/1.exp.json

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

Loading

0 comments on commit d15d5b1

Please sign in to comment.