Debian Codegraph @

confmap/doc_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package confmap_test
5
6 import (
7 "fmt"
8 "slices"
9 "time"
10
11 "go.opentelemetry.io/collector/confmap"
12 )
13
14 type DiskScrape struct {
15 Disk string `mapstructure:"disk"`
16 Scrape time.Duration `mapstructure:"scrape"`
17 }
18
19 // We can annotate a struct with mapstructure field annotations.
20 func Example_simpleUnmarshaling() {
21 conf := confmap.NewFromStringMap(map[string]any{
22 "disk": "c",
23 "scrape": "5s",
24 })
25 scrapeInfo := &DiskScrape{}
26 if err := conf.Unmarshal(scrapeInfo); err != nil {
27 panic(err)
28 }
29 fmt.Printf("Configuration contains the following:\nDisk: %q\nScrape: %s\n", scrapeInfo.Disk, scrapeInfo.Scrape)
30 // Output: Configuration contains the following:
31 // Disk: "c"
32 // Scrape: 5s
33 }
34
35 type CPUScrape struct {
36 Enabled bool `mapstructure:"enabled"`
37 }
38
39 type ComputerScrape struct {
40 DiskScrape `mapstructure:",squash"`
41 CPUScrape `mapstructure:",squash"`
42 }
43
44 // We can unmarshal embedded structs with mapstructure field annotations.
45 func Example_embeddedUnmarshaling() {
46 conf := confmap.NewFromStringMap(map[string]any{
47 "disk": "c",
48 "scrape": "5s",
49 "enabled": true,
50 })
51 scrapeInfo := &ComputerScrape{}
52 if err := conf.Unmarshal(scrapeInfo); err != nil {
53 panic(err)
54 }
55 fmt.Printf("Configuration contains the following:\nDisk: %q\nScrape: %s\nEnabled: %v\n", scrapeInfo.Disk, scrapeInfo.Scrape, scrapeInfo.Enabled)
56 // Output: Configuration contains the following:
57 // Disk: "c"
58 // Scrape: 5s
59 // Enabled: true
60 }
61
62 type NetworkScrape struct {
63 Enabled bool `mapstructure:"enabled"`
64 Networks []string `mapstructure:"networks"`
65 Wifi bool `mapstructure:"wifi"`
66 }
67
68 func (n *NetworkScrape) Unmarshal(c *confmap.Conf) error {
69 if err := c.Unmarshal(n, confmap.WithIgnoreUnused()); err != nil {
70 return err
71 }
72 if slices.Contains(n.Networks, "wlan0") {
73 n.Wifi = true
74 }
75 return nil
76 }
77
78 type ManualScrapeInfo struct {
79 Disk string
80 Scrape time.Duration
81 }
82
83 func (m *ManualScrapeInfo) Unmarshal(c *confmap.Conf) error {
84 m.Disk = c.Get("disk").(string)
85 if c.Get("vinyl") == "33" {
86 m.Scrape = 10 * time.Second
87 } else {
88 m.Scrape = 2 * time.Second
89 }
90 return nil
91 }
92
93 type RouterScrape struct {
94 NetworkScrape `mapstructure:",squash"`
95 }
96
97 // We can unmarshal an embedded struct with a custom `Unmarshal` method.
98 func Example_embeddedManualUnmarshaling() {
99 conf := confmap.NewFromStringMap(map[string]any{
100 "networks": []string{"eth0", "eth1", "wlan0"},
101 "enabled": true,
102 })
103 scrapeInfo := &RouterScrape{}
104 if err := conf.Unmarshal(scrapeInfo); err != nil {
105 panic(err)
106 }
107 fmt.Printf("Configuration contains the following:\nNetworks: %q\nWifi: %v\nEnabled: %v\n", scrapeInfo.Networks, scrapeInfo.Wifi, scrapeInfo.Enabled)
108 // Output: Configuration contains the following:
109 // Networks: ["eth0" "eth1" "wlan0"]
110 // Wifi: true
111 // Enabled: true
112 }
113
114 func Example_manualUnmarshaling() {
115 conf := confmap.NewFromStringMap(map[string]any{
116 "disk": "Beatles",
117 "vinyl": "33",
118 })
119 scrapeInfo := &ManualScrapeInfo{}
120 if err := conf.Unmarshal(scrapeInfo, confmap.WithIgnoreUnused()); err != nil {
121 panic(err)
122 }
123 fmt.Printf("Configuration contains the following:\nDisk: %q\nScrape: %s\n", scrapeInfo.Disk, scrapeInfo.Scrape)
124 // Output: Configuration contains the following:
125 // Disk: "Beatles"
126 // Scrape: 10s
127 }