-
Notifications
You must be signed in to change notification settings - Fork 45
/
post_response.go
57 lines (50 loc) · 1.47 KB
/
post_response.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
package models
import (
"encoding/json"
"fmt"
"strings"
)
// PostQueryResponse represents the data of a post
// that is returned to user upon a query
type PostQueryResponse struct {
Post
PollAnswers []UserAnswer `json:"poll_answers,omitempty" yaml:"poll_answers,omitempty"`
Reactions []PostReaction `json:"reactions" yaml:"reactions,omitempty"`
Children PostIDs `json:"children" yaml:"children"`
}
// String implements fmt.Stringer
func (response PostQueryResponse) String() string {
out := fmt.Sprintf(`
ID: %s
Reactions: %s
Children: %s
`, response.Post.PostID, response.Reactions, response.Children)
return strings.TrimSpace(out)
}
func NewPostResponse(
post Post, pollAnswers []UserAnswer, reactions []PostReaction, children PostIDs,
) PostQueryResponse {
return PostQueryResponse{
Post: post,
PollAnswers: pollAnswers,
Reactions: reactions,
Children: children,
}
}
// MarshalJSON implements json.Marshaler as Amino does
// not respect default json composition
func (response PostQueryResponse) MarshalJSON() ([]byte, error) {
type temp PostQueryResponse
return json.Marshal(temp(response))
}
// UnmarshalJSON implements json.Unmarshaler as Amino does
// not respect default json composition
func (response *PostQueryResponse) UnmarshalJSON(data []byte) error {
type postResponse PostQueryResponse
var temp postResponse
if err := json.Unmarshal(data, &temp); err != nil {
return err
}
*response = PostQueryResponse(temp)
return nil
}