From 9ccd339056990f8fb4357e59da54b3f6e4782a86 Mon Sep 17 00:00:00 2001 From: Tim Brunko Date: Wed, 22 Nov 2023 13:44:50 +0300 Subject: [PATCH 1/2] bugfix_value_type_of_dataschema Signed-off-by: Tim Brunko --- binding/format/protobuf/v2/protobuf.go | 11 ++++++++--- binding/format/protobuf/v2/protobuf_test.go | 6 ++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/binding/format/protobuf/v2/protobuf.go b/binding/format/protobuf/v2/protobuf.go index e29dababe..2e76773e6 100644 --- a/binding/format/protobuf/v2/protobuf.go +++ b/binding/format/protobuf/v2/protobuf.go @@ -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: %s", dataSchemaStr, err) + } + container.Attributes[dataschema], _ = attributeFor(uri) } if e.Subject() != "" { container.Attributes[subject], _ = attributeFor(e.Subject()) @@ -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) + e.SetDataSchema(vs.String()) case subject: vs, _ := v.(string) e.SetSubject(vs) diff --git a/binding/format/protobuf/v2/protobuf_test.go b/binding/format/protobuf/v2/protobuf_test.go index 3b76ae3e5..0c7dbf87b 100644 --- a/binding/format/protobuf/v2/protobuf_test.go +++ b/binding/format/protobuf/v2/protobuf_test.go @@ -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: "link"}}, "extra1": {Attr: &pb.CloudEventAttributeValue_CeString{CeString: "extra1 value"}}, "extra2": {Attr: &pb.CloudEventAttributeValue_CeInteger{CeInteger: 2}}, "extra3": {Attr: &pb.CloudEventAttributeValue_CeBoolean{CeBoolean: true}}, @@ -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("link") out.SetExtension("extra1", "extra1 value") out.SetExtension("extra2", 2) out.SetExtension("extra3", true) @@ -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: "link"}}, "extra1": {Attr: &pb.CloudEventAttributeValue_CeString{CeString: "extra1 value"}}, "extra2": {Attr: &pb.CloudEventAttributeValue_CeInteger{CeInteger: 2}}, "extra3": {Attr: &pb.CloudEventAttributeValue_CeBoolean{CeBoolean: true}}, @@ -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("link") out.SetExtension("extra1", "extra1 value") out.SetExtension("extra2", 2) out.SetExtension("extra3", true) @@ -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: "link"}}, "extra1": {Attr: &pb.CloudEventAttributeValue_CeString{CeString: "extra1 value"}}, "extra2": {Attr: &pb.CloudEventAttributeValue_CeInteger{CeInteger: 2}}, "extra3": {Attr: &pb.CloudEventAttributeValue_CeBoolean{CeBoolean: true}}, @@ -186,6 +191,7 @@ func TestFromProto(t *testing.T) { out.SetSource("/source") out.SetType("some.type") _ = out.SetData("application/json", `{"unit":"test"}`) + out.SetDataSchema("link") out.SetExtension("extra1", "extra1 value") out.SetExtension("extra2", 2) out.SetExtension("extra3", true) From 41e80f74b16532322a1f38cb4df7424c4c6bbc99 Mon Sep 17 00:00:00 2001 From: Tim Brunko Date: Tue, 12 Dec 2023 17:59:15 +0300 Subject: [PATCH 2/2] fixed couple issues Signed-off-by: Tim Brunko --- binding/format/protobuf/v2/protobuf.go | 2 +- binding/format/protobuf/v2/protobuf_test.go | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/binding/format/protobuf/v2/protobuf.go b/binding/format/protobuf/v2/protobuf.go index 2e76773e6..4b8c2544b 100644 --- a/binding/format/protobuf/v2/protobuf.go +++ b/binding/format/protobuf/v2/protobuf.go @@ -91,7 +91,7 @@ func ToProto(e *event.Event) (*pb.CloudEvent, error) { dataSchemaStr := e.DataSchema() uri, err := url.Parse(dataSchemaStr) if err != nil { - return nil, fmt.Errorf("failed to url.Parse %s: %s", dataSchemaStr, err) + return nil, fmt.Errorf("failed to url.Parse %s: %w", dataSchemaStr, err) } container.Attributes[dataschema], _ = attributeFor(uri) } diff --git a/binding/format/protobuf/v2/protobuf_test.go b/binding/format/protobuf/v2/protobuf_test.go index 0c7dbf87b..3099bc4ac 100644 --- a/binding/format/protobuf/v2/protobuf_test.go +++ b/binding/format/protobuf/v2/protobuf_test.go @@ -109,7 +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: "link"}}, + "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}}, @@ -125,7 +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("link") + out.SetDataSchema("https://example-data-schema.com") out.SetExtension("extra1", "extra1 value") out.SetExtension("extra2", 2) out.SetExtension("extra3", true) @@ -142,7 +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: "link"}}, + "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}}, @@ -158,7 +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("link") + out.SetDataSchema("https://example-data-schema.com") out.SetExtension("extra1", "extra1 value") out.SetExtension("extra2", 2) out.SetExtension("extra3", true) @@ -175,7 +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: "link"}}, + "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}}, @@ -191,7 +191,7 @@ func TestFromProto(t *testing.T) { out.SetSource("/source") out.SetType("some.type") _ = out.SetData("application/json", `{"unit":"test"}`) - out.SetDataSchema("link") + out.SetDataSchema("https://example-data-schema.com") out.SetExtension("extra1", "extra1 value") out.SetExtension("extra2", 2) out.SetExtension("extra3", true)