-
Notifications
You must be signed in to change notification settings - Fork 422
/
d2oracle.go
39 lines (35 loc) · 1.14 KB
/
d2oracle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import (
"context"
"fmt"
"oss.terrastruct.com/d2/d2format"
"oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/d2layouts/d2dagrelayout"
"oss.terrastruct.com/d2/d2lib"
"oss.terrastruct.com/d2/d2oracle"
"oss.terrastruct.com/d2/lib/textmeasure"
)
// Remember to add if err != nil checks in production.
func main() {
// From one.go
ruler, _ := textmeasure.NewRuler()
layoutResolver := func(engine string) (d2graph.LayoutGraph, error) {
return d2dagrelayout.DefaultLayout, nil
}
compileOpts := &d2lib.CompileOptions{
LayoutResolver: layoutResolver,
Ruler: ruler,
}
_, graph, _ := d2lib.Compile(context.Background(), "x -> y", compileOpts, nil)
// Create a shape with the ID, "meow"
graph, _, _ = d2oracle.Create(graph, nil, "meow")
// Style the shape green
color := "green"
graph, _ = d2oracle.Set(graph, nil, "meow.style.fill", nil, &color)
// Create a shape with the ID, "cat"
graph, _, _ = d2oracle.Create(graph, nil, "cat")
// Move the shape "meow" inside the container "cat"
graph, _ = d2oracle.Move(graph, nil, "meow", "cat.meow", false)
// Prints formatted D2 script
fmt.Print(d2format.Format(graph.AST))
}