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

Commit 2e95244

Browse files
committed
[#1904] Add godocs; Change receiver variable for consistency
1 parent cef4f97 commit 2e95244

File tree

4 files changed

+74
-58
lines changed

4 files changed

+74
-58
lines changed

core/profile.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (n *OpenBazaarNode) FetchProfile(peerID string, useCache bool) (pb.Profile,
5454
if err != nil {
5555
return pro, err
5656
}
57-
p.NormalizeSchema()
57+
p.NormalizeDataForAllSchemas()
5858
return *p.GetProtobuf(), nil
5959
}
6060

@@ -193,7 +193,7 @@ func (n *OpenBazaarNode) PatchProfile(patch map[string]interface{}) error {
193193
return fmt.Errorf("building profile for validation: %s", err.Error())
194194
}
195195

196-
repoProfile.NormalizeSchema()
196+
repoProfile.NormalizeDataForAllSchemas()
197197

198198
if err := repoProfile.Valid(); err != nil {
199199
return fmt.Errorf("invalid profile: %s", err.Error())

repo/currency.go

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -115,103 +115,103 @@ func NewCurrencyValue(amount string, currency CurrencyDefinition) (*CurrencyValu
115115
}
116116

117117
// AmountInt64 returns a valid int64 or an error
118-
func (v *CurrencyValue) AmountInt64() (int64, error) {
119-
if !v.Amount.IsInt64() {
118+
func (c *CurrencyValue) AmountInt64() (int64, error) {
119+
if !c.Amount.IsInt64() {
120120
return 0, ErrCurrencyValueInsufficientPrecision
121121
}
122-
return v.Amount.Int64(), nil
122+
return c.Amount.Int64(), nil
123123
}
124124

125125
// AmountUint64 returns a valid int64 or an error
126-
func (v *CurrencyValue) AmountUint64() (uint64, error) {
127-
if !v.Amount.IsUint64() {
126+
func (c *CurrencyValue) AmountUint64() (uint64, error) {
127+
if !c.Amount.IsUint64() {
128128
return 0, ErrCurrencyValueInsufficientPrecision
129129
}
130-
return v.Amount.Uint64(), nil
130+
return c.Amount.Uint64(), nil
131131
}
132132

133133
// AmountString returns the string representation of the amount
134-
func (v *CurrencyValue) AmountString() string {
135-
if v == nil || v.Amount == nil {
134+
func (c *CurrencyValue) AmountString() string {
135+
if c == nil || c.Amount == nil {
136136
return "0"
137137
}
138-
return v.Amount.String()
138+
return c.Amount.String()
139139
}
140140

141141
// String returns a string representation of a CurrencyValue
142-
func (v *CurrencyValue) String() string {
143-
if v == nil {
142+
func (c *CurrencyValue) String() string {
143+
if c == nil {
144144
return new(CurrencyValue).String()
145145
}
146-
return fmt.Sprintf("%s %s", v.Amount.String(), v.Currency.String())
146+
return fmt.Sprintf("%s %s", c.Amount.String(), c.Currency.String())
147147
}
148148

149149
// Valid returns an error if the CurrencyValue is invalid
150-
func (v *CurrencyValue) Valid() error {
151-
if v.Amount == nil {
150+
func (c *CurrencyValue) Valid() error {
151+
if c.Amount == nil {
152152
return ErrCurrencyValueAmountInvalid
153153
}
154-
if err := v.Currency.Valid(); err != nil {
154+
if err := c.Currency.Valid(); err != nil {
155155
return err
156156
}
157157
return nil
158158
}
159159

160160
// Equal indicates if the amount and variety of currency is equivalent
161-
func (v *CurrencyValue) Equal(other *CurrencyValue) bool {
162-
if v == nil && other == nil {
161+
func (c *CurrencyValue) Equal(other *CurrencyValue) bool {
162+
if c == nil && other == nil {
163163
return true
164164
}
165-
if v == nil || other == nil {
165+
if c == nil || other == nil {
166166
return false
167167
}
168-
if !v.Currency.Equal(other.Currency) {
169-
if v.Currency.Code == other.Currency.Code {
170-
vN, err := v.Normalize()
168+
if !c.Currency.Equal(other.Currency) {
169+
if c.Currency.Code == other.Currency.Code {
170+
cN, err := c.Normalize()
171171
if err != nil {
172172
return false
173173
}
174174
oN, err := other.Normalize()
175175
if err != nil {
176176
return false
177177
}
178-
return vN.Amount.Cmp(oN.Amount) == 0
178+
return cN.Amount.Cmp(oN.Amount) == 0
179179
}
180180
return false
181181
}
182-
return v.Amount.Cmp(other.Amount) == 0
182+
return c.Amount.Cmp(other.Amount) == 0
183183
}
184184

185185
// Normalize updates the CurrencyValue to match the divisibility of the locally defined CurrencyDefinition
186-
func (v *CurrencyValue) Normalize() (*CurrencyValue, error) {
187-
localDef, err := AllCurrencies().Lookup(string(v.Currency.Code))
186+
func (c *CurrencyValue) Normalize() (*CurrencyValue, error) {
187+
localDef, err := AllCurrencies().Lookup(string(c.Currency.Code))
188188
if err != nil {
189189
return nil, err
190190
}
191-
val, _, err := v.AdjustDivisibility(localDef.Divisibility)
191+
val, _, err := c.AdjustDivisibility(localDef.Divisibility)
192192
return val, err
193193
}
194194

195195
// AdjustDivisibility updates the Currency.Divisibility and adjusts the Amount to match the new
196196
// value. An error will be returned if the new divisibility is invalid or produces an unreliable
197197
// result. This is a helper function which is equivalent to ConvertTo using a copy of the
198198
// CurrencyDefinition using the updated divisibility and an exchangeRatio of 1.0
199-
func (v *CurrencyValue) AdjustDivisibility(div uint) (*CurrencyValue, big.Accuracy, error) {
200-
if v.Currency.Divisibility == div {
201-
return v, 0, nil
199+
func (c *CurrencyValue) AdjustDivisibility(div uint) (*CurrencyValue, big.Accuracy, error) {
200+
if c.Currency.Divisibility == div {
201+
return c, 0, nil
202202
}
203-
defWithNewDivisibility := v.Currency
203+
defWithNewDivisibility := c.Currency
204204
defWithNewDivisibility.Divisibility = div
205-
return v.ConvertTo(defWithNewDivisibility, 1.0)
205+
return c.ConvertTo(defWithNewDivisibility, 1.0)
206206
}
207207

208208
// ConvertTo will perform the following math given its arguments are valid:
209-
// v.Amount * exchangeRatio * (final.Currency.Divisibility/v.Currency.Divisibility)
210-
// where v is the receiver, exchangeRatio is the ratio of (1 final.Currency/v.Currency)
211-
// v and final must both be Valid() and exchangeRatio must not be zero. The accuracy
209+
// c.Amount * exchangeRatio * (final.Currency.Divisibility/c.Currency.Divisibility)
210+
// where c is the receiver, exchangeRatio is the ratio of (1 final.Currency/c.Currency)
211+
// c and final must both be Valid() and exchangeRatio must not be zero. The accuracy
212212
// indicates if decimal values were trimmed when converting the value back to integer.
213-
func (v *CurrencyValue) ConvertTo(final CurrencyDefinition, exchangeRatio float64) (*CurrencyValue, big.Accuracy, error) {
214-
if err := v.Valid(); err != nil {
213+
func (c *CurrencyValue) ConvertTo(final CurrencyDefinition, exchangeRatio float64) (*CurrencyValue, big.Accuracy, error) {
214+
if err := c.Valid(); err != nil {
215215
return nil, 0, fmt.Errorf("cannot convert invalid value: %s", err.Error())
216216
}
217217
if err := final.Valid(); err != nil {
@@ -221,15 +221,15 @@ func (v *CurrencyValue) ConvertTo(final CurrencyDefinition, exchangeRatio float6
221221
return nil, 0, ErrCurrencyValueNegativeRate
222222
}
223223

224-
amt := new(big.Float).SetInt(v.Amount)
224+
amt := new(big.Float).SetInt(c.Amount)
225225
exRatio := new(big.Float).SetFloat64(exchangeRatio)
226226
if exRatio == nil {
227227
return nil, 0, fmt.Errorf("exchange ratio (%f) is invalid", exchangeRatio)
228228
}
229229
newAmount := new(big.Float).SetPrec(53).Mul(amt, exRatio)
230230

231-
if v.Currency.Divisibility != final.Divisibility {
232-
initMagnitude := math.Pow10(int(v.Currency.Divisibility))
231+
if c.Currency.Divisibility != final.Divisibility {
232+
initMagnitude := math.Pow10(int(c.Currency.Divisibility))
233233
finalMagnitude := math.Pow10(int(final.Divisibility))
234234
divisibilityRatio := new(big.Float).SetFloat64(finalMagnitude / initMagnitude)
235235
newAmount.Mul(newAmount, divisibilityRatio)
@@ -249,39 +249,39 @@ func (v *CurrencyValue) ConvertTo(final CurrencyDefinition, exchangeRatio float6
249249

250250
// Cmp exposes the (*big.Int).Cmp behavior after verifying currency and adjusting
251251
// for different currency divisibilities.
252-
func (v *CurrencyValue) Cmp(other *CurrencyValue) (int, error) {
253-
if v.Currency.Code.String() != other.Currency.Code.String() {
252+
func (c *CurrencyValue) Cmp(other *CurrencyValue) (int, error) {
253+
if c.Currency.Code.String() != other.Currency.Code.String() {
254254
return 0, ErrCurrencyValueInvalidCmpDifferentCurrencies
255255
}
256-
if v.Currency.Equal(other.Currency) {
257-
return v.Amount.Cmp(other.Amount), nil
256+
if c.Currency.Equal(other.Currency) {
257+
return c.Amount.Cmp(other.Amount), nil
258258
}
259-
if v.Currency.Divisibility > other.Currency.Divisibility {
260-
adjOther, _, err := other.AdjustDivisibility(v.Currency.Divisibility)
259+
if c.Currency.Divisibility > other.Currency.Divisibility {
260+
adjOther, _, err := other.AdjustDivisibility(c.Currency.Divisibility)
261261
if err != nil {
262262
return 0, fmt.Errorf("adjusting other divisibility: %s", err.Error())
263263
}
264-
return v.Amount.Cmp(adjOther.Amount), nil
264+
return c.Amount.Cmp(adjOther.Amount), nil
265265
}
266-
selfAdj, _, err := v.AdjustDivisibility(other.Currency.Divisibility)
266+
selfAdj, _, err := c.AdjustDivisibility(other.Currency.Divisibility)
267267
if err != nil {
268268
return 0, fmt.Errorf("adjusting self divisibility: %s", err.Error())
269269
}
270270
return selfAdj.Amount.Cmp(other.Amount), nil
271271
}
272272

273273
// IsZero returns true if Amount is valid and equal to zero
274-
func (v *CurrencyValue) IsZero() bool {
275-
if v.Amount == nil {
274+
func (c *CurrencyValue) IsZero() bool {
275+
if c.Amount == nil {
276276
return false
277277
}
278-
return v.Amount.Cmp(big.NewInt(0)) == 0
278+
return c.Amount.Cmp(big.NewInt(0)) == 0
279279
}
280280

281281
// IsNegative returns true if Amount is valid and less-than zero
282-
func (v *CurrencyValue) IsNegative() bool {
283-
if v.Amount == nil {
282+
func (c *CurrencyValue) IsNegative() bool {
283+
if c.Amount == nil {
284284
return false
285285
}
286-
return v.Amount.Cmp(big.NewInt(0)) == -1
286+
return c.Amount.Cmp(big.NewInt(0)) == -1
287287
}

repo/profile.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,16 @@ var (
3636
ErrModeratorFixedFeeIsNegative = errors.New("fixed moderator fee is negative or not a parsable number")
3737
)
3838

39-
// Profile presents the user's metadata
39+
// Profile presents the user's metadata. The profile state is maintained within
40+
// a *pb.Profile internally which captures all state changes suitable to be persisted
41+
// via marshaling to JSON. This struct should ensure the integrity of *pb.Profile to
42+
// its data as indicated by the set schema version.
4043
type Profile struct {
4144
profileProto *pb.Profile
4245
}
4346

47+
// UnmarshalJSONProfile consumes a JSON byte slice and returns a Profile-wrapped
48+
// unmarshaled protobuf
4449
func UnmarshalJSONProfile(data []byte) (*Profile, error) {
4550
var (
4651
p = new(pb.Profile)
@@ -52,16 +57,27 @@ func UnmarshalJSONProfile(data []byte) (*Profile, error) {
5257
return NewProfileFromProtobuf(p)
5358
}
5459

60+
// NewProfileFromProtobuf returns a Profile wrapped around a profile protobuf
5561
func NewProfileFromProtobuf(p *pb.Profile) (*Profile, error) {
5662
clonedProfile := proto.Clone(p).(*pb.Profile)
5763
return &Profile{profileProto: clonedProfile}, nil
5864
}
5965

60-
func (p *Profile) NormalizeSchema() *Profile {
66+
// NormalizeDataForAllSchemas converts existing data from its current schema
67+
// into legacy schema. This does not guarantee success as legacy schema that
68+
// was abandoned due to unacceptable constraints will not be able to fulfill
69+
// the full capability of the newer schema. (Ex: FixedFee.BigAmount can support
70+
// full precision, whereas FixedFee.Amount is limited to math.MaxInt64
71+
func (p *Profile) NormalizeDataForAllSchemas() *Profile {
6172
p.normalizeFees()
6273
return p
6374
}
6475

76+
// GetProtobuf returns the underlying protobuf which represents the persistable
77+
// state of the profile. (Note: This method is a shim to access data which isn't
78+
// represented in this package's Profile methods. Consider adding missing getters
79+
// and setters which repsect the schema version instead of using the protobuf
80+
// directly for manipulation.)
6581
func (p *Profile) GetProtobuf() *pb.Profile {
6682
return p.profileProto
6783
}

repo/profile_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func TestProfileNormalizeSchema(t *testing.T) {
114114
t.Errorf("failed normalization on example (%d): %s", i, err)
115115
continue
116116
}
117-
p.NormalizeSchema()
117+
p.NormalizeDataForAllSchemas()
118118
e.validate(p.GetProtobuf())
119119
}
120120
}

0 commit comments

Comments
 (0)