Skip to content

Commit 7e3e4ad

Browse files
committed
simplify snapshot data parsing logic
* and fix some formatting in unit test resolves: EC-1534
1 parent 3df1a24 commit 7e3e4ad

File tree

2 files changed

+20
-31
lines changed

2 files changed

+20
-31
lines changed

internal/applicationsnapshot/input.go

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -183,37 +183,27 @@ func DetermineInputSpec(ctx context.Context, input Input) (*app.SnapshotSpec, *E
183183
}
184184

185185
func readSnapshotSource(input []byte) (app.SnapshotSpec, error) {
186-
var v map[string]interface{}
187-
188-
// Since JSON is a subset of YAML, yaml.Unmarshal can be used directly.
189-
if err := yaml.Unmarshal(input, &v); err != nil {
190-
log.Debugf("Problem parsing application snapshot from input: %v", err)
191-
return app.SnapshotSpec{}, fmt.Errorf("unable to parse Snapshot specification from input: %w", err)
186+
// Define a temporary struct to capture the wrapped spec
187+
var wrapper struct {
188+
Spec *app.SnapshotSpec `yaml:"spec"`
192189
}
193190

194-
// Extract the "spec" key from YAML, if present, to use as the snapshot.
195-
if spec, ok := v["spec"]; ok {
196-
v, ok = spec.(map[string]interface{})
197-
if !ok {
198-
return app.SnapshotSpec{}, fmt.Errorf("spec is not a valid map structure")
199-
}
200-
// Marshal the spec back to bytes for unmarshaling into SnapshotSpec
201-
specBytes, err := yaml.Marshal(v)
202-
if err != nil {
203-
return app.SnapshotSpec{}, fmt.Errorf("unable to marshal spec: %w", err)
204-
}
205-
input = specBytes
191+
// Attempt to unmarshal into the wrapper to check for cluster record format
192+
if err := yaml.Unmarshal(input, &wrapper); err == nil && wrapper.Spec != nil {
193+
// If successful and spec exists, return it directly
194+
log.Debugf("Read application snapshot from cluster record format")
195+
return *wrapper.Spec, nil
206196
}
207197

208-
var file app.SnapshotSpec
209-
err := yaml.Unmarshal(input, &file)
210-
if err != nil {
198+
// Fallback: unmarshal directly into SnapshotSpec for backward compatibility
199+
var spec app.SnapshotSpec
200+
if err := yaml.Unmarshal(input, &spec); err != nil {
211201
log.Debugf("Problem parsing application snapshot from file %s", input)
212202
return app.SnapshotSpec{}, fmt.Errorf("unable to parse Snapshot specification from %s: %w", input, err)
213203
}
214204

215205
log.Debugf("Read application snapshot from file %s", input)
216-
return file, nil
206+
return spec, nil
217207
}
218208

219209
// For an image index, remove the original component and replace it with an expanded component with all its image manifests

internal/applicationsnapshot/input_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -285,15 +285,14 @@ func TestReadSnapshotFile(t *testing.T) {
285285
}
286286
// Simulate a cluster record format with .spec wrapper in YAML
287287
clusterRecord := `apiVersion: appstudio.redhat.com/v1alpha1
288-
kind: Snapshot
289-
metadata:
290-
name: vsa-demo-app-x9xln
291-
namespace: user-ns2
292-
spec:
293-
components:
294-
- name: Named
295-
containerImage: registry.io/repository/image:tag
296-
`
288+
kind: Snapshot
289+
metadata:
290+
name: vsa-demo-app-x9xln
291+
namespace: user-ns2
292+
spec:
293+
components:
294+
- name: Named
295+
containerImage: registry.io/repository/image:tag`
297296

298297
content := []byte(clusterRecord)
299298
got, err := readSnapshotSource(content)

0 commit comments

Comments
 (0)