Skip to content

gomig/caster

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Caster

Error-safe value caster.

NewCaster

Create new caster instance from value.

// Signature
NewCaster(data any) Caster

// Example
import "github.com/gomig/caster"
c := caster.NewCaster(1234)

Note: Slice caster methods is type safe and only returns typed items.

import "github.com/gomig/caster"
c := caster.NewCaster([]any{"First", true, false})
c.Slice() // ["First", true, false]
c.BoolSlice(nil) // [true, false]
c.StringSlice(nil) // ["First"]
c.UIntSlice(nil) // nil

IsNilErr

Check if returns error is nil error.

// Signature
IsNilErr(err error) bool

// Example
import "github.com/gomig/caster"
c := caster.NewCaster(nil)
v, err := c.Int()
if caster.IsNilErr(err) {
    // ...
}

Methods

Caster has following methods:

IsNil

Check if value of caster is nil

IsNil() bool

Interface

Get raw value.

Interface() any

Slice

Parse data as []any

Slice() []any

Bool

Parse data as boolean or return error on fail

Bool() (bool, error)

BoolSafe

Parse data as boolean or return fallback

BoolSafe(fallback bool) bool

BoolSlice

Parse data as []bool or return fallback

BoolSlice(fallback []bool) []bool

Int

Parse data as int or return error on fail

Int() (int, error)

IntSafe

Parse data as int or return fallback

IntSafe(fallback int) int

IntSlice

Parse data as []int or return fallback

IntSlice(fallback []int) []int

Int8

Parse data as int8 or return error on fail

Int8() (int8, error)

Int8Safe

Parse data as int8 or return fallback

Int8Safe(fallback int8) int8

Int8Slice

Parse data as []int8 or return fallback

Int8Slice(fallback []int8) []int8

Int16

Parse data as int16 or return error on fail

Int16() (int16, error)

Int16Safe

Parse data as int16 or return fallback

Int16Safe(fallback int16) int16

Int16Slice

Parse data as []int16 or return fallback

Int16Slice(fallback []int16) []int16

Int32

Parse data as int32 or return error on fail

Int32() (int32, error)

Int32Safe

Parse data as int32 or return fallback

Int32Safe(fallback int32) int32

Int32Slice

Parse data as []int32 or return fallback

Int32Slice(fallback []int32) []int32

Int64

Parse data as int64 or return error on fail

Int64() (int64, error)

Int64Safe

Parse data as int64 or return fallback

Int64Safe(fallback int64) int64

Int64Slice

Parse data as []int64 or return fallback

Int64Slice(fallback []int64) []int64

UInt

Parse data as uint or return error on fail

UInt() (uint, error)

UIntSafe

Parse data as uint or return fallback

UIntSafe(fallback uint) uint

UIntSlice

Parse data as []uint or return fallback

UIntSlice(fallback []uint) []uint

UInt8

Parse data as uint8 or return error on fail

UInt8() (uint8, error)

UInt8Safe

Parse data as uint8 or return fallback

UInt8Safe(fallback uint8) uint8

UInt8Slice

Parse data as []uint8 or return fallback

UInt8Slice(fallback []uint8) []uint8

UInt16

Parse data as uint16 or return error on fail

UInt16() (uint16, error)

UInt16Safe

Parse data as uint16 or return fallback

UInt16Safe(fallback uint16) uint16

UInt16Slice

Parse data as []uint16 or return fallback

UInt16Slice(fallback []uint16) []uint16

UInt32

Parse data as uint32 or return error on fail

UInt32() (uint32, error)

UInt32Safe

Parse data as uint32 or return fallback

UInt32Safe(fallback uint32) uint32

UInt32Slice

Parse data as []uint32 or return fallback

UInt32Slice(fallback []uint32) []uint32

UInt64

Parse data as uint64 or return error on fail

UInt64() (uint64, error)

UInt64Safe

Parse data as uint64 or return fallback

UInt64Safe(fallback uint64) uint64

UInt64Slice

Parse data as []uint64 or return fallback

UInt64Slice(fallback []uint64) []uint64

Float32

Parse data as float32 or return error on fail

Float32() (float32, error)

Float32Safe

Parse data as float32 or return fallback

Float32Safe(fallback float32) float32

Float32Slice

Parse data as []float32 or return fallback

Float32Slice(fallback []float32) []float32

Float64

Parse data as float64 or return error on fail

Float64() (float64, error)

Float64Safe

Parse data as float64 or return fallback

Float64Safe(fallback float64) float64

Float64Slice

Parse data as []float64 or return fallback

Float64Slice(fallback []float64) []float64

String

Parse data as string or return error on fail

String() (string, error)

StringSafe

Parse data as string or return fallback

StringSafe(fallback string) string

StringSlice

Parse data as []string or return fallback

StringSlice(fallback []string) []string