u

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2023 License: MIT Imports: 0 Imported by: 10

README

u

u provides a simple way to create variables that are "unset" by default.

u is dependency free and has 100% test coverage.

Why?

Go's default values are great most of the time, but sometimes you want to know if a value has been set or not. This is especially true when you want to know if a value has been set to its zero value or not.

For example, let's say you had a preferences struct like this:

type Preferences struct {
    UseFeatureX bool
    Threshold int
}

func processPreferences(prefs Preferences) {
    // Uh oh...we're in a heap of trouble here...
    thirdPartyLibrary.EnableFeatureX(prefs.UseFeatureX)
    thirdPartyLibrary.SetThreshold(prefs.Threshold)
}

func main() {
    var prefs Preferences
    processPreferences(prefs)
}

There is no real way to know if UseFeatureX or Threshold have been set or not.

Using this library we can eliminate this problem:

type Preferences struct {
    UseFeatureX u.Bool
    Threshold u.Int
}

func processPreferences(prefs Preferences) {
    // #winning
    if prefs.UseFeatureX.IsSet() {
        thirdPartyLibrary.EnableFeatureX(prefs.UseFeatureX.Get())
    }
    if prefs.Threshold.IsSet() {
        thirdPartyLibrary.SetThreshold(prefs.Threshold.Get())
    }
}

func main() {
    var prefs Preferences
    processPreferences(prefs)
}

Why should any of this matter?

It matters when you are working with third party libraries or frameworks that have default values that may not be the same as the Zero Go values. Perhaps the default value for a preference is true and you only want to set it if an explicit value has been specified.

Usage

Basic Usage
var myVar u.Int

// Set the value
myVar.Set(10)

// Get the value
fmt.Println(myVar.Get()) // 10

// Check if the value has been set
fmt.Println(myVar.IsSet()) // true

// Unset the value
myVar.Unset()

// Check if the value has been set
fmt.Println(myVar.IsSet()) // false
Structs

New methods are provided for setting values in structs.

    type MyStruct struct {
        MyVar u.Int
        MyOption u.Bool
    }
	
    myStruct := MyStruct{
        MyVar: u.NewInt(42),
        MyOption: u.True,
    }

Why not use pointer values?

Yes, that's one way you can solve this issue. Personally, I try to avoid having pointer values as it increases the chance of dereferencing errors. It also feels like a hacky approach to the problem which is one missed test away from a runtime error.

Supported types

  • u.Bool
  • u.Int
  • u.Int8
  • u.Int16
  • u.Int32
  • u.Int64
  • u.Uint
  • u.Uint8
  • u.Uint16
  • u.Uint32
  • u.Uint64
  • u.Float32
  • u.Float64
  • u.Complex64
  • u.Complex128
  • u.String
  • u.Byte
  • u.Rune

Values

  • u.True
  • u.False

Custom types

Any type can be used with this library by creating a u.Var of that type. For example:


type MyCustomType struct {
    value string
}

var myVar u.Var[MyCustomType]

// Set the value
myVar.Set(MyCustomType{value: "hello"})

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var False = NewBool(false)

False is a `bool` that is set to false

View Source
var True = NewBool(true)

True is a `bool` that is set to true

Functions

This section is empty.

Types

type Bool

type Bool = Var[bool]

Bool is a `bool` that can be unset

func NewBool

func NewBool(val bool) Bool

NewBool creates a new Bool with the given value

type Byte

type Byte = Var[byte]

Byte is a `byte` that can be unset

func NewByte

func NewByte(val byte) Byte

NewByte creates a new Byte with the given value

type Complex128

type Complex128 = Var[complex128]

Complex128 is a `complex128` that can be unset

func NewComplex128

func NewComplex128(val complex128) Complex128

NewComplex128 creates a new Complex128 with the given value

type Complex64

type Complex64 = Var[complex64]

Complex64 is a `complex64` that can be unset

func NewComplex64

func NewComplex64(val complex64) Complex64

NewComplex64 creates a new Complex64 with the given value

type Float32

type Float32 = Var[float32]

Float32 is a `float32` that can be unset

func NewFloat32

func NewFloat32(val float32) Float32

NewFloat32 creates a new Float32 with the given value

type Float64

type Float64 = Var[float64]

Float64 is a `float64` that can be unset

func NewFloat64

func NewFloat64(val float64) Float64

NewFloat64 creates a new Float64 with the given value

type Int

type Int = Var[int]

Int is an `int` that can be unset

func NewInt

func NewInt(val int) Int

NewInt creates a new Int with the given value

type Int16

type Int16 = Var[int16]

Int16 is an `int16` that can be unset

func NewInt16

func NewInt16(val int16) Int16

NewInt16 creates a new Int16 with the given value

type Int32

type Int32 = Var[int32]

Int32 is an `int32` that can be unset

func NewInt32

func NewInt32(val int32) Int32

NewInt32 creates a new Int32 with the given value

type Int64

type Int64 = Var[int64]

Int64 is an `int64` that can be unset

func NewInt64

func NewInt64(val int64) Int64

NewInt64 creates a new Int64 with the given value

type Int8

type Int8 = Var[int8]

Int8 is an `int8` that can be unset

func NewInt8

func NewInt8(val int8) Int8

NewInt8 creates a new Int8 with the given value

type Rune

type Rune = Var[rune]

Rune is a `rune` that can be unset

func NewRune

func NewRune(val rune) Rune

NewRune creates a new Rune with the given value

type String

type String = Var[string]

String is a `string` that can be unset

func NewString

func NewString(val string) String

NewString creates a new String with the given value

type Uint

type Uint = Var[uint]

Uint is a `uint` that can be unset

func NewUint

func NewUint(val uint) Uint

NewUint creates a new Uint with the given value

type Uint16

type Uint16 = Var[uint16]

Uint16 is a `uint16` that can be unset

func NewUint16

func NewUint16(val uint16) Uint16

NewUint16 creates a new Uint16 with the given value

type Uint32

type Uint32 = Var[uint32]

Uint32 is a `uint32` that can be unset

func NewUint32

func NewUint32(val uint32) Uint32

NewUint32 creates a new Uint32 with the given value

type Uint64

type Uint64 = Var[uint64]

Uint64 is a `uint64` that can be unset

func NewUint64

func NewUint64(val uint64) Uint64

NewUint64 creates a new Uint64 with the given value

type Uint8

type Uint8 = Var[uint8]

Uint8 is a `uint8` that can be unset

func NewUint8

func NewUint8(val uint8) Uint8

NewUint8 creates a new Uint8 with the given value

type Var

type Var[T any] struct {
	// contains filtered or unexported fields
}

Var is a variable that can be set, unset and queried for its state.

func NewVar

func NewVar[T any](val T) Var[T]

NewVar creates a new Var with the given value

func (*Var[T]) Get

func (v *Var[T]) Get() T

Get the value of the variable Returns the zero value if unset

func (*Var[T]) IsSet

func (v *Var[T]) IsSet() bool

IsSet returns true when a value has been set

func (*Var[T]) Set

func (v *Var[T]) Set(val T)

Set the value of the variable

func (*Var[T]) Unset

func (v *Var[T]) Unset()

Unset resets the value to the zero value and sets the variable to "unset"

Jump to

Keyboard shortcuts

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