osbuild1/result: fix conversion of success field from v2
When a stage is successful in a manifest v2, the success field is omitted from the result. In other words, the default value of the success field is true which is against the default value of boolean in Go. This commit implements a workaround. Signed-off-by: Ondřej Budai <ondrej@budai.cz>
This commit is contained in:
parent
9ba6dcec4e
commit
ea8d080c8a
2 changed files with 53 additions and 0 deletions
|
|
@ -14,6 +14,32 @@ type StageResult struct {
|
|||
Error string `json:"string,omitempty"`
|
||||
}
|
||||
|
||||
// UnmarshalJSON decodes json-encoded StageResult.
|
||||
//
|
||||
// This method is here only as a workaround for the default value of the
|
||||
// success field, see the comment inside the method.
|
||||
func (sr *StageResult) UnmarshalJSON(data []byte) error {
|
||||
// Create a StageResult-like object with the Success value set to true
|
||||
// before json.Unmarshal is called. If the success field isn't in the
|
||||
// input JSON, the json decoder will not touch it and thus it will still
|
||||
// be true.
|
||||
//
|
||||
// The type alias is needed to prevent recursive calls of this method.
|
||||
type stageResultAlias StageResult
|
||||
stageResultDefault := stageResultAlias{
|
||||
Success: true,
|
||||
}
|
||||
|
||||
err := json.Unmarshal(data, &stageResultDefault)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*sr = StageResult(stageResultDefault)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type Result struct {
|
||||
Type string `json:"type"`
|
||||
Success bool `json:"success"`
|
||||
|
|
|
|||
|
|
@ -1 +1,28 @@
|
|||
package osbuild2
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestStageResult_UnmarshalJSON(t *testing.T) {
|
||||
cases := []struct {
|
||||
input string
|
||||
success bool
|
||||
}{
|
||||
{input: `{}`, success: true},
|
||||
{input: `{"success": true}`, success: true},
|
||||
{input: `{"success": false}`, success: false},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
t.Run(c.input, func(t *testing.T) {
|
||||
var result StageResult
|
||||
err := json.Unmarshal([]byte(c.input), &result)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, c.success, result.Success)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue