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

[TEP-0075]Add more setdefaults features for taskresults #5142

Merged
merged 1 commit into from
Jul 28, 2022
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
21 changes: 18 additions & 3 deletions pkg/apis/pipeline/v1/result_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,23 @@ import "context"

// SetDefaults set the default type for TaskResult
func (tr *TaskResult) SetDefaults(context.Context) {
if tr != nil && tr.Type == "" {
// ResultsTypeString is the default value
tr.Type = ResultsTypeString
if tr == nil {
return
}
if tr.Type == "" {
if tr.Properties != nil {
// Set type to object if `properties` is given
tr.Type = ResultsTypeObject
} else {
// ResultsTypeString is the default value
tr.Type = ResultsTypeString
}
}

// Set default type of object values to string
for key, propertySpec := range tr.Properties {
if propertySpec.Type == "" {
tr.Properties[key] = PropertySpec{Type: ParamType(ResultsTypeString)}
}
}
}
95 changes: 95 additions & 0 deletions pkg/apis/pipeline/v1/result_defaults_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
Copyright 2022 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1_test

import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/test/diff"
)

func TestTaskResult_SetDefaults(t *testing.T) {
tests := []struct {
name string
before *v1.TaskResult
after *v1.TaskResult
}{{
name: "empty taskresult",
before: nil,
after: nil,
}, {
name: "inferred string type",
before: &v1.TaskResult{
Name: "resultname",
},
after: &v1.TaskResult{
Name: "resultname",
Type: v1.ResultsTypeString,
},
}, {
name: "string type specified not changed",
before: &v1.TaskResult{
Name: "resultname",
Type: v1.ResultsTypeString,
},
after: &v1.TaskResult{
Name: "resultname",
Type: v1.ResultsTypeString,
},
}, {
name: "array type specified not changed",
before: &v1.TaskResult{
Name: "resultname",
Type: v1.ResultsTypeArray,
},
after: &v1.TaskResult{
Name: "resultname",
Type: v1.ResultsTypeArray,
},
}, {
name: "inferred object type from properties - PropertySpec type is provided",
before: &v1.TaskResult{
Name: "resultname",
Properties: map[string]v1.PropertySpec{"key1": {v1.ParamTypeString}},
},
after: &v1.TaskResult{
Name: "resultname",
Type: v1.ResultsTypeObject,
Properties: map[string]v1.PropertySpec{"key1": {v1.ParamTypeString}},
},
}, {
name: "inferred type from properties - PropertySpec type is not provided",
before: &v1.TaskResult{
Name: "resultname",
Properties: map[string]v1.PropertySpec{"key1": {}},
},
after: &v1.TaskResult{
Name: "resultname",
Type: v1.ResultsTypeObject,
Properties: map[string]v1.PropertySpec{"key1": {v1.ParamTypeString}},
},
}}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
tc.before.SetDefaults(ctx)
if d := cmp.Diff(tc.before, tc.after); d != "" {
t.Error(diff.PrintWantGot(d))
}
})
}
}
21 changes: 18 additions & 3 deletions pkg/apis/pipeline/v1beta1/result_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,23 @@ import "context"

// SetDefaults set the default type for TaskResult
func (tr *TaskResult) SetDefaults(context.Context) {
if tr != nil && tr.Type == "" {
// ResultsTypeString is the default value
tr.Type = ResultsTypeString
if tr == nil {
return
}
if tr.Type == "" {
if tr.Properties != nil {
// Set type to object if `properties` is given
tr.Type = ResultsTypeObject
} else {
// ResultsTypeString is the default value
tr.Type = ResultsTypeString
}
}

// Set default type of object values to string
for key, propertySpec := range tr.Properties {
if propertySpec.Type == "" {
tr.Properties[key] = PropertySpec{Type: ParamType(ResultsTypeString)}
}
}
}
95 changes: 95 additions & 0 deletions pkg/apis/pipeline/v1beta1/result_defaults_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
Copyright 2022 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1_test

import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/pipeline/test/diff"
)

func TestTaskResult_SetDefaults(t *testing.T) {
tests := []struct {
name string
before *v1beta1.TaskResult
after *v1beta1.TaskResult
}{{
name: "empty taskresult",
before: nil,
after: nil,
}, {
name: "inferred string type",
before: &v1beta1.TaskResult{
Name: "resultname",
},
after: &v1beta1.TaskResult{
Name: "resultname",
Type: v1beta1.ResultsTypeString,
Yongxuanzhang marked this conversation as resolved.
Show resolved Hide resolved
},
}, {
name: "string type specified not changed",
before: &v1beta1.TaskResult{
Name: "resultname",
Type: v1beta1.ResultsTypeString,
},
after: &v1beta1.TaskResult{
Name: "resultname",
Type: v1beta1.ResultsTypeString,
},
}, {
name: "array type specified not changed",
before: &v1beta1.TaskResult{
Name: "resultname",
Type: v1beta1.ResultsTypeArray,
},
after: &v1beta1.TaskResult{
Name: "resultname",
Type: v1beta1.ResultsTypeArray,
},
}, {
name: "inferred object type from properties - PropertySpec type is provided",
before: &v1beta1.TaskResult{
Name: "resultname",
Properties: map[string]v1beta1.PropertySpec{"key1": {v1beta1.ParamTypeString}},
},
after: &v1beta1.TaskResult{
Name: "resultname",
Type: v1beta1.ResultsTypeObject,
Properties: map[string]v1beta1.PropertySpec{"key1": {v1beta1.ParamTypeString}},
},
}, {
name: "inferred type from properties - PropertySpec type is not provided",
before: &v1beta1.TaskResult{
Name: "resultname",
Properties: map[string]v1beta1.PropertySpec{"key1": {}},
},
after: &v1beta1.TaskResult{
Name: "resultname",
Type: v1beta1.ResultsTypeObject,
Properties: map[string]v1beta1.PropertySpec{"key1": {v1beta1.ParamTypeString}},
},
Yongxuanzhang marked this conversation as resolved.
Show resolved Hide resolved
}}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
tc.before.SetDefaults(ctx)
if d := cmp.Diff(tc.before, tc.after); d != "" {
t.Error(diff.PrintWantGot(d))
}
})
}
}