Skip to content

Commit

Permalink
added string transform join
Browse files Browse the repository at this point in the history
  • Loading branch information
gschei committed Jul 7, 2024
1 parent 4476a5b commit 16260c6
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
10 changes: 10 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"
)

// StringConversionType converts a string.
Expand Down Expand Up @@ -257,6 +258,15 @@ type StringTransform struct {
// Extract a match from the input using a regular expression.
// +optional
Regexp *StringTransformRegexp `json:"regexp,omitempty"`

// Join the input strings.
Join *StringTransformJoin `json:"join,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
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"
errStringConvertTypeFailed = "type %s is not supported for string convert"
Expand Down Expand Up @@ -287,6 +289,11 @@ func ResolveString(t *v1beta1.StringTransform, input any) (string, error) {
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)
default:
return "", errors.Errorf(errStringTransformTypeFailed, string(t.Type))
}
Expand Down Expand Up @@ -351,6 +358,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
} else {
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
i any
}
type want struct {
Expand Down Expand Up @@ -1003,6 +1004,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",
},
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
Expand All @@ -1012,6 +1049,7 @@ func TestStringResolve(t *testing.T) {
Convert: tc.convert,
Trim: tc.trim,
Regexp: tc.regexp,
Join: tc.join,
}

got, err := ResolveString(tr, tc.i)
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")
}
default:
return field.Invalid(field.NewPath("type"), s.Type, "unknown string transform type")
}
Expand Down

0 comments on commit 16260c6

Please sign in to comment.