Merge pull request #245 from alixander/alixander/shape-sequence

syntax: shape: sequence
This commit is contained in:
Alexander Wang 2022-11-28 20:46:12 -08:00 committed by GitHub
commit 3f6b14cb00
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 266 additions and 45 deletions

View file

@ -266,6 +266,8 @@ func (c *compiler) compileKey(obj *d2graph.Object, m *d2ast.Map, mk *d2ast.Key)
}, unresolvedObj) }, unresolvedObj)
} else if obj.Parent == nil { } else if obj.Parent == nil {
// Top level reserved key set on root. // Top level reserved key set on root.
c.compileAttributes(&obj.Attributes, mk)
c.applyScalar(&obj.Attributes, reserved, mk.Value.ScalarBox())
return return
} }

View file

@ -1501,6 +1501,26 @@ dst.id <-> src.dst_id
} }
}, },
}, },
{
name: "basic_sequence",
text: `x: {
shape: sequence_diagram
}
`,
assertions: func(t *testing.T, g *d2graph.Graph) {
diff.AssertStringEq(t, "sequence_diagram", g.Objects[0].Attributes.Shape.Value)
},
},
{
name: "root_sequence",
text: `shape: sequence_diagram
`,
assertions: func(t *testing.T, g *d2graph.Graph) {
diff.AssertStringEq(t, "sequence_diagram", g.Root.Attributes.Shape.Value)
},
},
} }
for _, tc := range testCases { for _, tc := range testCases {

View file

@ -255,28 +255,29 @@ func NewPoint(x, y int) Point {
} }
const ( const (
ShapeRectangle = "rectangle" ShapeRectangle = "rectangle"
ShapeSquare = "square" ShapeSquare = "square"
ShapePage = "page" ShapePage = "page"
ShapeParallelogram = "parallelogram" ShapeParallelogram = "parallelogram"
ShapeDocument = "document" ShapeDocument = "document"
ShapeCylinder = "cylinder" ShapeCylinder = "cylinder"
ShapeQueue = "queue" ShapeQueue = "queue"
ShapePackage = "package" ShapePackage = "package"
ShapeStep = "step" ShapeStep = "step"
ShapeCallout = "callout" ShapeCallout = "callout"
ShapeStoredData = "stored_data" ShapeStoredData = "stored_data"
ShapePerson = "person" ShapePerson = "person"
ShapeDiamond = "diamond" ShapeDiamond = "diamond"
ShapeOval = "oval" ShapeOval = "oval"
ShapeCircle = "circle" ShapeCircle = "circle"
ShapeHexagon = "hexagon" ShapeHexagon = "hexagon"
ShapeCloud = "cloud" ShapeCloud = "cloud"
ShapeText = "text" ShapeText = "text"
ShapeCode = "code" ShapeCode = "code"
ShapeClass = "class" ShapeClass = "class"
ShapeSQLTable = "sql_table" ShapeSQLTable = "sql_table"
ShapeImage = "image" ShapeImage = "image"
ShapeSequenceDiagram = "sequence_diagram"
) )
var Shapes = []string{ var Shapes = []string{
@ -302,6 +303,7 @@ var Shapes = []string{
ShapeClass, ShapeClass,
ShapeSQLTable, ShapeSQLTable,
ShapeImage, ShapeImage,
ShapeSequenceDiagram,
} }
func IsShape(s string) bool { func IsShape(s string) bool {
@ -345,29 +347,30 @@ func (text MText) GetColor(theme *d2themes.Theme, isItalic bool) string {
} }
var DSL_SHAPE_TO_SHAPE_TYPE = map[string]string{ var DSL_SHAPE_TO_SHAPE_TYPE = map[string]string{
"": shape.SQUARE_TYPE, "": shape.SQUARE_TYPE,
ShapeRectangle: shape.SQUARE_TYPE, ShapeRectangle: shape.SQUARE_TYPE,
ShapeSquare: shape.REAL_SQUARE_TYPE, ShapeSquare: shape.REAL_SQUARE_TYPE,
ShapePage: shape.PAGE_TYPE, ShapePage: shape.PAGE_TYPE,
ShapeParallelogram: shape.PARALLELOGRAM_TYPE, ShapeParallelogram: shape.PARALLELOGRAM_TYPE,
ShapeDocument: shape.DOCUMENT_TYPE, ShapeDocument: shape.DOCUMENT_TYPE,
ShapeCylinder: shape.CYLINDER_TYPE, ShapeCylinder: shape.CYLINDER_TYPE,
ShapeQueue: shape.QUEUE_TYPE, ShapeQueue: shape.QUEUE_TYPE,
ShapePackage: shape.PACKAGE_TYPE, ShapePackage: shape.PACKAGE_TYPE,
ShapeStep: shape.STEP_TYPE, ShapeStep: shape.STEP_TYPE,
ShapeCallout: shape.CALLOUT_TYPE, ShapeCallout: shape.CALLOUT_TYPE,
ShapeStoredData: shape.STORED_DATA_TYPE, ShapeStoredData: shape.STORED_DATA_TYPE,
ShapePerson: shape.PERSON_TYPE, ShapePerson: shape.PERSON_TYPE,
ShapeDiamond: shape.DIAMOND_TYPE, ShapeDiamond: shape.DIAMOND_TYPE,
ShapeOval: shape.OVAL_TYPE, ShapeOval: shape.OVAL_TYPE,
ShapeCircle: shape.CIRCLE_TYPE, ShapeCircle: shape.CIRCLE_TYPE,
ShapeHexagon: shape.HEXAGON_TYPE, ShapeHexagon: shape.HEXAGON_TYPE,
ShapeCloud: shape.CLOUD_TYPE, ShapeCloud: shape.CLOUD_TYPE,
ShapeText: shape.TEXT_TYPE, ShapeText: shape.TEXT_TYPE,
ShapeCode: shape.CODE_TYPE, ShapeCode: shape.CODE_TYPE,
ShapeClass: shape.CLASS_TYPE, ShapeClass: shape.CLASS_TYPE,
ShapeSQLTable: shape.TABLE_TYPE, ShapeSQLTable: shape.TABLE_TYPE,
ShapeImage: shape.IMAGE_TYPE, ShapeImage: shape.IMAGE_TYPE,
ShapeSequenceDiagram: shape.SQUARE_TYPE,
} }
var SHAPE_TYPE_TO_DSL_SHAPE map[string]string var SHAPE_TYPE_TO_DSL_SHAPE map[string]string

133
testdata/d2compiler/TestCompile/basic_sequence.exp.json generated vendored Normal file
View file

@ -0,0 +1,133 @@
{
"graph": {
"ast": {
"range": "d2/testdata/d2compiler/TestCompile/basic_sequence.d2,0:0:0-3:0:33",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile/basic_sequence.d2,0:0:0-2:1:32",
"key": {
"range": "d2/testdata/d2compiler/TestCompile/basic_sequence.d2,0:0:0-0:1:1",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/basic_sequence.d2,0:0:0-0:1:1",
"value": [
{
"string": "x",
"raw_string": "x"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": "d2/testdata/d2compiler/TestCompile/basic_sequence.d2,0:3:3-2:0:31",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile/basic_sequence.d2,1:2:7-1:25:30",
"key": {
"range": "d2/testdata/d2compiler/TestCompile/basic_sequence.d2,1:2:7-1:7:12",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/basic_sequence.d2,1:2:7-1:7:12",
"value": [
{
"string": "shape",
"raw_string": "shape"
}
]
}
}
]
},
"primary": {},
"value": {
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/basic_sequence.d2,1:9:14-1:25:30",
"value": [
{
"string": "sequence_diagram",
"raw_string": "sequence_diagram"
}
]
}
}
}
}
]
}
}
}
}
]
},
"root": {
"id": "",
"id_val": "",
"label_dimensions": {
"width": 0,
"height": 0
},
"attributes": {
"label": {
"value": ""
},
"style": {},
"near_key": null,
"shape": {
"value": ""
}
}
},
"edges": null,
"objects": [
{
"id": "x",
"id_val": "x",
"label_dimensions": {
"width": 0,
"height": 0
},
"references": [
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile/basic_sequence.d2,0:0:0-0:1:1",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/basic_sequence.d2,0:0:0-0:1:1",
"value": [
{
"string": "x",
"raw_string": "x"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": 0
}
],
"attributes": {
"label": {
"value": "x"
},
"style": {},
"near_key": null,
"shape": {
"value": "sequence_diagram"
}
}
}
]
},
"err": null
}

63
testdata/d2compiler/TestCompile/root_sequence.exp.json generated vendored Normal file
View file

@ -0,0 +1,63 @@
{
"graph": {
"ast": {
"range": "d2/testdata/d2compiler/TestCompile/root_sequence.d2,0:0:0-1:0:24",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile/root_sequence.d2,0:0:0-0:23:23",
"key": {
"range": "d2/testdata/d2compiler/TestCompile/root_sequence.d2,0:0:0-0:5:5",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/root_sequence.d2,0:0:0-0:5:5",
"value": [
{
"string": "shape",
"raw_string": "shape"
}
]
}
}
]
},
"primary": {},
"value": {
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/root_sequence.d2,0:7:7-0:23:23",
"value": [
{
"string": "sequence_diagram",
"raw_string": "sequence_diagram"
}
]
}
}
}
}
]
},
"root": {
"id": "",
"id_val": "",
"label_dimensions": {
"width": 0,
"height": 0
},
"attributes": {
"label": {
"value": ""
},
"style": {},
"near_key": null,
"shape": {
"value": "sequence_diagram"
}
}
},
"edges": null,
"objects": null
},
"err": null
}