Documentation
¶
Index ¶
- func Ptr[T any](v T) *T
- type JSONOpt
- type Opt
- func Bool(b bool) Opt[bool]
- func Float(f float64) Opt[float64]
- func Float32(f float32) Opt[float32]
- func From[T any](value T) Opt[T]
- func Int(i int) Opt[int]
- func Int64(i int64) Opt[int64]
- func Null[T any]() Opt[T]
- func String(s string) Opt[string]
- func Time(t time.Time) Opt[time.Time]
- func Zero[T any]() Opt[T]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Opt ¶
Opt is a generic type that implements a field with three possible states: - field is not set in the request - field is explicitly set to `null` in the request - field is explicitly set to a valid value in the request
func From ¶
From constructs a Opt[T] with the given value, representing a field explicitly set in a JSON request.
Example ¶
package main
import (
"fmt"
"github.com/qntx/param"
)
func main() {
p := struct {
N param.Opt[int]
}{}
p.N = param.Int(123)
fmt.Println(p.N.Get())
}
Output: 123 true
func Null ¶
Null constructs a Opt[T] with an explicit `null`, representing a field set to `null` in a JSON request.
Example ¶
package main
import (
"fmt"
"github.com/qntx/param"
)
func main() {
p := struct {
N param.Opt[int]
}{}
p.N = param.Null[int]()
fmt.Printf("Specified: %v\n", p.N.IsSet())
fmt.Printf("Opt: %v\n", p.N.IsNull())
}
Output: Specified: true Opt: true
Example (MarshalOptional) ¶
package main
import (
"encoding/json"
"fmt"
"github.com/qntx/param"
)
func main() {
obj := struct {
ID param.Opt[int] `json:"id,omitempty"`
}{}
// when it's not set (by default)
b, err := json.Marshal(obj)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println("Unspecified:")
fmt.Printf(`JSON: %s`+"\n", b)
fmt.Println("---")
// when it's not set (explicitly)
obj.ID.Reset()
b, err = json.Marshal(obj)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println("Unspecified:")
fmt.Printf(`JSON: %s`+"\n", b)
fmt.Println("---")
// when it's set explicitly to nil
obj.ID.SetNull()
b, err = json.Marshal(obj)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println("Opt:")
fmt.Printf(`JSON: %s`+"\n", b)
fmt.Println("---")
// when it's set explicitly to the zero value
var v int
obj.ID.Set(v)
b, err = json.Marshal(obj)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println("Zero value:")
fmt.Printf(`JSON: %s`+"\n", b)
fmt.Println("---")
// when it's set explicitly to a specific value
v = 12345
obj.ID.Set(v)
b, err = json.Marshal(obj)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println("Value:")
fmt.Printf(`JSON: %s`+"\n", b)
fmt.Println("---")
}
Output: Unspecified: JSON: {} --- Unspecified: JSON: {} --- Opt: JSON: {"id":null} --- Zero value: JSON: {"id":0} --- Value: JSON: {"id":12345} ---
Example (MarshalRequired) ¶
package main
import (
"encoding/json"
"fmt"
"github.com/qntx/param"
)
func main() {
obj := struct {
ID param.Opt[int] `json:"id"`
}{}
// when it's not set (by default)
b, err := json.Marshal(obj)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println("Unspecified:")
fmt.Printf(`JSON: %s`+"\n", b)
fmt.Println("---")
// when it's not set (explicitly)
obj.ID.Reset()
b, err = json.Marshal(obj)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println("Unspecified:")
fmt.Printf(`JSON: %s`+"\n", b)
fmt.Println("---")
// when it's set explicitly to nil
obj.ID.SetNull()
b, err = json.Marshal(obj)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println("Opt:")
fmt.Printf(`JSON: %s`+"\n", b)
fmt.Println("---")
// when it's set explicitly to the zero value
var v int
obj.ID.Set(v)
b, err = json.Marshal(obj)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println("Zero value:")
fmt.Printf(`JSON: %s`+"\n", b)
fmt.Println("---")
// when it's set explicitly to a specific value
v = 12345
obj.ID.Set(v)
b, err = json.Marshal(obj)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println("Value:")
fmt.Printf(`JSON: %s`+"\n", b)
fmt.Println("---")
}
Output: Unspecified: JSON: {"id":0} --- Unspecified: JSON: {"id":0} --- Opt: JSON: {"id":null} --- Zero value: JSON: {"id":0} --- Value: JSON: {"id":12345} ---
Example (UnmarshalOptional) ¶
package main
import (
"encoding/json"
"fmt"
"github.com/qntx/param"
)
func main() {
obj := struct {
// Note that there is no pointer for null.Opt when it's
Name param.Opt[string] `json:"name,omitempty"`
}{}
// when it's not set
err := json.Unmarshal([]byte(`
{
}
`), &obj)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println("Unspecified:")
fmt.Printf("obj.Name.IsSet(): %v\n", obj.Name.IsSet())
fmt.Printf("obj.Name.IsNull(): %v\n", obj.Name.IsNull())
fmt.Println("---")
// when it's set explicitly to nil
err = json.Unmarshal([]byte(`
{
"name": null
}
`), &obj)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println("Opt:")
fmt.Printf("obj.Name.IsSet(): %v\n", obj.Name.IsSet())
fmt.Printf("obj.Name.IsNull(): %v\n", obj.Name.IsNull())
fmt.Println("---")
// when it's set explicitly to the zero value
err = json.Unmarshal([]byte(`
{
"name": ""
}
`), &obj)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println("Zero value:")
fmt.Printf("obj.Name.IsSet(): %v\n", obj.Name.IsSet())
fmt.Printf("obj.Name.IsNull(): %v\n", obj.Name.IsNull())
val, ok := obj.Name.Get()
if !ok {
fmt.Printf("Error: %v\n", "value is not specified or null")
return
}
fmt.Printf("obj.Name.Get(): %#v <nil>\n", val)
fmt.Printf("obj.Name.MustGet(): %#v\n", obj.Name.MustGet())
fmt.Println("---")
// when it's set explicitly to a specific value
err = json.Unmarshal([]byte(`
{
"name": "foo"
}
`), &obj)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println("Value:")
fmt.Printf("obj.Name.IsSet(): %v\n", obj.Name.IsSet())
fmt.Printf("obj.Name.IsNull(): %v\n", obj.Name.IsNull())
val, ok = obj.Name.Get()
if !ok {
fmt.Printf("Error: %v\n", "value is not specified or null")
return
}
fmt.Printf("obj.Name.Get(): %#v <nil>\n", val)
fmt.Printf("obj.Name.MustGet(): %#v\n", obj.Name.MustGet())
fmt.Println("---")
}
Output: Unspecified: obj.Name.IsSet(): false obj.Name.IsNull(): false --- Opt: obj.Name.IsSet(): true obj.Name.IsNull(): true --- Zero value: obj.Name.IsSet(): true obj.Name.IsNull(): false obj.Name.Get(): "" <nil> obj.Name.MustGet(): "" --- Value: obj.Name.IsSet(): true obj.Name.IsNull(): false obj.Name.Get(): "foo" <nil> obj.Name.MustGet(): "foo" ---
Example (UnmarshalRequired) ¶
package main
import (
"encoding/json"
"fmt"
"github.com/qntx/param"
)
func main() {
obj := struct {
Name param.Opt[string] `json:"name"`
}{}
// when it's not set
err := json.Unmarshal([]byte(`
{
}
`), &obj)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println("Unspecified:")
fmt.Printf("obj.Name.IsSet(): %v\n", obj.Name.IsSet())
fmt.Printf("obj.Name.IsNull(): %v\n", obj.Name.IsNull())
fmt.Println("---")
// when it's set explicitly to nil
err = json.Unmarshal([]byte(`
{
"name": null
}
`), &obj)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println("Opt:")
fmt.Printf("obj.Name.IsSet(): %v\n", obj.Name.IsSet())
fmt.Printf("obj.Name.IsNull(): %v\n", obj.Name.IsNull())
fmt.Println("---")
// when it's set explicitly to the zero value
err = json.Unmarshal([]byte(`
{
"name": ""
}
`), &obj)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println("Zero value:")
fmt.Printf("obj.Name.IsSet(): %v\n", obj.Name.IsSet())
fmt.Printf("obj.Name.IsNull(): %v\n", obj.Name.IsNull())
val, ok := obj.Name.Get()
if !ok {
fmt.Printf("Error: %v\n", "value is not specified or null")
return
}
fmt.Printf("obj.Name.Get(): %#v <nil>\n", val)
fmt.Printf("obj.Name.MustGet(): %#v\n", obj.Name.MustGet())
fmt.Println("---")
// when it's set explicitly to a specific value
err = json.Unmarshal([]byte(`
{
"name": "foo"
}
`), &obj)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Println("Value:")
fmt.Printf("obj.Name.IsSet(): %v\n", obj.Name.IsSet())
fmt.Printf("obj.Name.IsNull(): %v\n", obj.Name.IsNull())
val, ok = obj.Name.Get()
if !ok {
fmt.Printf("Error: %v\n", "value is not specified or null")
return
}
fmt.Printf("obj.Name.Get(): %#v <nil>\n", val)
fmt.Printf("obj.Name.MustGet(): %#v\n", obj.Name.MustGet())
fmt.Println("---")
}
Output: Unspecified: obj.Name.IsSet(): false obj.Name.IsNull(): false --- Opt: obj.Name.IsSet(): true obj.Name.IsNull(): true --- Zero value: obj.Name.IsSet(): true obj.Name.IsNull(): false obj.Name.Get(): "" <nil> obj.Name.MustGet(): "" --- Value: obj.Name.IsSet(): true obj.Name.IsNull(): false obj.Name.Get(): "foo" <nil> obj.Name.MustGet(): "foo" ---
func Zero ¶
Zero constructs a Opt[T] in the unset state, representing a field not provided in a JSON request.
func (Opt[T]) Get ¶
Get retrieves the underlying value, if present, and returns an empty value and `false` if not present.
func (Opt[T]) MarshalJSON ¶
func (Opt[T]) MustGet ¶
func (t Opt[T]) MustGet() T
MustGet retrieves the underlying value, if present, and panics if not present.