Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:added format flag #8080

Merged
merged 4 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/eventing-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,23 @@ For more details: <a href="https://github.com/knative/eventing/issues/5811">http
- <a href="https://en.wikipedia.org/wiki/ISO_8601">https://en.wikipedia.org/wiki/ISO_8601</a></p>
</td>
</tr>
<tr>
<td>
<code>format</code><br/>
<em>
string
</em>
</td>
<td>
<em>(Optional)</em>
<p>format specifies the desired event format for the cloud event.
It can be one of the following values:
- nil: default value, no specific format required.
- &ldquo;JSON&rdquo;: indicates the event should be in structured mode.
- &ldquo;binary&rdquo;: indicates the event should be in binary mode.
- &ldquo;ingress&rdquo;: indicates the event should be in ingress mode.</p>
</td>
</tr>
</tbody>
</table>
<h3 id="duck.knative.dev/v1.DeliveryStatus">DeliveryStatus
Expand Down
21 changes: 21 additions & 0 deletions pkg/apis/duck/v1/delivery_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package v1

import (
"context"
"strings"

"github.com/rickb777/date/period"
"knative.dev/pkg/apis"
Expand Down Expand Up @@ -81,6 +82,15 @@ type DeliverySpec struct {
//
// +optional
RetryAfterMax *string `json:"retryAfterMax,omitempty"`

// format specifies the desired event format for the cloud event.
// It can be one of the following values:
// - 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"`
}

func (ds *DeliverySpec) Validate(ctx context.Context) *apis.FieldError {
Expand Down Expand Up @@ -123,6 +133,17 @@ 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 {
errs = errs.Also(apis.ErrInvalidValue(*ds.Format, "format"))
Copy link
Member

@pierDipi pierDipi Jul 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add the details of the error with the list of valid values (3rd parameter to ErrInvalidValue)

apis.ErrInvalidValue(*ds.Format, "format", <detailed error>)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback! I will make the changes

}
}

if ds.RetryAfterMax != nil {
if feature.FromContext(ctx).IsEnabled(feature.DeliveryRetryAfter) {
p, me := period.Parse(*ds.RetryAfterMax)
Expand Down
22 changes: 21 additions & 1 deletion pkg/apis/duck/v1/delivery_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,27 @@ func TestDeliverySpecValidation(t *testing.T) {
want: func() *apis.FieldError {
return apis.ErrDisallowedFields("retryAfterMax")
}(),
}}
},
{
name: "valid format JSON",
spec: &DeliverySpec{Format: pointer.String("json")},
want: nil,
},
{
name: "vaalid format binary",
spec: &DeliverySpec{Format: pointer.String("binary")},
want: nil,
}, {
name: "valid format ingress",
spec: &DeliverySpec{Format: pointer.String("ingress")},
want: nil,
}, {
name: "invalid format",
spec: &DeliverySpec{Format: pointer.String("invalid")},
want: func() *apis.FieldError {
return apis.ErrInvalidValue("invalid", "format")
}(),
}}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/duck/v1/zz_generated.deepcopy.go

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