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

bugfix_value_type_of_dataschema #986

Merged
merged 2 commits into from
Dec 16, 2023
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
11 changes: 8 additions & 3 deletions binding/format/protobuf/v2/protobuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ func ToProto(e *event.Event) (*pb.CloudEvent, error) {
container.Attributes[datacontenttype], _ = attributeFor(e.DataContentType())
}
if e.DataSchema() != "" {
container.Attributes[dataschema], _ = attributeFor(e.DataSchema())
dataSchemaStr := e.DataSchema()
uri, err := url.Parse(dataSchemaStr)
if err != nil {
return nil, fmt.Errorf("failed to url.Parse %s: %w", dataSchemaStr, err)
}
container.Attributes[dataschema], _ = attributeFor(uri)
}
if e.Subject() != "" {
container.Attributes[subject], _ = attributeFor(e.Subject())
Expand Down Expand Up @@ -251,8 +256,8 @@ func FromProto(container *pb.CloudEvent) (*event.Event, error) {
vs, _ := v.(string)
e.SetDataContentType(vs)
case dataschema:
vs, _ := v.(string)
e.SetDataSchema(vs)
vs, _ := v.(types.URI)
Copy link
Member

Choose a reason for hiding this comment

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

question: assuming this is guaranteed to be of type types.URI here due to your URI check earlier (no panic)?

Copy link
Contributor Author

@timmyb32r timmyb32r Dec 12, 2023

Choose a reason for hiding this comment

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

good question. I've checked:
vs, _ := v.(types.URI) will return default constructed types.URI{}, and further called mechod .String() will return an empty string - so, it won't panic

e.SetDataSchema(vs.String())
case subject:
vs, _ := v.(string)
e.SetSubject(vs)
Expand Down
6 changes: 6 additions & 0 deletions binding/format/protobuf/v2/protobuf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func TestFromProto(t *testing.T) {
Type: "some.type",
Attributes: map[string]*pb.CloudEventAttributeValue{
"datacontenttype": {Attr: &pb.CloudEventAttributeValue_CeString{CeString: "application/json"}},
"dataschema": {Attr: &pb.CloudEventAttributeValue_CeUri{CeUri: "https://example-data-schema.com"}},
"extra1": {Attr: &pb.CloudEventAttributeValue_CeString{CeString: "extra1 value"}},
"extra2": {Attr: &pb.CloudEventAttributeValue_CeInteger{CeInteger: 2}},
"extra3": {Attr: &pb.CloudEventAttributeValue_CeBoolean{CeBoolean: true}},
Expand All @@ -124,6 +125,7 @@ func TestFromProto(t *testing.T) {
out.SetSource("/source")
out.SetType("some.type")
_ = out.SetData("application/json", map[string]interface{}{"unit": "test"})
out.SetDataSchema("https://example-data-schema.com")
out.SetExtension("extra1", "extra1 value")
out.SetExtension("extra2", 2)
out.SetExtension("extra3", true)
Expand All @@ -140,6 +142,7 @@ func TestFromProto(t *testing.T) {
Type: "some.type",
Attributes: map[string]*pb.CloudEventAttributeValue{
"datacontenttype": {Attr: &pb.CloudEventAttributeValue_CeString{CeString: "text/plain"}},
"dataschema": {Attr: &pb.CloudEventAttributeValue_CeUri{CeUri: "https://example-data-schema.com"}},
"extra1": {Attr: &pb.CloudEventAttributeValue_CeString{CeString: "extra1 value"}},
"extra2": {Attr: &pb.CloudEventAttributeValue_CeInteger{CeInteger: 2}},
"extra3": {Attr: &pb.CloudEventAttributeValue_CeBoolean{CeBoolean: true}},
Expand All @@ -155,6 +158,7 @@ func TestFromProto(t *testing.T) {
out.SetSource("/source")
out.SetType("some.type")
_ = out.SetData("text/plain", `this is some text with a "quote"`)
out.SetDataSchema("https://example-data-schema.com")
out.SetExtension("extra1", "extra1 value")
out.SetExtension("extra2", 2)
out.SetExtension("extra3", true)
Expand All @@ -171,6 +175,7 @@ func TestFromProto(t *testing.T) {
Type: "some.type",
Attributes: map[string]*pb.CloudEventAttributeValue{
"datacontenttype": {Attr: &pb.CloudEventAttributeValue_CeString{CeString: "application/json"}},
"dataschema": {Attr: &pb.CloudEventAttributeValue_CeUri{CeUri: "https://example-data-schema.com"}},
"extra1": {Attr: &pb.CloudEventAttributeValue_CeString{CeString: "extra1 value"}},
"extra2": {Attr: &pb.CloudEventAttributeValue_CeInteger{CeInteger: 2}},
"extra3": {Attr: &pb.CloudEventAttributeValue_CeBoolean{CeBoolean: true}},
Expand All @@ -186,6 +191,7 @@ func TestFromProto(t *testing.T) {
out.SetSource("/source")
out.SetType("some.type")
_ = out.SetData("application/json", `{"unit":"test"}`)
out.SetDataSchema("https://example-data-schema.com")
out.SetExtension("extra1", "extra1 value")
out.SetExtension("extra2", 2)
out.SetExtension("extra3", true)
Expand Down