├── LICENSE ├── README.md ├── bool.go ├── byte.go ├── complex128.go ├── complex64.go ├── float32.go ├── float64.go ├── go.mod ├── int.go ├── int16.go ├── int32.go ├── int64.go ├── int8.go ├── interface.go ├── rune.go ├── string.go ├── time.go ├── uint.go ├── uint16.go ├── uint32.go ├── uint64.go ├── uint8.go └── uintptr.go /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 2016 Adam Hintz 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/O5O33VK5S) 2 | -------------------------------------------------------------------------------- /bool.go: -------------------------------------------------------------------------------- 1 | package optional 2 | 3 | type Bool struct { 4 | isSet bool 5 | value bool 6 | } 7 | 8 | func NewBool(value bool) Bool { 9 | return Bool{ 10 | true, 11 | value, 12 | } 13 | } 14 | 15 | // EmptyBool returns a new Bool that does not have a value set. 16 | func EmptyBool() Bool { 17 | return Bool{ 18 | false, 19 | false, 20 | } 21 | } 22 | 23 | func (b Bool) IsSet() bool { 24 | return b.isSet 25 | } 26 | 27 | func (b Bool) Value() bool { 28 | return b.value 29 | } 30 | 31 | func (b Bool) Default(defaultValue bool) bool { 32 | if b.isSet { 33 | return b.value 34 | } 35 | return defaultValue 36 | } 37 | -------------------------------------------------------------------------------- /byte.go: -------------------------------------------------------------------------------- 1 | package optional 2 | 3 | type Byte struct { 4 | isSet bool 5 | value byte 6 | } 7 | 8 | func NewByte(value byte) Byte { 9 | return Byte{ 10 | true, 11 | value, 12 | } 13 | } 14 | 15 | // EmptyByte returns a new Byte that does not have a value set. 16 | func EmptyByte() Byte { 17 | return Byte{ 18 | false, 19 | 0, 20 | } 21 | } 22 | 23 | func (b Byte) IsSet() bool { 24 | return b.isSet 25 | } 26 | 27 | func (b Byte) Value() byte { 28 | return b.value 29 | } 30 | 31 | func (b Byte) Default(defaultValue byte) byte { 32 | if b.isSet { 33 | return b.value 34 | } 35 | return defaultValue 36 | } 37 | -------------------------------------------------------------------------------- /complex128.go: -------------------------------------------------------------------------------- 1 | package optional 2 | 3 | type Complex128 struct { 4 | isSet bool 5 | value complex128 6 | } 7 | 8 | func NewComplex128(value complex128) Complex128 { 9 | return Complex128{ 10 | true, 11 | value, 12 | } 13 | } 14 | 15 | // EmptyComplex128 returns a new Complex128 that does not have a value set. 16 | func EmptyComplex128() Complex128 { 17 | return Complex128{ 18 | false, 19 | 0, 20 | } 21 | } 22 | 23 | func (i Complex128) IsSet() bool { 24 | return i.isSet 25 | } 26 | 27 | func (i Complex128) Value() complex128 { 28 | return i.value 29 | } 30 | 31 | func (i Complex128) Default(defaultValue complex128) complex128 { 32 | if i.isSet { 33 | return i.value 34 | } 35 | return defaultValue 36 | } 37 | -------------------------------------------------------------------------------- /complex64.go: -------------------------------------------------------------------------------- 1 | package optional 2 | 3 | type Complex64 struct { 4 | isSet bool 5 | value complex64 6 | } 7 | 8 | func NewComplex64(value complex64) Complex64 { 9 | return Complex64{ 10 | true, 11 | value, 12 | } 13 | } 14 | 15 | // EmptyComplex64 returns a new Complex64 that does not have a value set. 16 | func EmptyComplex64() Complex64 { 17 | return Complex64{ 18 | false, 19 | 0, 20 | } 21 | } 22 | 23 | func (i Complex64) IsSet() bool { 24 | return i.isSet 25 | } 26 | 27 | func (i Complex64) Value() complex64 { 28 | return i.value 29 | } 30 | 31 | func (i Complex64) Default(defaultValue complex64) complex64 { 32 | if i.isSet { 33 | return i.value 34 | } 35 | return defaultValue 36 | } 37 | -------------------------------------------------------------------------------- /float32.go: -------------------------------------------------------------------------------- 1 | package optional 2 | 3 | type Float32 struct { 4 | isSet bool 5 | value float32 6 | } 7 | 8 | func NewFloat32(value float32) Float32 { 9 | return Float32{ 10 | true, 11 | value, 12 | } 13 | } 14 | 15 | // EmptyFloat32 returns a new Float32 that does not have a value set. 16 | func EmptyFloat32() Float32 { 17 | return Float32{ 18 | false, 19 | 0, 20 | } 21 | } 22 | 23 | func (i Float32) IsSet() bool { 24 | return i.isSet 25 | } 26 | 27 | func (i Float32) Value() float32 { 28 | return i.value 29 | } 30 | 31 | func (i Float32) Default(defaultValue float32) float32 { 32 | if i.isSet { 33 | return i.value 34 | } 35 | return defaultValue 36 | } 37 | -------------------------------------------------------------------------------- /float64.go: -------------------------------------------------------------------------------- 1 | package optional 2 | 3 | type Float64 struct { 4 | isSet bool 5 | value float64 6 | } 7 | 8 | func NewFloat64(value float64) Float64 { 9 | return Float64{ 10 | true, 11 | value, 12 | } 13 | } 14 | 15 | // EmptyFloat64 returns a new Float64 that does not have a value set. 16 | func EmptyFloat64() Float64 { 17 | return Float64{ 18 | false, 19 | 0, 20 | } 21 | } 22 | 23 | func (i Float64) IsSet() bool { 24 | return i.isSet 25 | } 26 | 27 | func (i Float64) Value() float64 { 28 | return i.value 29 | } 30 | 31 | func (i Float64) Default(defaultValue float64) float64 { 32 | if i.isSet { 33 | return i.value 34 | } 35 | return defaultValue 36 | } 37 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/antihax/optional 2 | 3 | go 1.13 4 | -------------------------------------------------------------------------------- /int.go: -------------------------------------------------------------------------------- 1 | package optional 2 | 3 | type Int struct { 4 | isSet bool 5 | value int 6 | } 7 | 8 | func NewInt(value int) Int { 9 | return Int{ 10 | true, 11 | value, 12 | } 13 | } 14 | 15 | // EmptyInt returns a new Int that does not have a value set. 16 | func EmptyInt() Int { 17 | return Int{ 18 | false, 19 | 0, 20 | } 21 | } 22 | 23 | func (i Int) IsSet() bool { 24 | return i.isSet 25 | } 26 | 27 | func (i Int) Value() int { 28 | return i.value 29 | } 30 | 31 | func (i Int) Default(defaultValue int) int { 32 | if i.isSet { 33 | return i.value 34 | } 35 | return defaultValue 36 | } 37 | -------------------------------------------------------------------------------- /int16.go: -------------------------------------------------------------------------------- 1 | package optional 2 | 3 | type Int16 struct { 4 | isSet bool 5 | value int16 6 | } 7 | 8 | func NewInt16(value int16) Int16 { 9 | return Int16{ 10 | true, 11 | value, 12 | } 13 | } 14 | 15 | // EmptyInt16 returns a new Int16 that does not have a value set. 16 | func EmptyInt16() Int16 { 17 | return Int16{ 18 | false, 19 | 0, 20 | } 21 | } 22 | 23 | func (i Int16) IsSet() bool { 24 | return i.isSet 25 | } 26 | 27 | func (i Int16) Value() int16 { 28 | return i.value 29 | } 30 | 31 | func (i Int16) Default(defaultValue int16) int16 { 32 | if i.isSet { 33 | return i.value 34 | } 35 | return defaultValue 36 | } 37 | -------------------------------------------------------------------------------- /int32.go: -------------------------------------------------------------------------------- 1 | package optional 2 | 3 | type Int32 struct { 4 | isSet bool 5 | value int32 6 | } 7 | 8 | func NewInt32(value int32) Int32 { 9 | return Int32{ 10 | true, 11 | value, 12 | } 13 | } 14 | 15 | // EmptyInt32 returns a new Int32 that does not have a value set. 16 | func EmptyInt32() Int32 { 17 | return Int32{ 18 | false, 19 | 0, 20 | } 21 | } 22 | 23 | func (i Int32) IsSet() bool { 24 | return i.isSet 25 | } 26 | 27 | func (i Int32) Value() int32 { 28 | return i.value 29 | } 30 | 31 | func (i Int32) Default(defaultValue int32) int32 { 32 | if i.isSet { 33 | return i.value 34 | } 35 | return defaultValue 36 | } 37 | -------------------------------------------------------------------------------- /int64.go: -------------------------------------------------------------------------------- 1 | package optional 2 | 3 | type Int64 struct { 4 | isSet bool 5 | value int64 6 | } 7 | 8 | func NewInt64(value int64) Int64 { 9 | return Int64{ 10 | true, 11 | value, 12 | } 13 | } 14 | 15 | // EmptyInt64 returns a new Int64 that does not have a value set. 16 | func EmptyInt64() Int64 { 17 | return Int64{ 18 | false, 19 | 0, 20 | } 21 | } 22 | 23 | func (i Int64) IsSet() bool { 24 | return i.isSet 25 | } 26 | 27 | func (i Int64) Value() int64 { 28 | return i.value 29 | } 30 | 31 | func (i Int64) Default(defaultValue int64) int64 { 32 | if i.isSet { 33 | return i.value 34 | } 35 | return defaultValue 36 | } 37 | -------------------------------------------------------------------------------- /int8.go: -------------------------------------------------------------------------------- 1 | package optional 2 | 3 | type Int8 struct { 4 | isSet bool 5 | value int8 6 | } 7 | 8 | func NewInt8(value int8) Int8 { 9 | return Int8{ 10 | true, 11 | value, 12 | } 13 | } 14 | 15 | // EmptyInt8 returns a new Int8 that does not have a value set. 16 | func EmptyInt8() Int8 { 17 | return Int8{ 18 | false, 19 | 0, 20 | } 21 | } 22 | 23 | func (i Int8) IsSet() bool { 24 | return i.isSet 25 | } 26 | 27 | func (i Int8) Value() int8 { 28 | return i.value 29 | } 30 | 31 | func (i Int8) Default(defaultValue int8) int8 { 32 | if i.isSet { 33 | return i.value 34 | } 35 | return defaultValue 36 | } 37 | -------------------------------------------------------------------------------- /interface.go: -------------------------------------------------------------------------------- 1 | package optional 2 | 3 | // Optional represents a generic optional type, stored as an interface{}. 4 | type Interface struct { 5 | isSet bool 6 | value interface{} 7 | } 8 | 9 | func NewInterface(value interface{}) Interface { 10 | return Interface{ 11 | true, 12 | value, 13 | } 14 | } 15 | 16 | // EmptyInterface returns a new Interface that does not have a value set. 17 | func EmptyInterface() Interface { 18 | return Interface{ 19 | false, 20 | nil, 21 | } 22 | } 23 | 24 | func (b Interface) IsSet() bool { 25 | return b.isSet 26 | } 27 | 28 | func (b Interface) Value() interface{} { 29 | return b.value 30 | } 31 | 32 | func (b Interface) Default(defaultValue interface{}) interface{} { 33 | if b.isSet { 34 | return b.value 35 | } 36 | return defaultValue 37 | } 38 | -------------------------------------------------------------------------------- /rune.go: -------------------------------------------------------------------------------- 1 | package optional 2 | 3 | type Rune struct { 4 | isSet bool 5 | value rune 6 | } 7 | 8 | func NewRune(value rune) Rune { 9 | return Rune{ 10 | true, 11 | value, 12 | } 13 | } 14 | 15 | // EmptyRune returns a new Rune that does not have a value set. 16 | func EmptyRune() Rune { 17 | return Rune{ 18 | false, 19 | 0, 20 | } 21 | } 22 | 23 | func (b Rune) IsSet() bool { 24 | return b.isSet 25 | } 26 | 27 | func (b Rune) Value() rune { 28 | return b.value 29 | } 30 | 31 | func (b Rune) Default(defaultValue rune) rune { 32 | if b.isSet { 33 | return b.value 34 | } 35 | return defaultValue 36 | } 37 | -------------------------------------------------------------------------------- /string.go: -------------------------------------------------------------------------------- 1 | package optional 2 | 3 | type String struct { 4 | isSet bool 5 | value string 6 | } 7 | 8 | func NewString(value string) String { 9 | return String{ 10 | true, 11 | value, 12 | } 13 | } 14 | 15 | // EmptyString returns a new String that does not have a value set. 16 | func EmptyString() String { 17 | return String{ 18 | false, 19 | "", 20 | } 21 | } 22 | 23 | func (b String) IsSet() bool { 24 | return b.isSet 25 | } 26 | 27 | func (b String) Value() string { 28 | return b.value 29 | } 30 | 31 | func (b String) Default(defaultValue string) string { 32 | if b.isSet { 33 | return b.value 34 | } 35 | return defaultValue 36 | } 37 | -------------------------------------------------------------------------------- /time.go: -------------------------------------------------------------------------------- 1 | package optional 2 | 3 | import "time" 4 | 5 | type Time struct { 6 | isSet bool 7 | value time.Time 8 | } 9 | 10 | func NewTime(value time.Time) Time { 11 | return Time{ 12 | true, 13 | value, 14 | } 15 | } 16 | 17 | // EmptyTime returns a new Time that does not have a value set. 18 | func EmptyTime() Time { 19 | return Time{ 20 | false, 21 | time.Time{}, 22 | } 23 | } 24 | 25 | func (b Time) IsSet() bool { 26 | return b.isSet 27 | } 28 | 29 | func (b Time) Value() time.Time { 30 | return b.value 31 | } 32 | 33 | func (b Time) Default(defaultValue time.Time) time.Time { 34 | if b.isSet { 35 | return b.value 36 | } 37 | return defaultValue 38 | } 39 | -------------------------------------------------------------------------------- /uint.go: -------------------------------------------------------------------------------- 1 | package optional 2 | 3 | type Uint struct { 4 | isSet bool 5 | value uint 6 | } 7 | 8 | func NewUint(value uint) Uint { 9 | return Uint{ 10 | true, 11 | value, 12 | } 13 | } 14 | 15 | // EmptyUint returns a new Uint that does not have a value set. 16 | func EmptyUint() Uint { 17 | return Uint{ 18 | false, 19 | 0, 20 | } 21 | } 22 | 23 | func (i Uint) IsSet() bool { 24 | return i.isSet 25 | } 26 | 27 | func (i Uint) Value() uint { 28 | return i.value 29 | } 30 | 31 | func (i Uint) Default(defaultValue uint) uint { 32 | if i.isSet { 33 | return i.value 34 | } 35 | return defaultValue 36 | } 37 | -------------------------------------------------------------------------------- /uint16.go: -------------------------------------------------------------------------------- 1 | package optional 2 | 3 | type Uint16 struct { 4 | isSet bool 5 | value uint16 6 | } 7 | 8 | func NewUint16(value uint16) Uint16 { 9 | return Uint16{ 10 | true, 11 | value, 12 | } 13 | } 14 | 15 | // EmptyUint16 returns a new Uint16 that does not have a value set. 16 | func EmptyUint16() Uint16 { 17 | return Uint16{ 18 | false, 19 | 0, 20 | } 21 | } 22 | 23 | func (i Uint16) IsSet() bool { 24 | return i.isSet 25 | } 26 | 27 | func (i Uint16) Value() uint16 { 28 | return i.value 29 | } 30 | 31 | func (i Uint16) Default(defaultValue uint16) uint16 { 32 | if i.isSet { 33 | return i.value 34 | } 35 | return defaultValue 36 | } 37 | -------------------------------------------------------------------------------- /uint32.go: -------------------------------------------------------------------------------- 1 | package optional 2 | 3 | type Uint32 struct { 4 | isSet bool 5 | value uint32 6 | } 7 | 8 | func NewUint32(value uint32) Uint32 { 9 | return Uint32{ 10 | true, 11 | value, 12 | } 13 | } 14 | 15 | // EmptyUint32 returns a new Uint32 that does not have a value set. 16 | func EmptyUint32() Uint32 { 17 | return Uint32{ 18 | false, 19 | 0, 20 | } 21 | } 22 | 23 | func (i Uint32) IsSet() bool { 24 | return i.isSet 25 | } 26 | 27 | func (i Uint32) Value() uint32 { 28 | return i.value 29 | } 30 | 31 | func (i Uint32) Default(defaultValue uint32) uint32 { 32 | if i.isSet { 33 | return i.value 34 | } 35 | return defaultValue 36 | } 37 | -------------------------------------------------------------------------------- /uint64.go: -------------------------------------------------------------------------------- 1 | package optional 2 | 3 | type Uint64 struct { 4 | isSet bool 5 | value uint64 6 | } 7 | 8 | func NewUint64(value uint64) Uint64 { 9 | return Uint64{ 10 | true, 11 | value, 12 | } 13 | } 14 | 15 | // EmptyUint64 returns a new Uint64 that does not have a value set. 16 | func EmptyUint64() Uint64 { 17 | return Uint64{ 18 | false, 19 | 0, 20 | } 21 | } 22 | 23 | func (i Uint64) IsSet() bool { 24 | return i.isSet 25 | } 26 | 27 | func (i Uint64) Value() uint64 { 28 | return i.value 29 | } 30 | 31 | func (i Uint64) Default(defaultValue uint64) uint64 { 32 | if i.isSet { 33 | return i.value 34 | } 35 | return defaultValue 36 | } 37 | -------------------------------------------------------------------------------- /uint8.go: -------------------------------------------------------------------------------- 1 | package optional 2 | 3 | type Uint8 struct { 4 | isSet bool 5 | value uint8 6 | } 7 | 8 | func NewUint8(value uint8) Uint8 { 9 | return Uint8{ 10 | true, 11 | value, 12 | } 13 | } 14 | 15 | // EmptyUint8 returns a new Uint8 that does not have a value set. 16 | func EmptyUint8() Uint8 { 17 | return Uint8{ 18 | false, 19 | 0, 20 | } 21 | } 22 | 23 | func (i Uint8) IsSet() bool { 24 | return i.isSet 25 | } 26 | 27 | func (i Uint8) Value() uint8 { 28 | return i.value 29 | } 30 | 31 | func (i Uint8) Default(defaultValue uint8) uint8 { 32 | if i.isSet { 33 | return i.value 34 | } 35 | return defaultValue 36 | } 37 | -------------------------------------------------------------------------------- /uintptr.go: -------------------------------------------------------------------------------- 1 | package optional 2 | 3 | type Uintptr struct { 4 | isSet bool 5 | value uintptr 6 | } 7 | 8 | func NewUintptr(value uintptr) Uintptr { 9 | return Uintptr{ 10 | true, 11 | value, 12 | } 13 | } 14 | 15 | // EmptyUintptr returns a new Uintptr that does not have a value set. 16 | func EmptyUintptr() Uintptr { 17 | return Uintptr{ 18 | false, 19 | 0, 20 | } 21 | } 22 | 23 | func (i Uintptr) IsSet() bool { 24 | return i.isSet 25 | } 26 | 27 | func (i Uintptr) Value() uintptr { 28 | return i.value 29 | } 30 | 31 | func (i Uintptr) Default(defaultValue uintptr) uintptr { 32 | if i.isSet { 33 | return i.value 34 | } 35 | return defaultValue 36 | } 37 | --------------------------------------------------------------------------------