Skip to content

Commit

Permalink
Merge pull request #2173 from alixander/create-board-api
Browse files Browse the repository at this point in the history
d2oracle: create board
  • Loading branch information
alixander authored Oct 22, 2024
2 parents 0bf1823 + d9387f7 commit dcd7c15
Show file tree
Hide file tree
Showing 8 changed files with 981 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ let us know and we'll be happy to include it here!
- **ent2d2**: [https://github.com/tmc/ent2d2](https://github.com/tmc/ent2d2)
- **MkDocs Plugin**: [https://github.com/landmaj/mkdocs-d2-plugin](https://github.com/landmaj/mkdocs-d2-plugin)
- **Remark Plugin**: [https://github.com/mech-a/remark-d2](https://github.com/mech-a/remark-d2)
- **Zed extension**: [https://github.com/gabeidx/zed-d2](https://github.com/gabeidx/zed-d2)

### Misc

Expand Down
6 changes: 4 additions & 2 deletions d2format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,14 @@ func (p *printer) _map(m *d2ast.Map) {
n := boards[i].Unbox()
// if this board is the very first line of the file, don't add an extra newline
if n.GetRange().Start.Line != 0 {
p.newline()
p.sb.WriteByte('\n')
}
// if scope only has boards, don't newline the first board
if i != 0 || len(m.Nodes) > len(boards) {
p.newline()
p.sb.WriteByte('\n')
}

p.sb.WriteString(p.indentStr)
p.node(n)
prev = n
}
Expand Down
18 changes: 9 additions & 9 deletions d2format/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ meow
diagram: int {constraint: foreign_key}
}
meow <- diagrams.id
steps: {
shape: sql_table
id: {type: int; constraint: primary_key}
Expand Down Expand Up @@ -664,8 +664,8 @@ x: @"x/../file"
b: {
e
scenarios: {
p: {
x
p: {
x
}
}
}
Expand All @@ -677,14 +677,14 @@ x: @"x/../file"
exp: `layers: {
b: {
e
scenarios: {
p: {
x
}
}
}
steps: {
a
}
Expand Down Expand Up @@ -762,7 +762,7 @@ only-layers: {
X
Y
}
layers: {
Z
}
Expand All @@ -772,10 +772,10 @@ layers: {
Test super nested: {
base-layer
last-layer
layers: {
layer-board
layers: {
grand-child-layer: {
grand-child-board
Expand All @@ -788,7 +788,7 @@ layers: {
scenarios: {
scenario-1: {
non-step
steps: {
step-1: {
Test
Expand Down
41 changes: 41 additions & 0 deletions d2oracle/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,47 @@ func _set(g *d2graph.Graph, baseAST *d2ast.Map, key string, tag, value *string)
}
}

if mk.Key != nil && len(mk.Key.Path) == 2 {
boardType := mk.Key.Path[0].Unbox().ScalarString()
if boardType == "layers" || boardType == "scenarios" || boardType == "steps" {
// Force map structure
var containerMap *d2ast.Map
for _, n := range scope.Nodes {
if n.MapKey != nil && n.MapKey.Key != nil && len(n.MapKey.Key.Path) == 1 &&
n.MapKey.Key.Path[0].Unbox().ScalarString() == boardType {
containerMap = n.MapKey.Value.Map
break
}
}

if containerMap == nil {
containerMap = &d2ast.Map{
Range: d2ast.MakeRange(",1:0:0-1:0:0"),
}
containerMK := &d2ast.Key{
Key: &d2ast.KeyPath{
Path: []*d2ast.StringBox{
d2ast.MakeValueBox(d2ast.RawString(boardType, true)).StringBox(),
},
},
Value: d2ast.MakeValueBox(containerMap),
}
appendMapKey(scope, containerMK)
}

itemMK := &d2ast.Key{
Key: &d2ast.KeyPath{
Path: []*d2ast.StringBox{
d2ast.MakeValueBox(d2ast.RawString(mk.Key.Path[1].Unbox().ScalarString(), true)).StringBox(),
},
},
Value: mk.Value,
}
appendMapKey(containerMap, itemMK)
return nil
}
}

writeableLabelMK := true
var objK *d2ast.Key
if baseAST != g.AST || imported {
Expand Down
61 changes: 61 additions & 0 deletions d2oracle/edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,67 @@ layers: {
b
}
}
`,
},
{
name: "add_layer/1",
text: `b`,
key: `layers.c`,

expKey: `layers.c`,
exp: `b
layers: {
c
}
`,
},
{
name: "add_layer/2",
text: `b
layers: {
c: {
x
}
}`,
key: `layers.b`,

expKey: `layers.b`,
exp: `b
layers: {
c: {
x
}
b
}
`,
},
{
name: "add_layer/3",
text: `b
layers: {
c: {
d
}
}
`,
key: `layers.c`,

boardPath: []string{"c"},
expKey: `layers.c`,
exp: `b
layers: {
c: {
d
layers: {
c
}
}
}
`,
},
{
Expand Down
158 changes: 158 additions & 0 deletions testdata/d2oracle/TestCreate/add_layer/1.exp.json

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

Loading

0 comments on commit dcd7c15

Please sign in to comment.