Skip to content

Commit d4a548f

Browse files
committed
Return struct instead of interface
1 parent 86a8672 commit d4a548f

File tree

4 files changed

+33
-37
lines changed

4 files changed

+33
-37
lines changed

data/file/equip_cat_decoder.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
_ "embed"
1010

1111
"github.com/meyermarcel/icm/cont"
12-
"github.com/meyermarcel/icm/data"
1312
)
1413

1514
const equipCatIDsFileName = "equipment-category-id.json"
@@ -19,8 +18,8 @@ var equipCatIDsJSON []byte
1918

2019
// NewEquipCatDecoder writes equipment category ID file to path if it not exists and
2120
// returns a struct that uses this file as a data source.
22-
func NewEquipCatDecoder(path string) (data.EquipCatDecoder, error) {
23-
equipCat := &equipCatDecoder{}
21+
func NewEquipCatDecoder(path string) (*EquipCatDecoder, error) {
22+
equipCat := &EquipCatDecoder{}
2423
pathToEquipCat := filepath.Join(path, equipCatIDsFileName)
2524
if err := initFile(pathToEquipCat, equipCatIDsJSON); err != nil {
2625
return nil, err
@@ -40,22 +39,22 @@ func NewEquipCatDecoder(path string) (data.EquipCatDecoder, error) {
4039
return equipCat, err
4140
}
4241

43-
type equipCatDecoder struct {
42+
type EquipCatDecoder struct {
4443
categories map[string]string
4544
}
4645

4746
// Decode decodes ID to equipment category ID.
48-
func (ec *equipCatDecoder) Decode(ID string) (bool, cont.EquipCat) {
49-
if val, ok := ec.categories[ID]; ok {
47+
func (ecd *EquipCatDecoder) Decode(ID string) (bool, cont.EquipCat) {
48+
if val, ok := ecd.categories[ID]; ok {
5049
return true, cont.NewEquipCatID(ID, val)
5150
}
5251
return false, cont.EquipCat{}
5352
}
5453

5554
// AllCatIDs returns all equipment category IDs.
56-
func (ec *equipCatDecoder) AllCatIDs() []string {
57-
keys := make([]string, 0, len(ec.categories))
58-
for k := range ec.categories {
55+
func (ecd *EquipCatDecoder) AllCatIDs() []string {
56+
keys := make([]string, 0, len(ecd.categories))
57+
for k := range ecd.categories {
5958
keys = append(keys, k)
6059
}
6160
return keys

data/file/size_decoder.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
_ "embed"
1010

1111
"github.com/meyermarcel/icm/cont"
12-
"github.com/meyermarcel/icm/data"
1312
)
1413

1514
const sizeFileName = "size.json"
@@ -29,7 +28,7 @@ type heightWidth struct {
2928

3029
// NewSizeDecoder writes last update lengths, height and width file to path if it not exists and
3130
// returns a struct that uses this file as a data source.
32-
func NewSizeDecoder(path string) (data.LengthDecoder, data.HeightWidthDecoder, error) {
31+
func NewSizeDecoder(path string) (*LengthDecoder, *HeightWidthDecoder, error) {
3332
pathToSizes := filepath.Join(path, sizeFileName)
3433
if err := initFile(pathToSizes, lengthHeightWidthJSON); err != nil {
3534
return nil, nil, err
@@ -39,42 +38,42 @@ func NewSizeDecoder(path string) (data.LengthDecoder, data.HeightWidthDecoder, e
3938
return nil, nil, err
4039
}
4140

42-
var size size
43-
if err := json.Unmarshal(b, &size); err != nil {
41+
var s size
42+
if err := json.Unmarshal(b, &s); err != nil {
4443
return nil, nil, err
4544
}
46-
for lengthCode := range size.Length {
45+
for lengthCode := range s.Length {
4746
if err := cont.IsLengthCode(lengthCode); err != nil {
4847
return nil, nil, err
4948
}
5049
}
51-
for heightWidthCode := range size.HeightWidth {
50+
for heightWidthCode := range s.HeightWidth {
5251
if err := cont.IsHeightWidthCode(heightWidthCode); err != nil {
5352
return nil, nil, err
5453
}
5554
}
56-
return &lengthDecoder{size.Length}, &heightWidthDecoder{size.HeightWidth}, nil
55+
return &LengthDecoder{s.Length}, &HeightWidthDecoder{s.HeightWidth}, nil
5756
}
5857

59-
type lengthDecoder struct {
58+
type LengthDecoder struct {
6059
lengths map[string]string
6160
}
6261

6362
// Decode returns length for a given length code.
64-
func (l *lengthDecoder) Decode(code string) (bool, cont.Length) {
65-
if val, ok := l.lengths[code]; ok {
63+
func (ld *LengthDecoder) Decode(code string) (bool, cont.Length) {
64+
if val, ok := ld.lengths[code]; ok {
6665
return true, cont.Length(val)
6766
}
6867
return false, ""
6968
}
7069

71-
type heightWidthDecoder struct {
70+
type HeightWidthDecoder struct {
7271
heightWidths map[string]heightWidth
7372
}
7473

7574
// Decode returns height and width for given height and width code.
76-
func (hw *heightWidthDecoder) Decode(code string) (bool, cont.Height, cont.Width) {
77-
if val, ok := hw.heightWidths[code]; ok {
75+
func (hwd *HeightWidthDecoder) Decode(code string) (bool, cont.Height, cont.Width) {
76+
if val, ok := hwd.heightWidths[code]; ok {
7877
return true, cont.Height(val.Height), cont.Width(val.Width)
7978
}
8079
return false, "", ""

data/file/timestamp_updater.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"path/filepath"
77
"strings"
88
"time"
9-
10-
"github.com/meyermarcel/icm/data"
119
)
1210

1311
const (
@@ -17,15 +15,15 @@ const (
1715
lastUpdate = "2018-10-29T15:00:00Z" + "\n"
1816
)
1917

20-
type timestampUpdater struct {
18+
type TimestampUpdater struct {
2119
path string
2220
timestamp string
2321
}
2422

2523
// NewTimestampUpdater writes last update file to path if it not exists and
2624
// returns a struct that uses this file as a data source.
27-
func NewTimestampUpdater(path string) (data.TimestampUpdater, error) {
28-
timestampUpdater := &timestampUpdater{path: path}
25+
func NewTimestampUpdater(path string) (*TimestampUpdater, error) {
26+
timestampUpdater := &TimestampUpdater{path: path}
2927
pathToFile := filepath.Join(timestampUpdater.path, lastUpdateFileName)
3028
if err := initFile(pathToFile, []byte(lastUpdate)); err != nil {
3129
return nil, err
@@ -39,16 +37,16 @@ func NewTimestampUpdater(path string) (data.TimestampUpdater, error) {
3937
}
4038

4139
// Update writes the recent time to last update file if timeout is exceeded.
42-
func (lu *timestampUpdater) Update() error {
43-
dateString := strings.TrimSuffix(lu.timestamp, "\n")
40+
func (tu *TimestampUpdater) Update() error {
41+
dateString := strings.TrimSuffix(tu.timestamp, "\n")
4442
loaded, err := time.Parse(dateFormat, dateString)
4543
if err != nil {
4644
return err
4745
}
4846
now := time.Now()
4947
afterTimeout := now.After(loaded.Add(timeout))
5048
if afterTimeout {
51-
err := os.WriteFile(filepath.Join(lu.path, lastUpdateFileName), []byte(now.Format(dateFormat)+"\n"), 0o644)
49+
err := os.WriteFile(filepath.Join(tu.path, lastUpdateFileName), []byte(now.Format(dateFormat)+"\n"), 0o644)
5250
if err != nil {
5351
return err
5452
}

data/file/type_decoder.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
_ "embed"
1010

1111
"github.com/meyermarcel/icm/cont"
12-
"github.com/meyermarcel/icm/data"
1312
)
1413

1514
const typeFileName = "type.json"
@@ -22,15 +21,14 @@ const groupFileName = "group.json"
2221
//go:embed group.json
2322
var groupJSON []byte
2423

25-
type typeAndGroupDecoder struct {
24+
type TypeAndGroupDecoder struct {
2625
types map[string]string
2726
groups map[string]string
2827
}
2928

3029
// NewTypeDecoder writes type and group file to path if it not exists and
3130
// returns a struct that uses this file as a data source.
32-
func NewTypeDecoder(path string) (data.TypeDecoder, error) {
33-
typeAndGroup := &typeAndGroupDecoder{}
31+
func NewTypeDecoder(path string) (*TypeAndGroupDecoder, error) {
3432
pathToType := filepath.Join(path, typeFileName)
3533
if err := initFile(pathToType, typeJSON); err != nil {
3634
return nil, err
@@ -39,6 +37,8 @@ func NewTypeDecoder(path string) (data.TypeDecoder, error) {
3937
if err != nil {
4038
return nil, err
4139
}
40+
41+
typeAndGroup := &TypeAndGroupDecoder{}
4242
if err := json.Unmarshal(b, &typeAndGroup.types); err != nil {
4343
return nil, err
4444
}
@@ -63,15 +63,15 @@ func NewTypeDecoder(path string) (data.TypeDecoder, error) {
6363
}
6464

6565
// Decode returns type and group information for the type code.
66-
func (tg *typeAndGroupDecoder) Decode(code string) (bool, cont.TypeInfo, cont.GroupInfo) {
67-
typeInfoStr, typeFound := tg.types[code]
66+
func (tgd *TypeAndGroupDecoder) Decode(code string) (bool, cont.TypeInfo, cont.GroupInfo) {
67+
typeInfoStr, typeFound := tgd.types[code]
6868
typeInfo := cont.TypeInfo(typeInfoStr)
6969

7070
if !typeFound {
7171
return false, "", ""
7272
}
7373

74-
groupInfoStr, groupFound := tg.groups[string(code[0])]
74+
groupInfoStr, groupFound := tgd.groups[string(code[0])]
7575
groupInfo := cont.GroupInfo(groupInfoStr)
7676

7777
if !groupFound {

0 commit comments

Comments
 (0)