Skip to content

Commit d846bbb

Browse files
authored
feat(datatransfer): make states/events printable (#366)
Resolves a long standing Quality Of Life improvement -- adds a string methods to status and events so they appear in readable form when the state machine fails
1 parent 106231c commit d846bbb

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

events.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package datatransfer
22

3-
import "time"
3+
import (
4+
"time"
5+
)
46

57
// EventCode is a name for an event that occurs on a data transfer channel
68
type EventCode int
@@ -171,6 +173,10 @@ var Events = map[EventCode]string{
171173
SendMessageError: "SendMessageError",
172174
}
173175

176+
func (e EventCode) String() string {
177+
return Events[e]
178+
}
179+
174180
// Event is a struct containing information about a data transfer event
175181
type Event struct {
176182
Code EventCode // What type of event it is

printing_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package datatransfer_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
9+
datatransfer "github.com/filecoin-project/go-data-transfer/v2"
10+
)
11+
12+
func TestPrinting(t *testing.T) {
13+
for key, value := range datatransfer.Events {
14+
require.Equal(t, fmt.Sprintf("%v", key), value)
15+
}
16+
for key, value := range datatransfer.Statuses {
17+
require.Equal(t, fmt.Sprintf("%v", key), value)
18+
}
19+
}

statuses.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ var NotAcceptedStates = statusList{
102102
func (s Status) IsAccepted() bool {
103103
return !NotAcceptedStates.Contains(s)
104104
}
105+
func (s Status) String() string {
106+
return Statuses[s]
107+
}
105108

106109
var FinalizationStatuses = statusList{Finalizing, Completed, Completing}
107110

0 commit comments

Comments
 (0)