|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License. |
| 2 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 3 | +// Copyright 2019-Present Datadog, Inc. |
| 4 | + |
| 5 | +package datadogV2 |
| 6 | + |
| 7 | +import ( |
| 8 | + "github.com/DataDog/datadog-api-client-go/v2/api/datadog" |
| 9 | +) |
| 10 | + |
| 11 | +// AnyValue - Represents any valid JSON value. |
| 12 | +type AnyValue struct { |
| 13 | + AnyValueString *string |
| 14 | + AnyValueNumber *float64 |
| 15 | + AnyValueObject map[string]interface{} |
| 16 | + AnyValueArray *[]AnyValueItem |
| 17 | + AnyValueBoolean *bool |
| 18 | + |
| 19 | + // UnparsedObject contains the raw value of the object if there was an error when deserializing into the struct |
| 20 | + UnparsedObject interface{} |
| 21 | +} |
| 22 | + |
| 23 | +// AnyValueStringAsAnyValue is a convenience function that returns string wrapped in AnyValue. |
| 24 | +func AnyValueStringAsAnyValue(v *string) AnyValue { |
| 25 | + return AnyValue{AnyValueString: v} |
| 26 | +} |
| 27 | + |
| 28 | +// AnyValueNumberAsAnyValue is a convenience function that returns float64 wrapped in AnyValue. |
| 29 | +func AnyValueNumberAsAnyValue(v *float64) AnyValue { |
| 30 | + return AnyValue{AnyValueNumber: v} |
| 31 | +} |
| 32 | + |
| 33 | +// AnyValueObjectAsAnyValue is a convenience function that returns map[string]interface{} wrapped in AnyValue. |
| 34 | +func AnyValueObjectAsAnyValue(v map[string]interface{}) AnyValue { |
| 35 | + return AnyValue{AnyValueObject: v} |
| 36 | +} |
| 37 | + |
| 38 | +// AnyValueArrayAsAnyValue is a convenience function that returns []AnyValueItem wrapped in AnyValue. |
| 39 | +func AnyValueArrayAsAnyValue(v *[]AnyValueItem) AnyValue { |
| 40 | + return AnyValue{AnyValueArray: v} |
| 41 | +} |
| 42 | + |
| 43 | +// AnyValueBooleanAsAnyValue is a convenience function that returns bool wrapped in AnyValue. |
| 44 | +func AnyValueBooleanAsAnyValue(v *bool) AnyValue { |
| 45 | + return AnyValue{AnyValueBoolean: v} |
| 46 | +} |
| 47 | + |
| 48 | +// UnmarshalJSON turns data into one of the pointers in the struct. |
| 49 | +func (obj *AnyValue) UnmarshalJSON(data []byte) error { |
| 50 | + var err error |
| 51 | + match := 0 |
| 52 | + // try to unmarshal data into AnyValueString |
| 53 | + err = datadog.Unmarshal(data, &obj.AnyValueString) |
| 54 | + if err == nil { |
| 55 | + if obj.AnyValueString != nil { |
| 56 | + jsonAnyValueString, _ := datadog.Marshal(obj.AnyValueString) |
| 57 | + if string(jsonAnyValueString) == "{}" { // empty struct |
| 58 | + obj.AnyValueString = nil |
| 59 | + } else { |
| 60 | + match++ |
| 61 | + } |
| 62 | + } else { |
| 63 | + obj.AnyValueString = nil |
| 64 | + } |
| 65 | + } else { |
| 66 | + obj.AnyValueString = nil |
| 67 | + } |
| 68 | + |
| 69 | + // try to unmarshal data into AnyValueNumber |
| 70 | + err = datadog.Unmarshal(data, &obj.AnyValueNumber) |
| 71 | + if err == nil { |
| 72 | + if obj.AnyValueNumber != nil { |
| 73 | + jsonAnyValueNumber, _ := datadog.Marshal(obj.AnyValueNumber) |
| 74 | + if string(jsonAnyValueNumber) == "{}" { // empty struct |
| 75 | + obj.AnyValueNumber = nil |
| 76 | + } else { |
| 77 | + match++ |
| 78 | + } |
| 79 | + } else { |
| 80 | + obj.AnyValueNumber = nil |
| 81 | + } |
| 82 | + } else { |
| 83 | + obj.AnyValueNumber = nil |
| 84 | + } |
| 85 | + |
| 86 | + // try to unmarshal data into AnyValueObject |
| 87 | + err = datadog.Unmarshal(data, &obj.AnyValueObject) |
| 88 | + if err == nil { |
| 89 | + if obj.AnyValueObject != nil { |
| 90 | + jsonAnyValueObject, _ := datadog.Marshal(obj.AnyValueObject) |
| 91 | + if string(jsonAnyValueObject) == "{}" && string(data) != "{}" { // empty struct |
| 92 | + obj.AnyValueObject = nil |
| 93 | + } else { |
| 94 | + match++ |
| 95 | + } |
| 96 | + } else { |
| 97 | + obj.AnyValueObject = nil |
| 98 | + } |
| 99 | + } else { |
| 100 | + obj.AnyValueObject = nil |
| 101 | + } |
| 102 | + |
| 103 | + // try to unmarshal data into AnyValueArray |
| 104 | + err = datadog.Unmarshal(data, &obj.AnyValueArray) |
| 105 | + if err == nil { |
| 106 | + if obj.AnyValueArray != nil { |
| 107 | + jsonAnyValueArray, _ := datadog.Marshal(obj.AnyValueArray) |
| 108 | + if string(jsonAnyValueArray) == "{}" && string(data) != "{}" { // empty struct |
| 109 | + obj.AnyValueArray = nil |
| 110 | + } else { |
| 111 | + match++ |
| 112 | + } |
| 113 | + } else { |
| 114 | + obj.AnyValueArray = nil |
| 115 | + } |
| 116 | + } else { |
| 117 | + obj.AnyValueArray = nil |
| 118 | + } |
| 119 | + |
| 120 | + // try to unmarshal data into AnyValueBoolean |
| 121 | + err = datadog.Unmarshal(data, &obj.AnyValueBoolean) |
| 122 | + if err == nil { |
| 123 | + if obj.AnyValueBoolean != nil { |
| 124 | + jsonAnyValueBoolean, _ := datadog.Marshal(obj.AnyValueBoolean) |
| 125 | + if string(jsonAnyValueBoolean) == "{}" { // empty struct |
| 126 | + obj.AnyValueBoolean = nil |
| 127 | + } else { |
| 128 | + match++ |
| 129 | + } |
| 130 | + } else { |
| 131 | + obj.AnyValueBoolean = nil |
| 132 | + } |
| 133 | + } else { |
| 134 | + obj.AnyValueBoolean = nil |
| 135 | + } |
| 136 | + |
| 137 | + if match != 1 { // more than 1 match |
| 138 | + // reset to nil |
| 139 | + obj.AnyValueString = nil |
| 140 | + obj.AnyValueNumber = nil |
| 141 | + obj.AnyValueObject = nil |
| 142 | + obj.AnyValueArray = nil |
| 143 | + obj.AnyValueBoolean = nil |
| 144 | + return datadog.Unmarshal(data, &obj.UnparsedObject) |
| 145 | + } |
| 146 | + return nil // exactly one match |
| 147 | +} |
| 148 | + |
| 149 | +// MarshalJSON turns data from the first non-nil pointers in the struct to JSON. |
| 150 | +func (obj AnyValue) MarshalJSON() ([]byte, error) { |
| 151 | + if obj.AnyValueString != nil { |
| 152 | + return datadog.Marshal(&obj.AnyValueString) |
| 153 | + } |
| 154 | + |
| 155 | + if obj.AnyValueNumber != nil { |
| 156 | + return datadog.Marshal(&obj.AnyValueNumber) |
| 157 | + } |
| 158 | + |
| 159 | + if obj.AnyValueObject != nil { |
| 160 | + return datadog.Marshal(&obj.AnyValueObject) |
| 161 | + } |
| 162 | + |
| 163 | + if obj.AnyValueArray != nil { |
| 164 | + return datadog.Marshal(&obj.AnyValueArray) |
| 165 | + } |
| 166 | + |
| 167 | + if obj.AnyValueBoolean != nil { |
| 168 | + return datadog.Marshal(&obj.AnyValueBoolean) |
| 169 | + } |
| 170 | + |
| 171 | + if obj.UnparsedObject != nil { |
| 172 | + return datadog.Marshal(obj.UnparsedObject) |
| 173 | + } |
| 174 | + return nil, nil // no data in oneOf schemas |
| 175 | +} |
| 176 | + |
| 177 | +// GetActualInstance returns the actual instance. |
| 178 | +func (obj *AnyValue) GetActualInstance() interface{} { |
| 179 | + if obj.AnyValueString != nil { |
| 180 | + return obj.AnyValueString |
| 181 | + } |
| 182 | + |
| 183 | + if obj.AnyValueNumber != nil { |
| 184 | + return obj.AnyValueNumber |
| 185 | + } |
| 186 | + |
| 187 | + if obj.AnyValueObject != nil { |
| 188 | + return obj.AnyValueObject |
| 189 | + } |
| 190 | + |
| 191 | + if obj.AnyValueArray != nil { |
| 192 | + return obj.AnyValueArray |
| 193 | + } |
| 194 | + |
| 195 | + if obj.AnyValueBoolean != nil { |
| 196 | + return obj.AnyValueBoolean |
| 197 | + } |
| 198 | + |
| 199 | + // all schemas are nil |
| 200 | + return nil |
| 201 | +} |
| 202 | + |
| 203 | +// NullableAnyValue handles when a null is used for AnyValue. |
| 204 | +type NullableAnyValue struct { |
| 205 | + value *AnyValue |
| 206 | + isSet bool |
| 207 | +} |
| 208 | + |
| 209 | +// Get returns the associated value. |
| 210 | +func (v NullableAnyValue) Get() *AnyValue { |
| 211 | + return v.value |
| 212 | +} |
| 213 | + |
| 214 | +// Set changes the value and indicates it's been called. |
| 215 | +func (v *NullableAnyValue) Set(val *AnyValue) { |
| 216 | + v.value = val |
| 217 | + v.isSet = true |
| 218 | +} |
| 219 | + |
| 220 | +// IsSet returns whether Set has been called. |
| 221 | +func (v NullableAnyValue) IsSet() bool { |
| 222 | + return v.isSet |
| 223 | +} |
| 224 | + |
| 225 | +// Unset sets the value to nil and resets the set flag/ |
| 226 | +func (v *NullableAnyValue) Unset() { |
| 227 | + v.value = nil |
| 228 | + v.isSet = false |
| 229 | +} |
| 230 | + |
| 231 | +// NewNullableAnyValue initializes the struct as if Set has been called. |
| 232 | +func NewNullableAnyValue(val *AnyValue) *NullableAnyValue { |
| 233 | + return &NullableAnyValue{value: val, isSet: true} |
| 234 | +} |
| 235 | + |
| 236 | +// MarshalJSON serializes the associated value. |
| 237 | +func (v NullableAnyValue) MarshalJSON() ([]byte, error) { |
| 238 | + return datadog.Marshal(v.value) |
| 239 | +} |
| 240 | + |
| 241 | +// UnmarshalJSON deserializes the payload and sets the flag as if Set has been called. |
| 242 | +func (v *NullableAnyValue) UnmarshalJSON(src []byte) error { |
| 243 | + v.isSet = true |
| 244 | + |
| 245 | + // this object is nullable so check if the payload is null or empty string |
| 246 | + if string(src) == "" || string(src) == "{}" { |
| 247 | + return nil |
| 248 | + } |
| 249 | + |
| 250 | + return datadog.Unmarshal(src, &v.value) |
| 251 | +} |
0 commit comments