Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/river/river_test.go
4094 views
1
package river_test
2
3
import (
4
"fmt"
5
"os"
6
7
"github.com/grafana/agent/pkg/river"
8
)
9
10
func ExampleUnmarshal() {
11
// Character is our block type which holds an individual character from a
12
// book.
13
type Character struct {
14
// Name of the character. The name is decoded from the block label.
15
Name string `river:",label"`
16
// Age of the character. The age is a required attribute within the block,
17
// and must be set in the config.
18
Age int `river:"age,attr"`
19
// Location the character lives in. The location is an optional attribute
20
// within the block. Optional attributes do not have to bet set.
21
Location string `river:"location,attr,optional"`
22
}
23
24
// Book is our overall type where we decode the overall River file into.
25
type Book struct {
26
// Title of the book (required attribute).
27
Title string `river:"title,attr"`
28
// List of characters. Each character is a labeled block. The optional tag
29
// means that it is valid not provide a character block. Decoding into a
30
// slice permits there to be multiple specified character blocks.
31
Characters []*Character `river:"character,block,optional"`
32
}
33
34
// Create our book with two characters.
35
input := `
36
title = "Wheel of Time"
37
38
character "Rand" {
39
age = 19
40
location = "Two Rivers"
41
}
42
43
character "Perrin" {
44
age = 19
45
location = "Two Rivers"
46
}
47
`
48
49
// Unmarshal the config into our Book type and print out the data.
50
var b Book
51
if err := river.Unmarshal([]byte(input), &b); err != nil {
52
panic(err)
53
}
54
55
fmt.Printf("%s characters:\n", b.Title)
56
57
for _, c := range b.Characters {
58
if c.Location != "" {
59
fmt.Printf("\t%s (age %d, location %s)\n", c.Name, c.Age, c.Location)
60
} else {
61
fmt.Printf("\t%s (age %d)\n", c.Name, c.Age)
62
}
63
}
64
65
// Output:
66
// Wheel of Time characters:
67
// Rand (age 19, location Two Rivers)
68
// Perrin (age 19, location Two Rivers)
69
}
70
71
// This example shows how functions may be called within user configurations.
72
// We focus on the `env` function from the standard library, which retrieves a
73
// value from an environment variable.
74
func ExampleUnmarshal_functions() {
75
// Set an environment variable to use in the test.
76
_ = os.Setenv("EXAMPLE", "Jane Doe")
77
78
type Data struct {
79
String string `river:"string,attr"`
80
}
81
82
input := `
83
string = env("EXAMPLE")
84
`
85
86
var d Data
87
if err := river.Unmarshal([]byte(input), &d); err != nil {
88
panic(err)
89
}
90
91
fmt.Println(d.String)
92
// Output: Jane Doe
93
}
94
95
func ExampleUnmarshalValue() {
96
input := `3 + 5`
97
98
var num int
99
if err := river.UnmarshalValue([]byte(input), &num); err != nil {
100
panic(err)
101
}
102
103
fmt.Println(num)
104
// Output: 8
105
}
106
107
func ExampleMarshal() {
108
type Person struct {
109
Name string `river:"name,attr"`
110
Age int `river:"age,attr"`
111
Location string `river:"location,attr,optional"`
112
}
113
114
p := Person{
115
Name: "John Doe",
116
Age: 43,
117
}
118
119
bb, err := river.Marshal(p)
120
if err != nil {
121
panic(err)
122
}
123
124
fmt.Println(string(bb))
125
// Output:
126
// name = "John Doe"
127
// age = 43
128
}
129
130
func ExampleMarshalValue() {
131
type Person struct {
132
Name string `river:"name,attr"`
133
Age int `river:"age,attr"`
134
}
135
136
p := Person{
137
Name: "John Doe",
138
Age: 43,
139
}
140
141
bb, err := river.MarshalValue(p)
142
if err != nil {
143
panic(err)
144
}
145
146
fmt.Println(string(bb))
147
// Output:
148
// {
149
// name = "John Doe",
150
// age = 43,
151
// }
152
}
153
154