Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql column label #801

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,12 +525,17 @@ func (c *compiler) compileSQLTable(obj *d2graph.Object) {
typ = ""
}
d2Col := d2target.SQLColumn{
Name: d2target.Text{Label: col.IDVal},
Name: col.IDVal,
Type: d2target.Text{Label: typ},
}
if col.Attributes.Constraint.Value != "" {
d2Col.Constraint = col.Attributes.Constraint.Value
}
if col.Attributes.Label.Value != "" {
d2Col.Label = d2target.Text{Label: col.Attributes.Label.Value}
} else {
d2Col.Label = d2target.Text{Label: col.IDVal}
}
obj.SQLTable.Columns = append(obj.SQLTable.Columns, d2Col)
}

Expand Down
12 changes: 12 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1594,6 +1594,18 @@ x.y -> a.b: {
tassert.Equal(t, "true", g.Edges[0].Attributes.Style.Animated.Value)
},
},
{
name: "table_column_label",

text: `x: {
shape: sql_table
w: int { label: width }
}
`,
assertions: func(t *testing.T, g *d2graph.Graph) {
tassert.Equal(t, "width", g.Objects[0].SQLTable.Columns[0].Label.Label)
},
},
{
name: "class_paren",

Expand Down
8 changes: 4 additions & 4 deletions d2graph/d2graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -861,8 +861,8 @@ func (obj *Object) GetDefaultSize(mtexts []*d2target.MText, ruler *textmeasure.R
if nameDims == nil {
return nil, fmt.Errorf("dimensions for sql_table name %#v not found", ctexts[0].Text)
}
c.Name.LabelWidth = nameDims.Width
c.Name.LabelHeight = nameDims.Height
c.Label.LabelWidth = nameDims.Width
c.Label.LabelHeight = nameDims.Height
maxNameWidth = go2.Max(maxNameWidth, nameDims.Width)

typeDims := GetTextDimensions(mtexts, ruler, ctexts[1], fontFamily)
Expand Down Expand Up @@ -1033,7 +1033,7 @@ func addSQLTableColumnIndices(e *Edge, srcID, dstID []string, obj, src, dst *Obj
srcAbsID := src.AbsIDArray()
if len(objAbsID)+len(srcID) > len(srcAbsID) {
for i, d2col := range src.SQLTable.Columns {
if d2col.Name.Label == srcID[len(srcID)-1] {
if d2col.Name == srcID[len(srcID)-1] {
d2col.Reference = dst.AbsID()
e.SrcTableColumnIndex = new(int)
*e.SrcTableColumnIndex = i
Expand All @@ -1047,7 +1047,7 @@ func addSQLTableColumnIndices(e *Edge, srcID, dstID []string, obj, src, dst *Obj
dstAbsID := dst.AbsIDArray()
if len(objAbsID)+len(dstID) > len(dstAbsID) {
for i, d2col := range dst.SQLTable.Columns {
if d2col.Name.Label == dstID[len(dstID)-1] {
if d2col.Name == dstID[len(dstID)-1] {
d2col.Reference = dst.AbsID()
e.DstTableColumnIndex = new(int)
*e.DstTableColumnIndex = i
Expand Down
4 changes: 2 additions & 2 deletions d2renderers/d2sketch/sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func Table(r *Runner, shape d2target.Shape) (string, error) {

var longestNameWidth int
for _, f := range shape.Columns {
longestNameWidth = go2.Max(longestNameWidth, f.Name.LabelWidth)
longestNameWidth = go2.Max(longestNameWidth, f.Label.LabelWidth)
}

rowBox := geo.NewBox(box.TopLeft.Copy(), box.Width, rowHeight)
Expand All @@ -339,7 +339,7 @@ func Table(r *Runner, shape d2target.Shape) (string, error) {
nameTL.X,
nameTL.Y+float64(shape.FontSize)*3/4,
fmt.Sprintf("text-anchor:%s;font-size:%vpx;fill:%s", "start", float64(shape.FontSize), shape.PrimaryAccentColor),
svg.EscapeText(f.Name.Label),
svg.EscapeText(f.Label.Label),
),
fmt.Sprintf(`<text class="text" x="%f" y="%f" style="%s">%s</text>`,
nameTL.X+float64(longestNameWidth)+2*d2target.NamePadding,
Expand Down
4 changes: 2 additions & 2 deletions d2renderers/d2svg/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ func drawTable(writer io.Writer, targetShape d2target.Shape) {

var longestNameWidth int
for _, f := range targetShape.Columns {
longestNameWidth = go2.Max(longestNameWidth, f.Name.LabelWidth)
longestNameWidth = go2.Max(longestNameWidth, f.Label.LabelWidth)
}

rowBox := geo.NewBox(box.TopLeft.Copy(), box.Width, rowHeight)
rowBox.TopLeft.Y += headerBox.Height
for _, f := range targetShape.Columns {
fmt.Fprint(writer,
tableRow(targetShape, rowBox, f.Name.Label, f.Type.Label, f.ConstraintAbbr(), float64(targetShape.FontSize), float64(longestNameWidth)),
tableRow(targetShape, rowBox, f.Label.Label, f.Type.Label, f.ConstraintAbbr(), float64(targetShape.FontSize), float64(longestNameWidth)),
)
rowBox.TopLeft.Y += rowHeight
fmt.Fprintf(writer, `<line x1="%f" y1="%f" x2="%f" y2="%f" style="stroke-width:2;stroke:%s" />`,
Expand Down
5 changes: 3 additions & 2 deletions d2target/sqltable.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ type SQLTable struct {
}

type SQLColumn struct {
Name Text `json:"name"`
Name string `json:"name"`
Type Text `json:"type"`
Label Text `json:"label"`
Constraint string `json:"constraint"`
Reference string `json:"reference"`
}

func (c SQLColumn) Texts(fontSize int) []*MText {
return []*MText{
{
Text: c.Name.Label,
Text: c.Label.Label,
FontSize: fontSize,
IsBold: false,
IsItalic: false,
Expand Down
8 changes: 8 additions & 0 deletions e2etests/stable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1939,6 +1939,14 @@ x.y -> a.b: {
style.animated: true
target-arrowhead.shape: cf-many
}
`,
},
{
name: "sql_table_column_label",
script: `x: {
shape: sql_table
w: int { label: width }
}
`,
},
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.