diff --git a/docs/eventing-api.md b/docs/eventing-api.md index cefd2e02164..213521267f1 100644 --- a/docs/eventing-api.md +++ b/docs/eventing-api.md @@ -508,7 +508,9 @@ For more details: http
format
string
alias)+(Appears on:DeliverySpec) +
++
FormatType is the type for delivery format
+ +Value | +Description | +
---|---|
"binary" |
++ |
"json" |
++ |
diff --git a/pkg/apis/duck/v1/delivery_types.go b/pkg/apis/duck/v1/delivery_types.go index b70b33d65da..e6ba6d5ece1 100644 --- a/pkg/apis/duck/v1/delivery_types.go +++ b/pkg/apis/duck/v1/delivery_types.go @@ -18,7 +18,6 @@ package v1 import ( "context" - "strings" "github.com/rickb777/date/period" "knative.dev/pkg/apis" @@ -88,9 +87,8 @@ type DeliverySpec struct { // - nil: default value, no specific format required. // - "JSON": indicates the event should be in structured mode. // - "binary": indicates the event should be in binary mode. - // - "ingress": indicates the event should be in ingress mode. //+optional - Format *string `json:"format,omitempty"` + Format *FormatType `json:"format,omitempty"` } func (ds *DeliverySpec) Validate(ctx context.Context) *apis.FieldError { @@ -134,12 +132,10 @@ func (ds *DeliverySpec) Validate(ctx context.Context) *apis.FieldError { } if ds.Format != nil { - validFormats := map[string]bool{ - "json": true, - "binary": true, - "ingress": true, - } - if _, ok := validFormats[strings.ToLower(*ds.Format)]; !ok { + switch *ds.Format { + case DeliveryFormatBinary, DeliveryFormatJson: + // nothing + default: errs = errs.Also(apis.ErrInvalidValue(*ds.Format, "format")) } } @@ -169,6 +165,14 @@ const ( BackoffPolicyExponential BackoffPolicyType = "exponential" ) +// FormatType is the type for delivery format +type FormatType string + +const ( + DeliveryFormatJson FormatType = "json" + DeliveryFormatBinary FormatType = "binary" +) + // DeliveryStatus contains the Status of an object supporting delivery options. This type is intended to be embedded into a status struct. type DeliveryStatus struct { // DeadLetterSink is a KReference that is the reference to the native, platform specific channel diff --git a/pkg/apis/duck/v1/delivery_types_test.go b/pkg/apis/duck/v1/delivery_types_test.go index 9605e21f588..5c8d3355ca6 100644 --- a/pkg/apis/duck/v1/delivery_types_test.go +++ b/pkg/apis/duck/v1/delivery_types_test.go @@ -21,6 +21,7 @@ import ( "github.com/google/go-cmp/cmp" "k8s.io/utils/pointer" + "k8s.io/utils/ptr" "knative.dev/pkg/apis" duckv1 "knative.dev/pkg/apis/duck/v1" @@ -144,20 +145,16 @@ func TestDeliverySpecValidation(t *testing.T) { }, { name: "valid format JSON", - spec: &DeliverySpec{Format: pointer.String("json")}, + spec: &DeliverySpec{Format: ptr.To(DeliveryFormatJson)}, want: nil, }, { name: "vaalid format binary", - spec: &DeliverySpec{Format: pointer.String("binary")}, - want: nil, - }, { - name: "valid format ingress", - spec: &DeliverySpec{Format: pointer.String("ingress")}, + spec: &DeliverySpec{Format: ptr.To(DeliveryFormatBinary)}, want: nil, }, { name: "invalid format", - spec: &DeliverySpec{Format: pointer.String("invalid")}, + spec: &DeliverySpec{Format: ptr.To(FormatType("invalid"))}, want: func() *apis.FieldError { return apis.ErrInvalidValue("invalid", "format") }(), diff --git a/pkg/apis/duck/v1/zz_generated.deepcopy.go b/pkg/apis/duck/v1/zz_generated.deepcopy.go index 313f09afa74..52f6b7bb627 100644 --- a/pkg/apis/duck/v1/zz_generated.deepcopy.go +++ b/pkg/apis/duck/v1/zz_generated.deepcopy.go @@ -203,7 +203,7 @@ func (in *DeliverySpec) DeepCopyInto(out *DeliverySpec) { } if in.Format != nil { in, out := &in.Format, &out.Format - *out = new(string) + *out = new(FormatType) **out = **in } return