Merge pull request #508 from alixander/fix-null

fix null key
This commit is contained in:
Alexander Wang 2022-12-26 11:25:19 -08:00 committed by GitHub
commit c0d565bd93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 115 additions and 2 deletions

View file

@ -31,3 +31,4 @@ Hope everyone is enjoying the holidays this week!
- Connections at the boundaries no longer get part of its stroke clipped. [#493](https://github.com/terrastruct/d2/pull/493) - Connections at the boundaries no longer get part of its stroke clipped. [#493](https://github.com/terrastruct/d2/pull/493)
- Fixes edge case where `style` being defined in same scope as `sql_table` causes compiler to skip compiling `sql_table`. [#506](https://github.com/terrastruct/d2/issues/506) - Fixes edge case where `style` being defined in same scope as `sql_table` causes compiler to skip compiling `sql_table`. [#506](https://github.com/terrastruct/d2/issues/506)
- Fixes panic passing a non-string value to `constraint`. [#248](https://github.com/terrastruct/d2/issues/248) - Fixes panic passing a non-string value to `constraint`. [#248](https://github.com/terrastruct/d2/issues/248)
- Fixes edge case where the key `null` was compiling wrongly. [#507](https://github.com/terrastruct/d2/issues/507)

View file

@ -1672,6 +1672,16 @@ choo: {
} }
}, },
}, },
{
name: "null",
text: `null
`,
assertions: func(t *testing.T, g *d2graph.Graph) {
tassert.Equal(t, "'null'", g.Objects[0].ID)
tassert.Equal(t, "null", g.Objects[0].IDVal)
},
},
{ {
name: "sql-regression", name: "sql-regression",

View file

@ -48,7 +48,7 @@ func escapeUnquotedValue(s string, inKey bool) string {
} }
if strings.EqualFold(s, "null") { if strings.EqualFold(s, "null") {
return "\\null" return `'null'`
} }
var b strings.Builder var b strings.Builder

View file

@ -170,7 +170,7 @@ func TestEscapeUnquoted(t *testing.T) {
{ {
name: "null", name: "null",
str: `null`, str: `null`,
exp: `\null`, exp: `'null'`,
}, },
{ {
name: "empty", name: "empty",

102
testdata/d2compiler/TestCompile/null.exp.json generated vendored Normal file
View file

@ -0,0 +1,102 @@
{
"graph": {
"ast": {
"range": "d2/testdata/d2compiler/TestCompile/null.d2,0:0:0-1:0:5",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile/null.d2,0:0:0-0:4:4",
"key": {
"range": "d2/testdata/d2compiler/TestCompile/null.d2,0:0:0-0:4:4",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/null.d2,0:0:0-0:4:4",
"value": [
{
"string": "null",
"raw_string": "null"
}
]
}
}
]
},
"primary": {},
"value": {}
}
}
]
},
"root": {
"id": "",
"id_val": "",
"label_dimensions": {
"width": 0,
"height": 0
},
"attributes": {
"label": {
"value": ""
},
"style": {},
"near_key": null,
"shape": {
"value": ""
},
"direction": {
"value": ""
}
},
"zIndex": 0
},
"edges": null,
"objects": [
{
"id": "'null'",
"id_val": "null",
"label_dimensions": {
"width": 0,
"height": 0
},
"references": [
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile/null.d2,0:0:0-0:4:4",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/null.d2,0:0:0-0:4:4",
"value": [
{
"string": "null",
"raw_string": "null"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": 0
}
],
"attributes": {
"label": {
"value": "null"
},
"style": {},
"near_key": null,
"shape": {
"value": ""
},
"direction": {
"value": ""
}
},
"zIndex": 0
}
]
},
"err": null
}