From e69ec49c48f9ea6e8d2db06462fdd0711ef4748b Mon Sep 17 00:00:00 2001 From: "riccardo.montagnin" Date: Thu, 24 Sep 2020 10:22:49 +0200 Subject: [PATCH] Trying fixing migration Signed-off-by: riccardo.montagnin --- x/posts/keeper/legacy/v0.10.0/types.go | 41 ++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/x/posts/keeper/legacy/v0.10.0/types.go b/x/posts/keeper/legacy/v0.10.0/types.go index a42b612610..15f482b26c 100644 --- a/x/posts/keeper/legacy/v0.10.0/types.go +++ b/x/posts/keeper/legacy/v0.10.0/types.go @@ -1,6 +1,7 @@ package v0100 import ( + "sort" "time" sdk "github.com/cosmos/cosmos-sdk/types" @@ -13,6 +14,46 @@ type PostID string // 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 +} + // Attachment contains the information representing any type of file provided with a post. // This file can be an image or a multimedia file (vocals, video, documents, etc.). type Attachment struct {