Skip to content

Commit

Permalink
Addressing staticchecks (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 authored Jan 5, 2024
1 parent 7e5cc60 commit 475dc1e
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 21 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/gha-go-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ jobs:
uses: actions/setup-go@v3
with:
go-version: 1.19
- name: test
- name: Run staticcheck
run: |
go install honnef.co/go/tools/cmd/staticcheck@latest
make static
- name: Run test
run: make test
- name: test-race
- name: Run test-race
run: make race
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.PHONY: static
static:
staticcheck ./...
.PHONY: test
test:
go test ./...
Expand Down
8 changes: 4 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"gopkg.in/yaml.v2"
"log"
"os"

"github.com/artie-labs/reader/constants"
)

const ctxKey = "_cfg"

type Kafka struct {
BootstrapServers string `yaml:"bootstrapServers"`
TopicPrefix string `yaml:"topicPrefix"`
Expand Down Expand Up @@ -104,11 +104,11 @@ func ReadConfig(fp string) (*Settings, error) {
}

func InjectIntoContext(ctx context.Context, settings *Settings) context.Context {
return context.WithValue(ctx, ctxKey, settings)
return context.WithValue(ctx, constants.ConfigKey, settings)
}

func FromContext(ctx context.Context) *Settings {
val := ctx.Value(ctxKey)
val := ctx.Value(constants.ConfigKey)
if val == nil {
return nil
}
Expand Down
9 changes: 9 additions & 0 deletions constants/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package constants

type contextKey string

const (
ConfigKey contextKey = "__cfg"
KafkaKey contextKey = "__kafka"
LoggerKey contextKey = "__logger"
)
4 changes: 2 additions & 2 deletions lib/kafkalib/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
RetryDelayMs = 250
)

var BatchEmptyErr = fmt.Errorf("batch is empty")
var ErrEmptyBatch = fmt.Errorf("batch is empty")

type Batch struct {
msgs []kafka.Message
Expand All @@ -25,7 +25,7 @@ type Batch struct {

func (b *Batch) IsValid() error {
if len(b.msgs) == 0 {
return BatchEmptyErr
return ErrEmptyBatch
}

if b.chunkSize < 1 {
Expand Down
10 changes: 4 additions & 6 deletions lib/kafkalib/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"github.com/artie-labs/reader/config"
"github.com/artie-labs/reader/constants"
"github.com/artie-labs/reader/lib/logger"
awsCfg "github.com/aws/aws-sdk-go-v2/config"
"github.com/segmentio/kafka-go"
Expand All @@ -12,14 +13,11 @@ import (
"time"
)

const (
ctxKey = "_kafka"
maxKafkaItemBytes = 4000000
)
const maxKafkaItemBytes = 4000000

func FromContext(ctx context.Context) *kafka.Writer {
log := logger.FromContext(ctx)
kafkaVal := ctx.Value(ctxKey)
kafkaVal := ctx.Value(constants.KafkaKey)
if kafkaVal == nil {
log.Fatal("kafka is not set in context.Context")
}
Expand Down Expand Up @@ -64,5 +62,5 @@ func InjectIntoContext(ctx context.Context) context.Context {
}
}

return context.WithValue(ctx, ctxKey, writer)
return context.WithValue(ctx, constants.KafkaKey, writer)
}
10 changes: 5 additions & 5 deletions lib/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ package logger

import (
"context"
"github.com/artie-labs/reader/config"
"github.com/evalphobia/logrus_sentry"
"github.com/sirupsen/logrus"
"os"
)

const loggerKey = "_log"
"github.com/artie-labs/reader/config"
"github.com/artie-labs/reader/constants"
)

func InjectLoggerIntoCtx(ctx context.Context) context.Context {
return context.WithValue(ctx, loggerKey, initLogger(config.FromContext(ctx)))
return context.WithValue(ctx, constants.LoggerKey, initLogger(config.FromContext(ctx)))
}

func FromContext(ctx context.Context) *logrus.Logger {
logVal := ctx.Value(loggerKey)
logVal := ctx.Value(constants.LoggerKey)
if logVal == nil {
return FromContext(InjectLoggerIntoCtx(ctx))
}
Expand Down
2 changes: 0 additions & 2 deletions sources/dynamodb/shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,4 @@ func (s *Store) processShard(ctx context.Context, shard *dynamodbstreams.Shard)
s.storage.SetShardProcessed(*shard.ShardId)
}
}

return
}

0 comments on commit 475dc1e

Please sign in to comment.