package river_test12import (3"fmt"4"os"56"github.com/grafana/agent/pkg/river"7)89func ExampleUnmarshal() {10// Character is our block type which holds an individual character from a11// book.12type Character struct {13// Name of the character. The name is decoded from the block label.14Name string `river:",label"`15// Age of the character. The age is a required attribute within the block,16// and must be set in the config.17Age int `river:"age,attr"`18// Location the character lives in. The location is an optional attribute19// within the block. Optional attributes do not have to bet set.20Location string `river:"location,attr,optional"`21}2223// Book is our overall type where we decode the overall River file into.24type Book struct {25// Title of the book (required attribute).26Title string `river:"title,attr"`27// List of characters. Each character is a labeled block. The optional tag28// means that it is valid not provide a character block. Decoding into a29// slice permits there to be multiple specified character blocks.30Characters []*Character `river:"character,block,optional"`31}3233// Create our book with two characters.34input := `35title = "Wheel of Time"3637character "Rand" {38age = 1939location = "Two Rivers"40}4142character "Perrin" {43age = 1944location = "Two Rivers"45}46`4748// Unmarshal the config into our Book type and print out the data.49var b Book50if err := river.Unmarshal([]byte(input), &b); err != nil {51panic(err)52}5354fmt.Printf("%s characters:\n", b.Title)5556for _, c := range b.Characters {57if c.Location != "" {58fmt.Printf("\t%s (age %d, location %s)\n", c.Name, c.Age, c.Location)59} else {60fmt.Printf("\t%s (age %d)\n", c.Name, c.Age)61}62}6364// Output:65// Wheel of Time characters:66// Rand (age 19, location Two Rivers)67// Perrin (age 19, location Two Rivers)68}6970// This example shows how functions may be called within user configurations.71// We focus on the `env` function from the standard library, which retrieves a72// value from an environment variable.73func ExampleUnmarshal_functions() {74// Set an environment variable to use in the test.75_ = os.Setenv("EXAMPLE", "Jane Doe")7677type Data struct {78String string `river:"string,attr"`79}8081input := `82string = env("EXAMPLE")83`8485var d Data86if err := river.Unmarshal([]byte(input), &d); err != nil {87panic(err)88}8990fmt.Println(d.String)91// Output: Jane Doe92}9394func ExampleUnmarshalValue() {95input := `3 + 5`9697var num int98if err := river.UnmarshalValue([]byte(input), &num); err != nil {99panic(err)100}101102fmt.Println(num)103// Output: 8104}105106func ExampleMarshal() {107type Person struct {108Name string `river:"name,attr"`109Age int `river:"age,attr"`110Location string `river:"location,attr,optional"`111}112113p := Person{114Name: "John Doe",115Age: 43,116}117118bb, err := river.Marshal(p)119if err != nil {120panic(err)121}122123fmt.Println(string(bb))124// Output:125// name = "John Doe"126// age = 43127}128129func ExampleMarshalValue() {130type Person struct {131Name string `river:"name,attr"`132Age int `river:"age,attr"`133}134135p := Person{136Name: "John Doe",137Age: 43,138}139140bb, err := river.MarshalValue(p)141if err != nil {142panic(err)143}144145fmt.Println(string(bb))146// Output:147// {148// name = "John Doe",149// age = 43,150// }151}152153154