Skip to content

Commit 920b733

Browse files
author
Ravi Atluri
committed
Refactor code to use 'any' type instead of 'interface{}' for improved type safety and readability; update related tests and configurations
1 parent ae381b1 commit 920b733

File tree

24 files changed

+64
-75
lines changed

24 files changed

+64
-75
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ formatters:
109109
- default
110110
- blank
111111
- dot
112-
- alias
112+
- prefix(github.com/gojekfarm)
113113
custom-order: true
114114

115115
run:

Makefile

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,15 @@ GO_BUILD_DIRS := $(ALL_GO_MOD_DIRS)
88
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
99
BIN_DIR := $(PROJECT_DIR)/.bin
1010

11-
fmt:
12-
@$(call run-go-mod-dir,go fmt ./...,"go fmt")
13-
14-
gofmt:
15-
@$(call run-go-mod-dir,go fmt ./...,"gofmt -s -w")
11+
fmt: golangci-lint
12+
@$(call run-go-mod-dir,$(GOLANGCI_LINT) fmt,".bin/golangci-lint fmt")
1613

1714
vet:
1815
@$(call run-go-mod-dir,go vet ./...,"go vet")
1916

2017
lint: golangci-lint
2118
$(GOLANGCI_LINT) run --timeout=10m -v
2219

23-
imports: gci
24-
@$(call run-go-mod-dir,$(GCI) -w -local github.com/gojekfarm ./ | { grep -v -e 'skip file .*' || true; },".bin/gci")
2520

2621
.PHONY: tidy
2722
tidy:
@@ -51,7 +46,7 @@ test-html: test-cov gocov-html
5146
@open coverage.html
5247

5348
.PHONY: check
54-
check: fmt vet imports lint
49+
check: fmt vet lint
5550
@git diff --quiet || test $$(git diff --name-only | grep -v -e 'go.mod$$' -e 'go.sum$$' | wc -l) -eq 0 || ( echo "The following changes (result of code generators and code checks) have been detected:" && git --no-pager diff && false ) # fail if Git working tree is dirty
5651

5752
# ========= Helpers ===========
@@ -60,10 +55,6 @@ GOLANGCI_LINT = $(BIN_DIR)/golangci-lint
6055
golangci-lint:
6156
$(call go-get-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/v2/cmd/golangci-lint@latest)
6257

63-
GCI = $(BIN_DIR)/gci
64-
gci:
65-
$(call go-get-tool,$(GCI),github.com/daixiang0/[email protected])
66-
6758
GOCOV = $(BIN_DIR)/gocov
6859
gocov:
6960
$(call go-get-tool,$(GOCOV),github.com/axw/gocov/[email protected])

examples/xload/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func main() {
8787
_ = f.Close()
8888
}
8989

90-
func prettyPrint(v interface{}) {
90+
func prettyPrint(v any) {
9191
out, _ := json.MarshalIndent(v, "", " ")
9292
println(string(out))
9393
}

generic/slice/filter_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ func TestFilter(t *testing.T) {
2222
}{
2323
{
2424
name: "EvenIntegers",
25-
elems: []interface{}{1, 2, 3, 4, 5, 6, 7},
25+
elems: []any{1, 2, 3, 4, 5, 6, 7},
2626
predicate: func(i any) bool {
2727
return i.(int)%2 == 0
2828
},
2929
want: []any{2, 4, 6},
3030
},
3131
{
3232
name: "ValidStructs",
33-
elems: []interface{}{
33+
elems: []any{
3434
testStruct{ID: 1, Valid: true},
3535
testStruct{ID: 2},
3636
testStruct{ID: 3, Valid: true},

generic/slice/find_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestFind(t *testing.T) {
2222
}{
2323
{
2424
name: "StructWithID2",
25-
elems: []interface{}{
25+
elems: []any{
2626
testStruct{ID: 1},
2727
testStruct{ID: 2},
2828
testStruct{ID: 3},
@@ -36,7 +36,7 @@ func TestFind(t *testing.T) {
3636
},
3737
{
3838
name: "PointerToStructWithID3",
39-
elems: []interface{}{
39+
elems: []any{
4040
&testStruct{ID: 1},
4141
&testStruct{ID: 2},
4242
&testStruct{ID: 3},
@@ -50,7 +50,7 @@ func TestFind(t *testing.T) {
5050
},
5151
{
5252
name: "NoStructWithMatchingID",
53-
elems: []interface{}{
53+
elems: []any{
5454
testStruct{ID: 1},
5555
testStruct{ID: 2},
5656
testStruct{ID: 3},
@@ -62,7 +62,7 @@ func TestFind(t *testing.T) {
6262
},
6363
{
6464
name: "NoPointerStructWithMatchingID",
65-
elems: []interface{}{
65+
elems: []any{
6666
&testStruct{ID: 1},
6767
&testStruct{ID: 2},
6868
&testStruct{ID: 4},

generic/slice/map_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ func TestMap(t *testing.T) {
2727
}{
2828
{
2929
name: "DoubleIntegers",
30-
elems: []interface{}{1, 2, 3, 4, 5, 6},
30+
elems: []any{1, 2, 3, 4, 5, 6},
3131
mapper: func(i any) any {
3232
return i.(int) * 2
3333
},
3434
want: []any{2, 4, 6, 8, 10, 12},
3535
},
3636
{
3737
name: "MapStructs",
38-
elems: []interface{}{testStructA{Value: 2}, testStructA{Value: 4}, testStructA{Value: 6}},
38+
elems: []any{testStructA{Value: 2}, testStructA{Value: 4}, testStructA{Value: 6}},
3939
mapper: func(i any) any {
4040
return testStructB{Value: i.(testStructA).Value * 2}
4141
},
@@ -75,7 +75,7 @@ func TestMapConcurrentWithContext(t *testing.T) {
7575
{
7676
name: "DoubleIntegers",
7777
ctx: context.Background(),
78-
elems: []interface{}{1, 2, 3, 4, 5, 6},
78+
elems: []any{1, 2, 3, 4, 5, 6},
7979
mapper: func(_ context.Context, i any) any {
8080
return i.(int) * 2
8181
},
@@ -84,7 +84,7 @@ func TestMapConcurrentWithContext(t *testing.T) {
8484
{
8585
name: "MapStructs",
8686
ctx: context.Background(),
87-
elems: []interface{}{testStructA{Value: 2}, testStructA{Value: 4}, testStructA{Value: 6}},
87+
elems: []any{testStructA{Value: 2}, testStructA{Value: 4}, testStructA{Value: 6}},
8888
mapper: func(_ context.Context, i any) any {
8989
return testStructB{Value: i.(testStructA).Value * 2}
9090
},
@@ -93,7 +93,7 @@ func TestMapConcurrentWithContext(t *testing.T) {
9393
{
9494
name: "SlowMapper",
9595
ctx: tCtx,
96-
elems: []interface{}{1, 2, 4, 8, 16, 32},
96+
elems: []any{1, 2, 4, 8, 16, 32},
9797
mapper: func(_ context.Context, i any) any {
9898
ii := i.(int)
9999
time.Sleep(time.Duration(ii) * time.Second)
@@ -104,7 +104,7 @@ func TestMapConcurrentWithContext(t *testing.T) {
104104
{
105105
name: "AlreadyCancelledContext",
106106
ctx: cCtx,
107-
elems: []interface{}{1, 2, 4, 8, 16, 32},
107+
elems: []any{1, 2, 4, 8, 16, 32},
108108
mapper: func(_ context.Context, i any) any {
109109
return i.(int) * 2
110110
},
@@ -128,15 +128,15 @@ func TestMapConcurrent(t *testing.T) {
128128
}{
129129
{
130130
name: "DoubleIntegers",
131-
elems: []interface{}{1, 2, 3, 4, 5, 6},
131+
elems: []any{1, 2, 3, 4, 5, 6},
132132
mapper: func(i any) any {
133133
return i.(int) * 2
134134
},
135135
want: []any{2, 4, 6, 8, 10, 12},
136136
},
137137
{
138138
name: "MapStructs",
139-
elems: []interface{}{testStructA{Value: 2}, testStructA{Value: 4}, testStructA{Value: 6}},
139+
elems: []any{testStructA{Value: 2}, testStructA{Value: 4}, testStructA{Value: 6}},
140140
mapper: func(i any) any {
141141
return testStructB{Value: i.(testStructA).Value * 2}
142142
},

xkafka/middleware/retry/example_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ package retry_test
22

33
import (
44
"context"
5-
"time"
6-
75
"log/slog"
6+
"time"
87

98
"github.com/gojekfarm/xtools/xkafka"
109
"github.com/gojekfarm/xtools/xkafka/middleware/retry"

xkafka/middleware/slog/slog.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ package slog
33

44
import (
55
"context"
6-
"time"
7-
86
"log/slog"
7+
"time"
98

109
"github.com/gojekfarm/xtools/xkafka"
1110
)

xkafka/middleware/slog/slog_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package slog
22

33
import (
44
"context"
5+
"log/slog"
56
"testing"
67

78
"github.com/stretchr/testify/assert"
8-
"log/slog"
99

1010
"github.com/gojekfarm/xtools/xkafka"
1111
)

xkafka/options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func (b Brokers) setConsumerConfig(o *consumerConfig) { o.brokers = b }
1212
func (b Brokers) setProducerConfig(o *producerConfig) { o.brokers = b }
1313

1414
// ConfigMap allows setting kafka configuration.
15-
type ConfigMap map[string]interface{}
15+
type ConfigMap map[string]any
1616

1717
func (cm ConfigMap) setConsumerConfig(o *consumerConfig) {
1818
for k, v := range cm {

0 commit comments

Comments
 (0)