Unverified Commit b7280d07 authored by Erin Swenson-Healey's avatar Erin Swenson-Healey Committed by GitHub
Browse files

Merge pull request #28 from filecoin-project/chores/json-marshaler-for-API-types

feat(json): add JSON marshalers, which enable JSON-RPC support for SortedSectorInfo
parents 641e9ea5 f2c160ac
......@@ -2,6 +2,7 @@ package go_sectorbuilder
import (
"bytes"
"encoding/json"
"os"
"runtime"
"sort"
......@@ -53,6 +54,20 @@ func (s *SortedSectorInfo) Values() []SectorInfo {
return s.f
}
// MarshalJSON JSON-encodes and serializes the SortedSectorInfo.
func (s SortedSectorInfo) MarshalJSON() ([]byte, error) {
return json.Marshal(s.f)
}
// UnmarshalJSON parses the JSON-encoded byte slice and stores the result in the
// value pointed to by s.f. Note that this method allows for construction of a
// SortedSectorInfo which violates its invariant (that its SectorInfo are sorted
// in some defined way). Callers should take care to never provide a byte slice
// which would violate this invariant.
func (s *SortedSectorInfo) UnmarshalJSON(b []byte) error {
return json.Unmarshal(b, &s.f)
}
type SectorInfo struct {
SectorID uint64
CommR [CommitmentBytesLen]byte
......
......@@ -6,6 +6,7 @@ import (
"errors"
"io"
"io/ioutil"
"math/big"
"os"
"testing"
"time"
......@@ -118,6 +119,32 @@ func TestSectorBuilderLifecycle(t *testing.T) {
require.Equal(t, pieceBytes, unsealedPieceBytes)
}
func TestJsonMarshalSymmetry(t *testing.T) {
for i := 0; i < 100; i++ {
xs := make([]sb.SectorInfo, 10)
for j := 0; j < 10; j++ {
var x sb.SectorInfo
_, err := io.ReadFull(rand.Reader, x.CommR[:])
require.NoError(t, err)
n, err := rand.Int(rand.Reader, big.NewInt(500))
require.NoError(t, err)
x.SectorID = n.Uint64()
xs[j] = x
}
toSerialize := sb.NewSortedSectorInfo(xs...)
serialized, err := toSerialize.MarshalJSON()
require.NoError(t, err)
var fromSerialized sb.SortedSectorInfo
err = fromSerialized.UnmarshalJSON(serialized)
require.NoError(t, err)
require.Equal(t, toSerialize, fromSerialized)
}
}
func pollForSectorSealingStatus(ptr unsafe.Pointer, sectorID uint64, targetState sealing_state.State, timeout time.Duration) (status sb.SectorSealingStatus, retErr error) {
timeoutCh := time.After(timeout)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment