Skip to content

Commit

Permalink
Add Object param and validate
Browse files Browse the repository at this point in the history
  • Loading branch information
chuangw6 committed May 1, 2022
1 parent 6f39e94 commit dab9308
Show file tree
Hide file tree
Showing 13 changed files with 531 additions and 45 deletions.
56 changes: 53 additions & 3 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.

16 changes: 15 additions & 1 deletion 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 @@ -135,11 +139,20 @@ func getContextParams(ctx context.Context, overlays ...Param) []Param {
Type: ParamTypeString,
StringVal: fmt.Sprintf("$(params.%s)", ps.Name),
}
} else {
} else if ps.Type == ParamTypeArray {
p.Value = ArrayOrString{
Type: ParamTypeArray,
ArrayVal: []string{fmt.Sprintf("$(params.%s[*])", ps.Name)},
}
} else {
objVal := make(map[string]string)
for k := range ps.Properties {
objVal[fmt.Sprintf("$(params.%s.%s)", p.Name, k)] = ""
}
p.Value = ArrayOrString{
Type: ParamTypeObject,
ObjectVal: objVal,
}
}
out = append(out, p)
}
Expand All @@ -161,6 +174,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{"$(params.c.key1)": ""},
},
},
},
},
{
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 dab9308

Please sign in to comment.