streng

package module
v0.0.0-...-28ae305 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 25, 2019 License: MIT Imports: 7 Imported by: 0

README

go-streng

Package streng provides a string option type, result type, and nullable type, for the Go programming language.

Documention

Online documentation, which includes examples, can be found at: http://godoc.org/github.com/reiver/go-streng

GoDoc

Option Types

Here is how you would declare a string option type:

var stringOption streng.Option

Result Types

Here is how you would declare a string result type:

var stringResult streng.Result

Nullable Types

Here is how you would declare a string nullable type:

var nullableString streng.Nullable

JSON Data Format

If you want to use one of these types with the JSON data format, and in particular using the built-in "encoding/json" package, then you should use streng.Nullable.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Nullable

type Nullable struct {
	// contains filtered or unexported fields
}

func Null

func Null() Nullable

func (Nullable) Else

func (receiver Nullable) Else(value string) Nullable

Else defaults this ‘streng.Nullable’ to ‘value’ if this ‘streng.Nullable’ has a value of ‘streng.Nothing().Nullable()’, or a value of ‘streng.Null()’, else it just returns itself as is.

func (Nullable) ElseUnwrap

func (receiver Nullable) ElseUnwrap(value string) string

func (Nullable) GoString

func (receiver Nullable) GoString() string

func (Nullable) Map

func (receiver Nullable) Map(fn func(string) string) Nullable

Map returns an ‘streng.Nullable’ containing the result of ‘fn’ applied to the value inside this ‘streng.Nullable’; if this ‘streng.Nullable’ is ‘streng.Nothing().Nullable()’, then it just returns ‘streng.Nothing()’; or if this ‘streng.Nullable’ is ‘streng.Null()’, then it just returns ‘streng.Null()’;

func (Nullable) MarshalJSON

func (receiver Nullable) MarshalJSON() ([]byte, error)

func (Nullable) Return

func (receiver Nullable) Return() (string, error)

func (Nullable) Then

func (receiver Nullable) Then(fn func(string) Nullable) Nullable

Then returns an ‘streng.Nullable’ containing the result of ‘fn’ applied to the value inside this ‘streng.Nullable’; if this ‘streng.Nullable’ is ‘streng.Nothing().Nullable()’, then it just returns ‘streng.Nothing()’; and if this ‘streng.Nullable’ is ‘streng.Null()’, then it just returns ‘streng.Null()’.

func (*Nullable) UnmarshalJSON

func (receiver *Nullable) UnmarshalJSON(data []byte) error

UnmarshalJSON makes streng.Nullable fit the json.Unmarshaler interface.

Note that streng.Nullable.UnmarshalJSON() also accepts JSON numbers, and not just JSON strings.

So if we have:

type Purchase struct {
	Amount streng.Nullable `json:"amount"`
}

It accepts this JSON with a string literal:

{
	"amount": "1.23"
}

But it also accepts this JSON with a number literal:

{
	"amount": 1.23
}

func (Nullable) Unwrap

func (receiver Nullable) Unwrap() (string, bool)

func (Nullable) Value

func (receiver Nullable) Value() (driver.Value, error)

Value makes ‘streng.Nullable’ fit the database/sql/driver.Valuer interface.

type Option

type Option struct {
	// contains filtered or unexported fields
}

Option is an ‘option type’ for string.

It can contain:

• ‘nothing’, or

• ‘something’.

Nothing

An uninitialize variable, of type ‘streng.Option’, contains ‘nothing’. I.e.,:...

var option streng.Option

You can determine if a variable of type ‘streng.Option’ contains ‘nothing’, or not, by using the ‘streng.Nothing()’ function.

For example:

if streng.Nothing() == option {
	//@TODO
}

Or:

switch option {
case streng.Nothing():
	//@TODO
default:
	//@TODO
}

You can also use the ‘streng.Nothing()’ function to give a variable of type ‘streng.Option’ the value of nothing ‘nothing’:

option = streng.Nothing()

Something

You can create a ‘streng.Option’ with ‘streng.Something()’. For example:

var option streng.Option = streng.Something("Hello world!")

func Nothing

func Nothing() Option

Nothing returns an empty streng.Option.

func Something

func Something(value string) Option

Something returns a streng.Option with ‘value’ in it.

func (Option) Else

func (receiver Option) Else(value string) Option

Else defaults this ‘streng.Option’ to ‘value’ if this ‘streng.Option’ has a value of ‘streng.Nothing()’, else it just returns itself as is.

func (Option) ElseUnwrap

func (receiver Option) ElseUnwrap(value string) string

func (Option) GoString

func (receiver Option) GoString() string

func (Option) Map

func (receiver Option) Map(fn func(string) string) Option

Map returns an ‘streng.Option’ containing the result of ‘fn’ applied to the value inside this ‘streng.Option’; if this ‘streng.Option’ is ‘streng.Nothing()’, then it just returns ‘streng.Nothing()’.

func (Option) MarshalJSON

func (receiver Option) MarshalJSON() ([]byte, error)

func (Option) Nullable

func (receiver Option) Nullable() Nullable

Nullable returns the equivalent ‘streng.Nullable’ for this ‘streng.Option’.

func (Option) Return

func (receiver Option) Return() (string, error)

Return returns the string inside, if there is one inside.

func (Option) Then

func (receiver Option) Then(fn func(string) Option) Option

Then returns an ‘streng.Option’ containing the result of ‘fn’ applied to the value inside this ‘streng.Option’; if this ‘streng.Option’ is ‘streng.Nothing()’, then it just returns ‘streng.Nothing()’.

func (*Option) UnmarshalJSON

func (receiver *Option) UnmarshalJSON(data []byte) error

func (Option) Unwrap

func (receiver Option) Unwrap() (string, bool)

Unwrap returns the string inside, if there is one inside.

func (Option) Value

func (receiver Option) Value() (driver.Value, error)

Value makes ‘streng.Option’ fit the database/sql/driver.Valuer interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL