Skip to content

Commit

Permalink
Merge branch 'version/0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
RiccardoM committed Jan 14, 2020
2 parents 521c2e1 + f25f7dd commit 39d1129
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 45 deletions.
12 changes: 0 additions & 12 deletions x/posts/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,5 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, data GenesisState) []abci.Valid
}
}

for postID, reactions := range data.Reactions {
for _, reaction := range reactions {
postID, err := ParsePostID(postID)
if err != nil {
panic(err)
}
if err := keeper.SaveReaction(ctx, postID, reaction); err != nil {
panic(err)
}
}
}

return []abci.ValidatorUpdate{}
}
110 changes: 95 additions & 15 deletions x/posts/internal/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,50 +58,130 @@ func TestKeeper_SavePost(t *testing.T) {
{
name: "Post with ID already present",
existingPosts: types.Posts{
types.NewPost(types.PostID(1), types.PostID(0), "Post", false, "desmos", map[string]string{}, testPost.Created, testPost.Creator),
types.NewPost(types.PostID(1),
types.PostID(0),
"Post",
false,
"desmos",
map[string]string{},
testPost.Created,
testPost.Creator,
),
},
lastPostID: types.PostID(1),
newPost: types.NewPost(types.PostID(1), types.PostID(0), "New post", false, "desmos", map[string]string{}, testPost.Created, testPost.Creator),
lastPostID: types.PostID(1),
newPost: types.NewPost(types.PostID(1),
types.PostID(0),
"New post",
false,
"desmos",
map[string]string{},
testPost.Created,
testPost.Creator,
),
expParentCommentsIDs: []types.PostID{},
expLastID: types.PostID(1),
},
{
name: "Post which ID is not already present",
existingPosts: types.Posts{
types.NewPost(types.PostID(1), types.PostID(0), "Post", false, "desmos", map[string]string{}, testPost.Created, testPost.Creator),
types.NewPost(types.PostID(1),
types.PostID(0),
"Post",
false,
"desmos",
map[string]string{},
testPost.Created,
testPost.Creator,
),
},
lastPostID: types.PostID(1),
newPost: types.NewPost(types.PostID(15), types.PostID(0), "New post", false, "desmos", map[string]string{}, testPost.Created, testPost.Creator),
lastPostID: types.PostID(1),
newPost: types.NewPost(types.PostID(15),
types.PostID(0),
"New post",
false,
"desmos",
map[string]string{},
testPost.Created,
testPost.Creator,
),
expParentCommentsIDs: []types.PostID{},
expLastID: types.PostID(15),
},
{
name: "Post with valid parent ID",
existingPosts: []types.Post{
types.NewPost(types.PostID(1), types.PostID(0), "Parent", false, "desmos", map[string]string{}, testPost.Created, testPost.Creator),
types.NewPost(types.PostID(1),
types.PostID(0),
"Parent",
false,
"desmos",
map[string]string{},
testPost.Created,
testPost.Creator,
),
},
lastPostID: types.PostID(1),
newPost: types.NewPost(types.PostID(15), types.PostID(1), "Comment", false, "desmos", map[string]string{}, testPost.Created, testPost.Creator),
lastPostID: types.PostID(1),
newPost: types.NewPost(types.PostID(15),
types.PostID(1),
"Comment",
false,
"desmos",
map[string]string{},
testPost.Created,
testPost.Creator,
),
expParentCommentsIDs: []types.PostID{types.PostID(15)},
expLastID: types.PostID(15),
},
{
name: "Post with ID greater ID than Last ID stored",
existingPosts: types.Posts{
types.NewPost(types.PostID(4), types.PostID(0), "Post lesser", false, "desmos", map[string]string{}, testPost.Created, testPostOwner),
types.NewPost(types.PostID(4),
types.PostID(0),
"Post lesser",
false,
"desmos",
map[string]string{},
testPost.Created,
testPostOwner,
),
},
lastPostID: types.PostID(4),
newPost: types.NewPost(types.PostID(5), types.PostID(0), "New post greater", false, "desmos", map[string]string{}, testPost.Created, testPostOwner),
lastPostID: types.PostID(4),
newPost: types.NewPost(types.PostID(5),
types.PostID(0),
"New post greater",
false,
"desmos",
map[string]string{"key": "value"},
testPost.Created,
testPostOwner,
),
expParentCommentsIDs: []types.PostID{},
expLastID: types.PostID(5),
},
{
name: "Post with ID lesser ID than Last ID stored",
existingPosts: types.Posts{
types.NewPost(types.PostID(4), types.PostID(0), "Post ID greater", false, "desmos", map[string]string{}, testPost.Created, testPostOwner),
types.NewPost(types.PostID(4),
types.PostID(0),
"Post ID greater",
false,
"desmos",
map[string]string{},
testPost.Created,
testPostOwner,
),
},
lastPostID: types.PostID(4),
newPost: types.NewPost(types.PostID(3), types.PostID(0), "New post ID lesser", false, "desmos", map[string]string{}, testPost.Created, testPostOwner),
lastPostID: types.PostID(4),
newPost: types.NewPost(types.PostID(3),
types.PostID(0),
"New post ID lesser",
false,
"desmos",
map[string]string{},
testPost.Created,
testPostOwner,
),
expParentCommentsIDs: []types.PostID{},
expLastID: types.PostID(4),
},
Expand Down
33 changes: 24 additions & 9 deletions x/posts/internal/types/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ func (ids PostIDs) AppendIfMissing(id PostID) (PostIDs, bool) {

// Post is a struct of a post
type Post struct {
PostID PostID `json:"id"` // Unique id
ParentID PostID `json:"parent_id"` // Post of which this one is a comment
Message string `json:"message"` // Message contained inside the post
Created time.Time `json:"created"` // RFC3339 date at which the post has been created
LastEdited time.Time `json:"last_edited"` // RFC3339 date at which the post has been edited the last time
AllowsComments bool `json:"allows_comments"` // Tells if users can reference this PostID as the parent
Subspace string `json:"subspace"` // Identifies the application that has posted the message
OptionalData map[string]string `json:"optional_data,omitempty"` // Arbitrary data that can be used from the developers
Creator sdk.AccAddress `json:"creator"` // Creator of the Post
PostID PostID `json:"id"` // Unique id
ParentID PostID `json:"parent_id"` // Post of which this one is a comment
Message string `json:"message"` // Message contained inside the post
Created time.Time `json:"created"` // RFC3339 date at which the post has been created
LastEdited time.Time `json:"last_edited"` // RFC3339 date at which the post has been edited the last time
AllowsComments bool `json:"allows_comments"` // Tells if users can reference this PostID as the parent
Subspace string `json:"subspace"` // Identifies the application that has posted the message
OptionalData OptionalData `json:"optional_data,omitempty"` // Arbitrary data that can be used from the developers
Creator sdk.AccAddress `json:"creator"` // Creator of the Post
}

func NewPost(id, parentID PostID, message string, allowsComments bool, subspace string, optionalData map[string]string,
Expand All @@ -136,6 +136,21 @@ func NewPost(id, parentID PostID, message string, allowsComments bool, subspace
}
}

func NewPostComplete(id, parentID PostID, message string, created, lastEdited time.Time, allowsComments bool,
subspace string, optionalData map[string]string, creator sdk.AccAddress) Post {
return Post{
PostID: id,
ParentID: parentID,
Message: message,
Created: created,
LastEdited: lastEdited,
AllowsComments: allowsComments,
Subspace: subspace,
OptionalData: optionalData,
Creator: creator,
}
}

// String implements fmt.Stringer
func (p Post) String() string {
bytes, err := json.Marshal(&p)
Expand Down
65 changes: 65 additions & 0 deletions x/posts/internal/types/post_optional_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package types

import (
"encoding/json"
"sort"
)

// OptionalData represents a Posts' optional data and allows for custom
// Amino and JSON serialization and deserialization.
type OptionalData map[string]string

// KeyValue is a simple key/value representation of one field of a OptionalData.
type KeyValue struct {
Key string
Value string
}

// MarshalAmino transforms the OptionalData to an array of key/value.
func (m OptionalData) MarshalAmino() ([]KeyValue, error) {
fieldKeys := make([]string, len(m))
i := 0
for key := range m {
fieldKeys[i] = key
i++
}

sort.Stable(sort.StringSlice(fieldKeys))

p := make([]KeyValue, len(m))
for i, key := range fieldKeys {
p[i] = KeyValue{
Key: key,
Value: m[key],
}
}

return p, nil
}

// UnmarshalAmino transforms the key/value array to a OptionalData.
func (m *OptionalData) UnmarshalAmino(keyValues []KeyValue) error {
tempMap := make(map[string]string, len(keyValues))
for _, p := range keyValues {
tempMap[p.Key] = p.Value
}

*m = tempMap

return nil
}

// MarshalJSON implements encode.Marshaler
func (m OptionalData) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]string(m))
}

// UnmarshalJSON implements decode.Unmarshaler
func (m *OptionalData) UnmarshalJSON(data []byte) error {
var value map[string]string
if err := json.Unmarshal(data, &value); err != nil {
return err
}
*m = value
return nil
}
20 changes: 11 additions & 9 deletions x/posts/legacy/v0.2.0/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@ type GenesisState struct {
// PostID represents a unique post id
type PostID uint64

type OptionalData map[string]string

// Post is a struct of a post
type Post struct {
PostID PostID `json:"id"` // Unique id
ParentID PostID `json:"parent_id"` // Post of which this one is a comment
Message string `json:"message"` // Message contained inside the post
Created time.Time `json:"created"` // RFC3339 date at which the post has been created
LastEdited time.Time `json:"last_edited"` // RFC3339 date at which the post has been edited the last time
AllowsComments bool `json:"allows_comments"` // Tells if users can reference this PostID as the parent
Subspace string `json:"subspace"` // Identifies the application that has posted the message
OptionalData map[string]string `json:"optional_data,omitempty"` // Arbitrary data that can be used from the developers
Creator sdk.AccAddress `json:"creator"` // Creator of the Post
PostID PostID `json:"id"` // Unique id
ParentID PostID `json:"parent_id"` // Post of which this one is a comment
Message string `json:"message"` // Message contained inside the post
Created time.Time `json:"created"` // RFC3339 date at which the post has been created
LastEdited time.Time `json:"last_edited"` // RFC3339 date at which the post has been edited the last time
AllowsComments bool `json:"allows_comments"` // Tells if users can reference this PostID as the parent
Subspace string `json:"subspace"` // Identifies the application that has posted the message
OptionalData OptionalData `json:"optional_data,omitempty"` // Arbitrary data that can be used from the developers
Creator sdk.AccAddress `json:"creator"` // Creator of the Post
}

// Reaction is a struct of a user reaction to a post
Expand Down

0 comments on commit 39d1129

Please sign in to comment.