Debian Codegraph @

config/configopaque/doc_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package configopaque_test
5
6 import (
7 "encoding/json"
8 "fmt"
9
10 yaml "go.yaml.in/yaml/v3"
11
12 "go.opentelemetry.io/collector/config/configopaque"
13 )
14
15 func Example_opaqueString() {
16 rawBytes := []byte(`{
17 "Censored": "sensitive",
18 "Uncensored": "not sensitive"
19 }`)
20
21 // JSON unmarshaling
22 var cfg ExampleConfigString
23 err := json.Unmarshal(rawBytes, &cfg)
24 if err != nil {
25 panic(err)
26 }
27
28 // YAML marshaling
29 bytes, err := yaml.Marshal(cfg)
30 if err != nil {
31 panic(err)
32 }
33 fmt.Printf("encoded cfg (YAML) is:\n%s\n\n", string(bytes))
34 // Output: encoded cfg (YAML) is:
35 // censored: '[REDACTED]'
36 // uncensored: not sensitive
37 }
38
39 type ExampleConfigString struct {
40 Censored configopaque.String
41 Uncensored string
42 }
43
44 func Example_opaqueSlice() {
45 cfg := &ExampleConfigSlice{
46 Censored: []configopaque.String{"data", "is", "sensitive"},
47 Uncensored: []string{"data", "is", "not", "sensitive"},
48 }
49
50 // JSON marshaling
51 bytes, err := json.MarshalIndent(cfg, "", " ")
52 if err != nil {
53 panic(err)
54 }
55 fmt.Printf("encoded cfg (JSON) is\n%s\n\n", string(bytes))
56 // Output: encoded cfg (JSON) is
57 // {
58 // "Censored": [
59 // "[REDACTED]",
60 // "[REDACTED]",
61 // "[REDACTED]"
62 // ],
63 // "Uncensored": [
64 // "data",
65 // "is",
66 // "not",
67 // "sensitive"
68 // ]
69 // }
70 }
71
72 type ExampleConfigSlice struct {
73 Censored []configopaque.String
74 Uncensored []string
75 }
76
77 func Example_opaqueMap() {
78 cfg := &ExampleConfigMap{
79 Censored: map[string]configopaque.String{
80 "token": "sensitivetoken",
81 },
82 Uncensored: map[string]string{
83 "key": "cloud.zone",
84 "value": "zone-1",
85 },
86 }
87
88 // yaml marshaling
89 bytes, err := yaml.Marshal(cfg)
90 if err != nil {
91 panic(err)
92 }
93 fmt.Printf("encoded cfg (YAML) is:\n%s\n\n", string(bytes))
94 // Output: encoded cfg (YAML) is:
95 // censored:
96 // token: '[REDACTED]'
97 // uncensored:
98 // key: cloud.zone
99 // value: zone-1
100 }
101
102 type ExampleConfigMap struct {
103 Censored map[string]configopaque.String
104 Uncensored map[string]string
105 }