common: refactor build states into a separate file

These states will be used for tracking the image builds and compose
states in the rest of our codebase. There should be no change in the
behavior. It is a 1 to 1 replacement with the only difference of using
type alias instead of plain string.
This commit is contained in:
Martin Sehnoutka 2020-02-10 09:26:28 +01:00 committed by Ondřej Budai
parent 9c91b12076
commit 62d186cd1b
2 changed files with 138 additions and 0 deletions

79
internal/common/states.go Normal file
View file

@ -0,0 +1,79 @@
package common
import (
"encoding/json"
)
func getStateMapping() []string {
return []string{"WAITING", "RUNNING", "FINISHED", "FAILED"}
}
type ImageBuildState int
const (
IBWaiting ImageBuildState = iota
IBRunning
IBFinished
IBFailed
)
// ToString converts ImageBuildState into a human readable string
func (ibs ImageBuildState) ToString() string {
return getStateMapping()[int(ibs)]
}
func unmarshalStateHelper(data []byte, mapping []string) (int, error) {
var stringInput string
err := json.Unmarshal(data, &stringInput)
if err != nil {
return 0, err
}
for n, str := range getStateMapping() {
if str == stringInput {
return n, nil
}
}
return 0, &CustomJsonConversionError{"invalid image build status:" + stringInput}
}
// UnmarshalJSON converts a JSON string into an ImageBuildState
func (ibs *ImageBuildState) UnmarshalJSON(data []byte) error {
val, err := unmarshalStateHelper(data, getStateMapping())
if err != nil {
return err
}
*ibs = ImageBuildState(val)
return nil
}
func (ibs ImageBuildState) MarshalJSON() ([]byte, error) {
return json.Marshal(getStateMapping()[ibs])
}
type ComposeState int
const (
CWaiting ComposeState = iota
CRunning
CFinished
CFailed
)
// ToString converts ImageBuildState into a human readable string
func (cs ComposeState) ToString() string {
return getStateMapping()[int(cs)]
}
// UnmarshalJSON converts a JSON string into an ImageBuildState
func (ibs *ComposeState) UnmarshalJSON(data []byte) error {
val, err := unmarshalStateHelper(data, getStateMapping())
if err != nil {
return err
}
*ibs = ComposeState(val)
return nil
}
func (ibs ComposeState) MarshalJSON() ([]byte, error) {
return json.Marshal(getStateMapping()[ibs])
}

View file

@ -0,0 +1,59 @@
package common
import (
"encoding/json"
"reflect"
"testing"
)
func TestJSONConversions(t *testing.T) {
type TestJson struct {
Ibs ImageBuildState `json:"ibs"`
Cs ComposeState `json:"cs"`
}
typedCases := []TestJson{
{
Ibs: IBWaiting,
Cs: CWaiting,
},
{
Ibs: IBRunning,
Cs: CFailed,
},
}
strCases := []string{
`{"ibs": "WAITING", "cs": "WAITING"}`,
`{"ibs": "RUNNING", "cs": "FAILED"}`,
}
for n, c := range strCases {
var inputStringAsStruct *TestJson
err := json.Unmarshal([]byte(c), &inputStringAsStruct)
if err != nil {
t.Fatal("Failed to unmarshal:", err)
}
if reflect.DeepEqual(inputStringAsStruct, typedCases[n]) {
t.Error("Unmarshaled compose request is not the one expected")
}
}
var byteArrays [][]byte
for _, c := range typedCases {
data, err := json.Marshal(c)
if err != nil {
t.Fatal("Failed to marshal state:", err)
}
byteArrays = append(byteArrays, data)
}
for n, b := range byteArrays {
var inputStringAsStruct *TestJson
err := json.Unmarshal(b, &inputStringAsStruct)
if err != nil {
t.Fatal("Failed to unmarshal:", err)
}
if reflect.DeepEqual(inputStringAsStruct, typedCases[n]) {
t.Error("Unmarshaled compose request is not the one expected")
}
}
}