-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdocs.go
89 lines (69 loc) · 1.71 KB
/
docs.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 nan - Zero allocation Nullable structures in one library with handy conversion functions,
marshallers and unmarshallers.
Features:
- short name "nan"
- handy conversion functions
- select which marshalers you want and limit dependencies to only those you actually need
- ability to convert your custom structs to nan compatible type with Valid field and all requested encoders/decoders
Supported types:
- bool
- float32
- float64
- int
- int8
- int16
- int32
- int64
- string
- time.Time
- more types will be added as necessary
Supported marshallers:
- Standart JSON
- encoding.TextMarshaler/TextUnmarshaler. Reuses standard JSON logic and format
- jsoniter
- easyjson
- go-json
- Scylla and Cassandra. Compatible with gocql
- SQL
Usage
Simply create struct field or variable with one of the exported types and use it without any changes to external API.
JSON input/output will be converted to null or non null values. Scylla and Cassandra will use wire format compatible
with gocql.
var data struct {
Code nan.NullString `json:"code"`
}
b, err := jsoniter.Marshal(data)
if err != nil {
panic(err)
}
// {"code":null}
println(string(b))
data.Code = nan.String("1")
// Equals to
// data.Code = nan.NullString{String: "1", Valid: true}
b, err = jsoniter.Marshal(data)
if err != nil {
panic(err)
}
// {"code":"1"}
println(string(b))
code := "2"
// From addr. Can has value or be nil
data.Code = nan.StringAddr(&code)
b, err = jsoniter.Marshal(data)
if err != nil {
panic(err)
}
// {"code":"2"}
println(string(b))
// To usual value from nan
codeVal := data.Code.String
// 2
println(codeVal)
// To value addr from nan
codeAddr := data.Code.Addr()
// 2
println(*codeAddr)
*/
package nan