Skip to content

Commit

Permalink
Issue 16: MarshalBinary Favorites
Browse files Browse the repository at this point in the history
Implement encoding.BinaryMarshaler for all Favorites structs.
  • Loading branch information
ifanchu committed Dec 30, 2020
1 parent 2290337 commit ef58d9f
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 1 deletion.
99 changes: 99 additions & 0 deletions fav.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package bbs

import (
"bytes"
"encoding"
"encoding/binary"
"errors"
"fmt"
"io/ioutil"
"log"
"time"
Expand Down Expand Up @@ -316,3 +318,100 @@ func NewFavLineItem(data []byte, startIndex int) (*FavLineItem, int, error) {
c++
return ret, c, nil
}

func (favf *FavFile) MarshalBinary() ([]byte, error) {
ret := make([]byte, 2)

binary.LittleEndian.PutUint16(ret[0:2], favf.Version)
folderInBytes, err := favf.Folder.MarshalBinary()
if err != nil {
return nil, err
}
ret = append(ret, folderInBytes...)

return ret, nil
}

func (favf *FavFolder) MarshalBinary() ([]byte, error) {
ret := make([]byte, 4)
c := 0

size := 2
binary.LittleEndian.PutUint16(ret[c:c+size], favf.NBoards)
c += size

ret[c] = favf.NLines
c += 1

ret[c] = favf.NFolders

for _, item := range favf.FavItems {
encoded, err := item.MarshalBinary()
if err != nil {
return nil, err
}
ret = append(ret, encoded...)
}

for _, item := range favf.FavItems {
if f, ok := item.Item.(*FavFolderItem); ok {
encoded, err := f.ThisFolder.MarshalBinary()
if err != nil {
return nil, err
}
ret = append(ret, encoded...)
}
}

return ret, nil
}

func (favi *FavItem) MarshalBinary() ([]byte, error) {
ret := make([]byte, 2)

ret[0] = uint8(favi.FavType)
ret[1] = favi.FavAttr
favim, ok := favi.Item.(encoding.BinaryMarshaler)
if !ok {
return nil, fmt.Errorf("FavItem.Item must implement encoding.BinaryMarshaler")
}
encoded, err := favim.MarshalBinary()
if err != nil {
return nil, err
}
ret = append(ret, encoded...)

return ret, nil
}

func (favbi *FavBoardItem) MarshalBinary() ([]byte, error) {
ret := make([]byte, sizeOfPttFavBoardBytes)
c := 0

size := 4
binary.LittleEndian.PutUint32(ret[c:c+size], favbi.BoardId)
c += size

binary.LittleEndian.PutUint32(ret[c:c+size], uint32(favbi.LastVisit.Unix()))
c += size

binary.LittleEndian.PutUint32(ret[c:c+size], favbi.Attr)

return ret, nil
}

func (favfi *FavFolderItem) MarshalBinary() ([]byte, error) {
ret := make([]byte, sizeOfPttFavFolderBytes)
ret[0] = favfi.FolderId

size := PTT_BTLEN + 1
copy(ret[1:1+size], Utf8ToBig5(favfi.Title))

return ret, nil
}

func (favli *FavLineItem) MarshalBinary() ([]byte, error) {
ret := make([]byte, sizeOfPttFavLineBytes)
ret[0] = favli.LineId
return ret, nil
}
19 changes: 18 additions & 1 deletion fav_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package bbs

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"testing"
"time"
Expand All @@ -26,6 +28,11 @@ func TestNewFavLineItem(t *testing.T) {
if line.LineId != 2 {
t.Errorf("Line should have ID 2 but was %d", line.LineId)
}

encoded, err := item.MarshalBinary()
if bytes.Compare(data, encoded) != 0 {
t.Errorf("Encoded FavLineItem should be equal to the input")
}
}

func TestOpenFavFile(t *testing.T) {
Expand Down Expand Up @@ -243,7 +250,8 @@ func TestOpenFavFile(t *testing.T) {
}

for _, c := range testCases {
fav, err := OpenFavFile(fmt.Sprintf("testcase/fav/%s", c.filename))
filePath := fmt.Sprintf("testcase/fav/%s", c.filename)
fav, err := OpenFavFile(filePath)
if err != nil {
t.Fatal(err)
}
Expand All @@ -270,6 +278,15 @@ func TestOpenFavFile(t *testing.T) {
t.Error("invalid fav type")
}
}

expectedBytes, err := ioutil.ReadFile(filePath)
if err != nil {
t.Fatal(err)
}
encoded, err := fav.MarshalBinary()
if bytes.Compare(expectedBytes, encoded) != 0 {
t.Errorf("Encoded FavFile should be equal to the input. Expected: \n%v\nEncoded: \n%v\n ", expectedBytes, encoded)
}
}

}

0 comments on commit ef58d9f

Please sign in to comment.