From efaa109ebc7efc25b22ae4809d723503a9867131 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Kupka?= Date: Thu, 17 Aug 2017 21:34:39 +0200 Subject: [PATCH] Add CommentOperation.JsonMetadata Had to move ContentMetadata from apis/database into types. --- apis/database/data.go | 105 +++++++++++------------------------------- types/operations.go | 67 ++++++++++++++++++++++++--- 2 files changed, 88 insertions(+), 84 deletions(-) diff --git a/apis/database/data.go b/apis/database/data.go index c9c04f9..0248788 100644 --- a/apis/database/data.go +++ b/apis/database/data.go @@ -1,11 +1,6 @@ package database import ( - // Stdlib - "encoding/json" - "strconv" - "strings" - // RPC "github.com/go-steem/rpc/types" ) @@ -57,85 +52,39 @@ type Block struct { } type Content struct { - Id *types.ID `json:"id"` - RootTitle string `json:"root_title"` - Active *types.Time `json:"active"` - AbsRshares *types.Int `json:"abs_rshares"` - PendingPayoutValue string `json:"pending_payout_value"` - TotalPendingPayoutValue string `json:"total_pending_payout_value"` - Category string `json:"category"` - Title string `json:"title"` - LastUpdate *types.Time `json:"last_update"` - Stats string `json:"stats"` - Body string `json:"body"` - Created *types.Time `json:"created"` - Replies []*Content `json:"replies"` - Permlink string `json:"permlink"` - JsonMetadata *ContentMetadata `json:"json_metadata"` - Children *types.Int `json:"children"` - NetRshares *types.Int `json:"net_rshares"` - URL string `json:"url"` - ActiveVotes []*VoteState `json:"active_votes"` - ParentPermlink string `json:"parent_permlink"` - CashoutTime *types.Time `json:"cashout_time"` - TotalPayoutValue string `json:"total_payout_value"` - ParentAuthor string `json:"parent_author"` - ChildrenRshares2 *types.Int `json:"children_rshares2"` - Author string `json:"author"` - Depth *types.Int `json:"depth"` - TotalVoteWeight *types.Int `json:"total_vote_weight"` + Id *types.ID `json:"id"` + RootTitle string `json:"root_title"` + Active *types.Time `json:"active"` + AbsRshares *types.Int `json:"abs_rshares"` + PendingPayoutValue string `json:"pending_payout_value"` + TotalPendingPayoutValue string `json:"total_pending_payout_value"` + Category string `json:"category"` + Title string `json:"title"` + LastUpdate *types.Time `json:"last_update"` + Stats string `json:"stats"` + Body string `json:"body"` + Created *types.Time `json:"created"` + Replies []*Content `json:"replies"` + Permlink string `json:"permlink"` + JsonMetadata *types.ContentMetadata `json:"json_metadata"` + Children *types.Int `json:"children"` + NetRshares *types.Int `json:"net_rshares"` + URL string `json:"url"` + ActiveVotes []*VoteState `json:"active_votes"` + ParentPermlink string `json:"parent_permlink"` + CashoutTime *types.Time `json:"cashout_time"` + TotalPayoutValue string `json:"total_payout_value"` + ParentAuthor string `json:"parent_author"` + ChildrenRshares2 *types.Int `json:"children_rshares2"` + Author string `json:"author"` + Depth *types.Int `json:"depth"` + TotalVoteWeight *types.Int `json:"total_vote_weight"` } func (content *Content) IsStory() bool { return content.ParentAuthor == "" } -type ContentMetadata struct { - Flag bool - Users []string - Tags []string - Image []string -} - -type ContentMetadataRaw struct { - Users types.StringSlice `json:"users"` - Tags types.StringSlice `json:"tags"` - Image types.StringSlice `json:"image"` -} - -func (metadata *ContentMetadata) UnmarshalJSON(data []byte) error { - unquoted, err := strconv.Unquote(string(data)) - if err != nil { - return err - } - - switch unquoted { - case "true": - metadata.Flag = true - return nil - case "false": - metadata.Flag = false - return nil - } - - if len(unquoted) == 0 { - var value ContentMetadata - metadata = &value - return nil - } - - var raw ContentMetadataRaw - if err := json.NewDecoder(strings.NewReader(unquoted)).Decode(&raw); err != nil { - return err - } - - metadata.Users = raw.Users - metadata.Tags = raw.Tags - metadata.Image = raw.Image - - return nil -} - type VoteState struct { Voter string `json:"voter"` Weight *types.Int `json:"weight"` diff --git a/types/operations.go b/types/operations.go index 84e729b..bccd3e7 100644 --- a/types/operations.go +++ b/types/operations.go @@ -3,6 +3,8 @@ package types import ( // Stdlib "encoding/json" + "strconv" + "strings" // RPC "github.com/go-steem/rpc/encoding/transaction" @@ -287,12 +289,13 @@ func (op *AccountWitnessProxyOperation) Data() interface{} { // In case Title is filled in and ParentAuthor is empty, it is a new post. // The post category can be read from ParentPermlink. type CommentOperation struct { - Author string `json:"author"` - Title string `json:"title"` - Permlink string `json:"permlink"` - ParentAuthor string `json:"parent_author"` - ParentPermlink string `json:"parent_permlink"` - Body string `json:"body"` + Author string `json:"author"` + Title string `json:"title"` + Permlink string `json:"permlink"` + ParentAuthor string `json:"parent_author"` + ParentPermlink string `json:"parent_permlink"` + Body string `json:"body"` + JsonMetadata *ContentMetadata `json:"json_metadata"` } func (op *CommentOperation) Type() OpType { @@ -307,6 +310,58 @@ func (op *CommentOperation) IsStoryOperation() bool { return op.ParentAuthor == "" } +type ContentMetadata struct { + Flag bool + Users []string + Tags []string + Image []string + App string + Format string +} + +type ContentMetadataRaw struct { + Users StringSlice `json:"users"` + Tags StringSlice `json:"tags"` + Image StringSlice `json:"image"` + App string `json:"app"` + Format string `json:"format"` +} + +func (metadata *ContentMetadata) UnmarshalJSON(data []byte) error { + unquoted, err := strconv.Unquote(string(data)) + if err != nil { + return err + } + + switch unquoted { + case "true": + metadata.Flag = true + return nil + case "false": + metadata.Flag = false + return nil + } + + if len(unquoted) == 0 { + var value ContentMetadata + metadata = &value + return nil + } + + var raw ContentMetadataRaw + if err := json.NewDecoder(strings.NewReader(unquoted)).Decode(&raw); err != nil { + return err + } + + metadata.Users = raw.Users + metadata.Tags = raw.Tags + metadata.Image = raw.Image + metadata.App = raw.App + metadata.Format = raw.Format + + return nil +} + // FC_REFLECT( steemit::chain::vote_operation, // (voter) // (author)