-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
200 lines (170 loc) · 5.08 KB
/
example_test.go
File metadata and controls
200 lines (170 loc) · 5.08 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package loam_test
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"github.com/aretw0/loam"
"github.com/aretw0/loam/pkg/core"
)
// Example_basic demonstrates how to initialize a Vault, save a note, and read it back.
func Example_basic() {
// Create a temporary directory for the example
tmpDir, err := os.MkdirTemp("", "loam-example-*")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(tmpDir)
// Initialize the Loam service (Vault) targeting the temporary directory.
// WithAutoInit(true) ensures the underlying storage (git repo) is initialized.
vault, err := loam.New(context.Background(), tmpDir, loam.WithAutoInit(true))
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
// 1. Save a Document
err = vault.SaveDocument(ctx, "hello-world", "This is my first note in Loam.", core.Metadata{
"tags": []string{"example"},
"author": "Gopher",
})
if err != nil {
log.Fatal(err)
}
// 2. Read it back
doc, err := vault.GetDocument(ctx, "hello-world")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Found document: %s\n", doc.ID)
// Output:
// Found document: hello-world
}
// ExampleNewTypedRepository demonstrates how to use the Generic Typed Wrapper for type safety.
func ExampleNewTypedRepository() {
// Setup: Temporary repository
tmpDir, err := os.MkdirTemp("", "loam-typed-example-*")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(tmpDir)
// Use loam.Init to get the Repository directly
repo, err := loam.Init(context.Background(), filepath.Join(tmpDir, "vault"), loam.WithAutoInit(true))
if err != nil {
log.Fatal(err)
}
// Define your Domain Model
type User struct {
Name string `json:"name"`
Email string `json:"email"`
}
// Wrap the repository
userRepo := loam.NewTypedRepository[User](repo)
ctx := context.Background()
// Save a typed document
err = userRepo.Save(ctx, &loam.DocumentModel[User]{
ID: "users/alice",
Content: "Alice's Profile",
Data: User{
Name: "Alice",
Email: "alice@example.com",
},
})
if err != nil {
log.Fatal(err)
}
// Retrieve it back
doc, err := userRepo.Get(ctx, "users/alice")
if err != nil {
log.Fatal(err)
}
fmt.Printf("User Name: %s\n", doc.Data.Name)
// Output:
// User Name: Alice
}
// Example_csvNestedData demonstrates Loam's "Smart CSV" capability,
// which automatically handles nested structures (like maps or slices)
// by serializing them as JSON within the CSV column.
func Example_csvNestedData() {
// Setup: Temporary repository
tmpDir, err := os.MkdirTemp("", "loam-csv-example-*")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(tmpDir)
repo, err := loam.Init(context.Background(), filepath.Join(tmpDir, "vault"), loam.WithAutoInit(true))
if err != nil {
log.Fatal(err)
}
type Metrics struct {
Host string `json:"host"`
Tags map[string]string `json:"tags"` // Nested Map
Load []int `json:"load"` // Nested Slice
}
metricsRepo := loam.NewTypedRepository[Metrics](repo)
ctx := context.Background()
// 1. Save complex data to CSV
err = metricsRepo.Save(ctx, &loam.DocumentModel[Metrics]{
ID: "metrics/server-01.csv", // .csv extension triggers CSV adapter
Data: Metrics{
Host: "server-01",
Tags: map[string]string{"env": "prod", "region": "us-east"},
Load: []int{10, 20, 15},
},
})
if err != nil {
log.Fatal(err)
}
// 2. Read it back
// Loam automatically parses the JSON strings inside the CSV back into Maps and Slices.
doc, err := metricsRepo.Get(ctx, "metrics/server-01.csv")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Host: %s\n", doc.Data.Host)
fmt.Printf("Tag Region: %s\n", doc.Data.Tags["region"])
fmt.Printf("Load: %v\n", doc.Data.Load)
// Output:
// Host: server-01
// Tag Region: us-east
// Load: [10 20 15]
}
// Example_strictMode demonstrates how to enable global strict mode for type fidelity.
// This ensures that large integers (int64) are not lost as float64 during parsing
// across ALL supported formats (JSON, YAML, Markdown).
func Example_strictMode() {
// Setup
tmpDir, err := os.MkdirTemp("", "loam-strict-*")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(tmpDir)
// Initialize with Global Strict Mode
// This applies strict parsing (json.Number) to all serializers.
repo, err := loam.Init(context.Background(), filepath.Join(tmpDir, "vault"),
loam.WithAutoInit(true),
loam.WithStrict(true),
)
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
// 1. JSON Example (Large Int)
jsonContent := `{"big_id": 9223372036854775807, "type": "json"}`
_ = os.WriteFile(filepath.Join(tmpDir, "vault", "strict.json"), []byte(jsonContent), 0644)
// 2. YAML Example (Large Int)
yamlContent := "big_id: 9223372036854775807\ntype: yaml"
_ = os.WriteFile(filepath.Join(tmpDir, "vault", "strict.yaml"), []byte(yamlContent), 0644)
// Read back and verify types
for _, file := range []string{"strict.json", "strict.yaml"} {
doc, err := repo.Get(ctx, file)
if err != nil {
log.Fatal(err)
}
val := doc.Metadata["big_id"]
fmt.Printf("[%s] Type: %T\n", doc.Metadata["type"], val)
}
// Output:
// [json] Type: json.Number
// [yaml] Type: json.Number
}