Skip to content

Commit

Permalink
Merge pull request #133 from gschei/add-transform-join
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanduplessis authored Jul 30, 2024
2 parents 63147b6 + ceff6e6 commit 1a3be23
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 0 deletions.
11 changes: 11 additions & 0 deletions input/v1beta1/resources_transforms.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ const (
StringTransformTypeTrimPrefix StringTransformType = "TrimPrefix"
StringTransformTypeTrimSuffix StringTransformType = "TrimSuffix"
StringTransformTypeRegexp StringTransformType = "Regexp"
StringTransformTypeJoin StringTransformType = "Join"
StringTransformTypeReplace StringTransformType = "Replace"
)

Expand Down Expand Up @@ -258,11 +259,21 @@ type StringTransform struct {
// +optional
Regexp *StringTransformRegexp `json:"regexp,omitempty"`

// Join the input strings.
// +optional
Join *StringTransformJoin `json:"join,omitempty"`

// Search/Replace applied to the input string.
// +optional
Replace *StringTransformReplace `json:"replace,omitempty"`
}

// A StringTransformJoin joins the input strings.
type StringTransformJoin struct {
// Separator to join the input strings.
Separator string `json:"separator"`
}

// A StringTransformRegexp extracts a match from the input using a regular
// expression.
type StringTransformRegexp struct {
Expand Down
20 changes: 20 additions & 0 deletions input/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions package/input/pt.fn.crossplane.io_resources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,15 @@ spec:
Format the input using a Go format string. See
https://golang.org/pkg/fmt/ for details.
type: string
join:
description: Join the input strings.
properties:
separator:
description: Separator to join the input strings.
type: string
required:
- separator
type: object
regexp:
description: Extract a match from the input using
a regular expression.
Expand Down Expand Up @@ -690,6 +699,15 @@ spec:
Format the input using a Go format string. See
https://golang.org/pkg/fmt/ for details.
type: string
join:
description: Join the input strings.
properties:
separator:
description: Separator to join the input strings.
type: string
required:
- separator
type: object
regexp:
description: Extract a match from the input using
a regular expression.
Expand Down Expand Up @@ -1125,6 +1143,15 @@ spec:
Format the input using a Go format string. See
https://golang.org/pkg/fmt/ for details.
type: string
join:
description: Join the input strings.
properties:
separator:
description: Separator to join the input strings.
type: string
required:
- separator
type: object
regexp:
description: Extract a match from the input using
a regular expression.
Expand Down
27 changes: 27 additions & 0 deletions transforms.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ const (
errStringTransformTypeConvert = "string transform of type %s convert is not set"
errStringTransformTypeTrim = "string transform of type %s trim is not set"
errStringTransformTypeRegexp = "string transform of type %s regexp is not set"
errStringTransformTypeJoin = "string transform of type %s join is not set"
errStringTransformTypeJoinFailed = "could not parse input array"
errStringTransformTypeRegexpFailed = "could not compile regexp"
errStringTransformTypeRegexpNoMatch = "regexp %q had no matches for group %d"
errStringTransformTypeReplace = "string transform of type %s replace is not set"
Expand Down Expand Up @@ -288,6 +290,11 @@ func ResolveString(t *v1beta1.StringTransform, input any) (string, error) { //no
return "", errors.Errorf(errStringTransformTypeRegexp, string(t.Type))
}
return stringRegexpTransform(input, *t.Regexp)
case v1beta1.StringTransformTypeJoin:
if t.Join == nil {
return "", errors.Errorf(errStringTransformTypeJoin, string(t.Type))
}
return stringJoinTransform(input, *t.Join)
case v1beta1.StringTransformTypeReplace:
if t.Replace == nil {
return "", errors.Errorf(errStringTransformTypeReplace, string(t.Type))
Expand Down Expand Up @@ -357,6 +364,26 @@ func stringTrimTransform(input any, t v1beta1.StringTransformType, trim string)
return str
}

func stringJoinTransform(input any, r v1beta1.StringTransformJoin) (string, error) {
arr, ok := input.([]interface{})
if !ok {
return "", errors.New(errStringTransformTypeJoinFailed)
}
if len(arr) == 0 {
return "", nil
}

var result string
for _, v := range arr {
result += fmt.Sprintf("%v%s", v, r.Separator)
}
if len(r.Separator) > 0 {
return result[:len(result)-1], nil
}

return result, nil
}

func stringRegexpTransform(input any, r v1beta1.StringTransformRegexp) (string, error) {
re, err := regexp.Compile(r.Match)
if err != nil {
Expand Down
38 changes: 38 additions & 0 deletions transforms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,7 @@ func TestStringResolve(t *testing.T) {
convert *v1beta1.StringConversionType
trim *string
regexp *v1beta1.StringTransformRegexp
join *v1beta1.StringTransformJoin
replace *v1beta1.StringTransformReplace
i any
}
Expand Down Expand Up @@ -1004,6 +1005,42 @@ func TestStringResolve(t *testing.T) {
err: errors.Wrap(errors.New("json: unsupported type: func()"), errMarshalJSON),
},
},
"JoinString": {
args: args{
stype: v1beta1.StringTransformTypeJoin,
join: &v1beta1.StringTransformJoin{
Separator: ",",
},
i: []interface{}{"cross", "plane"},
},
want: want{
o: "cross,plane",
},
},
"JoinStringEmptySeparator": {
args: args{
stype: v1beta1.StringTransformTypeJoin,
join: &v1beta1.StringTransformJoin{
Separator: "",
},
i: []interface{}{"cross", "plane"},
},
want: want{
o: "crossplane",
},
},
"JoinStringDifferentTypes": {
args: args{
stype: v1beta1.StringTransformTypeJoin,
join: &v1beta1.StringTransformJoin{
Separator: "-",
},
i: []interface{}{"cross", "plane", 42},
},
want: want{
o: "cross-plane-42",
},
},
"ReplaceFound": {
args: args{
stype: v1beta1.StringTransformTypeReplace,
Expand Down Expand Up @@ -1052,6 +1089,7 @@ func TestStringResolve(t *testing.T) {
Convert: tc.convert,
Trim: tc.trim,
Regexp: tc.regexp,
Join: tc.join,
Replace: tc.replace,
}

Expand Down
4 changes: 4 additions & 0 deletions validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,10 @@ func ValidateStringTransform(s *v1beta1.StringTransform) *field.Error { //nolint
if _, err := regexp.Compile(s.Regexp.Match); err != nil {
return field.Invalid(field.NewPath("regexp", "match"), s.Regexp.Match, "invalid regexp")
}
case v1beta1.StringTransformTypeJoin:
if s.Join == nil {
return field.Required(field.NewPath("join"), "join transform requires a join")
}
case v1beta1.StringTransformTypeReplace:
if s.Replace == nil {
return field.Required(field.NewPath("replace"), "replace transform requires a replace")
Expand Down

0 comments on commit 1a3be23

Please sign in to comment.