Skip to content

Commit b998f72

Browse files
committed
refactoring, use Go idiomatic naming
1 parent 0220e01 commit b998f72

File tree

4 files changed

+93
-93
lines changed

4 files changed

+93
-93
lines changed

interfaces.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,67 +30,67 @@ type ZeroCheckable interface {
3030
IsZero() bool
3131
}
3232

33-
// Identifiable defines types that implements the GetID method.
33+
// Identifiable defines types that implements the ID method.
3434
type Identifiable[T any] interface {
35-
GetID() T
35+
ID() T
3636
}
3737

38-
// ProductIDProvider defines types that implements the GetProductID method.
38+
// ProductIDProvider defines types that implements the ProductID method.
3939
type ProductIDProvider[T any] interface {
40-
GetProductID() T
40+
ProductID() T
4141
}
4242

43-
// CustomerIDProvider defines types that implements the GetCustomerID method.
43+
// CustomerIDProvider defines types that implements the CustomerID method.
4444
type CustomerIDProvider[T any] interface {
45-
GetCustomerID() T
45+
CustomerID() T
4646
}
4747

48-
// EventIDProvider defines types that implements the GetEventID method.
48+
// EventIDProvider defines types that implements the EventID method.
4949
type EventIDProvider[T any] interface {
50-
GetEventID() T
50+
EventID() T
5151
}
5252

53-
// AccountIDProvider defines types that implements the GetAccountID method.
53+
// AccountIDProvider defines types that implements the AccountID method.
5454
type AccountIDProvider[T any] interface {
55-
GetAccountID() T
55+
AccountID() T
5656
}
5757

58-
// Named defines types that implements the GetName method.
58+
// Named defines types that implements the Name method.
5959
type Named[T any] interface {
60-
GetName() T
60+
Name() T
6161
}
6262

63-
// EventNameProvider defines types that implements the GetEventName method.
63+
// EventNameProvider defines types that implements the EventName method.
6464
type EventNameProvider[T any] interface {
65-
GetEventName() T
65+
EventName() T
6666
}
6767

68-
// ValueTimeProvider defines types that implements the GetValueTime method.
68+
// ValueTimeProvider defines types that implements the ValueTime method.
6969
type ValueTimeProvider[T any] interface {
70-
GetValueTime() T
70+
ValueTime() T
7171
}
7272

73-
// CreatedAtProvider defines types that implements the GetCreatedAt method.
73+
// CreatedAtProvider defines types that implements the CreatedAt method.
7474
type CreatedAtProvider[T any] interface {
75-
GetCreatedAt() T
75+
CreatedAt() T
7676
}
7777

78-
// UpdatedAtProvider defines types that implements the GetUpdatedAt method.
78+
// UpdatedAtProvider defines types that implements the UpdatedAt method.
7979
type UpdatedAtProvider[T any] interface {
80-
GetUpdatedAt() T
80+
UpdatedAt() T
8181
}
8282

83-
// AmountProvider defines types that implements the GetAmount method.
83+
// AmountProvider defines types that implements the Amount method.
8484
type AmountProvider[T any] interface {
85-
GetAmount() T
85+
Amount() T
8686
}
8787

88-
// TransactionIDProvider defines types that implements the GetTransactionID method.
88+
// TransactionIDProvider defines types that implements the TransactionID method.
8989
type TransactionIDProvider[T any] interface {
90-
GetTransactionID() T
90+
TransactionID() T
9191
}
9292

93-
// PriorityProvider defines types that implements the GetPriority method.
93+
// PriorityProvider defines types that implements the Priority method.
9494
type PriorityProvider[T any] interface {
95-
GetPriority() T
95+
Priority() T
9696
}

structutil/getters.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,54 @@ import (
44
"github.com/jokruger/got"
55
)
66

7-
func GetID[I any, T got.Identifiable[I]](s T) I {
8-
return s.GetID()
7+
func ID[I any, T got.Identifiable[I]](s T) I {
8+
return s.ID()
99
}
1010

11-
func GetProductID[I any, T got.ProductIDProvider[I]](s T) I {
12-
return s.GetProductID()
11+
func ProductID[I any, T got.ProductIDProvider[I]](s T) I {
12+
return s.ProductID()
1313
}
1414

15-
func GetCustomerID[I any, T got.CustomerIDProvider[I]](s T) I {
16-
return s.GetCustomerID()
15+
func CustomerID[I any, T got.CustomerIDProvider[I]](s T) I {
16+
return s.CustomerID()
1717
}
1818

19-
func GetEventID[I any, T got.EventIDProvider[I]](s T) I {
20-
return s.GetEventID()
19+
func EventID[I any, T got.EventIDProvider[I]](s T) I {
20+
return s.EventID()
2121
}
2222

23-
func GetAccountID[I any, T got.AccountIDProvider[I]](s T) I {
24-
return s.GetAccountID()
23+
func AccountID[I any, T got.AccountIDProvider[I]](s T) I {
24+
return s.AccountID()
2525
}
2626

27-
func GetName[I any, T got.Named[I]](s T) I {
28-
return s.GetName()
27+
func Name[I any, T got.Named[I]](s T) I {
28+
return s.Name()
2929
}
3030

31-
func GetEventName[I any, T got.EventNameProvider[I]](s T) I {
32-
return s.GetEventName()
31+
func EventName[I any, T got.EventNameProvider[I]](s T) I {
32+
return s.EventName()
3333
}
3434

35-
func GetValueTime[I any, T got.ValueTimeProvider[I]](s T) I {
36-
return s.GetValueTime()
35+
func ValueTime[I any, T got.ValueTimeProvider[I]](s T) I {
36+
return s.ValueTime()
3737
}
3838

39-
func GetCreatedAt[I any, T got.CreatedAtProvider[I]](s T) I {
40-
return s.GetCreatedAt()
39+
func CreatedAt[I any, T got.CreatedAtProvider[I]](s T) I {
40+
return s.CreatedAt()
4141
}
4242

43-
func GetUpdatedAt[I any, T got.UpdatedAtProvider[I]](s T) I {
44-
return s.GetUpdatedAt()
43+
func UpdatedAt[I any, T got.UpdatedAtProvider[I]](s T) I {
44+
return s.UpdatedAt()
4545
}
4646

47-
func GetAmount[I any, T got.AmountProvider[I]](s T) I {
48-
return s.GetAmount()
47+
func Amount[I any, T got.AmountProvider[I]](s T) I {
48+
return s.Amount()
4949
}
5050

51-
func GetTransactionID[I any, T got.TransactionIDProvider[I]](s T) I {
52-
return s.GetTransactionID()
51+
func TransactionID[I any, T got.TransactionIDProvider[I]](s T) I {
52+
return s.TransactionID()
5353
}
5454

55-
func GetPriority[I any, T got.PriorityProvider[I]](s T) I {
56-
return s.GetPriority()
55+
func Priority[I any, T got.PriorityProvider[I]](s T) I {
56+
return s.Priority()
5757
}

tests/compare_test.go

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@ import (
1010
)
1111

1212
type TestCompareStruct struct {
13-
ID int
14-
Name string
13+
id int
14+
name string
1515
}
1616

17-
func (t TestCompareStruct) GetID() int { return t.ID }
18-
func (t TestCompareStruct) GetName() string { return t.Name }
17+
func (t TestCompareStruct) ID() int { return t.id }
18+
func (t TestCompareStruct) Name() string { return t.name }
1919

2020
type IDStruct struct {
21-
ID int
21+
id int
2222
}
2323

24-
func (i IDStruct) Compare(j IDStruct) int { return i.ID - j.ID }
24+
func (i IDStruct) Compare(j IDStruct) int { return i.id - j.id }
2525

2626
type TestCompareStruct2 struct {
27-
ID IDStruct
27+
id IDStruct
2828
}
2929

30-
func (t TestCompareStruct2) GetID() IDStruct { return t.ID }
30+
func (t TestCompareStruct2) ID() IDStruct { return t.id }
3131

3232
func TestCompare(t *testing.T) {
3333
t.Run("ints slice", func(t *testing.T) {
@@ -44,52 +44,52 @@ func TestCompare(t *testing.T) {
4444

4545
t.Run("structs slice", func(t *testing.T) {
4646
ms := []TestCompareStruct{
47-
{ID: 3, Name: "Three"},
48-
{ID: 1, Name: "One"},
49-
{ID: 2, Name: "Two"},
47+
{id: 3, name: "Three"},
48+
{id: 1, name: "One"},
49+
{id: 2, name: "Two"},
5050
}
51-
sliceutil.Sort(ms, basicutil.CompareBy[TestCompareStruct](structutil.GetID))
52-
if len(ms) != 3 || ms[0].ID != 1 || ms[1].ID != 2 || ms[2].ID != 3 {
51+
sliceutil.Sort(ms, basicutil.CompareBy[TestCompareStruct](structutil.ID))
52+
if len(ms) != 3 || ms[0].id != 1 || ms[1].id != 2 || ms[2].id != 3 {
5353
t.Error("Sort failed")
5454
}
55-
sliceutil.Sort(ms, got.Flip(basicutil.CompareBy[TestCompareStruct](structutil.GetID)))
56-
if len(ms) != 3 || ms[0].ID != 3 || ms[1].ID != 2 || ms[2].ID != 1 {
55+
sliceutil.Sort(ms, got.Flip(basicutil.CompareBy[TestCompareStruct](structutil.ID)))
56+
if len(ms) != 3 || ms[0].id != 3 || ms[1].id != 2 || ms[2].id != 1 {
5757
t.Error("Sort failed")
5858
}
5959
})
6060

6161
t.Run("structs slice, two fields", func(t *testing.T) {
6262
ms := []TestCompareStruct{
63-
{ID: 3, Name: "D"},
64-
{ID: 1, Name: "A"},
65-
{ID: 2, Name: "B"},
66-
{ID: 3, Name: "C"},
63+
{id: 3, name: "D"},
64+
{id: 1, name: "A"},
65+
{id: 2, name: "B"},
66+
{id: 3, name: "C"},
6767
}
68-
sliceutil.Sort(ms, basicutil.CompareBy2[TestCompareStruct](structutil.GetID, structutil.GetName))
69-
if len(ms) != 4 || ms[0].ID != 1 || ms[1].ID != 2 || ms[2].ID != 3 || ms[3].ID != 3 {
68+
sliceutil.Sort(ms, basicutil.CompareBy2[TestCompareStruct](structutil.ID, structutil.Name))
69+
if len(ms) != 4 || ms[0].id != 1 || ms[1].id != 2 || ms[2].id != 3 || ms[3].id != 3 {
7070
t.Error("Sort failed")
7171
}
72-
if ms[0].Name != "A" || ms[1].Name != "B" || ms[2].Name != "C" || ms[3].Name != "D" {
72+
if ms[0].name != "A" || ms[1].name != "B" || ms[2].name != "C" || ms[3].name != "D" {
7373
t.Error("Sort failed")
7474
}
75-
sliceutil.Sort(ms, got.Flip(basicutil.CompareBy2[TestCompareStruct](structutil.GetID, structutil.GetName)))
76-
if len(ms) != 4 || ms[0].ID != 3 || ms[1].ID != 3 || ms[2].ID != 2 || ms[3].ID != 1 {
75+
sliceutil.Sort(ms, got.Flip(basicutil.CompareBy2[TestCompareStruct](structutil.ID, structutil.Name)))
76+
if len(ms) != 4 || ms[0].id != 3 || ms[1].id != 3 || ms[2].id != 2 || ms[3].id != 1 {
7777
t.Error("Sort failed")
7878
}
79-
if ms[0].Name != "D" || ms[1].Name != "C" || ms[2].Name != "B" || ms[3].Name != "A" {
79+
if ms[0].name != "D" || ms[1].name != "C" || ms[2].name != "B" || ms[3].name != "A" {
8080
t.Error("Sort failed")
8181
}
8282
})
8383

8484
t.Run("compare structs by complex field", func(t *testing.T) {
8585
ms := []TestCompareStruct2{
86-
{ID: IDStruct{ID: 3}},
87-
{ID: IDStruct{ID: 1}},
88-
{ID: IDStruct{ID: 2}},
86+
{id: IDStruct{id: 3}},
87+
{id: IDStruct{id: 1}},
88+
{id: IDStruct{id: 2}},
8989
}
9090

91-
sliceutil.Sort(ms, structutil.CompareBy[TestCompareStruct2](structutil.GetID))
92-
if len(ms) != 3 || ms[0].ID.ID != 1 || ms[1].ID.ID != 2 || ms[2].ID.ID != 3 {
91+
sliceutil.Sort(ms, structutil.CompareBy[TestCompareStruct2](structutil.ID))
92+
if len(ms) != 3 || ms[0].id.id != 1 || ms[1].id.id != 2 || ms[2].id.id != 3 {
9393
t.Error("Sort failed")
9494
}
9595
})

tests/getters_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,27 @@ import (
88
)
99

1010
type TestGettersStruct struct {
11-
ID int
12-
Name string
11+
id int
12+
name string
1313
}
1414

15-
func (t TestGettersStruct) GetID() int {
16-
return t.ID
15+
func (t TestGettersStruct) ID() int {
16+
return t.id
1717
}
1818

19-
func (t TestGettersStruct) GetName() string {
20-
return t.Name
19+
func (t TestGettersStruct) Name() string {
20+
return t.name
2121
}
2222

2323
func TestGetters(t *testing.T) {
2424
ms := []TestGettersStruct{
25-
{ID: 1, Name: "One"},
26-
{ID: 2, Name: "Two"},
27-
{ID: 3, Name: "Three"},
25+
{id: 1, name: "One"},
26+
{id: 2, name: "Two"},
27+
{id: 3, name: "Three"},
2828
}
2929

3030
t.Run("get id", func(t *testing.T) {
31-
ids := sliceutil.Map(ms, structutil.GetID)
31+
ids := sliceutil.Map(ms, structutil.ID)
3232
if len(ids) != 3 {
3333
t.Fatalf("expected 3 ids, got %d", len(ids))
3434
}
@@ -38,7 +38,7 @@ func TestGetters(t *testing.T) {
3838
})
3939

4040
t.Run("get name", func(t *testing.T) {
41-
names := sliceutil.Map(ms, structutil.GetName)
41+
names := sliceutil.Map(ms, structutil.Name)
4242
if len(names) != 3 {
4343
t.Fatalf("expected 3 names, got %d", len(names))
4444
}

0 commit comments

Comments
 (0)