Skip to content

Commit

Permalink
retrieve type based on the default value
Browse files Browse the repository at this point in the history
  • Loading branch information
pritidesai committed Feb 18, 2022
1 parent 9605e3e commit b1e9db9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
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

0 comments on commit b1e9db9

Please sign in to comment.