address code review

This commit is contained in:
satoqz 2023-05-03 13:42:31 +02:00
parent a2c06de30b
commit 14f88e2db9
No known key found for this signature in database
7 changed files with 928 additions and 18 deletions

View file

@ -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

View file

@ -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
}
}

View file

@ -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]}
}
`,
},
{

View file

@ -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
}
}

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 12 KiB

View file

@ -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
}
}

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 12 KiB