diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index f3c0d2a77..f28d8c6ae 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -3,3 +3,5 @@ #### Improvements 🧹 #### Bugfixes ⛑️ + +- Fix importing files that override an existing value with an array. [#1762](https://github.com/terrastruct/d2/pull/1762) diff --git a/d2ir/import_test.go b/d2ir/import_test.go index 072abbf51..81a3e3f50 100644 --- a/d2ir/import_test.go +++ b/d2ir/import_test.go @@ -199,6 +199,18 @@ label: meow`, assert.Success(t, err) }, }, + { + name: "merge-arrays", + run: func(t testing.TB) { + _, err := compileFS(t, "index.d2", map[string]string{ + "index.d2": `x.class: [a] +...@d +`, + "d.d2": `x.class: [b]`, + }) + assert.Success(t, err) + }, + }, } runa(t, tca) diff --git a/d2ir/merge.go b/d2ir/merge.go index d68464bbe..e93ba9e2d 100644 --- a/d2ir/merge.go +++ b/d2ir/merge.go @@ -30,7 +30,7 @@ func OverlayField(bf, of *Field) { if bf.Map() != nil && of.Map() != nil { OverlayMap(bf.Map(), of.Map()) } else { - bf.Composite = of.Composite.Copy(bf).(*Map) + bf.Composite = of.Composite.Copy(bf).(Composite) } } diff --git a/testdata/d2ir/TestCompile/imports/merge-arrays.exp.json b/testdata/d2ir/TestCompile/imports/merge-arrays.exp.json new file mode 100644 index 000000000..a0da3f4da --- /dev/null +++ b/testdata/d2ir/TestCompile/imports/merge-arrays.exp.json @@ -0,0 +1,403 @@ +{ + "fields": [ + { + "name": "x", + "composite": { + "fields": [ + { + "name": "class", + "composite": { + "values": [ + { + "value": { + "range": "d.d2,0:10:10-0:11:11", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "references": [ + { + "string": { + "range": "index.d2,0:2:2-0:7:7", + "value": [ + { + "string": "class", + "raw_string": "class" + } + ] + }, + "key_path": { + "range": "index.d2,0:0:0-0:7:7", + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + }, + { + "unquoted_string": { + "range": "index.d2,0:2:2-0:7:7", + "value": [ + { + "string": "class", + "raw_string": "class" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "index.d2,0:0:0-0:12:12", + "key": { + "range": "index.d2,0:0:0-0:7:7", + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + }, + { + "unquoted_string": { + "range": "index.d2,0:2:2-0:7:7", + "value": [ + { + "string": "class", + "raw_string": "class" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "array": { + "range": "index.d2,0:9:9-1:0:13", + "nodes": [ + { + "unquoted_string": { + "range": "index.d2,0:10:10-0:11:11", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + } + } + } + }, + "due_to_glob": false, + "due_to_lazy_glob": false + }, + { + "string": { + "range": "d.d2,0:2:2-0:7:7", + "value": [ + { + "string": "class", + "raw_string": "class" + } + ] + }, + "key_path": { + "range": "d.d2,0:0:0-0:7:7", + "path": [ + { + "unquoted_string": { + "range": "d.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + }, + { + "unquoted_string": { + "range": "d.d2,0:2:2-0:7:7", + "value": [ + { + "string": "class", + "raw_string": "class" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "d.d2,0:0:0-0:12:12", + "key": { + "range": "d.d2,0:0:0-0:7:7", + "path": [ + { + "unquoted_string": { + "range": "d.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + }, + { + "unquoted_string": { + "range": "d.d2,0:2:2-0:7:7", + "value": [ + { + "string": "class", + "raw_string": "class" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "array": { + "range": "d.d2,0:9:9-0:12:12", + "nodes": [ + { + "unquoted_string": { + "range": "d.d2,0:10:10-0:11:11", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + } + } + } + }, + "due_to_glob": false, + "due_to_lazy_glob": false + } + ] + } + ], + "edges": null + }, + "references": [ + { + "string": { + "range": "index.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + }, + "key_path": { + "range": "index.d2,0:0:0-0:7:7", + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + }, + { + "unquoted_string": { + "range": "index.d2,0:2:2-0:7:7", + "value": [ + { + "string": "class", + "raw_string": "class" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "index.d2,0:0:0-0:12:12", + "key": { + "range": "index.d2,0:0:0-0:7:7", + "path": [ + { + "unquoted_string": { + "range": "index.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + }, + { + "unquoted_string": { + "range": "index.d2,0:2:2-0:7:7", + "value": [ + { + "string": "class", + "raw_string": "class" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "array": { + "range": "index.d2,0:9:9-1:0:13", + "nodes": [ + { + "unquoted_string": { + "range": "index.d2,0:10:10-0:11:11", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + } + } + } + }, + "due_to_glob": false, + "due_to_lazy_glob": false + }, + { + "string": { + "range": "d.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + }, + "key_path": { + "range": "d.d2,0:0:0-0:7:7", + "path": [ + { + "unquoted_string": { + "range": "d.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + }, + { + "unquoted_string": { + "range": "d.d2,0:2:2-0:7:7", + "value": [ + { + "string": "class", + "raw_string": "class" + } + ] + } + } + ] + }, + "context": { + "edge": null, + "key": { + "range": "d.d2,0:0:0-0:12:12", + "key": { + "range": "d.d2,0:0:0-0:7:7", + "path": [ + { + "unquoted_string": { + "range": "d.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + }, + { + "unquoted_string": { + "range": "d.d2,0:2:2-0:7:7", + "value": [ + { + "string": "class", + "raw_string": "class" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "array": { + "range": "d.d2,0:9:9-0:12:12", + "nodes": [ + { + "unquoted_string": { + "range": "d.d2,0:10:10-0:11:11", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + } + } + } + }, + "due_to_glob": false, + "due_to_lazy_glob": false + } + ] + } + ], + "edges": null +}