Skip to content

Commit 1a3be23

Browse files
Merge pull request #133 from gschei/add-transform-join
2 parents 63147b6 + ceff6e6 commit 1a3be23

File tree

6 files changed

+127
-0
lines changed

6 files changed

+127
-0
lines changed

input/v1beta1/resources_transforms.go

Lines changed: 11 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
StringTransformTypeReplace StringTransformType = "Replace"
212213
)
213214

@@ -258,11 +259,21 @@ type StringTransform struct {
258259
// +optional
259260
Regexp *StringTransformRegexp `json:"regexp,omitempty"`
260261

262+
// Join the input strings.
263+
// +optional
264+
Join *StringTransformJoin `json:"join,omitempty"`
265+
261266
// Search/Replace applied to the input string.
262267
// +optional
263268
Replace *StringTransformReplace `json:"replace,omitempty"`
264269
}
265270

271+
// A StringTransformJoin joins the input strings.
272+
type StringTransformJoin struct {
273+
// Separator to join the input strings.
274+
Separator string `json:"separator"`
275+
}
276+
266277
// A StringTransformRegexp extracts a match from the input using a regular
267278
// expression.
268279
type StringTransformRegexp struct {

input/v1beta1/zz_generated.deepcopy.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package/input/pt.fn.crossplane.io_resources.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,15 @@ spec:
318318
Format the input using a Go format string. See
319319
https://golang.org/pkg/fmt/ for details.
320320
type: string
321+
join:
322+
description: Join the input strings.
323+
properties:
324+
separator:
325+
description: Separator to join the input strings.
326+
type: string
327+
required:
328+
- separator
329+
type: object
321330
regexp:
322331
description: Extract a match from the input using
323332
a regular expression.
@@ -690,6 +699,15 @@ spec:
690699
Format the input using a Go format string. See
691700
https://golang.org/pkg/fmt/ for details.
692701
type: string
702+
join:
703+
description: Join the input strings.
704+
properties:
705+
separator:
706+
description: Separator to join the input strings.
707+
type: string
708+
required:
709+
- separator
710+
type: object
693711
regexp:
694712
description: Extract a match from the input using
695713
a regular expression.
@@ -1125,6 +1143,15 @@ spec:
11251143
Format the input using a Go format string. See
11261144
https://golang.org/pkg/fmt/ for details.
11271145
type: string
1146+
join:
1147+
description: Join the input strings.
1148+
properties:
1149+
separator:
1150+
description: Separator to join the input strings.
1151+
type: string
1152+
required:
1153+
- separator
1154+
type: object
11281155
regexp:
11291156
description: Extract a match from the input using
11301157
a regular expression.

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
errStringTransformTypeReplace = "string transform of type %s replace is not set"
@@ -288,6 +290,11 @@ func ResolveString(t *v1beta1.StringTransform, input any) (string, error) { //no
288290
return "", errors.Errorf(errStringTransformTypeRegexp, string(t.Type))
289291
}
290292
return stringRegexpTransform(input, *t.Regexp)
293+
case v1beta1.StringTransformTypeJoin:
294+
if t.Join == nil {
295+
return "", errors.Errorf(errStringTransformTypeJoin, string(t.Type))
296+
}
297+
return stringJoinTransform(input, *t.Join)
291298
case v1beta1.StringTransformTypeReplace:
292299
if t.Replace == nil {
293300
return "", errors.Errorf(errStringTransformTypeReplace, string(t.Type))
@@ -357,6 +364,26 @@ func stringTrimTransform(input any, t v1beta1.StringTransformType, trim string)
357364
return str
358365
}
359366

367+
func stringJoinTransform(input any, r v1beta1.StringTransformJoin) (string, error) {
368+
arr, ok := input.([]interface{})
369+
if !ok {
370+
return "", errors.New(errStringTransformTypeJoinFailed)
371+
}
372+
if len(arr) == 0 {
373+
return "", nil
374+
}
375+
376+
var result string
377+
for _, v := range arr {
378+
result += fmt.Sprintf("%v%s", v, r.Separator)
379+
}
380+
if len(r.Separator) > 0 {
381+
return result[:len(result)-1], nil
382+
}
383+
384+
return result, nil
385+
}
386+
360387
func stringRegexpTransform(input any, r v1beta1.StringTransformRegexp) (string, error) {
361388
re, err := regexp.Compile(r.Match)
362389
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
replace *v1beta1.StringTransformReplace
643644
i any
644645
}
@@ -1004,6 +1005,42 @@ func TestStringResolve(t *testing.T) {
10041005
err: errors.Wrap(errors.New("json: unsupported type: func()"), errMarshalJSON),
10051006
},
10061007
},
1008+
"JoinString": {
1009+
args: args{
1010+
stype: v1beta1.StringTransformTypeJoin,
1011+
join: &v1beta1.StringTransformJoin{
1012+
Separator: ",",
1013+
},
1014+
i: []interface{}{"cross", "plane"},
1015+
},
1016+
want: want{
1017+
o: "cross,plane",
1018+
},
1019+
},
1020+
"JoinStringEmptySeparator": {
1021+
args: args{
1022+
stype: v1beta1.StringTransformTypeJoin,
1023+
join: &v1beta1.StringTransformJoin{
1024+
Separator: "",
1025+
},
1026+
i: []interface{}{"cross", "plane"},
1027+
},
1028+
want: want{
1029+
o: "crossplane",
1030+
},
1031+
},
1032+
"JoinStringDifferentTypes": {
1033+
args: args{
1034+
stype: v1beta1.StringTransformTypeJoin,
1035+
join: &v1beta1.StringTransformJoin{
1036+
Separator: "-",
1037+
},
1038+
i: []interface{}{"cross", "plane", 42},
1039+
},
1040+
want: want{
1041+
o: "cross-plane-42",
1042+
},
1043+
},
10071044
"ReplaceFound": {
10081045
args: args{
10091046
stype: v1beta1.StringTransformTypeReplace,
@@ -1052,6 +1089,7 @@ func TestStringResolve(t *testing.T) {
10521089
Convert: tc.convert,
10531090
Trim: tc.trim,
10541091
Regexp: tc.regexp,
1092+
Join: tc.join,
10551093
Replace: tc.replace,
10561094
}
10571095

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
case v1beta1.StringTransformTypeReplace:
386390
if s.Replace == nil {
387391
return field.Required(field.NewPath("replace"), "replace transform requires a replace")

0 commit comments

Comments
 (0)