From bbb0656ca283b09d6f542827f9087ce0d0e32f17 Mon Sep 17 00:00:00 2001 From: stewartboyd119 Date: Mon, 23 Sep 2024 20:59:50 -0700 Subject: [PATCH] Simplified schemas --- test/evolution/avro1/schema_1_gen.go | 11 ++-- test/evolution/avro1x/schema_1_gen.go | 88 +++++++++++++-------------- test/evolution/avro2/schema_2_gen.go | 16 ++--- test/evolution/schema_1.avsc | 22 +++---- test/evolution/schema_2.avsc | 23 +++---- test/schema_registry_evo_test.go | 12 ++-- test/schema_registry_test.go | 14 ++--- 7 files changed, 82 insertions(+), 104 deletions(-) diff --git a/test/evolution/avro1/schema_1_gen.go b/test/evolution/avro1/schema_1_gen.go index c1a9995..f26338b 100644 --- a/test/evolution/avro1/schema_1_gen.go +++ b/test/evolution/avro1/schema_1_gen.go @@ -6,12 +6,9 @@ import ( "time" ) -// AryeoListingRecord is a generated struct. -type AryeoListingRecord struct { - // Id(uuid v7) of the underlying Aryeo Listing. - ID string `avro:"id"` - // Timestamp depicting when Aryeo's listing was delivered. +// Event is a generated struct. +type Event struct { + ID string `avro:"id"` DeliveredAtDateTimeUtc time.Time `avro:"deliveredAtDateTimeUtc"` - // Enum depicting type of event. - EventType string `avro:"eventType"` + EventType string `avro:"eventType"` } diff --git a/test/evolution/avro1x/schema_1_gen.go b/test/evolution/avro1x/schema_1_gen.go index e5a4641..e97ec5f 100644 --- a/test/evolution/avro1x/schema_1_gen.go +++ b/test/evolution/avro1x/schema_1_gen.go @@ -4,73 +4,67 @@ package avro1 import ( "fmt" - "github.com/heetch/avro/avrotypegen" "strconv" + + "github.com/heetch/avro/avrotypegen" ) -type AryeoListingEventType int +type Event struct { + Id string `json:"id"` + Deliveredatdatetimeutc int64 `json:"deliveredAtDateTimeUtc"` + Eventtype EventType `json:"eventType"` +} + +// AvroRecord implements the avro.AvroRecord interface. +func (Event) AvroRecord() avrotypegen.RecordInfo { + return avrotypegen.RecordInfo{ + Schema: `{"fields":[{"name":"id","type":"string"},{"name":"deliveredAtDateTimeUtc","type":{"logicalType":"timestamp-millis","type":"long"}},{"name":"eventType","type":{"default":"created","name":"EventType","symbols":["created","associated"],"type":"enum"}}],"name":"com.zillowgroup.Event","type":"record"}`, + Required: []bool{ + 0: true, + 1: true, + 2: true, + }, + } +} + +type EventType int const ( - AryeoListingEventTypeListingcreated AryeoListingEventType = iota - AryeoListingEventTypeListingassociated - AryeoListingEventTypeListingmedidelivered + EventTypeCreated EventType = iota + EventTypeAssociated ) -var _AryeoListingEventType_strings = []string{ - "listingCreated", - "listingAssociated", - "listingMediDelivered", +var _EventType_strings = []string{ + "created", + "associated", } -// String returns the textual representation of AryeoListingEventType. -func (e AryeoListingEventType) String() string { - if e < 0 || int(e) >= len(_AryeoListingEventType_strings) { - return "AryeoListingEventType(" + strconv.FormatInt(int64(e), 10) + ")" +// String returns the textual representation of EventType. +func (e EventType) String() string { + if e < 0 || int(e) >= len(_EventType_strings) { + return "EventType(" + strconv.FormatInt(int64(e), 10) + ")" } - return _AryeoListingEventType_strings[e] + return _EventType_strings[e] } // MarshalText implements encoding.TextMarshaler -// by returning the textual representation of AryeoListingEventType. -func (e AryeoListingEventType) MarshalText() ([]byte, error) { - if e < 0 || int(e) >= len(_AryeoListingEventType_strings) { - return nil, fmt.Errorf("AryeoListingEventType value %d is out of bounds", e) +// by returning the textual representation of EventType. +func (e EventType) MarshalText() ([]byte, error) { + if e < 0 || int(e) >= len(_EventType_strings) { + return nil, fmt.Errorf("EventType value %d is out of bounds", e) } - return []byte(_AryeoListingEventType_strings[e]), nil + return []byte(_EventType_strings[e]), nil } // UnmarshalText implements encoding.TextUnmarshaler -// by expecting the textual representation of AryeoListingEventType. -func (e *AryeoListingEventType) UnmarshalText(data []byte) error { +// by expecting the textual representation of EventType. +func (e *EventType) UnmarshalText(data []byte) error { // Note for future: this could be more efficient. - for i, s := range _AryeoListingEventType_strings { + for i, s := range _EventType_strings { if string(data) == s { - *e = AryeoListingEventType(i) + *e = EventType(i) return nil } } - return fmt.Errorf("unknown value %q for AryeoListingEventType", data) -} - -type AryeoListingRecord struct { - // Id(uuid v7) of the underlying Aryeo Listing - Id string `json:"id"` - - // Timestamp depicting when Aryeo's listing was delivered - Deliveredatdatetimeutc int64 `json:"deliveredAtDateTimeUtc"` - - // Enum depicting type of event - Eventtype AryeoListingEventType `json:"eventType"` -} - -// AvroRecord implements the avro.AvroRecord interface. -func (AryeoListingRecord) AvroRecord() avrotypegen.RecordInfo { - return avrotypegen.RecordInfo{ - Schema: `{"fields":[{"doc":"Id(uuid v7) of the underlying Aryeo Listing","name":"id","type":"string"},{"doc":"Timestamp depicting when Aryeo's listing was delivered","name":"deliveredAtDateTimeUtc","type":{"logicalType":"timestamp-millis","type":"long"}},{"doc":"Enum depicting type of event","name":"eventType","type":{"default":"listingCreated","name":"AryeoListingEventType","symbols":["listingCreated","listingAssociated","listingMediDelivered"],"type":"enum"}}],"name":"com.zillowgroup.rmx.pem_schema.AryeoListingRecord","type":"record"}`, - Required: []bool{ - 0: true, - 1: true, - 2: true, - }, - } + return fmt.Errorf("unknown value %q for EventType", data) } diff --git a/test/evolution/avro2/schema_2_gen.go b/test/evolution/avro2/schema_2_gen.go index a8298e8..3a3e6d8 100644 --- a/test/evolution/avro2/schema_2_gen.go +++ b/test/evolution/avro2/schema_2_gen.go @@ -11,14 +11,10 @@ type InteractiveContentRecord struct { URL string `avro:"url"` } -// AryeoListingRecord is a generated struct. -type AryeoListingRecord struct { - // Id(uuid v7) of the underlying Aryeo Listing. - ID string `avro:"id"` - // Timestamp depicting when Aryeo's listing was delivered. - DeliveredAtDateTimeUtc time.Time `avro:"deliveredAtDateTimeUtc"` - // Enum depicting type of event. - EventType string `avro:"eventType"` - // InteractiveContent associated to Aryeo Listing. - InteractiveContent *[]InteractiveContentRecord `avro:"interactiveContent"` +// Event is a generated struct. +type Event struct { + ID string `avro:"id"` + DeliveredAtDateTimeUtc time.Time `avro:"deliveredAtDateTimeUtc"` + EventType string `avro:"eventType"` + InteractiveContent *[]InteractiveContentRecord `avro:"interactiveContent"` } diff --git a/test/evolution/schema_1.avsc b/test/evolution/schema_1.avsc index 67d78ab..9788647 100644 --- a/test/evolution/schema_1.avsc +++ b/test/evolution/schema_1.avsc @@ -1,34 +1,30 @@ { "type": "record", - "name": "AryeoListingRecord", - "namespace": "com.zillowgroup.rmx.pem_schema", + "name": "Event", + "namespace": "com.zillowgroup", "fields": [ { "name": "id", - "type": "string", - "doc": "Id(uuid v7) of the underlying Aryeo Listing" + "type": "string" }, { "name": "deliveredAtDateTimeUtc", "type": { "type": "long", "logicalType": "timestamp-millis" - }, - "doc": "Timestamp depicting when Aryeo's listing was delivered" + } }, { "name": "eventType", "type": { "type": "enum", - "name": "AryeoListingEventType", + "name": "EventType", "symbols": [ - "listingCreated", - "listingAssociated", - "listingMediDelivered" + "created", + "associated" ], - "default": "listingCreated" - }, - "doc": "Enum depicting type of event" + "default": "created" + } } ] } \ No newline at end of file diff --git a/test/evolution/schema_2.avsc b/test/evolution/schema_2.avsc index 0a51671..1e765d3 100644 --- a/test/evolution/schema_2.avsc +++ b/test/evolution/schema_2.avsc @@ -1,34 +1,30 @@ { "type": "record", - "name": "AryeoListingRecord", - "namespace": "com.zillowgroup.rmx.pem_schema", + "name": "Event", + "namespace": "com.zillowgroup", "fields": [ { "name": "id", - "type": "string", - "doc": "Id(uuid v7) of the underlying Aryeo Listing" + "type": "string" }, { "name": "deliveredAtDateTimeUtc", "type": { "type": "long", "logicalType": "timestamp-millis" - }, - "doc": "Timestamp depicting when Aryeo's listing was delivered" + } }, { "name": "eventType", "type": { "type": "enum", - "name": "AryeoListingEventType", + "name": "EventType", "symbols": [ - "listingCreated", - "listingAssociated", - "listingMediDelivered" + "created", + "associated" ], - "default": "listingCreated" - }, - "doc": "Enum depicting type of event" + "default": "created" + } }, { "name": "interactiveContent", @@ -48,7 +44,6 @@ } } ], - "doc": "InteractiveContent associated to Aryeo Listing", "default": null } ] diff --git a/test/schema_registry_evo_test.go b/test/schema_registry_evo_test.go index e652c13..d9c1943 100644 --- a/test/schema_registry_evo_test.go +++ b/test/schema_registry_evo_test.go @@ -86,7 +86,7 @@ func Test_SchemaRegistryReal_Avro_AutoRegisterSchemas_BackwardCompatibleSchemasC require.NoError(t, err) listingID := uuid.NewString() - evt1 := avro1.AryeoListingRecord{ + evt1 := avro1.Event{ ID: listingID, DeliveredAtDateTimeUtc: time.Now().UTC().Truncate(time.Millisecond), EventType: "listingCreated", @@ -97,7 +97,7 @@ func Test_SchemaRegistryReal_Avro_AutoRegisterSchemas_BackwardCompatibleSchemasC listingID2 := uuid.NewString() - evt2 := avro2.AryeoListingRecord{ + evt2 := avro2.Event{ ID: listingID2, DeliveredAtDateTimeUtc: time.Now().UTC().Truncate(time.Millisecond), EventType: "listingCreated", @@ -131,20 +131,20 @@ func Test_SchemaRegistryReal_Avro_AutoRegisterSchemas_BackwardCompatibleSchemasC require.NoError(t, reader.Close()) - receivedEvt1 := avro1.AryeoListingRecord{} + receivedEvt1 := avro1.Event{} require.NoError(t, msg1.Decode(&receivedEvt1)) assertEqual(t, receivedEvt1, evt1) - receivedEvt2Schema1 := avro1.AryeoListingRecord{} + receivedEvt2Schema1 := avro1.Event{} require.NoError(t, msg2.Decode(&receivedEvt2Schema1)) - expectedEvt2 := avro1.AryeoListingRecord{ + expectedEvt2 := avro1.Event{ ID: evt2.ID, DeliveredAtDateTimeUtc: evt2.DeliveredAtDateTimeUtc, EventType: evt2.EventType, } assertEqual(t, expectedEvt2, receivedEvt2Schema1) - receivedEvt2Schema2 := avro2.AryeoListingRecord{} + receivedEvt2Schema2 := avro2.Event{} require.NoError(t, msg2.Decode(&receivedEvt2Schema2)) assertEqual(t, evt2, receivedEvt2Schema2) } diff --git a/test/schema_registry_test.go b/test/schema_registry_test.go index be17781..fe1dca8 100644 --- a/test/schema_registry_test.go +++ b/test/schema_registry_test.go @@ -59,7 +59,7 @@ func Test_SchemaRegistry_AutoRegisterSchemasFalse_WillNotWriteMessage(t *testing listingID := uuid.NewString() - evt1 := avro1.AryeoListingRecord{ + evt1 := avro1.Event{ ID: listingID, DeliveredAtDateTimeUtc: time.Now().UTC().Truncate(time.Millisecond), EventType: "listingCreated", @@ -103,7 +103,7 @@ func Test_SchemaRegistry_Avro_AutoRegisterSchemas_RequiresSchemaSpecification(t listingID := uuid.NewString() - evt1 := avro1.AryeoListingRecord{ + evt1 := avro1.Event{ ID: listingID, DeliveredAtDateTimeUtc: time.Now().UTC().Truncate(time.Millisecond), EventType: "listingCreated", @@ -145,10 +145,10 @@ func Test_SchemaNotRegistered_ResultsInWorkerDecodeError(t *testing.T) { listingID := uuid.NewString() - evt1 := avro1x.AryeoListingRecord{ + evt1 := avro1x.Event{ Id: listingID, Deliveredatdatetimeutc: rand.Int63(), - Eventtype: avro1x.AryeoListingEventTypeListingcreated, + Eventtype: avro1x.EventTypeCreated, } _, err = writer1.Write(ctx, evt1) @@ -173,7 +173,7 @@ func Test_SchemaNotRegistered_ResultsInWorkerDecodeError(t *testing.T) { wf := zkafka.NewWorkFactory(client) w := wf.CreateWithFunc(consumerTopicConfig, func(_ context.Context, msg *zkafka.Message) error { defer cancel() - gotErr = msg.Decode(&avro1.AryeoListingRecord{}) + gotErr = msg.Decode(&avro1.Event{}) return gotErr }) @@ -217,7 +217,7 @@ func Test_SchemaRegistry_Avro_SubjectNameSpecification(t *testing.T) { listingID := uuid.NewString() - evt1 := avro1.AryeoListingRecord{ + evt1 := avro1.Event{ ID: listingID, DeliveredAtDateTimeUtc: time.Now().UTC().Truncate(time.Millisecond), EventType: "listingCreated", @@ -255,7 +255,7 @@ func Test_SchemaRegistry_Avro_SubjectNameSpecification(t *testing.T) { require.NoError(t, reader.Close()) - receivedEvt1 := avro1.AryeoListingRecord{} + receivedEvt1 := avro1.Event{} require.NoError(t, msg1.Decode(&receivedEvt1)) assertEqual(t, evt1, receivedEvt1) }