UnmarshalInto parses input as YAML (or JSON) the same way as the Kubernetes API Server writing into output. It calls t.Fatal when something fails.
( t testing.TB, output Destination, input Data, )
| 17 | // UnmarshalInto parses input as YAML (or JSON) the same way as the Kubernetes |
| 18 | // API Server writing into output. It calls t.Fatal when something fails. |
| 19 | func UnmarshalInto[Data ~string | ~[]byte, Destination *T, T any]( |
| 20 | t testing.TB, output Destination, input Data, |
| 21 | ) { |
| 22 | t.Helper() |
| 23 | |
| 24 | // The REST API uses serializers: |
| 25 | // |
| 26 | // https://pkg.go.dev/k8s.io/apimachinery/pkg/runtime/serializer/json |
| 27 | // https://pkg.go.dev/k8s.io/apimachinery/pkg/runtime/serializer/yaml |
| 28 | // |
| 29 | // The util package follows similar paths (strict, preserve ints, etc.) |
| 30 | // |
| 31 | // https://pkg.go.dev/k8s.io/apimachinery/pkg/util/json |
| 32 | // https://pkg.go.dev/k8s.io/apimachinery/pkg/util/yaml |
| 33 | |
| 34 | data, err := yaml.YAMLToJSONStrict([]byte(input)) |
| 35 | assert.NilError(t, err) |
| 36 | |
| 37 | strict, err := json.UnmarshalStrict(data, output) |
| 38 | assert.NilError(t, err) |
| 39 | assert.NilError(t, errors.Join(strict...)) |
| 40 | } |
| 41 | |
| 42 | // UnmarshalIntoField parses input as YAML (or JSON) the same way as the Kubernetes API Server. |
| 43 | // The result goes into a (nested) field of output. It calls t.Fatal when something fails. |