-
Notifications
You must be signed in to change notification settings - Fork 1
/
metafield.go
89 lines (82 loc) · 3.33 KB
/
metafield.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
package models
import (
"encoding/json"
)
// Metafield represents a Metafield struct.
type Metafield struct {
Id *int `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
// Warning: When updating a metafield's scope attribute, all scope attributes must be passed. Partially complete scope attributes will override the existing settings.
Scope *MetafieldScope `json:"scope,omitempty"`
// the amount of subscriptions this metafield has been applied to in Chargify
DataCount *int `json:"data_count,omitempty"`
// Indicates how data should be added to the metafield. For example, a text type is just a string, so a given metafield of this type can have any value attached. On the other hand, dropdown and radio have a set of allowed values that can be input, and appear differently on a Public Signup Page. Defaults to 'text'
InputType *MetafieldInput `json:"input_type,omitempty"`
Enum Optional[MetafieldEnum] `json:"enum"`
AdditionalProperties map[string]any `json:"_"`
}
// MarshalJSON implements the json.Marshaler interface for Metafield.
// It customizes the JSON marshaling process for Metafield objects.
func (m Metafield) MarshalJSON() (
[]byte,
error) {
return json.Marshal(m.toMap())
}
// toMap converts the Metafield object to a map representation for JSON marshaling.
func (m Metafield) toMap() map[string]any {
structMap := make(map[string]any)
MapAdditionalProperties(structMap, m.AdditionalProperties)
if m.Id != nil {
structMap["id"] = m.Id
}
if m.Name != nil {
structMap["name"] = m.Name
}
if m.Scope != nil {
structMap["scope"] = m.Scope.toMap()
}
if m.DataCount != nil {
structMap["data_count"] = m.DataCount
}
if m.InputType != nil {
structMap["input_type"] = m.InputType
}
if m.Enum.IsValueSet() {
if m.Enum.Value() != nil {
structMap["enum"] = m.Enum.Value().toMap()
} else {
structMap["enum"] = nil
}
}
return structMap
}
// UnmarshalJSON implements the json.Unmarshaler interface for Metafield.
// It customizes the JSON unmarshaling process for Metafield objects.
func (m *Metafield) UnmarshalJSON(input []byte) error {
var temp metafield
err := json.Unmarshal(input, &temp)
if err != nil {
return err
}
additionalProperties, err := UnmarshalAdditionalProperties(input, "id", "name", "scope", "data_count", "input_type", "enum")
if err != nil {
return err
}
m.AdditionalProperties = additionalProperties
m.Id = temp.Id
m.Name = temp.Name
m.Scope = temp.Scope
m.DataCount = temp.DataCount
m.InputType = temp.InputType
m.Enum = temp.Enum
return nil
}
// metafield is a temporary struct used for validating the fields of Metafield.
type metafield struct {
Id *int `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
Scope *MetafieldScope `json:"scope,omitempty"`
DataCount *int `json:"data_count,omitempty"`
InputType *MetafieldInput `json:"input_type,omitempty"`
Enum Optional[MetafieldEnum] `json:"enum"`
}