Skip to content

Commit 25493e5

Browse files
committed
added string transform join
Signed-off-by: Gilbert Scheiblhofer <gilbert.scheiblhofer@gmx.at>
1 parent 4476a5b commit 25493e5

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

input/v1beta1/resources_transforms.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ const (
208208
StringTransformTypeTrimPrefix StringTransformType = "TrimPrefix"
209209
StringTransformTypeTrimSuffix StringTransformType = "TrimSuffix"
210210
StringTransformTypeRegexp StringTransformType = "Regexp"
211+
StringTransformTypeJoin StringTransformType = "Join"
211212
)
212213

213214
// StringConversionType converts a string.
@@ -257,6 +258,15 @@ type StringTransform struct {
257258
// Extract a match from the input using a regular expression.
258259
// +optional
259260
Regexp *StringTransformRegexp `json:"regexp,omitempty"`
261+
262+
// Join the input strings.
263+
Join *StringTransformJoin `json:"join,omitempty"`
264+
}
265+
266+
// A StringTransformJoin joins the input strings.
267+
type StringTransformJoin struct {
268+
// Separator to join the input strings.
269+
Separator string `json:"separator"`
260270
}
261271

262272
// A StringTransformRegexp extracts a match from the input using a regular

transforms.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ const (
5050
errStringTransformTypeConvert = "string transform of type %s convert is not set"
5151
errStringTransformTypeTrim = "string transform of type %s trim is not set"
5252
errStringTransformTypeRegexp = "string transform of type %s regexp is not set"
53+
errStringTransformTypeJoin = "string transform of type %s join is not set"
54+
errStringTransformTypeJoinFailed = "could not parse input array"
5355
errStringTransformTypeRegexpFailed = "could not compile regexp"
5456
errStringTransformTypeRegexpNoMatch = "regexp %q had no matches for group %d"
5557
errStringConvertTypeFailed = "type %s is not supported for string convert"
@@ -287,6 +289,11 @@ func ResolveString(t *v1beta1.StringTransform, input any) (string, error) {
287289
return "", errors.Errorf(errStringTransformTypeRegexp, string(t.Type))
288290
}
289291
return stringRegexpTransform(input, *t.Regexp)
292+
case v1beta1.StringTransformTypeJoin:
293+
if t.Join == nil {
294+
return "", errors.Errorf(errStringTransformTypeJoin, string(t.Type))
295+
}
296+
return stringJoinTransform(input, *t.Join)
290297
default:
291298
return "", errors.Errorf(errStringTransformTypeFailed, string(t.Type))
292299
}
@@ -351,6 +358,26 @@ func stringTrimTransform(input any, t v1beta1.StringTransformType, trim string)
351358
return str
352359
}
353360

361+
func stringJoinTransform(input any, r v1beta1.StringTransformJoin) (string, error) {
362+
arr, ok := input.([]interface{})
363+
if !ok {
364+
return "", errors.New(errStringTransformTypeJoinFailed)
365+
}
366+
if len(arr) == 0 {
367+
return "", nil
368+
}
369+
370+
var result string
371+
for _, v := range arr {
372+
result += fmt.Sprintf("%v%s", v, r.Separator)
373+
}
374+
if len(r.Separator) > 0 {
375+
return result[:len(result)-1], nil
376+
} else {
377+
return result, nil
378+
}
379+
}
380+
354381
func stringRegexpTransform(input any, r v1beta1.StringTransformRegexp) (string, error) {
355382
re, err := regexp.Compile(r.Match)
356383
if err != nil {

transforms_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ func TestStringResolve(t *testing.T) {
639639
convert *v1beta1.StringConversionType
640640
trim *string
641641
regexp *v1beta1.StringTransformRegexp
642+
join *v1beta1.StringTransformJoin
642643
i any
643644
}
644645
type want struct {
@@ -1003,6 +1004,42 @@ func TestStringResolve(t *testing.T) {
10031004
err: errors.Wrap(errors.New("json: unsupported type: func()"), errMarshalJSON),
10041005
},
10051006
},
1007+
"JoinString": {
1008+
args: args{
1009+
stype: v1beta1.StringTransformTypeJoin,
1010+
join: &v1beta1.StringTransformJoin{
1011+
Separator: ",",
1012+
},
1013+
i: []interface{}{"cross", "plane"},
1014+
},
1015+
want: want{
1016+
o: "cross,plane",
1017+
},
1018+
},
1019+
"JoinStringEmptySeparator": {
1020+
args: args{
1021+
stype: v1beta1.StringTransformTypeJoin,
1022+
join: &v1beta1.StringTransformJoin{
1023+
Separator: "",
1024+
},
1025+
i: []interface{}{"cross", "plane"},
1026+
},
1027+
want: want{
1028+
o: "crossplane",
1029+
},
1030+
},
1031+
"JoinStringDifferentTypes": {
1032+
args: args{
1033+
stype: v1beta1.StringTransformTypeJoin,
1034+
join: &v1beta1.StringTransformJoin{
1035+
Separator: "-",
1036+
},
1037+
i: []interface{}{"cross", "plane", 42},
1038+
},
1039+
want: want{
1040+
o: "cross-plane-42",
1041+
},
1042+
},
10061043
}
10071044
for name, tc := range cases {
10081045
t.Run(name, func(t *testing.T) {
@@ -1012,6 +1049,7 @@ func TestStringResolve(t *testing.T) {
10121049
Convert: tc.convert,
10131050
Trim: tc.trim,
10141051
Regexp: tc.regexp,
1052+
Join: tc.join,
10151053
}
10161054

10171055
got, err := ResolveString(tr, tc.i)

validate.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,10 @@ func ValidateStringTransform(s *v1beta1.StringTransform) *field.Error { //nolint
382382
if _, err := regexp.Compile(s.Regexp.Match); err != nil {
383383
return field.Invalid(field.NewPath("regexp", "match"), s.Regexp.Match, "invalid regexp")
384384
}
385+
case v1beta1.StringTransformTypeJoin:
386+
if s.Join == nil {
387+
return field.Required(field.NewPath("join"), "join transform requires a join")
388+
}
385389
default:
386390
return field.Invalid(field.NewPath("type"), s.Type, "unknown string transform type")
387391
}

0 commit comments

Comments
 (0)