-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_test.go
More file actions
81 lines (66 loc) · 1.68 KB
/
example_test.go
File metadata and controls
81 lines (66 loc) · 1.68 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
package bogo_test
import (
"fmt"
"log"
"github.com/bubunyo/bogo"
)
type User struct {
ID int64 `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Age int `json:"age"`
}
func Example() {
// Create a user
user := User{
ID: 12345,
Name: "John Doe",
Email: "john@example.com",
Age: 30,
}
// Marshal to Bogo binary format
data, err := bogo.Marshal(user)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Encoded %d bytes\n", len(data))
// Unmarshal back to struct
var decoded User
err = bogo.Unmarshal(data, &decoded)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Name: %s, Age: %d\n", decoded.Name, decoded.Age)
// Output: Encoded 68 bytes
// Name: John Doe, Age: 30
}
func ExampleEncoder() {
// Advanced configuration for high-performance scenarios
decoder := bogo.NewConfigurableDecoder(
bogo.WithSelectiveFields([]string{"id", "name"}), // Only decode these fields
bogo.WithDecoderMaxDepth(10), // Limit nesting
)
// Create some test data
testData := map[string]any{
"id": int64(123),
"name": "Alice",
"email": "alice@example.com", // This will be skipped
"profile": map[string]any{ // This will be skipped
"bio": "Long biography...",
"settings": map[string]any{"theme": "dark"},
},
}
// Encode normally
encoded, err := bogo.Marshal(testData)
if err != nil {
log.Fatal(err)
}
// Decode selectively (much faster for large objects)
result, err := decoder.Decode(encoded)
if err != nil {
log.Fatal(err)
}
resultMap := result.(map[string]any)
fmt.Printf("Selective decode - ID: %v, Name: %v\n", resultMap["id"], resultMap["name"])
// Output: Selective decode - ID: 123, Name: Alice
}