Skip to content

Commit f2ad5e1

Browse files
PC-11300 Fix yamls parsing (#193)
1 parent 964d93b commit f2ad5e1

File tree

5 files changed

+69
-21
lines changed

5 files changed

+69
-21
lines changed

manifest/v1alpha/dataexport/data_export.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,17 @@ func (d *Spec) UnmarshalJSON(bytes []byte) error {
8282
d.ExportType = genericSpec.ExportType
8383
switch d.ExportType {
8484
case DataExportTypeS3, DataExportTypeSnowflake:
85-
d.Spec = &S3DataExportSpec{}
85+
var s3Spec S3DataExportSpec
86+
if err := json.Unmarshal(genericSpec.Spec, &s3Spec); err != nil {
87+
return err
88+
}
89+
d.Spec = s3Spec
8690
case DataExportTypeGCS:
87-
d.Spec = &GCSDataExportSpec{}
88-
}
89-
if genericSpec.Spec != nil {
90-
if err := json.Unmarshal(genericSpec.Spec, &d.Spec); err != nil {
91+
var gcsSpec GCSDataExportSpec
92+
if err := json.Unmarshal(genericSpec.Spec, &gcsSpec); err != nil {
9193
return err
9294
}
95+
d.Spec = gcsSpec
9396
}
9497
return nil
9598
}

manifest/v1alpha/parser/parser.go

+9-16
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,16 @@ func parseGenericObject(unmarshal unmarshalFunc) (manifest.Object, error) {
8787
}
8888

8989
func getUnmarshalFunc(data []byte, format manifest.ObjectFormat) (unmarshalFunc, error) {
90-
var unmarshal unmarshalFunc
90+
jsonUnmarshal := func(v interface{}) error {
91+
dec := json.NewDecoder(bytes.NewReader(data))
92+
if UseStrictDecodingMode {
93+
dec.DisallowUnknownFields()
94+
}
95+
return dec.Decode(v)
96+
}
9197
switch format {
9298
case manifest.ObjectFormatJSON:
93-
unmarshal = func(v interface{}) error {
94-
dec := json.NewDecoder(bytes.NewReader(data))
95-
if UseStrictDecodingMode {
96-
dec.DisallowUnknownFields()
97-
}
98-
return dec.Decode(v)
99-
}
99+
return jsonUnmarshal, nil
100100
case manifest.ObjectFormatYAML:
101101
// Workaround for https://github.com/goccy/go-yaml/issues/313.
102102
// If the library changes its interpretation of empty pointer fields,
@@ -106,17 +106,10 @@ func getUnmarshalFunc(data []byte, format manifest.ObjectFormat) (unmarshalFunc,
106106
if err != nil {
107107
return nil, errors.Wrap(err, "failed to convert YAML to JSON")
108108
}
109-
var opts []yaml.DecodeOption
110-
if UseStrictDecodingMode {
111-
opts = append(opts, yaml.Strict())
112-
}
113-
unmarshal = func(v interface{}) error {
114-
return yaml.UnmarshalWithOptions(data, v, opts...)
115-
}
109+
return jsonUnmarshal, nil
116110
default:
117111
return nil, errors.Errorf("unsupported format: %s", format)
118112
}
119-
return unmarshal, nil
120113
}
121114

122115
func genericParseObject[T manifest.Object](unmarshal unmarshalFunc) (T, error) {

sdk/reader_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/stretchr/testify/require"
1818

1919
"github.com/nobl9/nobl9-go/manifest"
20+
"github.com/nobl9/nobl9-go/manifest/v1alpha/dataexport"
2021
)
2122

2223
//go:embed test_data/reader
@@ -143,6 +144,27 @@ func TestReadDefinitions_FromReader(t *testing.T) {
143144
})
144145
}
145146

147+
func TestReadDefinitions_UsingCustomizedUnmarshal(t *testing.T) {
148+
t.Run("report an error when unexpected structure was returned", func(t *testing.T) {
149+
definitions, err := ReadObjectsFromSources(
150+
context.Background(),
151+
NewObjectSourceReader(readTestFile(t, "dataexport.yaml"), "stdin"))
152+
require.NoError(t, err)
153+
154+
definitionsMatchExpected(t, definitions, expectedMeta{Name: "dataexport", ManifestSrc: "stdin"})
155+
156+
require.IsType(t, dataexport.DataExport{}, definitions[0])
157+
assert.Equal(
158+
t,
159+
definitions[0].(dataexport.DataExport).Spec.Spec,
160+
dataexport.S3DataExportSpec{
161+
BucketName: "example-bucket",
162+
RoleARN: "arn:aws:iam::341861879477:role/n9-access",
163+
},
164+
)
165+
})
166+
}
167+
146168
func TestReadDefinitions_FromURL(t *testing.T) {
147169
t.Run("successful definitions GET for http scheme", func(t *testing.T) {
148170
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -279,6 +301,7 @@ func TestReadDefinitions_FromFS(t *testing.T) {
279301
{Name: "service_and_agent", ManifestSrc: workingDir("test_data/reader/inputs/service_and_agent.yaml")},
280302
{Name: "projects_and_direct", ManifestSrc: workingDir("test_data/reader/inputs/projects_and_direct.yml")},
281303
{Name: "annotations", ManifestSrc: workingDir("test_data/reader/inputs/annotations.yaml")},
304+
{Name: "dataexport", ManifestSrc: workingDir("test_data/reader/inputs/dataexport.yaml")},
282305
{Name: "project", ManifestSrc: workingDir("test_data/reader/inputs/project.json")},
283306
}
284307

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"apiVersion": "n9/v1alpha",
3+
"kind": "DataExport",
4+
"metadata": {
5+
"name": "s3-data-export",
6+
"displayName": "S3 data export",
7+
"project": "default"
8+
},
9+
"spec": {
10+
"exportType": "S3",
11+
"spec": {
12+
"bucketName": "example-bucket",
13+
"roleArn": "arn:aws:iam::341861879477:role/n9-access"
14+
}
15+
},
16+
"status": null,
17+
"manifestSrc": "{{ .ManifestSrc }}"
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: n9/v1alpha
2+
kind: DataExport
3+
metadata:
4+
name: s3-data-export
5+
displayName: S3 data export
6+
project: default
7+
spec:
8+
exportType: S3
9+
spec:
10+
bucketName: example-bucket
11+
roleArn: arn:aws:iam::341861879477:role/n9-access

0 commit comments

Comments
 (0)