forked from pagarme/pagarme-golang-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagingResponse.go
51 lines (44 loc) · 1.11 KB
/
pagingResponse.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
package models
import (
"encoding/json"
"github.com/apimatic/go-core-runtime/types"
)
// Object used for returning lists of objects with pagination
type PagingResponse struct {
Total types.Optional[int] `json:"total"`
Previous types.Optional[string] `json:"previous"`
Next types.Optional[string] `json:"next"`
}
func (p *PagingResponse) MarshalJSON() (
[]byte,
error) {
return json.Marshal(p.toMap())
}
func (p *PagingResponse) toMap() map[string]any {
structMap := make(map[string]any)
if p.Total.IsValueSet() {
structMap["total"] = p.Total.Value()
}
if p.Previous.IsValueSet() {
structMap["previous"] = p.Previous.Value()
}
if p.Next.IsValueSet() {
structMap["next"] = p.Next.Value()
}
return structMap
}
func (p *PagingResponse) UnmarshalJSON(input []byte) error {
temp := &struct {
Total types.Optional[int] `json:"total"`
Previous types.Optional[string] `json:"previous"`
Next types.Optional[string] `json:"next"`
}{}
err := json.Unmarshal(input, &temp)
if err != nil {
return err
}
p.Total = temp.Total
p.Previous = temp.Previous
p.Next = temp.Next
return nil
}