-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnullable.go
More file actions
73 lines (65 loc) · 1.66 KB
/
Copy pathnullable.go
File metadata and controls
73 lines (65 loc) · 1.66 KB
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
package csv
import (
"errors"
"reflect"
)
// NullableField allows any type (T) to be nullable,
// the default CSV struct mapper will always use a zero value for a given field for any scalar value.
// This is a wrapper for nullable values to exist and easier to work with that something like sql.Null.
type NullableField[T any] []T
// UnmarshalCSV allows for a NullableField to be unmarshalled and its underlying type to be resolved if not null.
func (n *NullableField[T]) UnmarshalCSV(data string) error {
var blank T
if len(data) == 0 {
return nil
}
val, err := getDecoderProvider(reflect.TypeOf(blank), "nullable value", false)(data, false)
if err != nil {
return err
}
typedVal, ok := val.(T)
if !ok {
return errors.New("invalid decoder response for nullable")
}
tmp := NullableField[T]{typedVal}
*n = tmp
return nil
}
// MarshalCSV marshals the underlying type for a CSV.
func (n NullableField[T]) MarshalCSV() (string, error) {
if len(n) == 0 {
return "", nil
}
vOf := reflect.ValueOf(n[0])
return getEncoderProvider(vOf.Type(), false)(vOf)
}
// IsNull checks to see if the field is null or a value was set.
func (n NullableField[T]) IsNull() bool {
return len(n) == 0
}
// Set updates the nullable field value.
func (n *NullableField[T]) Set(val T) {
if len(*n) > 0 {
slc := *n
slc[0] = val
return
}
tmp := NullableField[T]{val}
*n = tmp
}
// Unset sets the field to null.
func (n *NullableField[T]) Unset() {
if len(*n) == 0 {
return
}
tmp := NullableField[T]{}
*n = tmp
}
// Get returns the value and if it was set or not.
func (n NullableField[T]) Get() (T, bool) {
var e T
if len(n) == 0 {
return e, false
}
return n[0], true
}