From c8f6c2b3232631d409bbc7977f578514c65877ba Mon Sep 17 00:00:00 2001 From: Chuang Wang Date: Sun, 1 May 2022 16:23:45 -0700 Subject: [PATCH] Rewrite ParamSpec SetDefaults --- pkg/apis/pipeline/v1beta1/param_types.go | 51 +++++++++++++----------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/pkg/apis/pipeline/v1beta1/param_types.go b/pkg/apis/pipeline/v1beta1/param_types.go index 6bc299cd94f..83dda5ff4e9 100644 --- a/pkg/apis/pipeline/v1beta1/param_types.go +++ b/pkg/apis/pipeline/v1beta1/param_types.go @@ -62,32 +62,37 @@ type PropertySpec struct { // SetDefaults set the default type func (pp *ParamSpec) SetDefaults(ctx context.Context) { - if pp != nil && pp.Type == "" { - // if the properties section is not empty, set object as the parameter type - if pp.Properties != nil { - pp.Type = ParamTypeObject - for _, property := range pp.Properties { - property.Type = ParamTypeString - } - } else if pp.Default != nil { - // propagate the parsed ArrayOrString's type to the parent ParamSpec's type - if pp.Default.Type != "" { - // propagate the default type if specified - pp.Type = pp.Default.Type + if pp == nil || pp.Type != "" { + return + } + + // if the properties section is not empty, set object as the parameter type + if pp.Properties != nil { + pp.Type = ParamTypeObject + for _, property := range pp.Properties { + property.Type = ParamTypeString + } + return + } + + if pp.Default != nil { + // propagate the parsed ArrayOrString's type to the parent ParamSpec's 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 if pp.Default.ObjectVal != nil { + pp.Type = ParamTypeObject } 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 if pp.Default.ObjectVal != nil { - pp.Type = ParamTypeObject - } else { - pp.Type = ParamTypeString - } + pp.Type = ParamTypeString } - } else { - // ParamTypeString is the default value (when no type can be inferred from the default value) - pp.Type = ParamTypeString } + } else { + // ParamTypeString is the default value (when no type can be inferred from the default value) + pp.Type = ParamTypeString } }