This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
/
listing_index.go
145 lines (137 loc) · 4.73 KB
/
listing_index.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package repo
import (
"encoding/json"
"fmt"
)
type (
ListingThumbnail struct {
Tiny string `json:"tiny"`
Small string `json:"small"`
Medium string `json:"medium"`
}
// ListingIndexData reprents a single node in the Listing index
ListingIndexData struct {
Hash string `json:"hash"`
Slug string `json:"slug"`
Title string `json:"title"`
Categories []string `json:"categories"`
NSFW bool `json:"nsfw"`
ContractType string `json:"contractType"`
Description string `json:"description"`
Thumbnail ListingThumbnail `json:"thumbnail"`
Price *CurrencyValue `json:"price"`
Modifier float32 `json:"modifier"`
ShipsTo []string `json:"shipsTo"`
FreeShipping []string `json:"freeShipping"`
Language string `json:"language"`
AverageRating float32 `json:"averageRating"`
RatingCount uint32 `json:"ratingCount"`
ModeratorIDs []string `json:"moderators"`
AcceptedCurrencies []string `json:"acceptedCurrencies"`
CryptoCurrencyCode string `json:"coinType"`
}
)
// UnmarshalJSONSignedListingIndex consumes a []byte payload of JSON representing
// a list of SignedListings and returns a parsed instance or an error if the payload
// cannot be successfully parsed
func UnmarshalJSONSignedListingIndex(data []byte) ([]ListingIndexData, error) {
var (
rawIndex []json.RawMessage
listingIndex []ListingIndexData
)
if err := json.Unmarshal(data, &rawIndex); err != nil {
return nil, err
}
// best effort parse
// TODO: intelligently parse payload based on
// detection of the correct version.
for _, listingJSON := range rawIndex {
l, err := parseUnknownSchemaData(listingJSON)
if err != nil {
return nil, err
}
listingIndex = append(listingIndex, l)
}
return listingIndex, nil
}
func parseUnknownSchemaData(data []byte) (ListingIndexData, error) {
sl, err := parseV5Data(data)
if err == nil {
return sl, nil
} else {
log.Debugf("failed attempt to parse as v5 listing index: %s", err)
}
sl, err = parseV4Data(data)
if err == nil {
return sl, nil
} else {
log.Debugf("failed attempt to parse as v4 listing index: %s", err)
}
return ListingIndexData{}, fmt.Errorf("failed parsing listing in index: %s", err)
}
func parseV5Data(data []byte) (ListingIndexData, error) {
var v5 ListingIndexData
if err := json.Unmarshal(data, &v5); err != nil {
return ListingIndexData{}, err
}
return v5, nil
}
func parseV4Data(data []byte) (ListingIndexData, error) {
var v4 struct {
Hash string `json:"hash"`
Slug string `json:"slug"`
Title string `json:"title"`
Categories []string `json:"categories"`
NSFW bool `json:"nsfw"`
ContractType string `json:"contractType"`
Description string `json:"description"`
Thumbnail ListingThumbnail `json:"thumbnail"`
Price struct {
CurrencyCode string `json:"currencyCode"`
Amount uint `json:"amount"`
Modifier float32 `json:"modifier"`
} `json:"price"`
ShipsTo []string `json:"shipsTo"`
FreeShipping []string `json:"freeShipping"`
Language string `json:"language"`
AverageRating float32 `json:"averageRating"`
RatingCount uint32 `json:"ratingCount"`
ModeratorIDs []string `json:"moderators"`
AcceptedCurrencies []string `json:"acceptedCurrencies"`
CryptoCurrencyCode string `json:"coinType"`
}
if err := json.Unmarshal(data, &v4); err != nil {
return ListingIndexData{}, err
}
var priceValue *CurrencyValue
if v4.Price.CurrencyCode != "" {
priceDef, err := AllCurrencies().Lookup(v4.Price.CurrencyCode)
if err != nil {
return ListingIndexData{}, err
}
priceValue, err = NewCurrencyValueFromUint(uint64(v4.Price.Amount), priceDef)
if err != nil {
return ListingIndexData{}, err
}
}
return ListingIndexData{
Hash: v4.Hash,
Slug: v4.Slug,
Title: v4.Title,
Categories: v4.Categories,
NSFW: v4.NSFW,
ContractType: v4.ContractType,
Description: v4.Description,
Thumbnail: v4.Thumbnail,
Modifier: v4.Price.Modifier,
Price: priceValue,
ShipsTo: v4.ShipsTo,
FreeShipping: v4.FreeShipping,
Language: v4.Language,
AverageRating: v4.AverageRating,
RatingCount: v4.RatingCount,
ModeratorIDs: v4.ModeratorIDs,
AcceptedCurrencies: v4.AcceptedCurrencies,
CryptoCurrencyCode: v4.CryptoCurrencyCode,
}, nil
}