Skip to content

Commit

Permalink
Add support for convert from json to object/list
Browse files Browse the repository at this point in the history
Signed-off-by: Hasan Turken <turkenh@gmail.com>
  • Loading branch information
turkenh committed Aug 29, 2023
1 parent 51016dd commit 0339892
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 27 deletions.
12 changes: 8 additions & 4 deletions apis/apiextensions/v1/composition_transforms.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,12 +448,13 @@ const (
TransformIOTypeFloat64 TransformIOType = "float64"

TransformIOTypeObject TransformIOType = "object"
TransformIOTypeArray TransformIOType = "array"
)

// IsValid checks if the given TransformIOType is valid.
func (c TransformIOType) IsValid() bool {
switch c {
case TransformIOTypeString, TransformIOTypeBool, TransformIOTypeInt, TransformIOTypeInt64, TransformIOTypeFloat64, TransformIOTypeObject:
case TransformIOTypeString, TransformIOTypeBool, TransformIOTypeInt, TransformIOTypeInt64, TransformIOTypeFloat64, TransformIOTypeObject, TransformIOTypeArray:
return true
}
return false
Expand All @@ -467,12 +468,13 @@ type ConvertTransformFormat string
const (
ConvertTransformFormatNone ConvertTransformFormat = "none"
ConvertTransformFormatQuantity ConvertTransformFormat = "quantity"
ConvertTransformFormatJSON ConvertTransformFormat = "json"
)

// IsValid returns true if the format is valid.
func (c ConvertTransformFormat) IsValid() bool {
switch c {
case ConvertTransformFormatNone, ConvertTransformFormatQuantity:
case ConvertTransformFormatNone, ConvertTransformFormatQuantity, ConvertTransformFormatJSON:
return true
}
return false
Expand All @@ -481,17 +483,19 @@ func (c ConvertTransformFormat) IsValid() bool {
// 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.
// +kubebuilder:validation:Enum=string;int;int64;bool;float64
// +kubebuilder:validation:Enum=string;int;int64;bool;float64;object;list
ToType TransformIOType `json:"toType"`

// The expected input format.
//
// * `quantity` - parses the input as a K8s [`resource.Quantity`](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity).
// Only used during `string -> float64` conversions.
// * `json` - parses the input as a JSON string.
// Only used during `string -> object` or `string -> list` conversions.
//
// If this property is null, the default conversion is applied.
//
// +kubebuilder:validation:Enum=none;quantity
// +kubebuilder:validation:Enum=none;quantity;json
// +kubebuilder:validation:Default=none
Format *ConvertTransformFormat `json:"format,omitempty"`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,12 +450,13 @@ const (
TransformIOTypeFloat64 TransformIOType = "float64"

TransformIOTypeObject TransformIOType = "object"
TransformIOTypeArray TransformIOType = "array"
)

// IsValid checks if the given TransformIOType is valid.
func (c TransformIOType) IsValid() bool {
switch c {
case TransformIOTypeString, TransformIOTypeBool, TransformIOTypeInt, TransformIOTypeInt64, TransformIOTypeFloat64, TransformIOTypeObject:
case TransformIOTypeString, TransformIOTypeBool, TransformIOTypeInt, TransformIOTypeInt64, TransformIOTypeFloat64, TransformIOTypeObject, TransformIOTypeArray:
return true
}
return false
Expand All @@ -469,12 +470,13 @@ type ConvertTransformFormat string
const (
ConvertTransformFormatNone ConvertTransformFormat = "none"
ConvertTransformFormatQuantity ConvertTransformFormat = "quantity"
ConvertTransformFormatJSON ConvertTransformFormat = "json"
)

// IsValid returns true if the format is valid.
func (c ConvertTransformFormat) IsValid() bool {
switch c {
case ConvertTransformFormatNone, ConvertTransformFormatQuantity:
case ConvertTransformFormatNone, ConvertTransformFormatQuantity, ConvertTransformFormatJSON:
return true
}
return false
Expand All @@ -483,17 +485,19 @@ func (c ConvertTransformFormat) IsValid() bool {
// 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.
// +kubebuilder:validation:Enum=string;int;int64;bool;float64
// +kubebuilder:validation:Enum=string;int;int64;bool;float64;object;list
ToType TransformIOType `json:"toType"`

// The expected input format.
//
// * `quantity` - parses the input as a K8s [`resource.Quantity`](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity).
// Only used during `string -> float64` conversions.
// * `json` - parses the input as a JSON string.
// Only used during `string -> object` or `string -> list` conversions.
//
// If this property is null, the default conversion is applied.
//
// +kubebuilder:validation:Enum=none;quantity
// +kubebuilder:validation:Enum=none;quantity;json
// +kubebuilder:validation:Default=none
Format *ConvertTransformFormat `json:"format,omitempty"`
}
Expand Down
54 changes: 42 additions & 12 deletions cluster/crds/apiextensions.crossplane.io_compositionrevisions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,14 @@ spec:
description: "The expected input format. \n *
`quantity` - parses the input as a K8s [`resource.Quantity`](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity).
Only used during `string -> float64` conversions.
\n If this property is null, the default conversion
is applied."
* `json` - parses the input as a JSON string.
Only used during `string -> object` or `string
-> list` conversions. \n If this property is
null, the default conversion is applied."
enum:
- none
- quantity
- json
type: string
toType:
description: ToType is the type of the output
Expand All @@ -279,6 +282,8 @@ spec:
- int64
- bool
- float64
- object
- list
type: string
required:
- toType
Expand Down Expand Up @@ -748,11 +753,14 @@ spec:
description: "The expected input format. \n
* `quantity` - parses the input as a K8s [`resource.Quantity`](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity).
Only used during `string -> float64` conversions.
\n If this property is null, the default conversion
is applied."
* `json` - parses the input as a JSON string.
Only used during `string -> object` or `string
-> list` conversions. \n If this property
is null, the default conversion is applied."
enum:
- none
- quantity
- json
type: string
toType:
description: ToType is the type of the output
Expand All @@ -763,6 +771,8 @@ spec:
- int64
- bool
- float64
- object
- list
type: string
required:
- toType
Expand Down Expand Up @@ -1163,11 +1173,14 @@ spec:
description: "The expected input format. \n
* `quantity` - parses the input as a K8s [`resource.Quantity`](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity).
Only used during `string -> float64` conversions.
\n If this property is null, the default conversion
is applied."
* `json` - parses the input as a JSON string.
Only used during `string -> object` or `string
-> list` conversions. \n If this property
is null, the default conversion is applied."
enum:
- none
- quantity
- json
type: string
toType:
description: ToType is the type of the output
Expand All @@ -1178,6 +1191,8 @@ spec:
- int64
- bool
- float64
- object
- list
type: string
required:
- toType
Expand Down Expand Up @@ -1744,11 +1759,14 @@ spec:
description: "The expected input format. \n *
`quantity` - parses the input as a K8s [`resource.Quantity`](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity).
Only used during `string -> float64` conversions.
\n If this property is null, the default conversion
is applied."
* `json` - parses the input as a JSON string.
Only used during `string -> object` or `string
-> list` conversions. \n If this property is
null, the default conversion is applied."
enum:
- none
- quantity
- json
type: string
toType:
description: ToType is the type of the output
Expand All @@ -1759,6 +1777,8 @@ spec:
- int64
- bool
- float64
- object
- list
type: string
required:
- toType
Expand Down Expand Up @@ -2228,11 +2248,14 @@ spec:
description: "The expected input format. \n
* `quantity` - parses the input as a K8s [`resource.Quantity`](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity).
Only used during `string -> float64` conversions.
\n If this property is null, the default conversion
is applied."
* `json` - parses the input as a JSON string.
Only used during `string -> object` or `string
-> list` conversions. \n If this property
is null, the default conversion is applied."
enum:
- none
- quantity
- json
type: string
toType:
description: ToType is the type of the output
Expand All @@ -2243,6 +2266,8 @@ spec:
- int64
- bool
- float64
- object
- list
type: string
required:
- toType
Expand Down Expand Up @@ -2643,11 +2668,14 @@ spec:
description: "The expected input format. \n
* `quantity` - parses the input as a K8s [`resource.Quantity`](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity).
Only used during `string -> float64` conversions.
\n If this property is null, the default conversion
is applied."
* `json` - parses the input as a JSON string.
Only used during `string -> object` or `string
-> list` conversions. \n If this property
is null, the default conversion is applied."
enum:
- none
- quantity
- json
type: string
toType:
description: ToType is the type of the output
Expand All @@ -2658,6 +2686,8 @@ spec:
- int64
- bool
- float64
- object
- list
type: string
required:
- toType
Expand Down
27 changes: 21 additions & 6 deletions cluster/crds/apiextensions.crossplane.io_compositions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,14 @@ spec:
description: "The expected input format. \n *
`quantity` - parses the input as a K8s [`resource.Quantity`](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity).
Only used during `string -> float64` conversions.
\n If this property is null, the default conversion
is applied."
* `json` - parses the input as a JSON string.
Only used during `string -> object` or `string
-> list` conversions. \n If this property is
null, the default conversion is applied."
enum:
- none
- quantity
- json
type: string
toType:
description: ToType is the type of the output
Expand All @@ -276,6 +279,8 @@ spec:
- int64
- bool
- float64
- object
- list
type: string
required:
- toType
Expand Down Expand Up @@ -748,11 +753,14 @@ spec:
description: "The expected input format. \n
* `quantity` - parses the input as a K8s [`resource.Quantity`](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity).
Only used during `string -> float64` conversions.
\n If this property is null, the default conversion
is applied."
* `json` - parses the input as a JSON string.
Only used during `string -> object` or `string
-> list` conversions. \n If this property
is null, the default conversion is applied."
enum:
- none
- quantity
- json
type: string
toType:
description: ToType is the type of the output
Expand All @@ -763,6 +771,8 @@ spec:
- int64
- bool
- float64
- object
- list
type: string
required:
- toType
Expand Down Expand Up @@ -1167,11 +1177,14 @@ spec:
description: "The expected input format. \n
* `quantity` - parses the input as a K8s [`resource.Quantity`](https://pkg.go.dev/k8s.io/apimachinery/pkg/api/resource#Quantity).
Only used during `string -> float64` conversions.
\n If this property is null, the default conversion
is applied."
* `json` - parses the input as a JSON string.
Only used during `string -> object` or `string
-> list` conversions. \n If this property
is null, the default conversion is applied."
enum:
- none
- quantity
- json
type: string
toType:
description: ToType is the type of the output
Expand All @@ -1182,6 +1195,8 @@ spec:
- int64
- bool
- float64
- object
- list
type: string
required:
- toType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,4 +488,12 @@ var conversions = map[conversionPair]func(any) (any, error){
{from: v1.TransformIOTypeFloat64, to: v1.TransformIOTypeBool, format: v1.ConvertTransformFormatNone}: func(i any) (any, error) { //nolint:unparam // See note above.
return i.(float64) == float64(1), nil
},
{from: v1.TransformIOTypeString, to: v1.TransformIOTypeObject, format: v1.ConvertTransformFormatJSON}: func(i any) (any, error) {
o := map[string]any{}
return o, json.Unmarshal([]byte(i.(string)), &o)
},
{from: v1.TransformIOTypeString, to: v1.TransformIOTypeArray, format: v1.ConvertTransformFormatJSON}: func(i any) (any, error) {
var o []any
return o, json.Unmarshal([]byte(i.(string)), &o)
},
}
Loading

0 comments on commit 0339892

Please sign in to comment.