Skip to content

Commit

Permalink
feat(transform): Support string joining arrays
Browse files Browse the repository at this point in the history
Add a new string tranform `Join` that wraps around `strings.Join` and
converts any given array into a single string separated by a user
defined string.

Signed-off-by: Maximilian Blatt <maximilian.blatt-extern@deutschebahn.com>
  • Loading branch information
MisterMX committed Jan 23, 2024
1 parent dddd346 commit 8d6af61
Show file tree
Hide file tree
Showing 11 changed files with 298 additions and 4 deletions.
18 changes: 17 additions & 1 deletion apis/apiextensions/v1/composition_transforms.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ const (
StringTransformTypeTrimPrefix StringTransformType = "TrimPrefix"
StringTransformTypeTrimSuffix StringTransformType = "TrimSuffix"
StringTransformTypeRegexp StringTransformType = "Regexp"
StringTransformTypeJoin StringTransformType = "Join"
)

// StringConversionType converts a string.
Expand All @@ -362,7 +363,7 @@ type StringTransform struct {

// Type of the string transform to be run.
// +optional
// +kubebuilder:validation:Enum=Format;Convert;TrimPrefix;TrimSuffix;Regexp
// +kubebuilder:validation:Enum=Format;Convert;TrimPrefix;TrimSuffix;Regexp;Join
// +kubebuilder:default=Format
Type StringTransformType `json:"type,omitempty"`

Expand All @@ -388,6 +389,10 @@ type StringTransform struct {
// Extract a match from the input using a regular expression.
// +optional
Regexp *StringTransformRegexp `json:"regexp,omitempty"`

// Join defines parameters to join a slice of values to a string.
// +optional
Join *StringTransformJoin `json:"join,omitempty"`
}

// Validate checks this StringTransform is valid.
Expand Down Expand Up @@ -417,6 +422,10 @@ func (s *StringTransform) Validate() *field.Error {
if _, err := regexp.Compile(s.Regexp.Match); err != nil {
return field.Invalid(field.NewPath("regexp", "match"), s.Regexp.Match, "invalid regexp")
}
case 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 Expand Up @@ -480,6 +489,13 @@ func (c ConvertTransformFormat) IsValid() bool {
return false
}

// StringTransformJoin defines parameters to join a slice of values to a string.
type StringTransformJoin struct {
// Separator defines the character that should separate the values from each
// other in the joined string.
Separator string `json:"separator"`
}

// A ConvertTransform converts the input into a new object whose type is supplied.
type ConvertTransform struct {
// ToType is the type of the output of this transform.
Expand Down
10 changes: 10 additions & 0 deletions apis/apiextensions/v1/zz_generated.conversion.go

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

20 changes: 20 additions & 0 deletions apis/apiextensions/v1/zz_generated.deepcopy.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ const (
StringTransformTypeTrimPrefix StringTransformType = "TrimPrefix"
StringTransformTypeTrimSuffix StringTransformType = "TrimSuffix"
StringTransformTypeRegexp StringTransformType = "Regexp"
StringTransformTypeJoin StringTransformType = "Join"
)

// StringConversionType converts a string.
Expand All @@ -364,7 +365,7 @@ type StringTransform struct {

// Type of the string transform to be run.
// +optional
// +kubebuilder:validation:Enum=Format;Convert;TrimPrefix;TrimSuffix;Regexp
// +kubebuilder:validation:Enum=Format;Convert;TrimPrefix;TrimSuffix;Regexp;Join
// +kubebuilder:default=Format
Type StringTransformType `json:"type,omitempty"`

Expand All @@ -390,6 +391,10 @@ type StringTransform struct {
// Extract a match from the input using a regular expression.
// +optional
Regexp *StringTransformRegexp `json:"regexp,omitempty"`

// Join defines parameters to join a slice of values to a string.
// +optional
Join *StringTransformJoin `json:"join,omitempty"`
}

// Validate checks this StringTransform is valid.
Expand Down Expand Up @@ -419,6 +424,10 @@ func (s *StringTransform) Validate() *field.Error {
if _, err := regexp.Compile(s.Regexp.Match); err != nil {
return field.Invalid(field.NewPath("regexp", "match"), s.Regexp.Match, "invalid regexp")
}
case 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 Expand Up @@ -482,6 +491,13 @@ func (c ConvertTransformFormat) IsValid() bool {
return false
}

// StringTransformJoin defines parameters to join a slice of values to a string.
type StringTransformJoin struct {
// Separator defines the character that should separate the values from each
// other in the joined string.
Separator string `json:"separator"`
}

// A ConvertTransform converts the input into a new object whose type is supplied.
type ConvertTransform struct {
// ToType is the type of the output of this transform.
Expand Down
20 changes: 20 additions & 0 deletions apis/apiextensions/v1beta1/zz_generated.deepcopy.go

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

78 changes: 78 additions & 0 deletions cluster/crds/apiextensions.crossplane.io_compositionrevisions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,18 @@ spec:
string. See https://golang.org/pkg/fmt/ for
details.
type: string
join:
description: Join defines parameters to join a
slice of values to a string.
properties:
separator:
description: Separator defines the character
that should separate the values from each
other in the joined string.
type: string
required:
- separator
type: object
regexp:
description: Extract a match from the input using
a regular expression.
Expand Down Expand Up @@ -473,6 +485,7 @@ spec:
- TrimPrefix
- TrimSuffix
- Regexp
- Join
type: string
type: object
type:
Expand Down Expand Up @@ -826,6 +839,18 @@ spec:
string. See https://golang.org/pkg/fmt/ for
details.
type: string
join:
description: Join defines parameters to join
a slice of values to a string.
properties:
separator:
description: Separator defines the character
that should separate the values from each
other in the joined string.
type: string
required:
- separator
type: object
regexp:
description: Extract a match from the input
using a regular expression.
Expand Down Expand Up @@ -856,6 +881,7 @@ spec:
- TrimPrefix
- TrimSuffix
- Regexp
- Join
type: string
type: object
type:
Expand Down Expand Up @@ -1285,6 +1311,18 @@ spec:
string. See https://golang.org/pkg/fmt/ for
details.
type: string
join:
description: Join defines parameters to join
a slice of values to a string.
properties:
separator:
description: Separator defines the character
that should separate the values from each
other in the joined string.
type: string
required:
- separator
type: object
regexp:
description: Extract a match from the input
using a regular expression.
Expand Down Expand Up @@ -1315,6 +1353,7 @@ spec:
- TrimPrefix
- TrimSuffix
- Regexp
- Join
type: string
type: object
type:
Expand Down Expand Up @@ -1902,6 +1941,18 @@ spec:
string. See https://golang.org/pkg/fmt/ for
details.
type: string
join:
description: Join defines parameters to join a
slice of values to a string.
properties:
separator:
description: Separator defines the character
that should separate the values from each
other in the joined string.
type: string
required:
- separator
type: object
regexp:
description: Extract a match from the input using
a regular expression.
Expand Down Expand Up @@ -1932,6 +1983,7 @@ spec:
- TrimPrefix
- TrimSuffix
- Regexp
- Join
type: string
type: object
type:
Expand Down Expand Up @@ -2285,6 +2337,18 @@ spec:
string. See https://golang.org/pkg/fmt/ for
details.
type: string
join:
description: Join defines parameters to join
a slice of values to a string.
properties:
separator:
description: Separator defines the character
that should separate the values from each
other in the joined string.
type: string
required:
- separator
type: object
regexp:
description: Extract a match from the input
using a regular expression.
Expand Down Expand Up @@ -2315,6 +2379,7 @@ spec:
- TrimPrefix
- TrimSuffix
- Regexp
- Join
type: string
type: object
type:
Expand Down Expand Up @@ -2744,6 +2809,18 @@ spec:
string. See https://golang.org/pkg/fmt/ for
details.
type: string
join:
description: Join defines parameters to join
a slice of values to a string.
properties:
separator:
description: Separator defines the character
that should separate the values from each
other in the joined string.
type: string
required:
- separator
type: object
regexp:
description: Extract a match from the input
using a regular expression.
Expand Down Expand Up @@ -2774,6 +2851,7 @@ spec:
- TrimPrefix
- TrimSuffix
- Regexp
- Join
type: string
type: object
type:
Expand Down

0 comments on commit 8d6af61

Please sign in to comment.