-
Notifications
You must be signed in to change notification settings - Fork 1
/
historic_usage.go
82 lines (75 loc) · 3.17 KB
/
historic_usage.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
package models
import (
"encoding/json"
"log"
"time"
)
// HistoricUsage represents a HistoricUsage struct.
// Optional for Event Based Components. If the `include=historic_usages` query param is provided, the last ten billing periods will be returned.
type HistoricUsage struct {
// Total usage of a component for billing period
TotalUsageQuantity *float64 `json:"total_usage_quantity,omitempty"`
// Start date of billing period
BillingPeriodStartsAt *time.Time `json:"billing_period_starts_at,omitempty"`
// End date of billing period
BillingPeriodEndsAt *time.Time `json:"billing_period_ends_at,omitempty"`
AdditionalProperties map[string]any `json:"_"`
}
// MarshalJSON implements the json.Marshaler interface for HistoricUsage.
// It customizes the JSON marshaling process for HistoricUsage objects.
func (h HistoricUsage) MarshalJSON() (
[]byte,
error) {
return json.Marshal(h.toMap())
}
// toMap converts the HistoricUsage object to a map representation for JSON marshaling.
func (h HistoricUsage) toMap() map[string]any {
structMap := make(map[string]any)
MapAdditionalProperties(structMap, h.AdditionalProperties)
if h.TotalUsageQuantity != nil {
structMap["total_usage_quantity"] = h.TotalUsageQuantity
}
if h.BillingPeriodStartsAt != nil {
structMap["billing_period_starts_at"] = h.BillingPeriodStartsAt.Format(time.RFC3339)
}
if h.BillingPeriodEndsAt != nil {
structMap["billing_period_ends_at"] = h.BillingPeriodEndsAt.Format(time.RFC3339)
}
return structMap
}
// UnmarshalJSON implements the json.Unmarshaler interface for HistoricUsage.
// It customizes the JSON unmarshaling process for HistoricUsage objects.
func (h *HistoricUsage) UnmarshalJSON(input []byte) error {
var temp historicUsage
err := json.Unmarshal(input, &temp)
if err != nil {
return err
}
additionalProperties, err := UnmarshalAdditionalProperties(input, "total_usage_quantity", "billing_period_starts_at", "billing_period_ends_at")
if err != nil {
return err
}
h.AdditionalProperties = additionalProperties
h.TotalUsageQuantity = temp.TotalUsageQuantity
if temp.BillingPeriodStartsAt != nil {
BillingPeriodStartsAtVal, err := time.Parse(time.RFC3339, *temp.BillingPeriodStartsAt)
if err != nil {
log.Fatalf("Cannot Parse billing_period_starts_at as % s format.", time.RFC3339)
}
h.BillingPeriodStartsAt = &BillingPeriodStartsAtVal
}
if temp.BillingPeriodEndsAt != nil {
BillingPeriodEndsAtVal, err := time.Parse(time.RFC3339, *temp.BillingPeriodEndsAt)
if err != nil {
log.Fatalf("Cannot Parse billing_period_ends_at as % s format.", time.RFC3339)
}
h.BillingPeriodEndsAt = &BillingPeriodEndsAtVal
}
return nil
}
// historicUsage is a temporary struct used for validating the fields of HistoricUsage.
type historicUsage struct {
TotalUsageQuantity *float64 `json:"total_usage_quantity,omitempty"`
BillingPeriodStartsAt *string `json:"billing_period_starts_at,omitempty"`
BillingPeriodEndsAt *string `json:"billing_period_ends_at,omitempty"`
}