Skip to content

Commit

Permalink
fix(item): Support ItemValue.Type == List. Fixes #2660 (#3129)
Browse files Browse the repository at this point in the history
* fix(item): Support ItemValue.Type == List. Fixes #2660

* fix(item): Support ItemValue.Type == List. Fixes #2660

* fix-2660

* fix-2660

* fix-2660
  • Loading branch information
alexec committed May 28, 2020
1 parent def3b97 commit 05dd786
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
16 changes: 15 additions & 1 deletion pkg/apis/workflow/v1alpha1/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const (
List
)

type Typer interface {
GetType() Type
}

// Item expands a single workflow step into multiple parallel steps
// The value of Item can be a map, string, bool, or number
//
Expand Down Expand Up @@ -98,6 +102,10 @@ func (i Item) MarshalJSON() ([]byte, error) {
}
}

func (i *Item) GetType() Type {
return i.Type
}

// +protobuf=true
// +protobuf.options.(gogoproto.goproto_stringer)=false
// +k8s:openapi-gen=true
Expand Down Expand Up @@ -159,6 +167,10 @@ func (iv ItemValue) Format(s fmt.State, verb rune) {
fmt.Fprintf(s, iv.String()) //nolint
}

func (i *ItemValue) GetType() Type {
return i.Type
}

// MarshalJSON implements the json.Marshaller interface.
func (iv ItemValue) MarshalJSON() ([]byte, error) {
switch iv.Type {
Expand All @@ -170,7 +182,9 @@ func (iv ItemValue) MarshalJSON() ([]byte, error) {
return json.Marshal(iv.NumVal)
case Map:
return json.Marshal(iv.MapVal)
case List:
return json.Marshal(iv.ListVal)
default:
return []byte{}, fmt.Errorf("impossible ItemValue.Type")
return []byte{}, fmt.Errorf("impossible ItemValue.Type %v", iv.Type)
}
}
40 changes: 24 additions & 16 deletions pkg/apis/workflow/v1alpha1/item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,37 @@ import (
)

func TestItem(t *testing.T) {
testData := map[string]Type{
for data, expectedType := range map[string]Type{
"0": Number,
"3.141": Number,
"true": Bool,
"\"hello\"": String,
"{\"val\":\"123\"}": Map,
"[\"1\",\"2\",\"3\",\"4\",\"5\"]": List,
} {
t.Run(string(expectedType), func(t *testing.T) {
t.Run("Item", func(t *testing.T) {
runItemTest(t, data, &Item{}, expectedType)
})
t.Run("ItemValue", func(t *testing.T) {
runItemTest(t, data, &ItemValue{}, expectedType)
})
})
}
}

for data, expectedType := range testData {
var itm Item
err := json.Unmarshal([]byte(data), &itm)
assert.NoError(t, err)
assert.Equal(t, itm.Type, expectedType)
jsonBytes, err := json.Marshal(itm)
assert.NoError(t, err)
assert.Equal(t, data, string(jsonBytes))
if itm.Type == String {
assert.Equal(t, data, fmt.Sprintf("\"%v\"", itm))
assert.Equal(t, data, fmt.Sprintf("\"%s\"", itm))
} else {
assert.Equal(t, data, fmt.Sprintf("%v", itm))
assert.Equal(t, data, fmt.Sprintf("%s", itm))
}
func runItemTest(t *testing.T, data string, itm Typer, expectedType Type) {
err := json.Unmarshal([]byte(data), itm)
assert.NoError(t, err)
assert.Equal(t, itm.GetType(), expectedType)
jsonBytes, err := json.Marshal(itm)
assert.NoError(t, err)
assert.Equal(t, data, string(jsonBytes))
if itm.GetType() == String {
assert.Equal(t, data, fmt.Sprintf("\"%v\"", itm))
assert.Equal(t, data, fmt.Sprintf("\"%s\"", itm))
} else {
assert.Equal(t, data, fmt.Sprintf("%v", itm))
assert.Equal(t, data, fmt.Sprintf("%s", itm))
}
}

0 comments on commit 05dd786

Please sign in to comment.