optl

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2022 License: BSD-3-Clause Imports: 5 Imported by: 0

README

Go version License

The optl package is an Go implementation of the null object pattern. This implementation is based on the Java 8 Optional class.


Features

  • Uses the new 'Generics' feature added in Go version 1.18, all types are supported.
  • The optl.Type implements the JSON Marshaller/Unmarshaller and the SQL Scanner/Valuer interfaces.
  • The optl.Type is immutable except for the UnmarshalJSON and Scan methods, these methods should not be used directly.

Installation

go get github.com/joselalvarez/optl@v1.0.0

Usage Overview

Import package

import (
	"github.com/joselalvarez/optl"
)

Variables and Type declaration

type Sample struct{
    Field1 optl.Type[string] // Optional string field
    Field2 optl.Type[int]    // Optional integer field
}
...
var bar optl.Type[Sample] // Optional struct variable
var foo optl.Type[float64] // Optional float variable

foo.IsEmpty() // true, by default the optional is empty

Constructors

foo := optl.Of("Hello optional") // Optional with value 
bar := optl.Empty[Sample]() // Empty optional eq. -> bar := optl.Type[Sample]
var ref *float64
...
baz := optl.OfNillable(ref) // Nillable optional

Get(), IsPresent() and IsEmpty() methods

foo := optl.Of("Hello optional")

foo.IsPresent() // true

bar := optl.Empty[int]()

bar.IsEmpty() // true

bar.Get() //Panic error!!

IfPresent(...) method

foo := optl.Of("Hello optional")

foo.IfPresent(func(value string){
    fmt.Println(value)
})

OrElse(...) method

foo := optl.Empty[int]()

foo.OrElse(0) // 0

OrElseGet(...) method

func Zero() int{
    return 0
}
...

foo := optl.Empty[int]() 

foo.OrElseGet(Zero) // 0

Filter(...) method


func Odd(i int) bool{
    return i % 2 != 0
}
...

foo := optl.Of(1)
 
foo.Filter(Odd).IsPresent() // true

JSON Marshalling

	sample := Sample{
		Field1: optl.Of("Text ..."),
		Field2: optl.Of(1),
	}

	bytes, _ := json.Marshal(sample)

	fmt.Println(string(bytes)) // {"Field1":"Text ...","Field2":1}

	sample = Sample{
		Field1: optl.Of("Other Text ..."),
		Field2: optl.Empty[int](),
	}

	bytes, _ = json.Marshal(sample)

	fmt.Println(string(bytes)) // {"Field1":"Other Text ...","Field2":null}

JSON Unmarshalling

	var sample Sample
	json.Unmarshal([]byte(`{"Field1":"Text ...","Field2":1}`), &sample)

	fmt.Println(sample.Field1.Get()) // Text ...
	fmt.Println(sample.Field2.Get()) // 1

    var sample2 Sample
	json.Unmarshal([]byte(`{"Field1":"Other Text ...","Field2":null}`), &sample2)

	fmt.Println(sample2.Field1.Get())     // Other Text ...
	fmt.Println(sample2.Field2.IsPresent()) // false

SQL Scanner and Valuer

The optl.Type implements the SQL Scan(...) anf Value() interfaces. The use of optionals as SQL values ​​is restricted to the following types:

  • optl.Type[bool]
  • optl.Type[string]
  • optl.Type[int | int8 | int16 | int32 | int64]
  • optl.Type[float32 | float64]
  • optl.Type[time.Time]
  • optl.Type[[]byte]

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Type

type Type[T interface{}] struct {
	// contains filtered or unexported fields
}

The Optional Type.

Example: var foo optl.Type[string]

func Empty

func Empty[T interface{}]() Type[T]

Constructor of an empty optional.

Sample 1: foo := optl.Empty[string]()

Sample 2: bar := optl.Empty[MyStruct]()

func Of

func Of[T interface{}](v T) Type[T]

Constructor of an optional with value.

Example 1: foo := optl.Of("Hello optional")

Example 2: bar := optl.Of(2.3)

Example 3: baz := optl.Of[int64](3)

Example 4: quux := optl.Of(MyStruct{...})

func OfNillable

func OfNillable[T interface{}](v *T) Type[T]

Constructor of an optional with nillable value (pointer to value).

var bar * string

...

Example: foo := optl.OfNillable(bar)

func (Type[T]) Filter

func (o Type[T]) Filter(f func(T) bool) Type[T]

If the value is present executes the lambda function passed as parameter with the current value. If this function return false return a empty optional else return the current optional.

func (Type[T]) Get

func (o Type[T]) Get() T

Return the value of the optional, if it is not present throw a panic error.

func (Type[T]) IfPresent

func (o Type[T]) IfPresent(f func(T))

If the value is present executes the lambda function passed as parameter with the current value.

func (Type[T]) IsEmpty

func (o Type[T]) IsEmpty() bool

Return true if value is not present.

func (Type[T]) IsPresent

func (o Type[T]) IsPresent() bool

Return true if value is present.

func (Type[T]) MarshalJSON

func (o Type[T]) MarshalJSON() ([]byte, error)

JSON marshaller

func (Type[T]) OrElse

func (o Type[T]) OrElse(v T) T

If value is present return the current value, else return the value passed as parameter.

func (Type[T]) OrElseGet

func (o Type[T]) OrElseGet(f func() T) T

If value is present return the current value, else return the value returned by lambda function passed as parameter.

func (*Type[T]) Scan

func (o *Type[T]) Scan(value interface{}) error

SQL scanner

func (*Type[T]) UnmarshalJSON

func (o *Type[T]) UnmarshalJSON(data []byte) error

JSON unmarshaller

func (Type[T]) Value

func (o Type[T]) Value() (driver.Value, error)

SQL valuer

Jump to

Keyboard shortcuts

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