Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit 7dd251f

Browse files
committed
Fix migration27 JSON serialization bugs
1 parent ef6bdb0 commit 7dd251f

File tree

2 files changed

+114
-4
lines changed

2 files changed

+114
-4
lines changed

repo/migrations/Migration027.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package migrations
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"github.com/OpenBazaar/jsonpb"
67
"github.com/OpenBazaar/openbazaar-go/ipfs"
78
"github.com/OpenBazaar/openbazaar-go/pb"
@@ -85,6 +86,41 @@ type (
8586
}
8687
)
8788

89+
func (c *Migration027V5CurrencyValue) MarshalJSON() ([]byte, error) {
90+
var value = struct {
91+
Amount string `json:"amount"`
92+
Currency Migration027V5CurrencyDefinition `json:"currency"`
93+
}{
94+
Amount: "0",
95+
Currency: c.Currency,
96+
}
97+
if c.Amount != nil {
98+
value.Amount = c.Amount.String()
99+
}
100+
101+
return json.Marshal(value)
102+
103+
}
104+
105+
func (c *Migration027V5CurrencyValue) UnmarshalJSON(b []byte) error {
106+
var value struct {
107+
Amount string `json:"amount"`
108+
Currency Migration027V5CurrencyDefinition `json:"currency"`
109+
}
110+
err := json.Unmarshal(b, &value)
111+
if err != nil {
112+
return err
113+
}
114+
amt, ok := new(big.Int).SetString(value.Amount, 10)
115+
if !ok {
116+
return fmt.Errorf("invalid amount (%s)", value.Amount)
117+
}
118+
119+
c.Amount = amt
120+
c.Currency = value.Currency
121+
return err
122+
}
123+
88124
var divisibilityMap = map[string]uint{
89125
"BTC": 8,
90126
"BCH": 8,
@@ -93,6 +129,22 @@ var divisibilityMap = map[string]uint{
93129
}
94130

95131
func parseV5intoV4(v5 Migration027V5ListingIndexData) Migration027V4ListingIndexData {
132+
if v5.ModeratorIDs == nil {
133+
v5.ModeratorIDs = []string{}
134+
}
135+
if v5.ShipsTo == nil {
136+
v5.ShipsTo = []string{}
137+
}
138+
if v5.FreeShipping == nil {
139+
v5.FreeShipping = []string{}
140+
}
141+
if v5.Categories == nil {
142+
v5.Categories = []string{}
143+
}
144+
if v5.AcceptedCurrencies == nil {
145+
v5.AcceptedCurrencies = []string{}
146+
}
147+
96148
return Migration027V4ListingIndexData{
97149
Hash: v5.Hash,
98150
Slug: v5.Slug,
@@ -132,6 +184,22 @@ func parseV4intoV5(v4 Migration027V4ListingIndexData) Migration027V5ListingIndex
132184
Divisibility: divisibility,
133185
},
134186
}
187+
if v4.ModeratorIDs == nil {
188+
v4.ModeratorIDs = []string{}
189+
}
190+
if v4.ShipsTo == nil {
191+
v4.ShipsTo = []string{}
192+
}
193+
if v4.FreeShipping == nil {
194+
v4.FreeShipping = []string{}
195+
}
196+
if v4.Categories == nil {
197+
v4.Categories = []string{}
198+
}
199+
if v4.AcceptedCurrencies == nil {
200+
v4.AcceptedCurrencies = []string{}
201+
}
202+
135203
return Migration027V5ListingIndexData{
136204
Hash: v4.Hash,
137205
Slug: v4.Slug,
@@ -222,6 +290,7 @@ func (Migration027) Up(repoPath, databasePassword string, testnetEnabled bool) e
222290
continue
223291
}
224292
metadata := metadataJSON.(map[string]interface{})
293+
metadata["version"] = 5
225294
itemJSON, itemExists := listing["item"]
226295
if !itemExists {
227296
continue
@@ -482,6 +551,7 @@ func (Migration027) Down(repoPath, databasePassword string, testnetEnabled bool)
482551
continue
483552
}
484553
metadata := metadataJSON.(map[string]interface{})
554+
metadata["version"] = 4
485555
itemJSON, itemExists := listing["item"]
486556
if !itemExists {
487557
continue

repo/migrations/Migration027_test.go

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var (
2727
"listing": {
2828
"slug": "Migration027_test_listing",
2929
"metadata": {
30+
"version": 4,
3031
"contractType": "PHYSICAL_GOOD",
3132
"format": "FIXED_PRICE",
3233
"pricingCurrency": "BTC",
@@ -58,13 +59,14 @@ var (
5859
}
5960
]
6061
},
61-
"signature": "2+M/+M866ZaPyqHpBdsqJ9gCgLPNOQoKKtaOrruZmDu6YXhc3RiKQtoZs1BTfC02k8TwfIU5LeFPhxagwC6UAg=="
62+
"signature": "HmJCQ+XGPOYgs6GWiBYWAyEi+J8t3yLcuzJlf396hI0isnLdQlffNxPnU/uxw6kqFIFyINYUYkXd/O4QPFpxDg=="
6263
}`
6364

6465
preMigration027CryptoListingJSON = `{
6566
"listing": {
6667
"slug": "Migration027_test_crypto_listing",
6768
"metadata": {
69+
"version": 4,
6870
"contractType": "CRYPTOCURRENCY",
6971
"format": "MARKET_PRICE",
7072
"coinType": "BAT",
@@ -79,13 +81,14 @@ var (
7981
]
8082
}
8183
},
82-
"signature": "7vmvT69nQQyCmp3Yng7ROWXOiJx0VKGbNRYN8wgGJSqsnRYXC9BEd81t8dd5Mm1wHWK6egGk2rEpct+qWaWdBA=="
84+
"signature": "h00CONM6qpY5yC5SRrpWgj/UGKUizLM6FNKLFMP5+feg/BytfmP0On9iZbLr/qoAYWetXfc1WiEuAWXv1vHrAg=="
8385
}`
8486

8587
postMigration027ListingJSON = `{
8688
"listing": {
8789
"slug": "Migration027_test_listing",
8890
"metadata": {
91+
"version": 5,
8992
"contractType": "PHYSICAL_GOOD",
9093
"format": "FIXED_PRICE"
9194
},
@@ -120,13 +123,14 @@ var (
120123
}
121124
]
122125
},
123-
"signature": "r7j6YM3ePiTRyIKmd9ILMvjvhv5jaAB5m1gMlPJ9ug05KZIfAPXXa+EtuF+ExGvtEOpdyK3BEFpU2rm65CyvDw=="
126+
"signature": "TrlzcAflSbJWLJ+efqTIREIeSTFwfCDAWbEMaa6iTSSgvROxgKyGt1fGMfrWleLsjObhIVXJKga5jZVZcIkPDw=="
124127
}`
125128

126129
postMigration027CryptoListingJSON = `{
127130
"listing": {
128131
"slug": "Migration027_test_crypto_listing",
129132
"metadata": {
133+
"version": 5,
130134
"contractType": "CRYPTOCURRENCY",
131135
"format": "MARKET_PRICE",
132136
"coinType": "BAT",
@@ -141,7 +145,7 @@ var (
141145
"priceModifier": 50
142146
}
143147
},
144-
"signature": "37KoDxNC4lYTd8hZQHzgpvJ4GJsXVTUp89D1LhDMapgFUhGmFSsiPAjkgdez7Y9wJ7lfH4ouZRrUcw/81kcCAQ=="
148+
"signature": "s0GBGyIYcClzUJ3SgWtRM3l3yi7FE/P52OTf0nTh0M21FKiWJQ8lIDdyUiDDdW8T8l8PLjE/QaToV45ZwlziDQ=="
145149
}`
146150
)
147151

@@ -305,6 +309,24 @@ func TestMigration027(t *testing.T) {
305309
t.Fatal(err)
306310
}
307311

312+
for _, l := range listingIndex {
313+
if l.ModeratorIDs == nil {
314+
t.Errorf("ModeratorIDs is null")
315+
}
316+
if l.ShipsTo == nil {
317+
t.Errorf("ShipsTo is null")
318+
}
319+
if l.FreeShipping == nil {
320+
t.Errorf("FreeShipping is null")
321+
}
322+
if l.Categories == nil {
323+
t.Errorf("Categories is null")
324+
}
325+
if l.AcceptedCurrencies == nil {
326+
t.Errorf("AcceptedCurrencies is null")
327+
}
328+
}
329+
308330
if listingIndex[0].Price.Amount.String() != strconv.Itoa(int(index[0].Price.Amount)) {
309331
t.Errorf("Incorrect price set")
310332
}
@@ -410,6 +432,24 @@ func TestMigration027(t *testing.T) {
410432
t.Fatal(err)
411433
}
412434

435+
for _, l := range listingIndex2 {
436+
if l.ModeratorIDs == nil {
437+
t.Errorf("ModeratorIDs is null")
438+
}
439+
if l.ShipsTo == nil {
440+
t.Errorf("ShipsTo is null")
441+
}
442+
if l.FreeShipping == nil {
443+
t.Errorf("FreeShipping is null")
444+
}
445+
if l.Categories == nil {
446+
t.Errorf("Categories is null")
447+
}
448+
if l.AcceptedCurrencies == nil {
449+
t.Errorf("AcceptedCurrencies is null")
450+
}
451+
}
452+
413453
if listingIndex[0].Price.Amount.String() != strconv.Itoa(int(listingIndex2[0].Price.Amount)) {
414454
t.Errorf("Incorrect price set")
415455
}

0 commit comments

Comments
 (0)