-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepaid_usage.go
130 lines (122 loc) · 5.94 KB
/
prepaid_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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package models
import (
"encoding/json"
"errors"
"strings"
)
// PrepaidUsage represents a PrepaidUsage struct.
type PrepaidUsage struct {
PreviousUnitBalance string `json:"previous_unit_balance"`
PreviousOverageUnitBalance string `json:"previous_overage_unit_balance"`
NewUnitBalance int `json:"new_unit_balance"`
NewOverageUnitBalance int `json:"new_overage_unit_balance"`
UsageQuantity int `json:"usage_quantity"`
OverageUsageQuantity int `json:"overage_usage_quantity"`
ComponentId int `json:"component_id"`
ComponentHandle string `json:"component_handle"`
Memo string `json:"memo"`
AllocationDetails []PrepaidUsageAllocationDetail `json:"allocation_details"`
AdditionalProperties map[string]any `json:"_"`
}
// MarshalJSON implements the json.Marshaler interface for PrepaidUsage.
// It customizes the JSON marshaling process for PrepaidUsage objects.
func (p PrepaidUsage) MarshalJSON() (
[]byte,
error) {
return json.Marshal(p.toMap())
}
// toMap converts the PrepaidUsage object to a map representation for JSON marshaling.
func (p PrepaidUsage) toMap() map[string]any {
structMap := make(map[string]any)
MapAdditionalProperties(structMap, p.AdditionalProperties)
structMap["previous_unit_balance"] = p.PreviousUnitBalance
structMap["previous_overage_unit_balance"] = p.PreviousOverageUnitBalance
structMap["new_unit_balance"] = p.NewUnitBalance
structMap["new_overage_unit_balance"] = p.NewOverageUnitBalance
structMap["usage_quantity"] = p.UsageQuantity
structMap["overage_usage_quantity"] = p.OverageUsageQuantity
structMap["component_id"] = p.ComponentId
structMap["component_handle"] = p.ComponentHandle
structMap["memo"] = p.Memo
structMap["allocation_details"] = p.AllocationDetails
return structMap
}
// UnmarshalJSON implements the json.Unmarshaler interface for PrepaidUsage.
// It customizes the JSON unmarshaling process for PrepaidUsage objects.
func (p *PrepaidUsage) UnmarshalJSON(input []byte) error {
var temp prepaidUsage
err := json.Unmarshal(input, &temp)
if err != nil {
return err
}
err = temp.validate()
if err != nil {
return err
}
additionalProperties, err := UnmarshalAdditionalProperties(input, "previous_unit_balance", "previous_overage_unit_balance", "new_unit_balance", "new_overage_unit_balance", "usage_quantity", "overage_usage_quantity", "component_id", "component_handle", "memo", "allocation_details")
if err != nil {
return err
}
p.AdditionalProperties = additionalProperties
p.PreviousUnitBalance = *temp.PreviousUnitBalance
p.PreviousOverageUnitBalance = *temp.PreviousOverageUnitBalance
p.NewUnitBalance = *temp.NewUnitBalance
p.NewOverageUnitBalance = *temp.NewOverageUnitBalance
p.UsageQuantity = *temp.UsageQuantity
p.OverageUsageQuantity = *temp.OverageUsageQuantity
p.ComponentId = *temp.ComponentId
p.ComponentHandle = *temp.ComponentHandle
p.Memo = *temp.Memo
p.AllocationDetails = *temp.AllocationDetails
return nil
}
// prepaidUsage is a temporary struct used for validating the fields of PrepaidUsage.
type prepaidUsage struct {
PreviousUnitBalance *string `json:"previous_unit_balance"`
PreviousOverageUnitBalance *string `json:"previous_overage_unit_balance"`
NewUnitBalance *int `json:"new_unit_balance"`
NewOverageUnitBalance *int `json:"new_overage_unit_balance"`
UsageQuantity *int `json:"usage_quantity"`
OverageUsageQuantity *int `json:"overage_usage_quantity"`
ComponentId *int `json:"component_id"`
ComponentHandle *string `json:"component_handle"`
Memo *string `json:"memo"`
AllocationDetails *[]PrepaidUsageAllocationDetail `json:"allocation_details"`
}
func (p *prepaidUsage) validate() error {
var errs []string
if p.PreviousUnitBalance == nil {
errs = append(errs, "required field `previous_unit_balance` is missing for type `Prepaid Usage`")
}
if p.PreviousOverageUnitBalance == nil {
errs = append(errs, "required field `previous_overage_unit_balance` is missing for type `Prepaid Usage`")
}
if p.NewUnitBalance == nil {
errs = append(errs, "required field `new_unit_balance` is missing for type `Prepaid Usage`")
}
if p.NewOverageUnitBalance == nil {
errs = append(errs, "required field `new_overage_unit_balance` is missing for type `Prepaid Usage`")
}
if p.UsageQuantity == nil {
errs = append(errs, "required field `usage_quantity` is missing for type `Prepaid Usage`")
}
if p.OverageUsageQuantity == nil {
errs = append(errs, "required field `overage_usage_quantity` is missing for type `Prepaid Usage`")
}
if p.ComponentId == nil {
errs = append(errs, "required field `component_id` is missing for type `Prepaid Usage`")
}
if p.ComponentHandle == nil {
errs = append(errs, "required field `component_handle` is missing for type `Prepaid Usage`")
}
if p.Memo == nil {
errs = append(errs, "required field `memo` is missing for type `Prepaid Usage`")
}
if p.AllocationDetails == nil {
errs = append(errs, "required field `allocation_details` is missing for type `Prepaid Usage`")
}
if len(errs) == 0 {
return nil
}
return errors.New(strings.Join(errs, "\n"))
}