Skip to content

Commit

Permalink
Merge pull request #173 from terrastruct/d2oracle-updates
Browse files Browse the repository at this point in the history
d2oracle updates
  • Loading branch information
alixander authored Jul 29, 2023
2 parents e95cb56 + 657413b commit 2caab19
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 12 deletions.
51 changes: 40 additions & 11 deletions docs/tour/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ All functions in `d2oracle` are pure: they do not mutate the original graph, the
Create a shape or connection.

```go
func Create(g *d2graph.Graph, key string) (newG *d2graph.Graph, newKey string, _ error)
func Create(g *d2graph.Graph, boardPath []string, key string) (newG *d2graph.Graph, newKey string, _ error)
```

**Parameters**:
- `g`: D2 graph
- `boardPath`: Path of the board to modify. Only applicable to multi-board diagrams;
otherwise, pass in `nil`.
- `key`: The ID of the shape or connection being created

**Return**:
Expand All @@ -34,7 +36,7 @@ for example, if you create a connection between 2 shapes that don't exist, they

```go
// This call creates 6 shapes and 1 connection
d2oracle.Create(g, "a.b.c -> x.y.z")
d2oracle.Create(g, nil, "a.b.c -> x.y.z")
```

If you call `Create` twice with the same shape ID, you will get an
Expand All @@ -45,30 +47,49 @@ error. If you call it twice with the same edge ID, you will create another edge.
For shapes, there may be an ID collision. `Create` appends a counter in this case.
```go
// newKey = "a"
g, newKey, _ = d2oracle.Create(g, "a")
g, newKey, _ = d2oracle.Create(g, nil, "a")
// newKey = "a 2"
_, newKey, _ = d2oracle.Create(g, "a")
_, newKey, _ = d2oracle.Create(g, nil, "a")
```


Connection IDs include the index.
```go
// newKey = "(a -> b[0])"
g, newKey, _ = d2oracle.Create(g, "a -> b")
g, newKey, _ = d2oracle.Create(g, nil, "a -> b")
// newKey = "(a -> b[1])"
_, newKey, _ = d2oracle.Create(g, "a -> b")
_, newKey, _ = d2oracle.Create(g, nil, "a -> b")
```

If you have a multi-board diagram like so:

```d2
x

layers: {
y: {}
}
```

```go
// This creates a shape "a" at the root
g, _ = d2oracle.Create(g, nil, "a")
// This creates a shape "a" at layer "y"
g, _ = d2oracle.Create(g, []string{"y"}, "a")
```

## Set

Set an attribute on a shape or connection.

```go
func Set(g *d2graph.Graph, key string, tag, value *string) (newG *d2graph.Graph, _ error)
func Set(g *d2graph.Graph, boardPath []string, key string, tag, value *string) (newG *d2graph.Graph, _ error)
```

**Parameters**:
- `g`: D2 graph
- `boardPath`: Path of the board to modify. Only applicable to multi-board diagrams;
otherwise, pass in `nil`.
- `key`: The identifier for the attribute
- `tag`: The language tag. This is only non-nil when text values are set that can be
different languages, e.g. a code snippet value.
Expand Down Expand Up @@ -117,16 +138,20 @@ that object's primary value (usually label).
g, _ = d2oracle.Set(g, "a", "md", "# I am A")
```



## Delete

Delete a shape or connection.

```go
func Delete(g *d2graph.Graph, key string) (newG *d2graph.Graph, _ error)
func Delete(g *d2graph.Graph, boardPath []string, key string) (newG *d2graph.Graph, _ error)
```

**Parameters**:
- `g`: D2 graph
- `boardPath`: Path of the board to modify. Only applicable to multi-board diagrams;
otherwise, pass in `nil`.
- `key`: The identifier for the shape or connection

**Return**:
Expand All @@ -149,11 +174,13 @@ Note that the ID != label. If you want to change the label, use `Set`.
:::

```go
func Rename(g *d2graph.Graph, key, newName string) (newG *d2graph.Graph, err error)
func Rename(g *d2graph.Graph, boardPath []string, key, newName string) (newG *d2graph.Graph, err error)
```

**Parameters**:
- `g`: D2 graph
- `boardPath`: Path of the board to modify. Only applicable to multi-board diagrams;
otherwise, pass in `nil`.
- `key`: The current identifier for the shape or connection
- `newName`: The new identifier for the shape or connection

Expand All @@ -173,7 +200,7 @@ g, _ = d2oracle.Rename(g, "a", "z)
Move a given shape or connection to a different container.

```go
func Move(g *d2graph.Graph, key, newKey string) (newG *d2graph.Graph, err error)
func Move(g *d2graph.Graph, boardPath []string, key, newKey string) (newG *d2graph.Graph, err error)
```

:::info
Expand All @@ -182,6 +209,8 @@ If you give two keys of the same scope (e.g. "a" to "b"), it's the same as `Rena

**Parameters**:
- `g`: D2 graph
- `boardPath`: Path of the board to modify. Only applicable to multi-board diagrams;
otherwise, pass in `nil`.
- `key`: The current identifier for the shape or connection
- `newKey`: The new identifier for the shape or connection

Expand Down Expand Up @@ -226,7 +255,7 @@ y
x -> z
```

`deltas, err := MoveIDDeltas(g, "x", "y.x")`
`deltas, err := MoveIDDeltas(g, nil, "x", "y.x")`

`deltas`:
```json
Expand Down
1 change: 1 addition & 0 deletions static/bespoke-d2/chicken.d2
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Chicken's plan: {
near: top-center
shape: text
}

steps: {
1: {
Approach road
Expand Down
1 change: 1 addition & 0 deletions static/bespoke-d2/imports-nested.d2
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
serviceA -> serviceB
serviceB.link: layers.serviceB

layers: {
serviceB: @../d2/imports-nested-serviceB
}
1 change: 1 addition & 0 deletions static/bespoke-d2/lotr.d2
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ layers: {
go deeper: {
link: layers.moria
}

layers: {
moria: {
dwarves
Expand Down
1 change: 1 addition & 0 deletions static/bespoke-d2/tiktok.d2
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ layers: {
Virginia data center <-> Hong Kong data center
Virginia data center.link: layers.virginia
Hong Kong data center.link: layers.hongkong

layers: {
virginia: {
direction: right
Expand Down
3 changes: 2 additions & 1 deletion static/bespoke-d2/wcc.d2
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ layers: {
link: steps.1
style.font-size: 24
}

steps: {
1: {
titled: Earn pre-requisite titles (IM)
Expand Down Expand Up @@ -149,7 +150,7 @@ layers: {

best of 14 games -> tiebreaks: if needed
tiebreaks.link: layers.tiebreaks

layers: {
tiebreaks: {
description: |md
Expand Down
1 change: 1 addition & 0 deletions static/d2/imports-nested-serviceB.d2
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ aws vault.key -> data
aws vault.token -> data
stripe.customer id -> data
data.link: layers.data

layers: {
data: @imports-nested-data
}

0 comments on commit 2caab19

Please sign in to comment.