Skip to content

Commit 6541383

Browse files
committed
rot13adl demo: example of creating data using synthetic view, then accessing substrate.
Fixed a symbol to be exported that's needed for this to be possible when outside the package. This still probably deserves an interface, too, though. Comments on that also updated, but we'll still leave that for future work (more examples of more ADLs wanted before we try to solidify on something there).
1 parent 8096285 commit 6541383

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

adl/rot13adl/example_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package rot13adl_test
22

33
import (
4+
"bytes"
45
"fmt"
56
"strings"
67

@@ -38,6 +39,39 @@ func ExampleUnmarshallingToADL() {
3839
// adl view value: "a cool string"
3940
}
4041

42+
func ExampleCreatingViaADL() {
43+
// Create a NodeBuilder for the ADL -- the high-level synthesized thing (not the substrate).
44+
nb := rot13adl.Prototype.Node.NewBuilder()
45+
46+
// Create a ADL node via its builder. This is just like creating any other node in IPLD.
47+
nb.AssignString("woohoo")
48+
n := nb.Build()
49+
50+
// We can inspect the synthetic ADL node like any other node!
51+
fmt.Printf("adl node kind: %v\n", n.ReprKind())
52+
fmt.Printf("adl view value: %q\n", must.String(n))
53+
54+
// We can get the substrate view and examine that as a node too.
55+
// (This requires a cast to see that we have an ADL, though. Not all IPLD nodes have a 'Substrate' property.)
56+
substrateNode := n.(rot13adl.R13String).Substrate()
57+
fmt.Printf("substrate node kind: %v\n", substrateNode.ReprKind())
58+
fmt.Printf("substrate value: %q\n", must.String(substrateNode))
59+
60+
// To marshal the ADL, just use marshal methods on its substrate as normal:
61+
var marshalBuffer bytes.Buffer
62+
err := dagjson.Marshal(substrateNode, json.NewEncoder(&marshalBuffer, json.EncodeOptions{}))
63+
fmt.Printf("marshalled: %v\n", marshalBuffer.String())
64+
fmt.Printf("marshal error: %v\n", err)
65+
66+
// Output:
67+
// adl node kind: string
68+
// adl view value: "woohoo"
69+
// substrate node kind: string
70+
// substrate value: "jbbubb"
71+
// marshalled: "jbbubb"
72+
// marshal error: <nil>
73+
}
74+
4175
// It's worth noting that the builders for an ADL substrate node still return the substrate.
4276
// (This is interesting in contrast to Schemas, where codegenerated representation-level builders
4377
// yield the type-level node values (and not the representation level node).)

adl/rot13adl/rot13node.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ import (
2828

2929
// -- Node -->
3030

31-
var _ ipld.Node = (*_R13String)(nil)
31+
var _ ipld.Node = (R13String)(nil)
32+
33+
type R13String = *_R13String
3234

3335
type _R13String struct {
3436
raw string // the raw content, before our ADL lens is applied to it.

adl/rot13adl/rot13node_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ func TestInspectingSubstrate(t *testing.T) {
6868
Require(t, err, ShouldEqual, nil)
6969
n := nb.Build()
7070
// Ask it about its substrate, and inspect that.
71-
sn := n.(*_R13String).Substrate()
72-
// TODO: the cast above isn't available outside this package: we should probably make an interface with `Substrate()` and make it available.
71+
sn := n.(R13String).Substrate()
72+
// TODO: It's unfortunate this is only available as a concrete type cast: we should probably make a standard feature detection interface with `Substrate()`.
7373
// Is it reasonable to make this part of a standard feature detection pattern,
7474
// and make that interface reside in the main IPLD package? Or in an `adl` package that contains such standard interfaces?
7575
ss, err := sn.AsString()

0 commit comments

Comments
 (0)