From e7d263099fd49a47b6656236c8953af043fe93ee Mon Sep 17 00:00:00 2001 From: Nazar Kanaev Date: Sat, 4 Jul 2020 14:20:46 +0100 Subject: [PATCH] item.status encoder/decoder --- storage/item.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/storage/item.go b/storage/item.go index dd00a02..8b2400b 100644 --- a/storage/item.go +++ b/storage/item.go @@ -3,6 +3,7 @@ package storage import ( "fmt" "time" + "encoding/json" ) type ItemStatus int @@ -13,6 +14,31 @@ const ( STARRED ItemStatus = 2 ) +var StatusRepresentations = map[ItemStatus]string { + UNREAD: "unread", + READ: "read", + STARRED: "starred", +} + +var StatusValues = map[string]ItemStatus { + "unread": UNREAD, + "read": READ, + "starred": STARRED, +} + +func (s ItemStatus) MarshalJSON() ([]byte, error) { + return json.Marshal(StatusRepresentations[s]) +} + +func (s *ItemStatus) UnmarshalJSON(b []byte) error { + var str string + if err := json.Unmarshal(b, &s); err != nil { + return err + } + *s = StatusValues[str] + return nil +} + type Item struct { Id string `json:"id"` FeedId int64 `json:"feed_id"`