Skip to content

Commit

Permalink
Merge pull request #2261 from alixander/update-lib-examples
Browse files Browse the repository at this point in the history
docs: update lib examples with proper context logging
  • Loading branch information
alixander authored Dec 27, 2024
2 parents 1ea36d5 + aedc7f8 commit 21a9aec
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
8 changes: 5 additions & 3 deletions docs/examples/lib/1-d2lib/d2lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package main

import (
"context"
"io/ioutil"
"os"
"path/filepath"

"oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/d2layouts/d2dagrelayout"
"oss.terrastruct.com/d2/d2lib"
"oss.terrastruct.com/d2/d2renderers/d2svg"
"oss.terrastruct.com/d2/d2themes/d2themescatalog"
"oss.terrastruct.com/d2/lib/log"
"oss.terrastruct.com/d2/lib/textmeasure"
"oss.terrastruct.com/util-go/go2"
)
Expand All @@ -28,7 +29,8 @@ func main() {
LayoutResolver: layoutResolver,
Ruler: ruler,
}
diagram, _, _ := d2lib.Compile(context.Background(), "x -> y", compileOpts, renderOpts)
ctx := log.WithDefault(context.Background())
diagram, _, _ := d2lib.Compile(ctx, "x -> y", compileOpts, renderOpts)
out, _ := d2svg.Render(diagram, renderOpts)
_ = ioutil.WriteFile(filepath.Join("out.svg"), out, 0600)
_ = os.WriteFile(filepath.Join("out.svg"), out, 0600)
}
4 changes: 3 additions & 1 deletion docs/examples/lib/2-d2oracle/d2oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"oss.terrastruct.com/d2/d2layouts/d2dagrelayout"
"oss.terrastruct.com/d2/d2lib"
"oss.terrastruct.com/d2/d2oracle"
"oss.terrastruct.com/d2/lib/log"
"oss.terrastruct.com/d2/lib/textmeasure"
)

Expand All @@ -23,7 +24,8 @@ func main() {
LayoutResolver: layoutResolver,
Ruler: ruler,
}
_, graph, _ := d2lib.Compile(context.Background(), "x -> y", compileOpts, nil)
ctx := log.WithDefault(context.Background())
_, graph, _ := d2lib.Compile(ctx, "x -> y", compileOpts, nil)

// Create a shape with the ID, "meow"
graph, _, _ = d2oracle.Create(graph, nil, "meow")
Expand Down
10 changes: 6 additions & 4 deletions docs/examples/lib/3-lowlevel/lowlevel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"context"
"io/ioutil"
"os"
"path/filepath"
"strings"

Expand All @@ -11,6 +11,7 @@ import (
"oss.terrastruct.com/d2/d2layouts/d2dagrelayout"
"oss.terrastruct.com/d2/d2renderers/d2svg"
"oss.terrastruct.com/d2/d2themes/d2themescatalog"
"oss.terrastruct.com/d2/lib/log"
"oss.terrastruct.com/d2/lib/textmeasure"
)

Expand All @@ -20,11 +21,12 @@ func main() {
graph.ApplyTheme(d2themescatalog.NeutralDefault.ID)
ruler, _ := textmeasure.NewRuler()
_ = graph.SetDimensions(nil, ruler, nil)
_ = d2dagrelayout.Layout(context.Background(), graph, nil)
diagram, _ := d2exporter.Export(context.Background(), graph, nil)
ctx := log.WithDefault(context.Background())
_ = d2dagrelayout.Layout(ctx, graph, nil)
diagram, _ := d2exporter.Export(ctx, graph, nil)
diagram.Config = config
out, _ := d2svg.Render(diagram, &d2svg.RenderOpts{
ThemeID: &d2themescatalog.NeutralDefault.ID,
})
_ = ioutil.WriteFile(filepath.Join("out.svg"), out, 0600)
_ = os.WriteFile(filepath.Join("out.svg"), out, 0600)
}

0 comments on commit 21a9aec

Please sign in to comment.