Skip to content
This repository was archived by the owner on Aug 2, 2021. It is now read-only.
3 changes: 1 addition & 2 deletions api/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -959,13 +959,12 @@ func (s *Server) HandleGetTag(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-cache, private, max-age=0")
r.Header.Del("ETag")
w.WriteHeader(http.StatusOK)
bytesToSend, err := tag.MarshalBinary()
err := json.NewEncoder(w).Encode(&tag)
if err != nil {
getTagFail.Inc(1)
respondError(w, r, "marshalling error", http.StatusInternalServerError)
return
}
http.ServeContent(w, r, tag.Name, time.Now(), bytes.NewReader(bytesToSend))
}

// calculateNumberOfChunks calculates the number of chunks in an arbitrary content length
Expand Down
8 changes: 2 additions & 6 deletions api/http/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,8 @@ func TestGetTagUsingHash(t *testing.T) {
if err != nil {
t.Fatal(err)
}

tag := &chunk.Tag{}
fmt.Println(len(retrievedData))
err = tag.UnmarshalBinary(retrievedData)
err = json.Unmarshal(retrievedData, &tag)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -164,10 +162,8 @@ func TestGetTagUsingTagId(t *testing.T) {
if err != nil {
t.Fatal(err)
}

tag := &chunk.Tag{}
fmt.Println(len(retrievedData))
err = tag.UnmarshalBinary(retrievedData)
err = json.Unmarshal(retrievedData, &tag)
if err != nil {
t.Fatal(err)
}
Expand Down
56 changes: 56 additions & 0 deletions chunk/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package chunk

import (
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
"sync/atomic"
"time"
Expand Down Expand Up @@ -216,3 +218,57 @@ func decodeInt64Splice(buffer *[]byte) int64 {
*buffer = (*buffer)[n:]
return val
}

// marshall friendly tag structure
type MarshallTag struct {
Uid uint32
Name string
Address string
Total int64
Split int64
Seen int64
Stored int64
Sent int64
Synced int64
StartedAt time.Time
}

// MarshalJSON marshals the tag structure in to JSON encoded byte slice
func (t *Tag) MarshalJSON() ([]byte, error) {
j := MarshallTag{
Uid: t.Uid,
Name: t.Name,
Address: hex.EncodeToString(t.Address),
Total: t.total,
Split: t.split,
Seen: t.seen,
Stored: t.stored,
Sent: t.sent,
Synced: t.synced,
StartedAt: t.startedAt,
}
return json.Marshal(j)
}

// UnmarshalJSON unwraps the JSON encoded byte slice to tag structure
func (t *Tag) UnmarshalJSON(b []byte) error {
mTag := &MarshallTag{}
if err := json.Unmarshal(b, &mTag); err != nil {
return err
}
t.Uid = mTag.Uid
t.Name = mTag.Name
addStr, err := hex.DecodeString(mTag.Address)
if err != nil {
return err
}
t.Address = addStr
t.total = mTag.Total
t.split = mTag.Split
t.seen = mTag.Seen
t.stored = mTag.Stored
t.sent = mTag.Sent
t.synced = mTag.Synced
t.startedAt = mTag.StartedAt
return nil
}
44 changes: 44 additions & 0 deletions chunk/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,47 @@ func TestMarshallingNoAddr(t *testing.T) {
t.Fatalf("expected tag addresses to be equal length")
}
}

func TestJsonMarshallingUnMarshalling(t *testing.T) {
tg := NewTag(111, "test/tag", 10)
tg.Address = []byte{0, 1, 2, 3, 4, 5, 6}

b, err := tg.MarshalJSON()
if err != nil {
t.Fatal(err)
}

unmarshalledTag := &Tag{}
err = unmarshalledTag.UnmarshalJSON(b)
if err != nil {
t.Fatal(err)
}

if unmarshalledTag.Uid != tg.Uid {
t.Fatalf("tag uids not equal. want %d got %d", tg.Uid, unmarshalledTag.Uid)
}

if unmarshalledTag.Name != tg.Name {
t.Fatalf("tag names not equal. want %s got %s", tg.Name, unmarshalledTag.Name)
}

for _, state := range allStates {
uv, tv := unmarshalledTag.Get(state), tg.Get(state)
if uv != tv {
t.Fatalf("state %d inconsistent. expected %d to equal %d", state, uv, tv)
}
}

if unmarshalledTag.Total() != tg.Total() {
t.Fatalf("tag names not equal. want %d got %d", tg.Total(), unmarshalledTag.Total())
}

if len(unmarshalledTag.Address) != len(tg.Address) {
t.Fatalf("tag addresses length mismatch, want %d, got %d", len(tg.Address), len(unmarshalledTag.Address))
}

if !bytes.Equal(unmarshalledTag.Address, tg.Address) {
t.Fatalf("expected tag address to be %v got %v", unmarshalledTag.Address, tg.Address)
}

}