From 3a6834662dca19902d342185181d9846c62f087b Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Sat, 24 Dec 2022 13:11:45 -0800 Subject: [PATCH] fix sql constraint array panic --- d2compiler/compile.go | 4 ++++ d2compiler/compile_test.go | 10 ++++++++++ testdata/d2compiler/TestCompile/sql-panic.exp.json | 12 ++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 testdata/d2compiler/TestCompile/sql-panic.exp.json diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 819dae9e3..9ca656098 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -688,6 +688,10 @@ func (c *compiler) compileSQLTable(obj *d2graph.Object) { continue } if n.MapKey.Key.Path[0].Unbox().ScalarString() == "constraint" { + if n.MapKey.Value.StringBox().Unbox() == nil { + c.errorf(n.MapKey.GetRange().Start, n.MapKey.GetRange().End, "constraint value must be a string") + return + } d2Col.Constraint = n.MapKey.Value.StringBox().Unbox().ScalarString() } } diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index 4009aa68b..afe9ceec4 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -1668,6 +1668,16 @@ choo: { tassert.Equal(t, 3, len(g.Objects)) }, }, + { + name: "sql-panic", + text: `test { + shape: sql_table + test_id: varchar(64) {constraint: [primary_key, foreign_key]} +} +`, + expErr: `d2/testdata/d2compiler/TestCompile/sql-panic.d2:3:27: constraint value must be a string +`, + }, } for _, tc := range testCases { diff --git a/testdata/d2compiler/TestCompile/sql-panic.exp.json b/testdata/d2compiler/TestCompile/sql-panic.exp.json new file mode 100644 index 000000000..c65b828a6 --- /dev/null +++ b/testdata/d2compiler/TestCompile/sql-panic.exp.json @@ -0,0 +1,12 @@ +{ + "graph": null, + "err": { + "ioerr": null, + "errs": [ + { + "range": "d2/testdata/d2compiler/TestCompile/sql-panic.d2,2:26:54-2:64:92", + "errmsg": "d2/testdata/d2compiler/TestCompile/sql-panic.d2:3:27: constraint value must be a string" + } + ] + } +}