Skip to content

Commit

Permalink
Refactor: add memberKind ans relation constants
Browse files Browse the repository at this point in the history
Signed-off-by: JeffMboya <[email protected]>

Refactor: add memberKind ans relation constants

Signed-off-by: JeffMboya <[email protected]>

Refactor: add memberKind ans relation constants

Signed-off-by: JeffMboya <[email protected]>

Refactor: add memberKind ans relation constants

Signed-off-by: JeffMboya <[email protected]>

Refactor: add memberKind ans relation constants

Signed-off-by: JeffMboya <[email protected]>
  • Loading branch information
JeffMboya committed May 20, 2024
1 parent 3f8243e commit 73f7017
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 34 deletions.
7 changes: 5 additions & 2 deletions bootstrap/events/consumer/streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const (
channelPrefix = "group."
channelUpdate = channelPrefix + "update"
channelRemove = channelPrefix + "remove"

memberKind = "things"
relation = "group"
)

type eventHandler struct {
Expand Down Expand Up @@ -100,7 +103,7 @@ func decodeRemoveChannel(event map[string]interface{}) removeEvent {
}

func decodeConnectThing(event map[string]interface{}) connectionEvent {
if event["memberKind"] != "things" && event["relation"] != "group" {
if event["memberKind"] != memberKind && event["relation"] != relation {
return connectionEvent{}
}

Expand All @@ -111,7 +114,7 @@ func decodeConnectThing(event map[string]interface{}) connectionEvent {
}

func decodeDisconnectThing(event map[string]interface{}) connectionEvent {
if event["memberKind"] != "things" && event["relation"] != "group" {
if event["memberKind"] != memberKind && event["relation"] != relation {
return connectionEvent{}
}
return connectionEvent{
Expand Down
16 changes: 8 additions & 8 deletions bootstrap/mocks/configs.go

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

16 changes: 8 additions & 8 deletions bootstrap/mocks/service.go

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

23 changes: 11 additions & 12 deletions bootstrap/postgres/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,27 @@ func (cr configRepository) Save(ctx context.Context, cfg bootstrap.Config, chsCo
err = repoerr.ErrConflict
}

if errRollback := cr.rollback("failed to insert a Config", tx); errRollback != nil {
if errRollback := cr.rollback(err, tx); errRollback != nil {
return "", errors.Wrap(err, errRollback)
}
}

if err := insertChannels(ctx, cfg.Owner, cfg.Channels, tx); err != nil {
if errRollback := cr.rollback("failed to insert Channels", tx); errRollback != nil {
if errRollback := cr.rollback(err, tx); errRollback != nil {
return "", errors.Wrap(err, errRollback)
}
return "", errors.Wrap(errSaveChannels, err)
}

if err := insertConnections(ctx, cfg, chsConnIDs, tx); err != nil {
if errRollback := cr.rollback("failed to insert connections", tx); errRollback != nil {
if errRollback := cr.rollback(err, tx); errRollback != nil {
return "", errors.Wrap(err, errRollback)
}
return "", errors.Wrap(errSaveConnections, err)
}

if err := tx.Commit(); err != nil {
if errRollback := cr.rollback("failed to commit Config save", tx); errRollback != nil {
if errRollback := cr.rollback(err, tx); errRollback != nil {
return "", errors.Wrap(err, errRollback)
}
return "", err
Expand Down Expand Up @@ -321,7 +321,7 @@ func (cr configRepository) UpdateConnections(ctx context.Context, owner, id stri
}

if err := insertChannels(ctx, owner, channels, tx); err != nil {
if rollbackErr := cr.rollback("failed to insert Channels during the update", tx); rollbackErr != nil {
if rollbackErr := cr.rollback(err, tx); rollbackErr != nil {
return err
}
return errors.Wrap(repoerr.ErrUpdateEntity, err)
Expand All @@ -333,14 +333,14 @@ func (cr configRepository) UpdateConnections(ctx context.Context, owner, id stri
return repoerr.ErrNotFound
}
}
if errRollback := cr.rollback("failed to update connections during the update", tx); errRollback != nil {
if errRollback := cr.rollback(err, tx); errRollback != nil {
return errors.Wrap(err, errRollback)
}
return errors.Wrap(repoerr.ErrUpdateEntity, err)
}

if err := tx.Commit(); err != nil {
if errRollback := cr.rollback("failed to commit Config update", tx); errRollback != nil {
if errRollback := cr.rollback(err, tx); errRollback != nil {
return errors.Wrap(err, errRollback)
}
return errors.Wrap(repoerr.ErrUpdateEntity, err)
Expand Down Expand Up @@ -505,13 +505,12 @@ func (cr configRepository) retrieveAll(owner string, filter bootstrap.Filter) (s
return fmt.Sprintf(template, f), params
}

func (cr configRepository) rollback(content string, tx *sqlx.Tx) error {
errMsg := errors.New(content)
func (cr configRepository) rollback(defErr error, tx *sqlx.Tx) error {
if err := tx.Rollback(); err != nil {
errRollback := errors.New("failed to rollback")
return errors.Wrap(errMsg, errors.Wrap(errRollback, err))
return errors.Wrap(defErr, errors.Wrap(errors.New("failed to rollback"), err))
}
return errMsg

return defErr
}

func insertChannels(_ context.Context, owner string, channels []bootstrap.Channel, tx *sqlx.Tx) error {
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ require (
github.com/goccy/go-json v0.10.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
Expand Down Expand Up @@ -188,5 +187,4 @@ require (
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
mvdan.cc/gofumpt v0.6.0 // indirect
)
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -826,5 +826,3 @@ gotest.tools/v3 v3.3.0/go.mod h1:Mcr9QNxkg0uMvy/YElmo4SpXgJKWgQvYrT7Kw5RzJ1A=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
mvdan.cc/gofumpt v0.6.0 h1:G3QvahNDmpD+Aek/bNOLrFR2XC6ZAdo62dZu65gmwGo=
mvdan.cc/gofumpt v0.6.0/go.mod h1:4L0wf+kgIPZtcCWXynNS2e6bhmj73umwnuXSZarixzA=

0 comments on commit 73f7017

Please sign in to comment.