diff --git a/claim/result.go b/claim/result.go index ef8fce07..6e44a7ca 100644 --- a/claim/result.go +++ b/claim/result.go @@ -100,15 +100,13 @@ func (r Results) Swap(i, j int) { // OutputMetadata is the output metadata from an operation. // Any metadata can be stored, however this provides methods // for safely querying and retrieving well-known metadata. -type OutputMetadata map[string]interface{} +type OutputMetadata map[string]map[string]string // GetMetadata for the specified output and key. func (o OutputMetadata) GetMetadata(outputName string, metadataKey string) (string, bool) { if output, ok := o[outputName]; ok { - if outputMetadata, ok := output.(map[string]string); ok { - if value, ok := outputMetadata[metadataKey]; ok { - return value, true - } + if value, ok := output[metadataKey]; ok { + return value, true } } @@ -122,20 +120,15 @@ func (o *OutputMetadata) SetMetadata(outputName string, metadataKey string, valu } metadata := *o - output, ok := metadata[outputName] + outputMetadata, ok := metadata[outputName] if !ok { - output = map[string]string{ + outputMetadata = map[string]string{ metadataKey: value, } - metadata[outputName] = output + metadata[outputName] = outputMetadata return nil } - outputMetadata, ok := output.(map[string]string) - if !ok { - 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) - } - outputMetadata[metadataKey] = value return nil } diff --git a/claim/result_test.go b/claim/result_test.go index 79f3e2a1..4b47679b 100644 --- a/claim/result_test.go +++ b/claim/result_test.go @@ -1,6 +1,8 @@ package claim import ( + "encoding/json" + "io/ioutil" "sort" "testing" @@ -99,16 +101,6 @@ func TestResultOutputs_SetMetadata(t *testing.T) { require.NoError(t, err, "SetMetadata failed") assert.Equal(t, wantoutputs, outputs, "SetMetadata did not produce the expected structure") }) - - t.Run("existing invalid structure", func(t *testing.T) { - outputs := OutputMetadata{ - outputName: map[string]interface{}{ - metadataKey: struct{}{}, - }, - } - err := outputs.SetMetadata(outputName, metadataKey, metadataValue) - 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 {}") - }) } func TestResultOutputs_GetMetadata(t *testing.T) { @@ -144,19 +136,6 @@ func TestResultOutputs_GetMetadata(t *testing.T) { require.False(t, ok, "GetMetadata should report that it did not find the value") assert.Empty(t, gotValue, "GetMetadata should return an empty value when one isn't found") }) - - t.Run("output has different structure", func(t *testing.T) { - outputs := OutputMetadata{ - outputName: map[string]interface{}{ - "other": struct{}{}, - }, - } - - gotValue, ok := outputs.GetMetadata(outputName, metadataKey) - require.False(t, ok, "GetMetadata should report that it did not find the value") - assert.Empty(t, gotValue, "GetMetadata should return an empty value when one isn't found") - }) - } func TestResultOutputs_SetContentDigest(t *testing.T) { @@ -265,3 +244,19 @@ func TestResult_HasLogs(t *testing.T) { r.OutputMetadata.SetGeneratedByBundle(OutputInvocationImageLogs, true) assert.True(t, r.HasLogs(), "expected HasLogs to return true") } + +// Verify that when we unmarshal a result, the output metadata can be read back +func TestResult_UnmarshalOutputMetadata(t *testing.T) { + data, err := ioutil.ReadFile("testdata/result.json") + require.NoError(t, err, "error reading testdata result") + + var result Result + err = json.Unmarshal(data, &result) + require.NoError(t, err, "error unmarshaling result") + + contentDigest, _ := result.OutputMetadata.GetContentDigest(OutputInvocationImageLogs) + assert.Equal(t, "sha256:28ccd0529aa1edefb0e771a28c31c0193f656718af985fed197235ba98fc5696", contentDigest, "the content digest output metadata could not be read") + + generatedFlag, _ := result.OutputMetadata.GetGeneratedByBundle("output1") + assert.True(t, generatedFlag, "the generatedByBundle output metadata could not be read") +} diff --git a/claim/testdata/result.json b/claim/testdata/result.json new file mode 100644 index 00000000..59fa2fb2 --- /dev/null +++ b/claim/testdata/result.json @@ -0,0 +1,16 @@ +{ + "id": "01G1VJH2HP97B5B0N5S37KYMVG", + "claimId": "01G1VJGY43HT3KZN82DS6DDPWK", + "created": "2022-04-29T16:09:47.190534-05:00", + "status": "succeeded", + "outputs": { + "io.cnab.outputs.invocationImageLogs": { + "contentDigest": "sha256:28ccd0529aa1edefb0e771a28c31c0193f656718af985fed197235ba98fc5696", + "generatedByBundle": "false" + }, + "output1": { + "contentDigest": "sha256:abc123", + "generatedByBundle": "true" + } + } +}