Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set the param type based on the default value #4608

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion pkg/apis/pipeline/v1beta1/param_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,17 @@ func (pp *ParamSpec) SetDefaults(ctx context.Context) {
if pp != nil && pp.Type == "" {
if pp.Default != nil {
// propagate the parsed ArrayOrString's type to the parent ParamSpec's type
pp.Type = pp.Default.Type
if pp.Default.Type != "" {
// propagate the default type if specified
pp.Type = pp.Default.Type
} else {
// determine the type based on the array or string values when default value is specified but not the type
if pp.Default.ArrayVal != nil {
pp.Type = ParamTypeArray
} else {
pp.Type = ParamTypeString
}
}
} else {
// ParamTypeString is the default value (when no type can be inferred from the default value)
pp.Type = ParamTypeString
Expand Down
39 changes: 31 additions & 8 deletions pkg/apis/pipeline/v1beta1/param_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,52 @@ func TestParamSpec_SetDefaults(t *testing.T) {
Type: v1beta1.ParamTypeString,
},
}, {
name: "inferred type from default value",
name: "inferred type from default value - array",
before: &v1beta1.ParamSpec{
Name: "parametername",
Default: v1beta1.NewArrayOrString("an", "array"),
Name: "parametername",
Default: &v1beta1.ArrayOrString{
ArrayVal: []string{"array"},
},
},
defaultsApplied: &v1beta1.ParamSpec{
Name: "parametername",
Type: v1beta1.ParamTypeArray,
Default: &v1beta1.ArrayOrString{
ArrayVal: []string{"array"},
},
},
}, {
name: "inferred type from default value - string",
before: &v1beta1.ParamSpec{
Name: "parametername",
Default: &v1beta1.ArrayOrString{
StringVal: "an",
},
},
defaultsApplied: &v1beta1.ParamSpec{
Name: "parametername",
Type: v1beta1.ParamTypeArray,
Default: v1beta1.NewArrayOrString("an", "array"),
Name: "parametername",
Type: v1beta1.ParamTypeString,
Default: &v1beta1.ArrayOrString{
StringVal: "an",
},
},
}, {
name: "fully defined ParamSpec",
before: &v1beta1.ParamSpec{
Name: "parametername",
Type: v1beta1.ParamTypeArray,
Description: "a description",
Default: v1beta1.NewArrayOrString("an", "array"),
Default: &v1beta1.ArrayOrString{
ArrayVal: []string{"array"},
},
},
defaultsApplied: &v1beta1.ParamSpec{
Name: "parametername",
Type: v1beta1.ParamTypeArray,
Description: "a description",
Default: v1beta1.NewArrayOrString("an", "array"),
Default: &v1beta1.ArrayOrString{
ArrayVal: []string{"array"},
},
},
}}
for _, tc := range tests {
Expand Down
15 changes: 15 additions & 0 deletions pkg/apis/pipeline/v1beta1/pipeline_defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ func TestPipelineSpec_SetDefaults(t *testing.T) {
Name: "string-param", Type: v1beta1.ParamTypeString,
}},
},
}, {
desc: "param type - param type must be derived based on the default value " + string(v1beta1.ParamTypeString),
ps: &v1beta1.PipelineSpec{
Params: []v1beta1.ParamSpec{{
Name: "string-param",
Default: &v1beta1.ArrayOrString{
StringVal: "foo",
},
}},
},
want: &v1beta1.PipelineSpec{
Params: []v1beta1.ParamSpec{{
Name: "string-param", Type: v1beta1.ParamTypeString, Default: &v1beta1.ArrayOrString{StringVal: "foo"},
}},
},
}, {
desc: "pipeline task with taskSpec - default param type must be " + string(v1beta1.ParamTypeString),
ps: &v1beta1.PipelineSpec{
Expand Down