From 23472aa4f283042334bf93a33fcffec20e448d44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Wed, 15 Jan 2020 01:08:03 +0100 Subject: [PATCH] statemachine: don't force users to see reflect --- lib/statemachine/group.go | 6 +++--- lib/statemachine/machine_test.go | 7 +++---- storage/miner.go | 3 +-- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/statemachine/group.go b/lib/statemachine/group.go index 5322b0665..2c8562955 100644 --- a/lib/statemachine/group.go +++ b/lib/statemachine/group.go @@ -25,12 +25,12 @@ type StateGroup struct { sms map[datastore.Key]*StateMachine } -// stateType: T - (reflect.TypeOf(MyStateStruct{})) -func New(ds datastore.Datastore, hnd StateHandler, stateType reflect.Type) *StateGroup { +// stateType: T - (MyStateStruct{}) +func New(ds datastore.Datastore, hnd StateHandler, stateType interface{}) *StateGroup { return &StateGroup{ sts: statestore.New(ds), hnd: hnd, - stateType: stateType, + stateType: reflect.TypeOf(stateType), sms: map[datastore.Key]*StateMachine{}, } diff --git a/lib/statemachine/machine_test.go b/lib/statemachine/machine_test.go index d445a5283..49b8324ce 100644 --- a/lib/statemachine/machine_test.go +++ b/lib/statemachine/machine_test.go @@ -2,7 +2,6 @@ package statemachine import ( "context" - "reflect" "testing" "github.com/ipfs/go-datastore" @@ -67,7 +66,7 @@ func TestBasic(t *testing.T) { th := &testHandler{t: t, done: make(chan struct{}), proceed: make(chan struct{})} close(th.proceed) - smm := New(ds, th, reflect.TypeOf(TestState{})) + smm := New(ds, th, TestState{}) if err := smm.Send(uint64(2), &TestEvent{A: "start"}); err != nil { t.Fatalf("%+v", err) @@ -82,7 +81,7 @@ func TestPersist(t *testing.T) { ds := datastore.NewMapDatastore() th := &testHandler{t: t, done: make(chan struct{}), proceed: make(chan struct{})} - smm := New(ds, th, reflect.TypeOf(TestState{})) + smm := New(ds, th, TestState{}) if err := smm.Send(uint64(2), &TestEvent{A: "start"}); err != nil { t.Fatalf("%+v", err) @@ -93,7 +92,7 @@ func TestPersist(t *testing.T) { return } - smm = New(ds, th, reflect.TypeOf(TestState{})) + smm = New(ds, th, TestState{}) if err := smm.Send(uint64(2), &TestEvent{A: "restart"}); err != nil { t.Fatalf("%+v", err) } diff --git a/storage/miner.go b/storage/miner.go index ef9f72d47..5b116feec 100644 --- a/storage/miner.go +++ b/storage/miner.go @@ -3,7 +3,6 @@ package storage import ( "context" "errors" - "reflect" "time" "github.com/ipfs/go-cid" @@ -86,7 +85,7 @@ func NewMiner(api storageMinerApi, addr address.Address, h host.Host, ds datasto } // TODO: separate sector stuff from miner struct - m.sectors = statemachine.New(namespace.Wrap(ds, datastore.NewKey(SectorStorePrefix)), m, reflect.TypeOf(SectorInfo{})) + m.sectors = statemachine.New(namespace.Wrap(ds, datastore.NewKey(SectorStorePrefix)), m, SectorInfo{}) return m, nil }