Skip to content

Commit 72bae09

Browse files
authored
refactor: don't design interfaces, discover them. (#111)
* refactor: don't design interfaces, discover them. * chore: simplify dependendy injection
1 parent 11994ae commit 72bae09

File tree

7 files changed

+80
-111
lines changed

7 files changed

+80
-111
lines changed

src/wasm/activity-service/activity/activity.go

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package activity
1717

1818
import (
19+
"errors"
1920
"strconv"
2021
"time"
2122

@@ -25,6 +26,8 @@ import (
2526
"github.com/muktihari/fit/proto"
2627
)
2728

29+
var ErrNoActivity = errors.New("no activity")
30+
2831
// Activity is an activity. It use FIT SDK's structure as its base since FIT is currently the most advance format.
2932
type Activity struct {
3033
Creator Creator

src/wasm/activity-service/activity/fit/service.go renamed to src/wasm/activity-service/activity/fit/fit.go

+12-11
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,27 @@ import (
3131
"github.com/muktihari/fit/proto"
3232
"github.com/openivity/activity-service/activity"
3333
"github.com/openivity/activity-service/mem"
34+
"github.com/openivity/activity-service/service"
3435
"golang.org/x/exp/slices"
3536
)
3637

37-
var _ activity.Service = (*service)(nil)
38+
var _ service.DecodeEncoder = (*DecodeEncoder)(nil)
3839

3940
var decoderPool = sync.Pool{New: func() any { return decoder.New(nil) }}
4041
var encoderPool = sync.Pool{New: func() any { return encoder.New(nil) }}
4142

42-
type service struct {
43+
type DecodeEncoder struct {
4344
preprocessor *activity.Preprocessor
4445
}
4546

46-
// NewService creates new FIT service.
47-
func NewService(preproc *activity.Preprocessor) activity.Service {
48-
return &service{
47+
// NewDecodeEncoder creates new FIT decode-encoder.
48+
func NewDecodeEncoder(preproc *activity.Preprocessor) *DecodeEncoder {
49+
return &DecodeEncoder{
4950
preprocessor: preproc,
5051
}
5152
}
5253

53-
func (s *service) Decode(ctx context.Context, r io.Reader) ([]activity.Activity, error) {
54+
func (s *DecodeEncoder) Decode(ctx context.Context, r io.Reader) ([]activity.Activity, error) {
5455
lis := filedef.NewListener()
5556
defer lis.Close()
5657

@@ -114,7 +115,7 @@ func (s *service) Decode(ctx context.Context, r io.Reader) ([]activity.Activity,
114115
return activities, nil
115116
}
116117

117-
func (s *service) convertToActivity(activityFile *filedef.Activity) activity.Activity {
118+
func (s *DecodeEncoder) convertToActivity(activityFile *filedef.Activity) activity.Activity {
118119
var timezone int8
119120
if activityFile.Activity != nil {
120121
localTimestamp := activityFile.Activity.LocalTimestamp
@@ -221,7 +222,7 @@ func (s *service) convertToActivity(activityFile *filedef.Activity) activity.Act
221222
}
222223

223224
// recalculateSummary recalculates values based on Laps and Records.
224-
func (s *service) recalculateSummary(ses *activity.Session) {
225+
func (s *DecodeEncoder) recalculateSummary(ses *activity.Session) {
225226
records := slices.Clone(ses.Records)
226227
if len(ses.Laps) == 1 { // Ensure lap's time windows match with session, FIT produces by Strava contains wrong time.
227228
ses.Laps[0].StartTime = ses.StartTime
@@ -250,7 +251,7 @@ func (s *service) recalculateSummary(ses *activity.Session) {
250251
ses.Summarize()
251252
}
252253

253-
func (s *service) handleUnrelatedMessages(activityFile *filedef.Activity) []proto.Message {
254+
func (s *DecodeEncoder) handleUnrelatedMessages(activityFile *filedef.Activity) []proto.Message {
254255
size := len(activityFile.DeveloperDataIds) +
255256
len(activityFile.FieldDescriptions) +
256257
len(activityFile.DeviceInfos) +
@@ -312,7 +313,7 @@ func (s *service) handleUnrelatedMessages(activityFile *filedef.Activity) []prot
312313
return unrelatedMessages
313314
}
314315

315-
func (s *service) Encode(ctx context.Context, activities []activity.Activity) ([][]byte, error) {
316+
func (s *DecodeEncoder) Encode(ctx context.Context, activities []activity.Activity) ([][]byte, error) {
316317
buf := mem.GetBuffer()
317318
defer mem.PutBuffer(buf)
318319

@@ -340,7 +341,7 @@ func (s *service) Encode(ctx context.Context, activities []activity.Activity) ([
340341
return bs, nil
341342
}
342343

343-
func (s *service) makeLastSummary(a *activity.Activity) {
344+
func (s *DecodeEncoder) makeLastSummary(a *activity.Activity) {
344345
var lastTimestamp time.Time
345346
for i := len(a.Sessions) - 1; i >= 0; i-- {
346347
ses := a.Sessions[i]

src/wasm/activity-service/activity/gpx/service.go renamed to src/wasm/activity-service/activity/gpx/gpx.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/openivity/activity-service/activity"
2626
"github.com/openivity/activity-service/activity/gpx/schema"
2727
"github.com/openivity/activity-service/mem"
28+
"github.com/openivity/activity-service/service"
2829
"github.com/openivity/activity-service/strutils"
2930
"github.com/openivity/activity-service/xmlutils"
3031
"golang.org/x/exp/slices"
@@ -35,18 +36,18 @@ const (
3536
metadataLink = "https://openivity.github.io"
3637
)
3738

38-
var _ activity.Service = (*service)(nil)
39+
var _ service.DecodeEncoder = (*DecodeEncoder)(nil)
3940

40-
type service struct {
41+
type DecodeEncoder struct {
4142
preprocessor *activity.Preprocessor
4243
}
4344

44-
// NewService creates new GPX service.
45-
func NewService(preproc *activity.Preprocessor) activity.Service {
46-
return &service{preprocessor: preproc}
45+
// NewDecodeEncoder creates new GPX decode-encoder.
46+
func NewDecodeEncoder(preproc *activity.Preprocessor) *DecodeEncoder {
47+
return &DecodeEncoder{preprocessor: preproc}
4748
}
4849

49-
func (s *service) Decode(ctx context.Context, r io.Reader) ([]activity.Activity, error) {
50+
func (s *DecodeEncoder) Decode(ctx context.Context, r io.Reader) ([]activity.Activity, error) {
5051
tok := xmltokenizer.New(r)
5152

5253
var gpx schema.GPX
@@ -150,7 +151,7 @@ loop:
150151
return []activity.Activity{act}, nil
151152
}
152153

153-
func (s *service) Encode(ctx context.Context, activities []activity.Activity) ([][]byte, error) {
154+
func (s *DecodeEncoder) Encode(ctx context.Context, activities []activity.Activity) ([][]byte, error) {
154155
bs := make([][]byte, len(activities))
155156

156157
buf := mem.GetBuffer()
@@ -171,7 +172,7 @@ func (s *service) Encode(ctx context.Context, activities []activity.Activity) ([
171172
return bs, nil
172173
}
173174

174-
func (s *service) convertActivityToGPX(act *activity.Activity) schema.GPX {
175+
func (s *DecodeEncoder) convertActivityToGPX(act *activity.Activity) schema.GPX {
175176
gpx := schema.GPX{
176177
Creator: act.Creator.Name,
177178
Metadata: schema.Metadata{

src/wasm/activity-service/activity/service.go

-32
This file was deleted.

src/wasm/activity-service/activity/tcx/service.go renamed to src/wasm/activity-service/activity/tcx/tcx.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/openivity/activity-service/activity"
2828
"github.com/openivity/activity-service/activity/tcx/schema"
2929
"github.com/openivity/activity-service/mem"
30+
"github.com/openivity/activity-service/service"
3031
"github.com/openivity/activity-service/strutils"
3132
"github.com/openivity/activity-service/xmlutils"
3233
"golang.org/x/exp/slices"
@@ -36,18 +37,18 @@ const (
3637
applicationName = "openitivy.github.io"
3738
)
3839

39-
var _ activity.Service = (*service)(nil)
40+
var _ service.DecodeEncoder = (*DecodeEncoder)(nil)
4041

41-
type service struct {
42+
type DecodeEncoder struct {
4243
preprocessor *activity.Preprocessor
4344
}
4445

45-
// NewService creates new TCX service.
46-
func NewService(preproc *activity.Preprocessor) activity.Service {
47-
return &service{preprocessor: preproc}
46+
// NewDecodeEncoder creates new TCX decode-encoder.
47+
func NewDecodeEncoder(preproc *activity.Preprocessor) *DecodeEncoder {
48+
return &DecodeEncoder{preprocessor: preproc}
4849
}
4950

50-
func (s *service) Decode(ctx context.Context, r io.Reader) ([]activity.Activity, error) {
51+
func (s *DecodeEncoder) Decode(ctx context.Context, r io.Reader) ([]activity.Activity, error) {
5152
tok := xmltokenizer.New(r)
5253

5354
var tcx schema.TCX
@@ -188,7 +189,7 @@ loop:
188189
return []activity.Activity{act}, nil
189190
}
190191

191-
func (s *service) Encode(ctx context.Context, activities []activity.Activity) ([][]byte, error) {
192+
func (s *DecodeEncoder) Encode(ctx context.Context, activities []activity.Activity) ([][]byte, error) {
192193
bs := make([][]byte, len(activities))
193194

194195
buf := mem.GetBuffer()
@@ -206,7 +207,7 @@ func (s *service) Encode(ctx context.Context, activities []activity.Activity) ([
206207
return bs, nil
207208
}
208209

209-
func (s *service) convertActivityToTCX(act *activity.Activity) schema.TCX {
210+
func (s *DecodeEncoder) convertActivityToTCX(act *activity.Activity) schema.TCX {
210211
tcx := schema.TCX{
211212
Author: &schema.Application{
212213
Name: applicationName,

src/wasm/activity-service/main.go

+10-14
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,12 @@ var manufacturerJson []byte
5252
func main() {
5353
preproc := activity.NewPreprocessor()
5454

55-
fs := fit.NewService(preproc)
56-
gs := gpx.NewService(preproc)
57-
ts := tcx.NewService(preproc)
58-
59-
manufacturers := makeManufacturers()
60-
61-
svc := service.New(fs, gs, ts, manufacturers)
55+
svc := service.New(
56+
fit.NewDecodeEncoder(preproc),
57+
gpx.NewDecodeEncoder(preproc),
58+
tcx.NewDecodeEncoder(preproc),
59+
makeManufacturers(),
60+
)
6261

6362
js.Global().Set("decode", createDecodeFunc(svc))
6463
js.Global().Set("encode", createEncodeFunc(svc))
@@ -124,7 +123,7 @@ func makeManufacturers() map[typedef.Manufacturer]activity.Manufacturer {
124123
return manufacturers
125124
}
126125

127-
func createDecodeFunc(s service.Service) js.Func {
126+
func createDecodeFunc(s *service.Service) js.Func {
128127
return js.FuncOf(func(this js.Value, args []js.Value) any {
129128
input := args[0] // input is an Array<Uint8Array>
130129
if input.Length() == 0 {
@@ -140,7 +139,6 @@ func createDecodeFunc(s service.Service) js.Func {
140139
}
141140

142141
result := s.Decode(context.Background(), rs)
143-
144142
decodedActivities = result.Activities
145143

146144
buf := mem.GetBuffer()
@@ -152,7 +150,7 @@ func createDecodeFunc(s service.Service) js.Func {
152150
})
153151
}
154152

155-
func createEncodeFunc(svc service.Service) js.Func {
153+
func createEncodeFunc(svc *service.Service) js.Func {
156154
return js.FuncOf(func(this js.Value, args []js.Value) any {
157155
input := args[0] // input is an JSON string
158156
if input.Length() == 0 {
@@ -169,11 +167,9 @@ func createEncodeFunc(svc service.Service) js.Func {
169167
}
170168

171169
encodeSpec.Activities = cloneActivities(decodedActivities)
172-
173170
elapsed := time.Since(begin)
174171

175172
result := svc.Encode(context.Background(), encodeSpec)
176-
177173
result.DeserializeInputTook = elapsed
178174

179175
buf := mem.GetBuffer()
@@ -185,15 +181,15 @@ func createEncodeFunc(svc service.Service) js.Func {
185181
})
186182
}
187183

188-
func createManufacturerListFunc(svc service.Service) js.Func {
184+
func createManufacturerListFunc(svc *service.Service) js.Func {
189185
return js.FuncOf(func(this js.Value, args []js.Value) any {
190186
manufacturerList := svc.ManufacturerList()
191187
b := manufacturerList.MarshalAppendJSON(make([]byte, 0, 50<<10))
192188
return string(b)
193189
})
194190
}
195191

196-
func createSportListFunc(svc service.Service) js.Func {
192+
func createSportListFunc(svc *service.Service) js.Func {
197193
return js.FuncOf(func(this js.Value, args []js.Value) any {
198194
sportList := svc.SportList()
199195
b := sportList.MarshalAppendJSON(make([]byte, 0, 8<<10))

0 commit comments

Comments
 (0)