Skip to content

Commit

Permalink
Support object type param and add validation
Browse files Browse the repository at this point in the history
Note: This new feature is gated by the alpha flag.
Changes:
- Add `properties` section in ParamSpec struct
- Add `ObjectVal` in ArrayOrString struct
- Implement UnmarshalJSON and MarshalJSON for ArrayOrString of object type.
- Change SetDefault function to include object type
- Add type validation for object type in task_validation.go
- Add object param validation in validate_resources.go
  • Loading branch information
chuangw6 committed May 10, 2022
1 parent 79e591b commit 37adff2
Show file tree
Hide file tree
Showing 14 changed files with 662 additions and 58 deletions.
2 changes: 1 addition & 1 deletion pkg/apis/pipeline/v1alpha1/task_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (ts *TaskSpec) Validate(ctx context.Context) *apis.FieldError {
return err
}
// Validate that the parameters type are correct
if err := v1beta1.ValidateParameterTypes(ts.Params); err != nil {
if err := v1beta1.ValidateParameterTypes(ctx, ts.Params); err != nil {
return err
}

Expand Down
58 changes: 54 additions & 4 deletions pkg/apis/pipeline/v1beta1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 18 additions & 6 deletions pkg/apis/pipeline/v1beta1/param_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func addContextParams(ctx context.Context, in []Param) context.Context {
if v.StringVal != "" {
p.Value.Type = ParamTypeString
}
if v.ObjectVal != nil {
p.Value.Type = ParamTypeObject
}
}
out[p.Name] = ParamSpec{
Name: p.Name,
Expand Down Expand Up @@ -86,6 +89,7 @@ func addContextParamSpec(ctx context.Context, in []ParamSpec) context.Context {
Name: p.Name,
Type: p.Type,
Description: p.Description,
Properties: p.Properties,
Default: p.Default,
}
out[p.Name] = cps
Expand Down Expand Up @@ -127,19 +131,26 @@ func getContextParams(ctx context.Context, overlays ...Param) []Param {

// If there is no overlay, pass through the param to the next level.
// e.g. for strings $(params.name), for arrays $(params.name[*]).
// for object: {key: param's name, value: $(params.name[*])}
p := Param{
Name: ps.Name,
}
if ps.Type == ParamTypeString {
p.Value = ArrayOrString{
Type: ParamTypeString,
StringVal: fmt.Sprintf("$(params.%s)", ps.Name),
}
} else {
switch ps.Type {
case ParamTypeArray:
p.Value = ArrayOrString{
Type: ParamTypeArray,
ArrayVal: []string{fmt.Sprintf("$(params.%s[*])", ps.Name)},
}
case ParamTypeObject:
p.Value = ArrayOrString{
Type: ParamTypeObject,
ObjectVal: map[string]string{ps.Name: fmt.Sprintf("$(params.%s[*])", ps.Name)},
}
default:
p.Value = ArrayOrString{
Type: ParamTypeString,
StringVal: fmt.Sprintf("$(params.%s)", ps.Name),
}
}
out = append(out, p)
}
Expand All @@ -161,6 +172,7 @@ func getContextParamSpecs(ctx context.Context) []ParamSpec {
Name: ps.Name,
Type: ps.Type,
Description: ps.Description,
Properties: ps.Properties,
Default: ps.Default,
})
}
Expand Down
85 changes: 85 additions & 0 deletions pkg/apis/pipeline/v1beta1/param_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,30 @@ func TestAddContextParams(t *testing.T) {
},
},
},
{
name: "add-object-param",
params: []Param{{Name: "c", Value: *NewObject(map[string]string{"key1": "val1", "key2": "val2"})}},
want: paramCtxVal{
"a": ParamSpec{
Name: "a",
Type: ParamTypeString,
},
"b": ParamSpec{
Name: "b",
Type: ParamTypeArray,
},
"c": ParamSpec{
Name: "c",
Type: ParamTypeObject,
},
},
},
{
name: "existing-param",
params: []Param{
{Name: "a", Value: *NewArrayOrString("foo1")},
{Name: "b", Value: *NewArrayOrString("bar1", "baz1")},
{Name: "c", Value: *NewObject(map[string]string{"key1": "val1", "key2": "val2"})},
},
want: paramCtxVal{
"a": ParamSpec{
Expand All @@ -71,6 +90,10 @@ func TestAddContextParams(t *testing.T) {
Name: "b",
Type: ParamTypeArray,
},
"c": ParamSpec{
Name: "c",
Type: ParamTypeObject,
},
},
},
{
Expand All @@ -91,6 +114,10 @@ func TestAddContextParams(t *testing.T) {
Name: "b",
Type: ParamTypeArray,
},
"c": ParamSpec{
Name: "c",
Type: ParamTypeObject,
},
},
},
} {
Expand Down Expand Up @@ -118,11 +145,18 @@ func TestAddContextParamSpec(t *testing.T) {
name: "add-paramspec",
params: []ParamSpec{{
Name: "a",
}, {
Name: "b",
Properties: map[string]PropertySpec{"key1": {}},
}},
want: paramCtxVal{
"a": ParamSpec{
Name: "a",
},
"b": ParamSpec{
Name: "b",
Properties: map[string]PropertySpec{"key1": {}},
},
},
},
{
Expand All @@ -132,6 +166,12 @@ func TestAddContextParamSpec(t *testing.T) {
Type: ParamTypeArray,
Default: NewArrayOrString("foo", "bar"),
Description: "tacocat",
}, {
Name: "b",
Type: ParamTypeObject,
Properties: map[string]PropertySpec{"key2": {}},
Default: NewObject(map[string]string{"key2": "val"}),
Description: "my object",
}},
want: paramCtxVal{
"a": ParamSpec{
Expand All @@ -140,6 +180,13 @@ func TestAddContextParamSpec(t *testing.T) {
Default: NewArrayOrString("foo", "bar"),
Description: "tacocat",
},
"b": ParamSpec{
Name: "b",
Type: ParamTypeObject,
Properties: map[string]PropertySpec{"key2": {}},
Default: NewObject(map[string]string{"key2": "val"}),
Description: "my object",
},
},
},
{
Expand All @@ -159,6 +206,13 @@ func TestAddContextParamSpec(t *testing.T) {
Default: NewArrayOrString("foo", "bar"),
Description: "tacocat",
},
"b": ParamSpec{
Name: "b",
Type: ParamTypeObject,
Properties: map[string]PropertySpec{"key2": {}},
Default: NewObject(map[string]string{"key2": "val"}),
Description: "my object",
},
},
},
} {
Expand Down Expand Up @@ -187,6 +241,13 @@ func TestGetContextParams(t *testing.T) {
Default: NewArrayOrString("bar"),
Description: "racecar",
},
{
Name: "c",
Type: ParamTypeObject,
Properties: map[string]PropertySpec{"key1": {}},
Default: NewObject(map[string]string{"key1": "val1"}),
Description: "my object",
},
}

ctx = addContextParamSpec(ctx, want)
Expand All @@ -210,13 +271,23 @@ func TestGetContextParams(t *testing.T) {
ArrayVal: []string{"$(params.b[*])"},
},
},
{
Name: "c",
Value: ArrayOrString{
Type: ParamTypeObject,
ObjectVal: map[string]string{"c": "$(params.c[*])"},
},
},
},
},
{
name: "with-overlay",
overlay: []Param{{
Name: "a",
Value: *NewArrayOrString("tacocat"),
}, {
Name: "c",
Value: *NewObject(map[string]string{"key2": "val2"}),
}},
want: []Param{
{
Expand All @@ -230,6 +301,13 @@ func TestGetContextParams(t *testing.T) {
ArrayVal: []string{"$(params.b[*])"},
},
},
{
Name: "c",
Value: ArrayOrString{
Type: ParamTypeObject,
ObjectVal: map[string]string{"key2": "val2"},
},
},
},
},
} {
Expand Down Expand Up @@ -257,6 +335,13 @@ func TestGetContextParamSpecs(t *testing.T) {
Default: NewArrayOrString("bar"),
Description: "racecar",
},
{
Name: "c",
Type: ParamTypeObject,
Properties: map[string]PropertySpec{"key1": {}},
Default: NewObject(map[string]string{"key1": "val1"}),
Description: "my object",
},
}

ctx = addContextParamSpec(ctx, want)
Expand Down
Loading

0 comments on commit 37adff2

Please sign in to comment.