Skip to content

Commit 89f7156

Browse files
authored
Merge pull request #283 from cnabio/retrieve-output-metadata
Fix unmarshal for claim results
2 parents 63af4ce + 71ac698 commit 89f7156

File tree

3 files changed

+40
-36
lines changed

3 files changed

+40
-36
lines changed

claim/result.go

+6-13
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,13 @@ func (r Results) Swap(i, j int) {
100100
// OutputMetadata is the output metadata from an operation.
101101
// Any metadata can be stored, however this provides methods
102102
// for safely querying and retrieving well-known metadata.
103-
type OutputMetadata map[string]interface{}
103+
type OutputMetadata map[string]map[string]string
104104

105105
// GetMetadata for the specified output and key.
106106
func (o OutputMetadata) GetMetadata(outputName string, metadataKey string) (string, bool) {
107107
if output, ok := o[outputName]; ok {
108-
if outputMetadata, ok := output.(map[string]string); ok {
109-
if value, ok := outputMetadata[metadataKey]; ok {
110-
return value, true
111-
}
108+
if value, ok := output[metadataKey]; ok {
109+
return value, true
112110
}
113111
}
114112

@@ -122,20 +120,15 @@ func (o *OutputMetadata) SetMetadata(outputName string, metadataKey string, valu
122120
}
123121
metadata := *o
124122

125-
output, ok := metadata[outputName]
123+
outputMetadata, ok := metadata[outputName]
126124
if !ok {
127-
output = map[string]string{
125+
outputMetadata = map[string]string{
128126
metadataKey: value,
129127
}
130-
metadata[outputName] = output
128+
metadata[outputName] = outputMetadata
131129
return nil
132130
}
133131

134-
outputMetadata, ok := output.(map[string]string)
135-
if !ok {
136-
return errors.Errorf("cannot set the claim result's OutputMetadata[%s][%s] because it is not type map[string]string but %T", outputName, metadataKey, output)
137-
}
138-
139132
outputMetadata[metadataKey] = value
140133
return nil
141134
}

claim/result_test.go

+18-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package claim
22

33
import (
4+
"encoding/json"
5+
"io/ioutil"
46
"sort"
57
"testing"
68

@@ -99,16 +101,6 @@ func TestResultOutputs_SetMetadata(t *testing.T) {
99101
require.NoError(t, err, "SetMetadata failed")
100102
assert.Equal(t, wantoutputs, outputs, "SetMetadata did not produce the expected structure")
101103
})
102-
103-
t.Run("existing invalid structure", func(t *testing.T) {
104-
outputs := OutputMetadata{
105-
outputName: map[string]interface{}{
106-
metadataKey: struct{}{},
107-
},
108-
}
109-
err := outputs.SetMetadata(outputName, metadataKey, metadataValue)
110-
require.EqualError(t, err, "cannot set the claim result's OutputMetadata[test1][so-meta] because it is not type map[string]string but map[string]interface {}")
111-
})
112104
}
113105

114106
func TestResultOutputs_GetMetadata(t *testing.T) {
@@ -144,19 +136,6 @@ func TestResultOutputs_GetMetadata(t *testing.T) {
144136
require.False(t, ok, "GetMetadata should report that it did not find the value")
145137
assert.Empty(t, gotValue, "GetMetadata should return an empty value when one isn't found")
146138
})
147-
148-
t.Run("output has different structure", func(t *testing.T) {
149-
outputs := OutputMetadata{
150-
outputName: map[string]interface{}{
151-
"other": struct{}{},
152-
},
153-
}
154-
155-
gotValue, ok := outputs.GetMetadata(outputName, metadataKey)
156-
require.False(t, ok, "GetMetadata should report that it did not find the value")
157-
assert.Empty(t, gotValue, "GetMetadata should return an empty value when one isn't found")
158-
})
159-
160139
}
161140

162141
func TestResultOutputs_SetContentDigest(t *testing.T) {
@@ -265,3 +244,19 @@ func TestResult_HasLogs(t *testing.T) {
265244
r.OutputMetadata.SetGeneratedByBundle(OutputInvocationImageLogs, true)
266245
assert.True(t, r.HasLogs(), "expected HasLogs to return true")
267246
}
247+
248+
// Verify that when we unmarshal a result, the output metadata can be read back
249+
func TestResult_UnmarshalOutputMetadata(t *testing.T) {
250+
data, err := ioutil.ReadFile("testdata/result.json")
251+
require.NoError(t, err, "error reading testdata result")
252+
253+
var result Result
254+
err = json.Unmarshal(data, &result)
255+
require.NoError(t, err, "error unmarshaling result")
256+
257+
contentDigest, _ := result.OutputMetadata.GetContentDigest(OutputInvocationImageLogs)
258+
assert.Equal(t, "sha256:28ccd0529aa1edefb0e771a28c31c0193f656718af985fed197235ba98fc5696", contentDigest, "the content digest output metadata could not be read")
259+
260+
generatedFlag, _ := result.OutputMetadata.GetGeneratedByBundle("output1")
261+
assert.True(t, generatedFlag, "the generatedByBundle output metadata could not be read")
262+
}

claim/testdata/result.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"id": "01G1VJH2HP97B5B0N5S37KYMVG",
3+
"claimId": "01G1VJGY43HT3KZN82DS6DDPWK",
4+
"created": "2022-04-29T16:09:47.190534-05:00",
5+
"status": "succeeded",
6+
"outputs": {
7+
"io.cnab.outputs.invocationImageLogs": {
8+
"contentDigest": "sha256:28ccd0529aa1edefb0e771a28c31c0193f656718af985fed197235ba98fc5696",
9+
"generatedByBundle": "false"
10+
},
11+
"output1": {
12+
"contentDigest": "sha256:abc123",
13+
"generatedByBundle": "true"
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)