Skip to content

Commit 936d850

Browse files
Daniel1984JacobPlaster
authored andcommitted
adding common patterns of parsing data to provile resource, updating tests
1 parent 38b2b18 commit 936d850

File tree

6 files changed

+25
-11
lines changed

6 files changed

+25
-11
lines changed

pkg/models/pulse/pulse.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ var pulseFields = map[string]int{
3636
"PulseProfile": 18,
3737
}
3838

39-
// NewSingleFromRaw returns pointer to Pulse message
40-
func NewSingleFromRaw(raw []interface{}) (*Pulse, error) {
39+
// FromRaw returns pointer to Pulse message
40+
func FromRaw(raw []interface{}) (*Pulse, error) {
4141
if len(raw) < 19 {
4242
return nil, fmt.Errorf("data slice too short for Pulse Message: %#v", raw)
4343
}
@@ -83,8 +83,8 @@ func NewSingleFromRaw(raw []interface{}) (*Pulse, error) {
8383
return p, nil
8484
}
8585

86-
// NewFromRaw returns slice of Pulse message pointers
87-
func NewFromRaw(raws []interface{}) ([]*Pulse, error) {
86+
// SnapshotFromRaw returns slice of Pulse message pointers
87+
func SnapshotFromRaw(raws []interface{}) ([]*Pulse, error) {
8888
if len(raws) < 1 {
8989
return nil, fmt.Errorf("data slice is too short for Pulse History: %#v", raws)
9090
}
@@ -93,7 +93,7 @@ func NewFromRaw(raws []interface{}) ([]*Pulse, error) {
9393

9494
for _, raw := range raws {
9595
raw := raw.([]interface{})
96-
p, err := NewSingleFromRaw(raw)
96+
p, err := FromRaw(raw)
9797
if err != nil {
9898
return nil, err
9999
}

pkg/models/pulse/pulse_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestNewPulseFromRaw(t *testing.T) {
1515
[]interface{}{"abc123"},
1616
}
1717

18-
pm, err := pulse.NewFromRaw(payload)
18+
pm, err := pulse.SnapshotFromRaw(payload)
1919
require.NotNil(t, err)
2020
require.Nil(t, pm)
2121
})
@@ -45,7 +45,7 @@ func TestNewPulseFromRaw(t *testing.T) {
4545
},
4646
}
4747

48-
pm, err := pulse.NewFromRaw(payload)
48+
pm, err := pulse.SnapshotFromRaw(payload)
4949
require.Nil(t, err)
5050

5151
expected := &pulse.Pulse{
@@ -106,7 +106,7 @@ func TestNewPulseFromRaw(t *testing.T) {
106106
},
107107
}
108108

109-
pm, err := pulse.NewFromRaw(payload)
109+
pm, err := pulse.SnapshotFromRaw(payload)
110110
require.Nil(t, err)
111111

112112
expected := &pulse.Pulse{
@@ -127,6 +127,8 @@ func TestNewPulseFromRaw(t *testing.T) {
127127
Picture: "picture",
128128
Text: "text",
129129
TwitterHandle: "twitter",
130+
Followers: 30,
131+
Following: 5,
130132
},
131133
}
132134

pkg/models/pulseprofile/pulseprofile.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ type PulseProfile struct {
1414
Picture string
1515
Text string
1616
TwitterHandle string
17+
Followers int64
18+
Following int64
1719
}
1820

1921
var pulseProfileFields = map[string]int{
@@ -23,6 +25,8 @@ var pulseProfileFields = map[string]int{
2325
"Picture": 5,
2426
"Text": 6,
2527
"TwitterHandle": 9,
28+
"Followers": 11,
29+
"Following": 12,
2630
}
2731

2832
// NewFromRaw takes in slice of interfaces and converts them to
@@ -40,6 +44,8 @@ func NewFromRaw(raw []interface{}) (*PulseProfile, error) {
4044
pp.Picture = convert.SValOrEmpty(raw[pulseProfileFields["Picture"]])
4145
pp.Text = convert.SValOrEmpty(raw[pulseProfileFields["Text"]])
4246
pp.TwitterHandle = convert.SValOrEmpty(raw[pulseProfileFields["TwitterHandle"]])
47+
pp.Followers = convert.I64ValOrZero(raw[pulseProfileFields["Followers"]])
48+
pp.Following = convert.I64ValOrZero(raw[pulseProfileFields["Following"]])
4349

4450
return pp, nil
4551
}

pkg/models/pulseprofile/pulseprofile_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ func TestNewProfileFromRaw(t *testing.T) {
4444
Picture: "picture",
4545
Text: "text",
4646
TwitterHandle: "twitter",
47+
Followers: 30,
48+
Following: 5,
4749
}
4850
assert.Equal(t, expected, pp)
4951
})

v2/rest/pulse.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (ps *PulseService) PublicPulseHistory(limit int, end common.Mts) ([]*pulse.
5555
return nil, err
5656
}
5757

58-
pph, err := pulse.NewFromRaw(raw)
58+
pph, err := pulse.SnapshotFromRaw(raw)
5959
if err != nil {
6060
return nil, err
6161
}
@@ -86,7 +86,7 @@ func (ps *PulseService) AddPulse(p *pulse.Pulse) (*pulse.Pulse, error) {
8686
return nil, err
8787
}
8888

89-
pm, err := pulse.NewSingleFromRaw(raw)
89+
pm, err := pulse.FromRaw(raw)
9090
if err != nil {
9191
return nil, err
9292
}
@@ -116,7 +116,7 @@ func (ps *PulseService) PulseHistory(isPublic bool) ([]*pulse.Pulse, error) {
116116
return nil, err
117117
}
118118

119-
pph, err := pulse.NewFromRaw(raw)
119+
pph, err := pulse.SnapshotFromRaw(raw)
120120
if err != nil {
121121
return nil, err
122122
}

v2/rest/pulse_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ func TestPublicPulseProfile(t *testing.T) {
7777
Picture: "picture",
7878
Text: "text",
7979
TwitterHandle: "twitter",
80+
Followers: 30,
81+
Following: 5,
8082
}
8183
assert.Equal(t, expected, pp)
8284
})
@@ -400,6 +402,8 @@ func TestPulseHistory(t *testing.T) {
400402
Picture: "picture",
401403
Text: "text",
402404
TwitterHandle: "twitter",
405+
Followers: 30,
406+
Following: 5,
403407
},
404408
}
405409

0 commit comments

Comments
 (0)