-
Notifications
You must be signed in to change notification settings - Fork 1
/
mrr.go
91 lines (84 loc) · 3.01 KB
/
mrr.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package models
import (
"encoding/json"
"log"
"time"
)
// MRR represents a MRR struct.
type MRR struct {
AmountInCents *int64 `json:"amount_in_cents,omitempty"`
AmountFormatted *string `json:"amount_formatted,omitempty"`
Currency *string `json:"currency,omitempty"`
CurrencySymbol *string `json:"currency_symbol,omitempty"`
Breakouts *Breakouts `json:"breakouts,omitempty"`
// ISO8601 timestamp
AtTime *time.Time `json:"at_time,omitempty"`
AdditionalProperties map[string]any `json:"_"`
}
// MarshalJSON implements the json.Marshaler interface for MRR.
// It customizes the JSON marshaling process for MRR objects.
func (m MRR) MarshalJSON() (
[]byte,
error) {
return json.Marshal(m.toMap())
}
// toMap converts the MRR object to a map representation for JSON marshaling.
func (m MRR) toMap() map[string]any {
structMap := make(map[string]any)
MapAdditionalProperties(structMap, m.AdditionalProperties)
if m.AmountInCents != nil {
structMap["amount_in_cents"] = m.AmountInCents
}
if m.AmountFormatted != nil {
structMap["amount_formatted"] = m.AmountFormatted
}
if m.Currency != nil {
structMap["currency"] = m.Currency
}
if m.CurrencySymbol != nil {
structMap["currency_symbol"] = m.CurrencySymbol
}
if m.Breakouts != nil {
structMap["breakouts"] = m.Breakouts.toMap()
}
if m.AtTime != nil {
structMap["at_time"] = m.AtTime.Format(time.RFC3339)
}
return structMap
}
// UnmarshalJSON implements the json.Unmarshaler interface for MRR.
// It customizes the JSON unmarshaling process for MRR objects.
func (m *MRR) UnmarshalJSON(input []byte) error {
var temp mrr
err := json.Unmarshal(input, &temp)
if err != nil {
return err
}
additionalProperties, err := UnmarshalAdditionalProperties(input, "amount_in_cents", "amount_formatted", "currency", "currency_symbol", "breakouts", "at_time")
if err != nil {
return err
}
m.AdditionalProperties = additionalProperties
m.AmountInCents = temp.AmountInCents
m.AmountFormatted = temp.AmountFormatted
m.Currency = temp.Currency
m.CurrencySymbol = temp.CurrencySymbol
m.Breakouts = temp.Breakouts
if temp.AtTime != nil {
AtTimeVal, err := time.Parse(time.RFC3339, *temp.AtTime)
if err != nil {
log.Fatalf("Cannot Parse at_time as % s format.", time.RFC3339)
}
m.AtTime = &AtTimeVal
}
return nil
}
// mrr is a temporary struct used for validating the fields of MRR.
type mrr struct {
AmountInCents *int64 `json:"amount_in_cents,omitempty"`
AmountFormatted *string `json:"amount_formatted,omitempty"`
Currency *string `json:"currency,omitempty"`
CurrencySymbol *string `json:"currency_symbol,omitempty"`
Breakouts *Breakouts `json:"breakouts,omitempty"`
AtTime *string `json:"at_time,omitempty"`
}