-
Notifications
You must be signed in to change notification settings - Fork 1
/
subscription_filter.go
115 lines (108 loc) · 6.1 KB
/
subscription_filter.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package models
import (
"encoding/json"
"log"
"time"
)
// SubscriptionFilter represents a SubscriptionFilter struct.
// Nested filter used for List Subscription Components For Site Filter
type SubscriptionFilter struct {
// Allows fetching components allocations that belong to the subscription with matching states based on provided values. To use this filter you also have to include the following param in the request `include=subscription`. Use in query `filter[subscription][states]=active,canceled&include=subscription`.
States []SubscriptionStateFilter `json:"states,omitempty"`
// The type of filter you'd like to apply to your search. To use this filter you also have to include the following param in the request `include=subscription`.
DateField *SubscriptionListDateField `json:"date_field,omitempty"`
// The start date (format YYYY-MM-DD) with which to filter the date_field. Returns components that belong to the subscription with a timestamp at or after midnight (12:00:00 AM) in your site’s time zone on the date specified. To use this filter you also have to include the following param in the request `include=subscription`.
StartDate *time.Time `json:"start_date,omitempty"`
// The end date (format YYYY-MM-DD) with which to filter the date_field. Returns components that belong to the subscription with a timestamp up to and including 11:59:59PM in your site’s time zone on the date specified. To use this filter you also have to include the following param in the request `include=subscription`.
EndDate *time.Time `json:"end_date,omitempty"`
// The start date and time (format YYYY-MM-DD HH:MM:SS) with which to filter the date_field. Returns components that belong to the subscription with a timestamp at or after exact time provided in query. You can specify timezone in query - otherwise your site''s time zone will be used. If provided, this parameter will be used instead of start_date. To use this filter you also have to include the following param in the request `include=subscription`.
StartDatetime *time.Time `json:"start_datetime,omitempty"`
// The end date and time (format YYYY-MM-DD HH:MM:SS) with which to filter the date_field. Returns components that belong to the subscription with a timestamp at or before exact time provided in query. You can specify timezone in query - otherwise your site''s time zone will be used. If provided, this parameter will be used instead of end_date. To use this filter you also have to include the following param in the request `include=subscription`.
EndDatetime *time.Time `json:"end_datetime,omitempty"`
AdditionalProperties map[string]any `json:"_"`
}
// MarshalJSON implements the json.Marshaler interface for SubscriptionFilter.
// It customizes the JSON marshaling process for SubscriptionFilter objects.
func (s SubscriptionFilter) MarshalJSON() (
[]byte,
error) {
return json.Marshal(s.toMap())
}
// toMap converts the SubscriptionFilter object to a map representation for JSON marshaling.
func (s SubscriptionFilter) toMap() map[string]any {
structMap := make(map[string]any)
MapAdditionalProperties(structMap, s.AdditionalProperties)
if s.States != nil {
structMap["states"] = s.States
}
if s.DateField != nil {
structMap["date_field"] = s.DateField
}
if s.StartDate != nil {
structMap["start_date"] = s.StartDate.Format(DEFAULT_DATE)
}
if s.EndDate != nil {
structMap["end_date"] = s.EndDate.Format(DEFAULT_DATE)
}
if s.StartDatetime != nil {
structMap["start_datetime"] = s.StartDatetime.Format(time.RFC3339)
}
if s.EndDatetime != nil {
structMap["end_datetime"] = s.EndDatetime.Format(time.RFC3339)
}
return structMap
}
// UnmarshalJSON implements the json.Unmarshaler interface for SubscriptionFilter.
// It customizes the JSON unmarshaling process for SubscriptionFilter objects.
func (s *SubscriptionFilter) UnmarshalJSON(input []byte) error {
var temp subscriptionFilter
err := json.Unmarshal(input, &temp)
if err != nil {
return err
}
additionalProperties, err := UnmarshalAdditionalProperties(input, "states", "date_field", "start_date", "end_date", "start_datetime", "end_datetime")
if err != nil {
return err
}
s.AdditionalProperties = additionalProperties
s.States = temp.States
s.DateField = temp.DateField
if temp.StartDate != nil {
StartDateVal, err := time.Parse(DEFAULT_DATE, *temp.StartDate)
if err != nil {
log.Fatalf("Cannot Parse start_date as % s format.", DEFAULT_DATE)
}
s.StartDate = &StartDateVal
}
if temp.EndDate != nil {
EndDateVal, err := time.Parse(DEFAULT_DATE, *temp.EndDate)
if err != nil {
log.Fatalf("Cannot Parse end_date as % s format.", DEFAULT_DATE)
}
s.EndDate = &EndDateVal
}
if temp.StartDatetime != nil {
StartDatetimeVal, err := time.Parse(time.RFC3339, *temp.StartDatetime)
if err != nil {
log.Fatalf("Cannot Parse start_datetime as % s format.", time.RFC3339)
}
s.StartDatetime = &StartDatetimeVal
}
if temp.EndDatetime != nil {
EndDatetimeVal, err := time.Parse(time.RFC3339, *temp.EndDatetime)
if err != nil {
log.Fatalf("Cannot Parse end_datetime as % s format.", time.RFC3339)
}
s.EndDatetime = &EndDatetimeVal
}
return nil
}
// subscriptionFilter is a temporary struct used for validating the fields of SubscriptionFilter.
type subscriptionFilter struct {
States []SubscriptionStateFilter `json:"states,omitempty"`
DateField *SubscriptionListDateField `json:"date_field,omitempty"`
StartDate *string `json:"start_date,omitempty"`
EndDate *string `json:"end_date,omitempty"`
StartDatetime *string `json:"start_datetime,omitempty"`
EndDatetime *string `json:"end_datetime,omitempty"`
}