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

Commit 89b9570

Browse files
committed
[#1904] Support legacy schema in (*repo.Profile).NormalizeProfileProtobuf
1 parent 1b29fe4 commit 89b9570

File tree

2 files changed

+49
-17
lines changed

2 files changed

+49
-17
lines changed

repo/profile.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,21 @@ func (p *Profile) normalizeFees() {
211211
}
212212

213213
if ff, err := p.GetModeratedFixedFee(); err == nil {
214-
var amtInt uint64
215-
if ai, err := strconv.Atoi(p.profileProto.ModeratorInfo.Fee.FixedFee.BigAmount); err == nil {
216-
amtInt = uint64(ai)
214+
switch p.GetVersion() {
215+
case 5:
216+
var amtInt uint64
217+
if ai, err := strconv.Atoi(p.profileProto.ModeratorInfo.Fee.FixedFee.BigAmount); err == nil {
218+
amtInt = uint64(ai)
219+
}
220+
p.profileProto.ModeratorInfo.Fee.FixedFee.CurrencyCode = ff.Currency.Code.String()
221+
p.profileProto.ModeratorInfo.Fee.FixedFee.Amount = amtInt
222+
default: // v4 and earlier
223+
p.profileProto.ModeratorInfo.Fee.FixedFee.AmountCurrency = &pb.CurrencyDefinition{
224+
Code: ff.Currency.Code.String(),
225+
Divisibility: uint32(ff.Currency.Divisibility),
226+
}
227+
p.profileProto.ModeratorInfo.Fee.FixedFee.BigAmount = ff.Amount.String()
217228
}
218-
p.profileProto.ModeratorInfo.Fee.FixedFee.CurrencyCode = ff.Currency.Code.String()
219-
p.profileProto.ModeratorInfo.Fee.FixedFee.Amount = amtInt
220229
}
221230
}
222231
}

repo/profile_test.go

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,18 @@ func TestProfileFromProtobufMissingModInfo(t *testing.T) {
2020
}
2121

2222
func TestNormalizeProfileProtobuf(t *testing.T) {
23-
var exampleFee = &pb.Moderator_Price{
24-
BigAmount: "10",
25-
AmountCurrency: &pb.CurrencyDefinition{
26-
Code: "BTC",
27-
Divisibility: 8,
28-
},
29-
}
23+
var (
24+
exampleFee = &pb.Moderator_Price{
25+
BigAmount: "10",
26+
AmountCurrency: &pb.CurrencyDefinition{
27+
Code: "BTC",
28+
Divisibility: 8,
29+
},
30+
}
31+
)
3032
var examples = []struct {
3133
example func() *pb.Profile
32-
validate func(*pb.Profile, *testing.T)
34+
validate func(*pb.Profile)
3335
}{
3436
{ // profile with percent fee should remove non-percent fee values
3537
example: func() *pb.Profile {
@@ -39,7 +41,7 @@ func TestNormalizeProfileProtobuf(t *testing.T) {
3941
p.ModeratorInfo.Fee.Percentage = 1.1
4042
return p
4143
},
42-
validate: func(p *pb.Profile, t *testing.T) {
44+
validate: func(p *pb.Profile) {
4345
if p.ModeratorInfo.Fee.FixedFee != nil {
4446
t.Errorf("expected fixed fee to be removed, but was not")
4547
}
@@ -53,7 +55,7 @@ func TestNormalizeProfileProtobuf(t *testing.T) {
5355
p.ModeratorInfo.Fee.Percentage = 1.1
5456
return p
5557
},
56-
validate: func(p *pb.Profile, t *testing.T) {
58+
validate: func(p *pb.Profile) {
5759
if p.ModeratorInfo.Fee.Percentage != 0 {
5860
t.Errorf("expected percentage to be zero, but was not")
5961
}
@@ -66,7 +68,7 @@ func TestNormalizeProfileProtobuf(t *testing.T) {
6668
p.ModeratorInfo.Fee.FixedFee = exampleFee
6769
return p
6870
},
69-
validate: func(p *pb.Profile, t *testing.T) {
71+
validate: func(p *pb.Profile) {
7072
if actual := p.ModeratorInfo.Fee.FixedFee.CurrencyCode; actual != exampleFee.AmountCurrency.Code {
7173
t.Errorf("expected legacy code to be (%s), but was (%s)", exampleFee.AmountCurrency.Code, actual)
7274
}
@@ -80,6 +82,27 @@ func TestNormalizeProfileProtobuf(t *testing.T) {
8082
}
8183
},
8284
},
85+
{ // legacy v4 profile with fixed fee should populate current schema
86+
example: func() *pb.Profile {
87+
pb := factory.MustLoadProfileFixture("v4-profile-moderator-fixed-fee")
88+
rp, err := repo.UnmarshalJSONProfile(pb)
89+
if err != nil {
90+
t.Fatal(err)
91+
}
92+
p := rp.GetProtobuf()
93+
p.ModeratorInfo.Fee.FixedFee.CurrencyCode = "BTC"
94+
p.ModeratorInfo.Fee.FixedFee.Amount = 10
95+
return p
96+
},
97+
validate: func(p *pb.Profile) {
98+
if actual := p.ModeratorInfo.Fee.FixedFee.AmountCurrency.Code; actual != "BTC" {
99+
t.Errorf("expected fee amount currency code to be (%s), but was (%s)", "BTC", actual)
100+
}
101+
if actualAmt := p.ModeratorInfo.Fee.FixedFee.BigAmount; actualAmt != "10" {
102+
t.Errorf("expected fee amount to be (%s), but was (%s)", "10", actualAmt)
103+
}
104+
},
105+
},
83106
}
84107

85108
for i, e := range examples {
@@ -91,7 +114,7 @@ func TestNormalizeProfileProtobuf(t *testing.T) {
91114
t.Errorf("failed normalization on example (%d): %s", i, err)
92115
continue
93116
}
94-
e.validate(p.GetProtobuf(), t)
117+
e.validate(p.GetProtobuf())
95118
}
96119
}
97120

0 commit comments

Comments
 (0)