Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/util/yaml_test.go
4093 views
1
package util
2
3
import (
4
"testing"
5
6
"github.com/stretchr/testify/require"
7
)
8
9
// TestUnmarshalYAMLMerged_CustomUnmarshal checks to see that
10
// UnmarshalYAMLMerged works with merging types that have custom unmarshal
11
// methods which do extra checks after calling unmarshal.
12
func TestUnmarshalYAMLMerged_CustomUnmarshal(t *testing.T) {
13
in := `
14
fieldA: foo
15
fieldB: bar
16
`
17
18
var (
19
val1 typeOne
20
val2 typeTwo
21
)
22
23
err := UnmarshalYAMLMerged([]byte(in), &val1, &val2)
24
require.NoError(t, err)
25
26
require.Equal(t, "foo", val1.FieldA)
27
require.Equal(t, "bar", val2.FieldB)
28
require.True(t, val2.Unmarshaled)
29
}
30
31
type typeOne struct {
32
FieldA string `yaml:"fieldA"`
33
}
34
35
type typeTwo struct {
36
FieldB string `yaml:"fieldB"`
37
Unmarshaled bool `yaml:"-"`
38
}
39
40
func (t *typeTwo) UnmarshalYAML(unmarshal func(interface{}) error) error {
41
type rawType typeTwo
42
if err := unmarshal((*rawType)(t)); err != nil {
43
return err
44
}
45
t.Unmarshaled = true
46
return nil
47
}
48
49