From fbf1147945cf893f53fd81715f9dd24af2fa5454 Mon Sep 17 00:00:00 2001 From: Robin Tang Date: Mon, 23 Sep 2024 08:32:53 -0700 Subject: [PATCH] Refactor. --- models/event/event.go | 23 +++++++++++++---------- models/event/event_test.go | 12 ++++++------ 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/models/event/event.go b/models/event/event.go index 2b8bf293b..c25cfb8fb 100644 --- a/models/event/event.go +++ b/models/event/event.go @@ -2,7 +2,6 @@ package event import ( "cmp" - "errors" "fmt" "log/slog" "sort" @@ -99,27 +98,31 @@ func ToMemoryEvent(event cdc.Event, pkMap map[string]any, tc kafkalib.TopicConfi }, nil } -func (e *Event) IsValid() bool { +func (e *Event) Validate() error { // Does it have a PK or table set? if stringutil.Empty(e.Table) { - return false + return fmt.Errorf("table name is empty") } if len(e.PrimaryKeyMap) == 0 { - return false + return fmt.Errorf("primary key map is empty") } if len(e.Data) == 0 { - return false + return fmt.Errorf("event has no data") } if e.mode == config.History { // History mode does not have the delete column marker. - return true + return nil } + // Check if delete flag exists. - _, isOk := e.Data[constants.DeleteColumnMarker] - return isOk + if _, isOk := e.Data[constants.DeleteColumnMarker]; !isOk { + return fmt.Errorf("delete column marker does not exist") + } + + return nil } // PrimaryKeys is returned in a sorted manner to be safe. @@ -148,8 +151,8 @@ func (e *Event) PrimaryKeyValue() string { // Save will save the event into our in memory event // It will return (flush bool, flushReason string, err error) func (e *Event) Save(cfg config.Config, inMemDB *models.DatabaseData, tc kafkalib.TopicConfig, message artie.Message) (bool, string, error) { - if !e.IsValid() { - return false, "", errors.New("event not valid") + if err := e.Validate(); err != nil { + return false, "", fmt.Errorf("event validation failed: %w", err) } // Does the table exist? diff --git a/models/event/event_test.go b/models/event/event_test.go index c15797ff5..dfdda4286 100644 --- a/models/event/event_test.go +++ b/models/event/event_test.go @@ -12,14 +12,14 @@ var idMap = map[string]any{ "id": 123, } -func (e *EventsTestSuite) TestEvent_IsValid() { +func (e *EventsTestSuite) TestEvent_Validate() { { _evt := Event{Table: "foo"} - assert.False(e.T(), _evt.IsValid()) + assert.ErrorContains(e.T(), _evt.Validate(), "primary key map is empty") } { _evt := Event{Table: "foo", PrimaryKeyMap: idMap} - assert.False(e.T(), _evt.IsValid()) + assert.ErrorContains(e.T(), _evt.Validate(), "event has no data") } { _evt := Event{ @@ -30,7 +30,7 @@ func (e *EventsTestSuite) TestEvent_IsValid() { }, mode: config.History, } - assert.True(e.T(), _evt.IsValid()) + assert.Nil(e.T(), _evt.Validate()) } { _evt := Event{ @@ -40,7 +40,7 @@ func (e *EventsTestSuite) TestEvent_IsValid() { "foo": "bar", }, } - assert.False(e.T(), _evt.IsValid()) + assert.ErrorContains(e.T(), _evt.Validate(), "") } { _evt := Event{ @@ -48,7 +48,7 @@ func (e *EventsTestSuite) TestEvent_IsValid() { PrimaryKeyMap: idMap, Data: map[string]any{constants.DeleteColumnMarker: true, constants.OnlySetDeleteColumnMarker: true}, } - assert.True(e.T(), _evt.IsValid()) + assert.Nil(e.T(), _evt.Validate()) } }