Documentation ¶
Overview ¶
Example (Get) ¶
package main import ( "fmt" "github.com/markphelps/optional" ) func main() { values := []optional.String{ optional.NewString("foo"), optional.NewString(""), optional.NewString("bar"), {}, } for _, v := range values { if v.Present() { value, err := v.Get() if err == nil { fmt.Println(value) } } } }
Output: foo bar
Example (If) ¶
package main import ( "fmt" "github.com/markphelps/optional" ) func main() { values := []optional.String{ optional.NewString("foo"), optional.NewString(""), optional.NewString("bar"), {}, } for _, v := range values { v.If(func(s string) { fmt.Println("present") }) } }
Output: present present present
Example (MarshalJSON) ¶
package main import ( "encoding/json" "fmt" "github.com/markphelps/optional" ) func main() { var values = []struct { Field optional.String `json:"field,omitempty"` }{ { Field: optional.NewString("foo"), }, { Field: optional.NewString(""), }, { Field: optional.NewString("bar"), }, {}, } for _, v := range values { out, _ := json.Marshal(v) fmt.Println(string(out)) } }
Output: {"field":"foo"} {"field":""} {"field":"bar"}
Example (OrElse) ¶
package main import ( "fmt" "github.com/markphelps/optional" ) func main() { values := []optional.String{ optional.NewString("foo"), optional.NewString(""), optional.NewString("bar"), {}, } for _, v := range values { fmt.Println(v.OrElse("not present")) } }
Output: foo bar not present
Example (UnmarshalJSON) ¶
package main import ( "encoding/json" "fmt" "github.com/markphelps/optional" ) func main() { var values = []string{ `{"field":"foo"}`, `{"field":""}`, `{"field":"bar"}`, "{}", } for _, v := range values { var o = &struct { Field optional.String `json:"field,omitempty"` }{} if err := json.Unmarshal([]byte(v), o); err != nil { fmt.Println(err) } o.Field.If(func(s string) { fmt.Println(s) }) } }
Output: foo bar
Index ¶
- type Bool
- type Byte
- type Complex128
- func (c Complex128) Get() (complex128, error)
- func (c Complex128) If(fn func(complex128))
- func (c Complex128) MarshalJSON() ([]byte, error)
- func (c Complex128) OrElse(v complex128) complex128
- func (c Complex128) Present() bool
- func (c Complex128) Set(v complex128)
- func (c *Complex128) UnmarshalJSON(data []byte) error
- type Complex64
- func (c Complex64) Get() (complex64, error)
- func (c Complex64) If(fn func(complex64))
- func (c Complex64) MarshalJSON() ([]byte, error)
- func (c Complex64) OrElse(v complex64) complex64
- func (c Complex64) Present() bool
- func (c Complex64) Set(v complex64)
- func (c *Complex64) UnmarshalJSON(data []byte) error
- type Error
- type Float32
- type Float64
- type Int
- type Int16
- type Int32
- type Int64
- type Int8
- type Rune
- type String
- type Uint
- type Uint16
- type Uint32
- type Uint64
- type Uint8
- type Uintptr
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bool ¶
type Bool struct {
// contains filtered or unexported fields
}
Bool is an optional bool
func (Bool) MarshalJSON ¶ added in v0.4.0
func (*Bool) UnmarshalJSON ¶ added in v0.4.0
type Byte ¶
type Byte struct {
// contains filtered or unexported fields
}
Byte is an optional byte
func (Byte) MarshalJSON ¶ added in v0.4.0
func (*Byte) UnmarshalJSON ¶ added in v0.4.0
type Complex128 ¶
type Complex128 struct {
// contains filtered or unexported fields
}
Complex128 is an optional complex128
func NewComplex128 ¶ added in v0.3.0
func NewComplex128(v complex128) Complex128
NewComplex128 creates a optional.Complex128 from a complex128
func (Complex128) Get ¶
func (c Complex128) Get() (complex128, error)
Get returns the complex128 value or an error if not present
func (Complex128) If ¶ added in v0.2.0
func (c Complex128) If(fn func(complex128))
If calls the function f with the value if the value is present
func (Complex128) MarshalJSON ¶ added in v0.4.0
func (c Complex128) MarshalJSON() ([]byte, error)
func (Complex128) OrElse ¶
func (c Complex128) OrElse(v complex128) complex128
OrElse returns the complex128 value or a default value if the value is not present
func (Complex128) Present ¶
func (c Complex128) Present() bool
Present returns whether or not the value is present
func (*Complex128) UnmarshalJSON ¶ added in v0.4.0
func (c *Complex128) UnmarshalJSON(data []byte) error
type Complex64 ¶
type Complex64 struct {
// contains filtered or unexported fields
}
Complex64 is an optional complex64
func NewComplex64 ¶ added in v0.3.0
NewComplex64 creates a optional.Complex64 from a complex64
func (Complex64) If ¶ added in v0.2.0
If calls the function f with the value if the value is present
func (Complex64) MarshalJSON ¶ added in v0.4.0
func (Complex64) OrElse ¶
OrElse returns the complex64 value or a default value if the value is not present
func (*Complex64) UnmarshalJSON ¶ added in v0.4.0
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error is an optional error
func (Error) MarshalJSON ¶ added in v0.4.0
func (*Error) UnmarshalJSON ¶ added in v0.4.0
type Float32 ¶
type Float32 struct {
// contains filtered or unexported fields
}
Float32 is an optional float32
func NewFloat32 ¶ added in v0.3.0
NewFloat32 creates a optional.Float32 from a float32
func (Float32) MarshalJSON ¶ added in v0.4.0
func (Float32) OrElse ¶
OrElse returns the float32 value or a default value if the value is not present
func (*Float32) UnmarshalJSON ¶ added in v0.4.0
type Float64 ¶
type Float64 struct {
// contains filtered or unexported fields
}
Float64 is an optional float64
func NewFloat64 ¶ added in v0.3.0
NewFloat64 creates a optional.Float64 from a float64
func (Float64) MarshalJSON ¶ added in v0.4.0
func (Float64) OrElse ¶
OrElse returns the float64 value or a default value if the value is not present
func (*Float64) UnmarshalJSON ¶ added in v0.4.0
type Int ¶
type Int struct {
// contains filtered or unexported fields
}
Int is an optional int
func (Int) MarshalJSON ¶ added in v0.4.0
func (*Int) UnmarshalJSON ¶ added in v0.4.0
type Int16 ¶
type Int16 struct {
// contains filtered or unexported fields
}
Int16 is an optional int16
func (Int16) MarshalJSON ¶ added in v0.4.0
func (*Int16) UnmarshalJSON ¶ added in v0.4.0
type Int32 ¶
type Int32 struct {
// contains filtered or unexported fields
}
Int32 is an optional int32
func (Int32) MarshalJSON ¶ added in v0.4.0
func (*Int32) UnmarshalJSON ¶ added in v0.4.0
type Int64 ¶
type Int64 struct {
// contains filtered or unexported fields
}
Int64 is an optional int64
func (Int64) MarshalJSON ¶ added in v0.4.0
func (*Int64) UnmarshalJSON ¶ added in v0.4.0
type Int8 ¶
type Int8 struct {
// contains filtered or unexported fields
}
Int8 is an optional int8
func (Int8) MarshalJSON ¶ added in v0.4.0
func (*Int8) UnmarshalJSON ¶ added in v0.4.0
type Rune ¶
type Rune struct {
// contains filtered or unexported fields
}
Rune is an optional rune
func (Rune) MarshalJSON ¶ added in v0.4.0
func (*Rune) UnmarshalJSON ¶ added in v0.4.0
type String ¶
type String struct {
// contains filtered or unexported fields
}
String is an optional string
func (String) MarshalJSON ¶ added in v0.4.0
func (String) OrElse ¶
OrElse returns the string value or a default value if the value is not present
func (*String) UnmarshalJSON ¶ added in v0.4.0
type Uint ¶
type Uint struct {
// contains filtered or unexported fields
}
Uint is an optional uint
func (Uint) MarshalJSON ¶ added in v0.4.0
func (*Uint) UnmarshalJSON ¶ added in v0.4.0
type Uint16 ¶
type Uint16 struct {
// contains filtered or unexported fields
}
Uint16 is an optional uint16
func (Uint16) MarshalJSON ¶ added in v0.4.0
func (Uint16) OrElse ¶
OrElse returns the uint16 value or a default value if the value is not present
func (*Uint16) UnmarshalJSON ¶ added in v0.4.0
type Uint32 ¶
type Uint32 struct {
// contains filtered or unexported fields
}
Uint32 is an optional uint32
func (Uint32) MarshalJSON ¶ added in v0.4.0
func (Uint32) OrElse ¶
OrElse returns the uint32 value or a default value if the value is not present
func (*Uint32) UnmarshalJSON ¶ added in v0.4.0
type Uint64 ¶
type Uint64 struct {
// contains filtered or unexported fields
}
Uint64 is an optional uint64
func (Uint64) MarshalJSON ¶ added in v0.4.0
func (Uint64) OrElse ¶
OrElse returns the uint64 value or a default value if the value is not present
func (*Uint64) UnmarshalJSON ¶ added in v0.4.0
type Uint8 ¶
type Uint8 struct {
// contains filtered or unexported fields
}
Uint8 is an optional uint8
func (Uint8) MarshalJSON ¶ added in v0.4.0
func (*Uint8) UnmarshalJSON ¶ added in v0.4.0
type Uintptr ¶
type Uintptr struct {
// contains filtered or unexported fields
}
Uintptr is an optional uintptr
func NewUintptr ¶ added in v0.3.0
NewUintptr creates a optional.Uintptr from a uintptr
func (Uintptr) MarshalJSON ¶ added in v0.4.0
func (Uintptr) OrElse ¶
OrElse returns the uintptr value or a default value if the value is not present
func (*Uintptr) UnmarshalJSON ¶ added in v0.4.0
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
optional
Optional is a tool that generates 'optional' type wrappers around a given type T. Typically this process would be run using go generate, like this: //go:generate optional -type=Foo running this command optional -type=Foo in the same directory will create the file optional_foo.go containing a definition of type OptionalFoo struct { ...
|
Optional is a tool that generates 'optional' type wrappers around a given type T. Typically this process would be run using go generate, like this: //go:generate optional -type=Foo running this command optional -type=Foo in the same directory will create the file optional_foo.go containing a definition of type OptionalFoo struct { ... |