Skip to content

Commit

Permalink
feat(scale): add MustMarshal function (#2991)
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Dec 8, 2022
1 parent 5191424 commit 32a80aa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/scale/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ func Marshal(v interface{}) (b []byte, err error) {
return
}

// MustMarshal runs Marshal and panics on error.
func MustMarshal(v interface{}) (b []byte) {
b, err := Marshal(v)
if err != nil {
panic(err)
}
return b
}

type encodeState struct {
io.Writer
*fieldScaleIndicesCache
Expand Down
20 changes: 20 additions & 0 deletions pkg/scale/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@ func Test_Encoder_Encode(t *testing.T) {
assert.Equal(t, expectedWritten, written)
}

func Test_MustMarshal(t *testing.T) {
t.Parallel()

t.Run("success", func(t *testing.T) {
t.Parallel()

b := MustMarshal([]byte{1})
assert.Equal(t, []byte{4, 1}, b)
})

t.Run("panics on error", func(t *testing.T) {
t.Parallel()

const expected = "unsupported type: chan struct {}"
assert.PanicsWithError(t, expected, func() {
MustMarshal(make(chan struct{}))
})
})
}

type test struct {
name string
in interface{}
Expand Down

0 comments on commit 32a80aa

Please sign in to comment.