-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_segment.go
102 lines (94 loc) · 4.72 KB
/
create_segment.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
package models
import (
"encoding/json"
"errors"
"strings"
)
// CreateSegment represents a CreateSegment struct.
type CreateSegment struct {
// A value that will occur in your events that you want to bill upon. The type of the value depends on the property type in the related event based billing metric.
SegmentProperty1Value *CreateSegmentSegmentProperty1Value `json:"segment_property_1_value,omitempty"`
// A value that will occur in your events that you want to bill upon. The type of the value depends on the property type in the related event based billing metric.
SegmentProperty2Value *CreateSegmentSegmentProperty2Value `json:"segment_property_2_value,omitempty"`
// A value that will occur in your events that you want to bill upon. The type of the value depends on the property type in the related event based billing metric.
SegmentProperty3Value *CreateSegmentSegmentProperty3Value `json:"segment_property_3_value,omitempty"`
// A value that will occur in your events that you want to bill upon. The type of the value depends on the property type in the related event based billing metric.
SegmentProperty4Value *CreateSegmentSegmentProperty4Value `json:"segment_property_4_value,omitempty"`
// The identifier for the pricing scheme. See [Product Components](https://help.chargify.com/products/product-components.html) for an overview of pricing schemes.
PricingScheme PricingScheme `json:"pricing_scheme"`
Prices []CreateOrUpdateSegmentPrice `json:"prices,omitempty"`
AdditionalProperties map[string]any `json:"_"`
}
// MarshalJSON implements the json.Marshaler interface for CreateSegment.
// It customizes the JSON marshaling process for CreateSegment objects.
func (c CreateSegment) MarshalJSON() (
[]byte,
error) {
return json.Marshal(c.toMap())
}
// toMap converts the CreateSegment object to a map representation for JSON marshaling.
func (c CreateSegment) toMap() map[string]any {
structMap := make(map[string]any)
MapAdditionalProperties(structMap, c.AdditionalProperties)
if c.SegmentProperty1Value != nil {
structMap["segment_property_1_value"] = c.SegmentProperty1Value.toMap()
}
if c.SegmentProperty2Value != nil {
structMap["segment_property_2_value"] = c.SegmentProperty2Value.toMap()
}
if c.SegmentProperty3Value != nil {
structMap["segment_property_3_value"] = c.SegmentProperty3Value.toMap()
}
if c.SegmentProperty4Value != nil {
structMap["segment_property_4_value"] = c.SegmentProperty4Value.toMap()
}
structMap["pricing_scheme"] = c.PricingScheme
if c.Prices != nil {
structMap["prices"] = c.Prices
}
return structMap
}
// UnmarshalJSON implements the json.Unmarshaler interface for CreateSegment.
// It customizes the JSON unmarshaling process for CreateSegment objects.
func (c *CreateSegment) UnmarshalJSON(input []byte) error {
var temp createSegment
err := json.Unmarshal(input, &temp)
if err != nil {
return err
}
err = temp.validate()
if err != nil {
return err
}
additionalProperties, err := UnmarshalAdditionalProperties(input, "segment_property_1_value", "segment_property_2_value", "segment_property_3_value", "segment_property_4_value", "pricing_scheme", "prices")
if err != nil {
return err
}
c.AdditionalProperties = additionalProperties
c.SegmentProperty1Value = temp.SegmentProperty1Value
c.SegmentProperty2Value = temp.SegmentProperty2Value
c.SegmentProperty3Value = temp.SegmentProperty3Value
c.SegmentProperty4Value = temp.SegmentProperty4Value
c.PricingScheme = *temp.PricingScheme
c.Prices = temp.Prices
return nil
}
// createSegment is a temporary struct used for validating the fields of CreateSegment.
type createSegment struct {
SegmentProperty1Value *CreateSegmentSegmentProperty1Value `json:"segment_property_1_value,omitempty"`
SegmentProperty2Value *CreateSegmentSegmentProperty2Value `json:"segment_property_2_value,omitempty"`
SegmentProperty3Value *CreateSegmentSegmentProperty3Value `json:"segment_property_3_value,omitempty"`
SegmentProperty4Value *CreateSegmentSegmentProperty4Value `json:"segment_property_4_value,omitempty"`
PricingScheme *PricingScheme `json:"pricing_scheme"`
Prices []CreateOrUpdateSegmentPrice `json:"prices,omitempty"`
}
func (c *createSegment) validate() error {
var errs []string
if c.PricingScheme == nil {
errs = append(errs, "required field `pricing_scheme` is missing for type `Create Segment`")
}
if len(errs) == 0 {
return nil
}
return errors.New(strings.Join(errs, "\n"))
}