/
nullable.go
39 lines (32 loc) · 982 Bytes
/
nullable.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
package tfe
import "encoding/json"
// Nullable acts as a wrapper to enable using "omitempty" on fields that should retain the ability to explicitly be set
// to null.
//
// This is particularly useful for attributes that are set to null if provided as null in a json request body.
// Conversely, if they are omitted from the request body, they will retain their existing value.
//
// This struct should use generics, but this project doesn't support them yet :(
type Nullable struct {
value interface{}
}
func (i Nullable) MarshalJSON() ([]byte, error) {
if i.value != nil {
return json.Marshal(i.value)
}
return json.Marshal(nil)
}
// int version of Nullable
type NullableInt struct {
value *int
}
func (i NullableInt) MarshalJSON() ([]byte, error) {
return Nullable{i.value}.MarshalJSON()
}
// string version of Nullable
type NullableString struct {
value *string
}
func (i NullableString) MarshalJSON() ([]byte, error) {
return Nullable{i.value}.MarshalJSON()
}