forked from matthewmcneely/modusGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecord_example_test.go
More file actions
49 lines (39 loc) · 1.57 KB
/
Copy pathrecord_example_test.go
File metadata and controls
49 lines (39 loc) · 1.57 KB
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
40
41
42
43
44
45
46
47
48
49
/*
* SPDX-FileCopyrightText: © 2017-2026 Istari Digital, Inc.
* SPDX-License-Identifier: Apache-2.0
*/
package modusgraph_test
import (
"context"
"fmt"
mg "github.com/matthewmcneely/modusgraph"
)
// Actor is a schema-defining record. Implementing mg.Schema (a single
// SchemaTypeName method) marks it as a generated schema type; code generators
// such as modusgraph-gen emit this method.
type Actor struct {
UID string `json:"uid,omitempty"`
DType []string `json:"dgraph.type,omitempty"`
Name string `json:"name,omitempty" dgraph:"index=exact"`
}
func (a *Actor) SchemaTypeName() string { return "Actor" }
// ActorBuilder is a wrapper around Actor — the shape a generated fluent builder
// or domain wrapper takes. Exposing Unwrap lets the modusgraph client route the
// wrapper to its backing record, so the wrapper can be passed straight to
// Insert/Update/Get without the caller reaching for the inner value.
type ActorBuilder struct{ actor *Actor }
func (b *ActorBuilder) Unwrap() *Actor { return b.actor }
// ExampleSchema shows the wrapper pattern: the client unwraps an ActorBuilder
// to its Actor before persisting, so generated wrapper types work transparently
// while plain structs are unaffected.
func ExampleSchema() {
client, _ := mg.NewClient("dgraph://localhost:9080")
defer client.Close()
ctx := context.Background()
builder := &ActorBuilder{actor: &Actor{Name: "Sigourney Weaver"}}
// Insert the wrapper; the client unwraps it to the Actor record.
if err := client.Insert(ctx, builder); err != nil {
panic(err)
}
fmt.Println(builder.actor.Name)
}