Skip to content

Commit

Permalink
Merge pull request #18 from heindrichpaul/feature/Pile-updates
Browse files Browse the repository at this point in the history
added marshalling to deckofCards Pile
  • Loading branch information
heindrichpaul authored May 30, 2019
2 parents cad587e + 21f9bfa commit ea70ac0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"

jsoniter "github.com/json-iterator/go"
"github.com/twinj/uuid"
)

Expand Down Expand Up @@ -175,3 +176,17 @@ func (z *Pile) GetCardAtID(index int) (*Draw, error) {
draw.Success = true
return draw, nil
}

//UnmarshalPile unmarshals a byte array into a pointer to a Pile for internal use.
func UnmarshalPile(data []byte) (*Pile, error) {
var r Pile
json := jsoniter.ConfigCompatibleWithStandardLibrary
err := json.Unmarshal(data, &r)
return &r, err
}

//Marshal marshals a pointer to a Pile into a byte array for transmission.
func (z *Pile) Marshal() ([]byte, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(z)
}
22 changes: 22 additions & 0 deletions pile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,25 @@ func TestPileString(t *testing.T) {
t.FailNow()
}
}

func TestPileUnmarshal(t *testing.T) {
pile := NewPile()
marshalPile, err := pile.Marshal()
if err != nil {
t.Logf("There was an error marshaling the pile: %s\n", err.Error())
t.FailNow()
}
upile, err := UnmarshalPile(marshalPile)
if err != nil {
t.Logf("There was an error unmarshaling the pile: %s\n", err.Error())
t.FailNow()
}
if pile.PileID != upile.PileID {
t.Logf("The PileID's do not match\n")
t.FailNow()
}
if pile.Remaining != upile.Remaining {
t.Logf("The Remaining cards do not match\n")
t.FailNow()
}
}

0 comments on commit ea70ac0

Please sign in to comment.