Merge branch 'terrastruct:master' into master

This commit is contained in:
Vojtěch Fošnár 2023-02-19 21:03:54 +01:00 committed by GitHub
commit 7bf795b455
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 199 additions and 29 deletions

View file

@ -6,6 +6,7 @@
#### Improvements 🧹
- Cleaner watch mode logs without timestamps. [830](https://github.com/terrastruct/d2/pull/830)
- `near` key set to direct parent or ancestor throws an appropriate error message. [#851](https://github.com/terrastruct/d2/pull/851)
#### Bugfixes ⛑️
@ -13,4 +14,4 @@
- Fixes rare compiler bug when using underscores in edges to create objects across containers. [#824](https://github.com/terrastruct/d2/pull/824)
- Fixes rare possibility of rendered connections being hidden or cut off. [#828](https://github.com/terrastruct/d2/pull/828)
- Creating nested children within `sql_table` and `class` shapes are now prevented (caused confusion when accidentally done). [#834](https://github.com/terrastruct/d2/pull/834)
- Fixes graph deserialization bug. [#837](https://github.com/terrastruct/d2/pull/837)
- Fixes graph deserialization bug. [#837](https://github.com/terrastruct/d2/pull/837)

View file

@ -648,21 +648,33 @@ func (c *compiler) validateKey(obj *d2graph.Object, f *d2ir.Field) {
func (c *compiler) validateNear(g *d2graph.Graph) {
for _, obj := range g.Objects {
if obj.Attributes.NearKey != nil {
_, isKey := g.Root.HasChild(d2graph.Key(obj.Attributes.NearKey))
nearObj, isKey := g.Root.HasChild(d2graph.Key(obj.Attributes.NearKey))
_, isConst := d2graph.NearConstants[d2graph.Key(obj.Attributes.NearKey)[0]]
if !isKey && !isConst {
c.errorf(obj.Attributes.NearKey, "near key %#v must be the absolute path to a shape or one of the following constants: %s", d2format.Format(obj.Attributes.NearKey), strings.Join(d2graph.NearConstantsArray, ", "))
continue
}
if !isKey && isConst && obj.Parent != g.Root {
c.errorf(obj.Attributes.NearKey, "constant near keys can only be set on root level shapes")
continue
}
if !isKey && isConst && len(obj.ChildrenArray) > 0 {
c.errorf(obj.Attributes.NearKey, "constant near keys cannot be set on shapes with children")
continue
}
if !isKey && isConst {
if isKey {
// Doesn't make sense to set near to an ancestor or descendant
nearIsAncestor := false
for curr := obj; curr != nil; curr = curr.Parent {
if curr == nearObj {
nearIsAncestor = true
break
}
}
if nearIsAncestor {
c.errorf(obj.Attributes.NearKey, "near keys cannot be set to an ancestor")
continue
}
nearIsDescendant := false
for curr := nearObj; curr != nil; curr = curr.Parent {
if curr == obj {
nearIsDescendant = true
break
}
}
if nearIsDescendant {
c.errorf(obj.Attributes.NearKey, "near keys cannot be set to an descendant")
continue
}
} else if isConst {
is := false
for _, e := range g.Edges {
if e.Src == obj || e.Dst == obj {
@ -674,6 +686,17 @@ func (c *compiler) validateNear(g *d2graph.Graph) {
c.errorf(obj.Attributes.NearKey, "constant near keys cannot be set on connected shapes")
continue
}
if obj.Parent != g.Root {
c.errorf(obj.Attributes.NearKey, "constant near keys can only be set on root level shapes")
continue
}
if len(obj.ChildrenArray) > 0 {
c.errorf(obj.Attributes.NearKey, "constant near keys cannot be set on shapes with children")
continue
}
} else {
c.errorf(obj.Attributes.NearKey, "near key %#v must be the absolute path to a shape or one of the following constants: %s", d2format.Format(obj.Attributes.NearKey), strings.Join(d2graph.NearConstantsArray, ", "))
continue
}
}
}

View file

@ -1398,6 +1398,29 @@ x -> y: {
text: `x.near: top-center
`,
},
{
name: "near-invalid",
text: `mongodb: MongoDB {
perspective: perspective (View) {
password
}
explanation: |md
perspective.model.js
| {
near: mongodb
}
}
a: {
near: a.b
b
}
`,
expErr: `d2/testdata/d2compiler/TestCompile/near-invalid.d2:9:11: near keys cannot be set to an ancestor
d2/testdata/d2compiler/TestCompile/near-invalid.d2:14:9: near keys cannot be set to an descendant`,
},
{
name: "near_bad_constant",

View file

@ -2220,7 +2220,7 @@ a.b.c: {
`,
},
{
name: "near",
name: "invalid-near",
text: `x: {
near: y
@ -2234,6 +2234,33 @@ y
near: x.y
y
}
`,
expErr: `failed to move: "y" to "x.y": failed to recompile:
x: {
near: x.y
y
}
d2/testdata/d2oracle/TestMove/invalid-near.d2:2:9: near keys cannot be set to an descendant`,
},
{
name: "near",
text: `x: {
near: y
}
a
y
`,
key: `y`,
newKey: `a.y`,
exp: `x: {
near: a.y
}
a: {
y
}
`,
},
{

16
testdata/d2compiler/TestCompile/near-invalid.exp.json generated vendored Normal file
View file

@ -0,0 +1,16 @@
{
"graph": null,
"err": {
"ioerr": null,
"errs": [
{
"range": "d2/testdata/d2compiler/TestCompile/near-invalid.d2,8:10:133-8:17:140",
"errmsg": "d2/testdata/d2compiler/TestCompile/near-invalid.d2:9:11: near keys cannot be set to an ancestor"
},
{
"range": "d2/testdata/d2compiler/TestCompile/near-invalid.d2,13:8:161-13:11:164",
"errmsg": "d2/testdata/d2compiler/TestCompile/near-invalid.d2:14:9: near keys cannot be set to an descendant"
}
]
}
}

4
testdata/d2oracle/TestMove/invalid-near.exp.json generated vendored Normal file
View file

@ -0,0 +1,4 @@
{
"graph": null,
"err": "failed to move: \"y\" to \"x.y\": failed to recompile:\nx: {\n near: x.y\n y\n}\n\nd2/testdata/d2oracle/TestMove/invalid-near.d2:2:9: near keys cannot be set to an descendant"
}

View file

@ -2,11 +2,11 @@
"graph": {
"name": "",
"ast": {
"range": "d2/testdata/d2oracle/TestMove/near.d2,0:0:0-4:0:23",
"range": "d2/testdata/d2oracle/TestMove/near.d2,0:0:0-6:0:30",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2oracle/TestMove/near.d2,0:0:0-3:1:22",
"range": "d2/testdata/d2oracle/TestMove/near.d2,0:0:0-2:1:18",
"key": {
"range": "d2/testdata/d2oracle/TestMove/near.d2,0:0:0-0:1:1",
"path": [
@ -26,7 +26,7 @@
"primary": {},
"value": {
"map": {
"range": "d2/testdata/d2oracle/TestMove/near.d2,0:3:3-3:0:21",
"range": "d2/testdata/d2oracle/TestMove/near.d2,0:3:3-2:0:17",
"nodes": [
{
"map_key": {
@ -53,23 +53,52 @@
"range": "d2/testdata/d2oracle/TestMove/near.d2,1:8:13-1:11:16",
"value": [
{
"string": "x.y",
"raw_string": "x.y"
"string": "a.y",
"raw_string": "a.y"
}
]
}
}
}
},
}
]
}
}
}
},
{
"map_key": {
"range": "d2/testdata/d2oracle/TestMove/near.d2,3:0:19-5:1:29",
"key": {
"range": "d2/testdata/d2oracle/TestMove/near.d2,3:0:19-3:1:20",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2oracle/TestMove/near.d2,3:0:19-3:1:20",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": "d2/testdata/d2oracle/TestMove/near.d2,3:3:22-5:0:28",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2oracle/TestMove/near.d2,2:2:19-2:3:20",
"range": "d2/testdata/d2oracle/TestMove/near.d2,4:2:26-4:3:27",
"key": {
"range": "d2/testdata/d2oracle/TestMove/near.d2,2:2:19-2:3:20",
"range": "d2/testdata/d2oracle/TestMove/near.d2,4:2:26-4:3:27",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2oracle/TestMove/near.d2,2:2:19-2:3:20",
"range": "d2/testdata/d2oracle/TestMove/near.d2,4:2:26-4:3:27",
"value": [
{
"string": "y",
@ -160,8 +189,8 @@
"range": ",0:0:0-0:1:1",
"value": [
{
"string": "x",
"raw_string": "x"
"string": "a",
"raw_string": "a"
}
]
}
@ -191,6 +220,53 @@
},
"zIndex": 0
},
{
"id": "a",
"id_val": "a",
"label_dimensions": {
"width": 0,
"height": 0
},
"references": [
{
"key": {
"range": "d2/testdata/d2oracle/TestMove/near.d2,3:0:19-3:1:20",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2oracle/TestMove/near.d2,3:0:19-3:1:20",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
}
],
"attributes": {
"label": {
"value": "a"
},
"style": {},
"near_key": null,
"shape": {
"value": "rectangle"
},
"direction": {
"value": ""
},
"constraint": {
"value": ""
}
},
"zIndex": 0
},
{
"id": "y",
"id_val": "y",
@ -201,11 +277,11 @@
"references": [
{
"key": {
"range": "d2/testdata/d2oracle/TestMove/near.d2,2:2:19-2:3:20",
"range": "d2/testdata/d2oracle/TestMove/near.d2,4:2:26-4:3:27",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2oracle/TestMove/near.d2,2:2:19-2:3:20",
"range": "d2/testdata/d2oracle/TestMove/near.d2,4:2:26-4:3:27",
"value": [
{
"string": "y",