Skip to content

Commit

Permalink
fix: MarketPrice add UnmarshalJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
SevereCloud committed Dec 4, 2020
1 parent 0913475 commit 931a245
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
22 changes: 22 additions & 0 deletions object/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,28 @@ type MarketPrice struct {
OldAmountText string `json:"old_amount_text"`
}

// UnmarshalJSON MarketPrice.
//
// BUG(VK): unavailable product, in fave.get return [].
func (m *MarketPrice) UnmarshalJSON(data []byte) error {
if bytes.Equal(data, []byte("[]")) {
return nil
}

type renamedMarketPrice MarketPrice

var r renamedMarketPrice

err := json.Unmarshal(data, &r)
if err != nil {
return err
}

*m = MarketPrice(r)

return nil
}

// MarketSection struct.
type MarketSection struct {
ID int `json:"id"` // Section ID
Expand Down
19 changes: 19 additions & 0 deletions object/market_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ func TestMarketMarketItem__UnmarshalJSON(t *testing.T) {
assert.Error(t, err)
}

func TestMarketPrice__UnmarshalJSON(t *testing.T) {
t.Parallel()

f := func(data []byte, wantMarket object.MarketPrice) {
var market object.MarketPrice

err := json.Unmarshal(data, &market)
assert.NoError(t, err)
assert.Equal(t, wantMarket, market)
}

f([]byte("[]"), object.MarketPrice{})
f([]byte(`{"text":"a"}`), object.MarketPrice{Text: "a"})

var market object.MarketPrice
err := json.Unmarshal([]byte("0"), &market)
assert.Error(t, err)
}

func TestMarketMarketAlbum_ToAttachment(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 931a245

Please sign in to comment.