From 14f88e2db9d1fc8b72859b518728b2f336129603 Mon Sep 17 00:00:00 2001 From: satoqz Date: Wed, 3 May 2023 13:42:31 +0200 Subject: [PATCH] address code review --- d2compiler/compile.go | 32 +- d2graph/d2graph.go | 4 +- e2etests/stable_test.go | 24 ++ .../dagre/board.exp.json | 348 ++++++++++++++++++ .../dagre/sketch.exp.svg | 95 +++++ .../elk/board.exp.json | 348 ++++++++++++++++++ .../elk/sketch.exp.svg | 95 +++++ 7 files changed, 928 insertions(+), 18 deletions(-) create mode 100644 e2etests/testdata/stable/sql_table_constraints_width/dagre/board.exp.json create mode 100644 e2etests/testdata/stable/sql_table_constraints_width/dagre/sketch.exp.svg create mode 100644 e2etests/testdata/stable/sql_table_constraints_width/elk/board.exp.json create mode 100644 e2etests/testdata/stable/sql_table_constraints_width/elk/sketch.exp.svg diff --git a/d2compiler/compile.go b/d2compiler/compile.go index c01544066..46d854cb7 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -307,26 +307,26 @@ func (c *compiler) compileLabel(attrs *d2graph.Attributes, f d2ir.Node) { func (c *compiler) compileReserved(attrs *d2graph.Attributes, f *d2ir.Field) { if f.Primary() == nil { - compositeAccepted := strings.EqualFold(f.Name, "class") || strings.EqualFold(f.Name, "constraint") - if f.Composite != nil && !compositeAccepted { - c.errorf(f.LastPrimaryKey(), "reserved field %v does not accept composite", f.Name) - } - switch f.Name { - case "class": - if arr, ok := f.Composite.(*d2ir.Array); ok { - for _, class := range arr.Values { - if scalar, ok := class.(*d2ir.Scalar); ok { - attrs.Classes = append(attrs.Classes, scalar.Value.ScalarString()) + if f.Composite != nil { + switch f.Name { + case "class": + if arr, ok := f.Composite.(*d2ir.Array); ok { + for _, class := range arr.Values { + if scalar, ok := class.(*d2ir.Scalar); ok { + attrs.Classes = append(attrs.Classes, scalar.Value.ScalarString()) + } } } - } - case "constraint": - if arr, ok := f.Composite.(*d2ir.Array); ok { - for _, constraint := range arr.Values { - if scalar, ok := constraint.(*d2ir.Scalar); ok { - attrs.Constraint = append(attrs.Constraint, scalar.Value.ScalarString()) + case "constraint": + if arr, ok := f.Composite.(*d2ir.Array); ok { + for _, constraint := range arr.Values { + if scalar, ok := constraint.(*d2ir.Scalar); ok { + attrs.Constraint = append(attrs.Constraint, scalar.Value.ScalarString()) + } } } + default: + c.errorf(f.LastPrimaryKey(), "reserved field %v does not accept composite", f.Name) } } return diff --git a/d2graph/d2graph.go b/d2graph/d2graph.go index 56647269f..56a87b5dc 100644 --- a/d2graph/d2graph.go +++ b/d2graph/d2graph.go @@ -1036,8 +1036,8 @@ func (obj *Object) GetDefaultSize(mtexts []*d2target.MText, ruler *textmeasure.R } maxTypeWidth = go2.Max(maxTypeWidth, typeDims.Width) - // 60 covers UNQ constraint with padding, 50 for further constraints covers UNQ + space - if newWidth := 60 + 50*(len(c.Constraint)-1); newWidth > constraintWidth { + // 60 covers UNQ constraint with padding, 40 for further constraints covers UNQ + space + if newWidth := 60 + 40*(len(c.Constraint)-1); newWidth > constraintWidth { constraintWidth = newWidth } } diff --git a/e2etests/stable_test.go b/e2etests/stable_test.go index 79a385e9b..e65916494 100644 --- a/e2etests/stable_test.go +++ b/e2etests/stable_test.go @@ -2385,6 +2385,30 @@ manager2: BatchManager { +getJobs(): "Job[]" +setTimeout(seconds int) } +`, + }, + { + name: "sql_table_constraints_width", + script: ` +a: { + shape: sql_table + x: INT {constraint: unique} +} + +b: { + shape: sql_table + x: INT {constraint: [primary_key; foreign_key]} +} + +c: { + shape: sql_table + x: INT {constraint: [foreign_key; unique]} +} + +d: { + shape: sql_table + x: INT {constraint: [primary_key; foreign_key; unique]} +} `, }, { diff --git a/e2etests/testdata/stable/sql_table_constraints_width/dagre/board.exp.json b/e2etests/testdata/stable/sql_table_constraints_width/dagre/board.exp.json new file mode 100644 index 000000000..07f1bd9f4 --- /dev/null +++ b/e2etests/testdata/stable/sql_table_constraints_width/dagre/board.exp.json @@ -0,0 +1,348 @@ +{ + "name": "", + "isFolderOnly": false, + "fontFamily": "SourceSansPro", + "shapes": [ + { + "id": "a", + "type": "sql_table", + "pos": { + "x": 0, + "y": 0 + }, + "width": 148, + "height": 72, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "N1", + "stroke": "N7", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": [ + { + "name": { + "label": "x", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 9, + "labelHeight": 26 + }, + "type": { + "label": "INT", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 29, + "labelHeight": 26 + }, + "constraint": [ + "unique" + ], + "reference": "" + } + ], + "label": "a", + "fontSize": 20, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 11, + "labelHeight": 31, + "zIndex": 0, + "level": 1, + "primaryAccentColor": "B2", + "secondaryAccentColor": "AA2", + "neutralAccentColor": "N2" + }, + { + "id": "b", + "type": "sql_table", + "pos": { + "x": 208, + "y": 0 + }, + "width": 188, + "height": 72, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "N1", + "stroke": "N7", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": [ + { + "name": { + "label": "x", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 9, + "labelHeight": 26 + }, + "type": { + "label": "INT", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 29, + "labelHeight": 26 + }, + "constraint": [ + "primary_key", + "foreign_key" + ], + "reference": "" + } + ], + "label": "b", + "fontSize": 20, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 12, + "labelHeight": 31, + "zIndex": 0, + "level": 1, + "primaryAccentColor": "B2", + "secondaryAccentColor": "AA2", + "neutralAccentColor": "N2" + }, + { + "id": "c", + "type": "sql_table", + "pos": { + "x": 456, + "y": 0 + }, + "width": 188, + "height": 72, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "N1", + "stroke": "N7", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": [ + { + "name": { + "label": "x", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 9, + "labelHeight": 26 + }, + "type": { + "label": "INT", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 29, + "labelHeight": 26 + }, + "constraint": [ + "foreign_key", + "unique" + ], + "reference": "" + } + ], + "label": "c", + "fontSize": 20, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 11, + "labelHeight": 31, + "zIndex": 0, + "level": 1, + "primaryAccentColor": "B2", + "secondaryAccentColor": "AA2", + "neutralAccentColor": "N2" + }, + { + "id": "d", + "type": "sql_table", + "pos": { + "x": 704, + "y": 0 + }, + "width": 228, + "height": 72, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "N1", + "stroke": "N7", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": [ + { + "name": { + "label": "x", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 9, + "labelHeight": 26 + }, + "type": { + "label": "INT", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 29, + "labelHeight": 26 + }, + "constraint": [ + "primary_key", + "foreign_key", + "unique" + ], + "reference": "" + } + ], + "label": "d", + "fontSize": 20, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 13, + "labelHeight": 31, + "zIndex": 0, + "level": 1, + "primaryAccentColor": "B2", + "secondaryAccentColor": "AA2", + "neutralAccentColor": "N2" + } + ], + "connections": [], + "root": { + "id": "", + "type": "", + "pos": { + "x": 0, + "y": 0 + }, + "width": 0, + "height": 0, + "opacity": 0, + "strokeDash": 0, + "strokeWidth": 0, + "borderRadius": 0, + "fill": "N7", + "stroke": "", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 0, + "labelHeight": 0, + "zIndex": 0, + "level": 0 + } +} diff --git a/e2etests/testdata/stable/sql_table_constraints_width/dagre/sketch.exp.svg b/e2etests/testdata/stable/sql_table_constraints_width/dagre/sketch.exp.svg new file mode 100644 index 000000000..d294404e2 --- /dev/null +++ b/e2etests/testdata/stable/sql_table_constraints_width/dagre/sketch.exp.svg @@ -0,0 +1,95 @@ +axINTUNQbxINTPK FKcxINTFK UNQdxINTPK FK UNQ + + + \ No newline at end of file diff --git a/e2etests/testdata/stable/sql_table_constraints_width/elk/board.exp.json b/e2etests/testdata/stable/sql_table_constraints_width/elk/board.exp.json new file mode 100644 index 000000000..e30fede94 --- /dev/null +++ b/e2etests/testdata/stable/sql_table_constraints_width/elk/board.exp.json @@ -0,0 +1,348 @@ +{ + "name": "", + "isFolderOnly": false, + "fontFamily": "SourceSansPro", + "shapes": [ + { + "id": "a", + "type": "sql_table", + "pos": { + "x": 12, + "y": 12 + }, + "width": 148, + "height": 72, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "N1", + "stroke": "N7", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": [ + { + "name": { + "label": "x", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 9, + "labelHeight": 26 + }, + "type": { + "label": "INT", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 29, + "labelHeight": 26 + }, + "constraint": [ + "unique" + ], + "reference": "" + } + ], + "label": "a", + "fontSize": 20, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 11, + "labelHeight": 31, + "zIndex": 0, + "level": 1, + "primaryAccentColor": "B2", + "secondaryAccentColor": "AA2", + "neutralAccentColor": "N2" + }, + { + "id": "b", + "type": "sql_table", + "pos": { + "x": 180, + "y": 12 + }, + "width": 188, + "height": 72, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "N1", + "stroke": "N7", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": [ + { + "name": { + "label": "x", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 9, + "labelHeight": 26 + }, + "type": { + "label": "INT", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 29, + "labelHeight": 26 + }, + "constraint": [ + "primary_key", + "foreign_key" + ], + "reference": "" + } + ], + "label": "b", + "fontSize": 20, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 12, + "labelHeight": 31, + "zIndex": 0, + "level": 1, + "primaryAccentColor": "B2", + "secondaryAccentColor": "AA2", + "neutralAccentColor": "N2" + }, + { + "id": "c", + "type": "sql_table", + "pos": { + "x": 388, + "y": 12 + }, + "width": 188, + "height": 72, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "N1", + "stroke": "N7", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": [ + { + "name": { + "label": "x", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 9, + "labelHeight": 26 + }, + "type": { + "label": "INT", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 29, + "labelHeight": 26 + }, + "constraint": [ + "foreign_key", + "unique" + ], + "reference": "" + } + ], + "label": "c", + "fontSize": 20, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 11, + "labelHeight": 31, + "zIndex": 0, + "level": 1, + "primaryAccentColor": "B2", + "secondaryAccentColor": "AA2", + "neutralAccentColor": "N2" + }, + { + "id": "d", + "type": "sql_table", + "pos": { + "x": 596, + "y": 12 + }, + "width": 228, + "height": 72, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "N1", + "stroke": "N7", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": [ + { + "name": { + "label": "x", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 9, + "labelHeight": 26 + }, + "type": { + "label": "INT", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 29, + "labelHeight": 26 + }, + "constraint": [ + "primary_key", + "foreign_key", + "unique" + ], + "reference": "" + } + ], + "label": "d", + "fontSize": 20, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 13, + "labelHeight": 31, + "zIndex": 0, + "level": 1, + "primaryAccentColor": "B2", + "secondaryAccentColor": "AA2", + "neutralAccentColor": "N2" + } + ], + "connections": [], + "root": { + "id": "", + "type": "", + "pos": { + "x": 0, + "y": 0 + }, + "width": 0, + "height": 0, + "opacity": 0, + "strokeDash": 0, + "strokeWidth": 0, + "borderRadius": 0, + "fill": "N7", + "stroke": "", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 0, + "labelHeight": 0, + "zIndex": 0, + "level": 0 + } +} diff --git a/e2etests/testdata/stable/sql_table_constraints_width/elk/sketch.exp.svg b/e2etests/testdata/stable/sql_table_constraints_width/elk/sketch.exp.svg new file mode 100644 index 000000000..f15db75c8 --- /dev/null +++ b/e2etests/testdata/stable/sql_table_constraints_width/elk/sketch.exp.svg @@ -0,0 +1,95 @@ +axINTUNQbxINTPK FKcxINTFK UNQdxINTPK FK UNQ + + + \ No newline at end of file