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

Commit cef4f97

Browse files
committed
[#1904] Apply legacy schema normalization to (*OpenBazaarNode).FetchProfile
1 parent 89b9570 commit cef4f97

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed

core/profile.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ func (n *OpenBazaarNode) FetchProfile(peerID string, useCache bool) (pb.Profile,
5050
if err != nil || len(b) == 0 {
5151
return pro, err
5252
}
53-
err = jsonpb.UnmarshalString(string(b), &pro)
53+
p, err := repo.UnmarshalJSONProfile(b)
5454
if err != nil {
5555
return pro, err
5656
}
57-
return pro, nil
57+
p.NormalizeSchema()
58+
return *p.GetProtobuf(), nil
5859
}
5960

6061
// UpdateProfile - update user profile
@@ -187,16 +188,18 @@ func (n *OpenBazaarNode) PatchProfile(patch map[string]interface{}) error {
187188
return err
188189
}
189190

190-
normalProfile, err := repo.NormalizeProfileProtobuf(p)
191+
repoProfile, err := repo.UnmarshalJSONProfile(newProfile)
191192
if err != nil {
192193
return fmt.Errorf("building profile for validation: %s", err.Error())
193194
}
194195

195-
if err := normalProfile.Valid(); err != nil {
196+
repoProfile.NormalizeSchema()
197+
198+
if err := repoProfile.Valid(); err != nil {
196199
return fmt.Errorf("invalid profile: %s", err.Error())
197200
}
198201

199-
return n.UpdateProfile(normalProfile.GetProtobuf())
202+
return n.UpdateProfile(repoProfile.GetProtobuf())
200203
}
201204

202205
func (n *OpenBazaarNode) appendCountsToProfile(profile *pb.Profile) (*pb.Profile, bool) {

repo/profile.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,9 @@ func NewProfileFromProtobuf(p *pb.Profile) (*Profile, error) {
5757
return &Profile{profileProto: clonedProfile}, nil
5858
}
5959

60-
func NormalizeProfileProtobuf(p *pb.Profile) (*Profile, error) {
61-
repoProfile, err := NewProfileFromProtobuf(p)
62-
if err != nil {
63-
return nil, err
64-
}
65-
66-
repoProfile.normalizeFees()
67-
return repoProfile, nil
60+
func (p *Profile) NormalizeSchema() *Profile {
61+
p.normalizeFees()
62+
return p
6863
}
6964

7065
func (p *Profile) GetProtobuf() *pb.Profile {

repo/profile_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestProfileFromProtobufMissingModInfo(t *testing.T) {
1919
}
2020
}
2121

22-
func TestNormalizeProfileProtobuf(t *testing.T) {
22+
func TestProfileNormalizeSchema(t *testing.T) {
2323
var (
2424
exampleFee = &pb.Moderator_Price{
2525
BigAmount: "10",
@@ -108,12 +108,13 @@ func TestNormalizeProfileProtobuf(t *testing.T) {
108108
for i, e := range examples {
109109
var (
110110
subject = e.example()
111-
p, err = repo.NormalizeProfileProtobuf(subject)
111+
p, err = repo.NewProfileFromProtobuf(subject)
112112
)
113113
if err != nil {
114114
t.Errorf("failed normalization on example (%d): %s", i, err)
115115
continue
116116
}
117+
p.NormalizeSchema()
117118
e.validate(p.GetProtobuf())
118119
}
119120
}

0 commit comments

Comments
 (0)