Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 16: MarshalBinary Favorites #28

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}

}