cast

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2025 License: ISC Imports: 6 Imported by: 4

README

Golang Dynamic Type Cast

GitHub Tag Go Reference License Go Report Card Contributors Issues

The cast package provides utilities for type casting and conversion in Go. It includes functions for converting values to various Go types, such as bool, int, uint, float, string, and their respective slices. Additionally, it offers a Caster interface for more structured type conversion.

Installation

To install the cast package, use the following command:

go get github.com/go-universal/cast

Then, import the package in your Go code:

import "github.com/go-universal/cast"

Functions

ToBool

Converts an interface to a bool.
Signature:

func ToBool(value interface{}) (bool, error)

Example:

result, err := cast.ToBool("true")
fmt.Println(result) // Output: true
ToBoolSlice

Converts an interface to a slice of bool.
Signature:

func ToBoolSlice(value interface{}) ([]bool, error)

Example:

result, err := cast.ToBoolSlice([]string{"true", "false"})
fmt.Println(result) // Output: [true false]
ToSigned

Converts an interface to a signed integer type (int, int8, int16, int32, int64).
Signature:

func ToSigned[T int | int8 | int16 | int32 | int64](value interface{}) (T, error)

Example:

result, err := cast.ToSigned[int]("123")
fmt.Println(result) // Output: 123
ToSignedSlice

Converts an interface to a slice of signed integers.
Signature:

func ToSignedSlice[T int | int8 | int16 | int32 | int64](value interface{}) ([]T, error)

Example:

result, err := cast.ToSignedSlice[int]([]string{"1", "2"})
fmt.Println(result) // Output: [1 2]
ToUnsigned

Converts an interface to an unsigned integer type (uint, uint8, uint16, uint32, uint64).
Signature:

func ToUnsigned[T uint | uint8 | uint16 | uint32 | uint64](value interface{}) (T, error)

Example:

result, err := cast.ToUnsigned[uint]("123")
fmt.Println(result) // Output: 123
ToUnsignedSlice

Converts an interface to a slice of unsigned integers.
Signature:

func ToUnsignedSlice[T uint | uint8 | uint16 | uint32 | uint64](value interface{}) ([]T, error)

Example:

result, err := cast.ToUnsignedSlice[uint]([]string{"1", "2"})
fmt.Println(result) // Output: [1 2]
ToFloat

Converts an interface to a float type (float32, float64).
Signature:

func ToFloat[T float32 | float64](value interface{}) (T, error)

Example:

result, err := cast.ToFloat[float64]("123.45")
fmt.Println(result) // Output: 123.45
ToFloatSlice

Converts an interface to a slice of floats.
Signature:

func ToFloatSlice[T float32 | float64](value interface{}) ([]T, error)

Example:

result, err := cast.ToFloatSlice[float64]([]string{"1.1", "2.2"})
fmt.Println(result) // Output: [1.1 2.2]
ToString

Converts an interface to a string.
Signature:

func ToString(value interface{}) (string, error)

Example:

result, err := cast.ToString(123)
fmt.Println(result) // Output: "123"
ToStringSlice

Converts an interface to a slice of string.
Signature:

func ToStringSlice(value interface{}) ([]string, error)

Example:

result, err := cast.ToStringSlice([]int{1, 2})
fmt.Println(result) // Output: ["1" "2"]
ToSlice

Converts an interface to a slice of interface{}.
Signature:

func ToSlice(value interface{}) ([]interface{}, error)

Example:

result, err := cast.ToSlice([]int{1, 2})
fmt.Println(result) // Output: [1 2]

Caster Interface

The Caster interface provides methods for type casting and conversion. It allows structured and reusable type conversions with fallback mechanisms.

Methods
  • IsNil() bool: Checks if the value is nil.
  • Interface() interface{}: Returns the value as an interface{}.
  • Bool() (bool, error): Converts the value to a bool.
  • BoolSafe(fallback bool) bool: Converts the value to a bool, returning a fallback value on error.
  • BoolSlice() ([]bool, error): Converts the value to a slice of bool.
  • BoolSliceSafe(fallback []bool) []bool: Converts the value to a slice of bool, returning a fallback value on error.
  • Int() (int, error): Converts the value to an int.
  • IntSafe(fallback int) int: Converts the value to an int, returning a fallback value on error.
  • IntSlice() ([]int, error): Converts the value to a slice of int.
  • IntSliceSafe(fallback []int) []int: Converts the value to a slice of int, returning a fallback value on error.
  • Float64() (float64, error): Converts the value to a float64.
  • Float64Safe(fallback float64) float64: Converts the value to a float64, returning a fallback value on error.
  • String() (string, error): Converts the value to a string.
  • StringSafe(fallback string) string: Converts the value to a string, returning a fallback value on error.
Example Usage
caster := cast.NewCaster("123")

// Convert to int
intValue, err := caster.Int()
fmt.Println(intValue) // Output: 123

// Convert to bool with fallback
boolValue := caster.BoolSafe(false)
fmt.Println(boolValue) // Output: true

// Convert to string slice
stringSlice, err := caster.StringSlice()
fmt.Println(stringSlice) // Output: ["1" "2" "3"]

Error Handling

The package provides utility functions to identify specific error types:

  • IsNilError(err error) bool: Checks if the error is due to a nil value.
  • IsCastError(err error) bool: Checks if the error is due to an invalid type conversion.
  • IsOverflowError(err error) bool: Checks if the error is due to a value overflow.

This documentation provides a comprehensive overview of the cast package and its capabilities. For more details, refer to the source code or examples provided in the repository.

Documentation

Overview

From html/template/content.go Copyright 2011 The Go Authors. All rights reserved.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsCastError

func IsCastError(err error) bool

IsCastError returns true if the error is not nil and represents a type casting error.

func IsNilError

func IsNilError(err error) bool

IsNilError returns true if the error is not nil and represents a nil value error.

func IsOverflowError

func IsOverflowError(err error) bool

IsOverflowError returns true if the error is not nil and represents a value overflow error.

func ToBool

func ToBool(value interface{}) (bool, error)

ToBool converts an interface to a bool. Returns an error if the conversion is not possible.

func ToBoolSlice

func ToBoolSlice(value interface{}) ([]bool, error)

ToBoolSlice converts an interface to a slice of bool. Returns an error if the conversion is not possible.

func ToFloat

func ToFloat[T float32 | float64](value interface{}) (T, error)

ToFloat converts an interface to a float type (float32 or float64).

func ToFloatSlice

func ToFloatSlice[T float32 | float64](value interface{}) ([]T, error)

ToFloatSlice converts an interface to a slice of float types (float32 or float64).

func ToSigned

func ToSigned[T int | int8 | int16 | int32 | int64](value interface{}) (T, error)

ToSigned converts an interface to a signed integer type (int, int8, int16, int32, int64).

func ToSignedSlice

func ToSignedSlice[T int | int8 | int16 | int32 | int64](value interface{}) ([]T, error)

ToSignedSlice converts an interface to a slice of signed integers (int, int8, int16, int32, int64).

func ToSlice

func ToSlice(value interface{}) ([]interface{}, error)

ToSlice converts an interface to a slice of interface{}. Returns an error if the conversion is not possible.

func ToString

func ToString(value interface{}) (string, error)

ToString converts an interface to a string. Returns an error if the conversion is not possible.

func ToStringSlice

func ToStringSlice(value interface{}) ([]string, error)

ToStringSlice converts an interface to a slice of string. Returns an error if the conversion is not possible.

func ToUnsigned

func ToUnsigned[T uint | uint8 | uint16 | uint32 | uint64](value interface{}) (T, error)

ToUnsigned converts an interface to an unsigned integer type (uint, uint8, uint16, uint32, uint64).

func ToUnsignedSlice

func ToUnsignedSlice[T uint | uint8 | uint16 | uint32 | uint64](value interface{}) ([]T, error)

ToUnsignedSlice converts an interface to a slice of unsigned integers (uint, uint8, uint16, uint32, uint64).

Types

type BoolProvider

type BoolProvider interface {
	Bool() (bool, error)
}

BoolProvider defines an interface for providing a boolean value with an error.

type BoolSliceProvider

type BoolSliceProvider interface {
	BoolSlice() ([]bool, error)
}

BoolSliceProvider defines an interface for providing a slice of boolean with an error.

type Caster

type Caster interface {
	// IsNil checks if the value is nil.
	IsNil() bool

	// Interface returns the value as an interface{}.
	Interface() interface{}

	// Slice converts the value to a slice of interface{}.
	Slice() ([]interface{}, error)

	// SliceSafe converts the value to a slice of interface{}, with a fallback on error.
	SliceSafe(fallback []interface{}) []interface{}

	// Unmarshal decodes the value into the provided output using JSON.
	Unmarshal(out interface{}) error

	// Bool converts the value to a bool.
	Bool() (bool, error)

	// BoolSafe converts the value to a bool, with a fallback on error.
	BoolSafe(fallback bool) bool

	// BoolSlice converts the value to a slice of bool.
	BoolSlice() ([]bool, error)

	// BoolSliceSafe converts the value to a slice of bool, with a fallback on error.
	BoolSliceSafe(fallback []bool) []bool

	// Int converts the value to an int.
	Int() (int, error)

	// IntSafe converts the value to an int, with a fallback on error.
	IntSafe(fallback int) int

	// IntSlice converts the value to a slice of int.
	IntSlice() ([]int, error)

	// IntSliceSafe converts the value to a slice of int, with a fallback on error.
	IntSliceSafe(fallback []int) []int

	// Int8 converts the value to an int8.
	Int8() (int8, error)

	// Int8Safe converts the value to an int8, with a fallback on error.
	Int8Safe(fallback int8) int8

	// Int8Slice converts the value to a slice of int8.
	Int8Slice() ([]int8, error)

	// Int8SliceSafe converts the value to a slice of int8, with a fallback on error.
	Int8SliceSafe(fallback []int8) []int8

	// Int16 converts the value to an int16.
	Int16() (int16, error)

	// Int16Safe converts the value to an int16, with a fallback on error.
	Int16Safe(fallback int16) int16

	// Int16Slice converts the value to a slice of int16.
	Int16Slice() ([]int16, error)

	// Int16SliceSafe converts the value to a slice of int16, with a fallback on error.
	Int16SliceSafe(fallback []int16) []int16

	// Int32 converts the value to an int32.
	Int32() (int32, error)

	// Int32Safe converts the value to an int32, with a fallback on error.
	Int32Safe(fallback int32) int32

	// Int32Slice converts the value to a slice of int32.
	Int32Slice() ([]int32, error)

	// Int32SliceSafe converts the value to a slice of int32, with a fallback on error.
	Int32SliceSafe(fallback []int32) []int32

	// Int64 converts the value to an int64.
	Int64() (int64, error)

	// Int64Safe converts the value to an int64, with a fallback on error.
	Int64Safe(fallback int64) int64

	// Int64Slice converts the value to a slice of int64.
	Int64Slice() ([]int64, error)

	// Int64SliceSafe converts the value to a slice of int64, with a fallback on error.
	Int64SliceSafe(fallback []int64) []int64

	// Uint converts the value to a uint.
	Uint() (uint, error)

	// UintSafe converts the value to a uint, with a fallback on error.
	UintSafe(fallback uint) uint

	// UintSlice converts the value to a slice of uint.
	UintSlice() ([]uint, error)

	// UintSliceSafe converts the value to a slice of uint, with a fallback on error.
	UintSliceSafe(fallback []uint) []uint

	// Uint8 converts the value to a uint8.
	Uint8() (uint8, error)

	// Uint8Safe converts the value to a uint8, with a fallback on error.
	Uint8Safe(fallback uint8) uint8

	// Uint8Slice converts the value to a slice of uint8.
	Uint8Slice() ([]uint8, error)

	// Uint8SliceSafe converts the value to a slice of uint8, with a fallback on error.
	Uint8SliceSafe(fallback []uint8) []uint8

	// Uint16 converts the value to a uint16.
	Uint16() (uint16, error)

	// Uint16Safe converts the value to a uint16, with a fallback on error.
	Uint16Safe(fallback uint16) uint16

	// Uint16Slice converts the value to a slice of uint16.
	Uint16Slice() ([]uint16, error)

	// Uint16SliceSafe converts the value to a slice of uint16, with a fallback on error.
	Uint16SliceSafe(fallback []uint16) []uint16

	// Uint32 converts the value to a uint32.
	Uint32() (uint32, error)

	// Uint32Safe converts the value to a uint32, with a fallback on error.
	Uint32Safe(fallback uint32) uint32

	// Uint32Slice converts the value to a slice of uint32.
	Uint32Slice() ([]uint32, error)

	// Uint32SliceSafe converts the value to a slice of uint32, with a fallback on error.
	Uint32SliceSafe(fallback []uint32) []uint32

	// Uint64 converts the value to a uint64.
	Uint64() (uint64, error)

	// Uint64Safe converts the value to a uint64, with a fallback on error.
	Uint64Safe(fallback uint64) uint64

	// Uint64Slice converts the value to a slice of uint64.
	Uint64Slice() ([]uint64, error)

	// Uint64SliceSafe converts the value to a slice of uint64, with a fallback on error.
	Uint64SliceSafe(fallback []uint64) []uint64

	// Float32 converts the value to a float32.
	Float32() (float32, error)

	// Float32Safe converts the value to a float32, with a fallback on error.
	Float32Safe(fallback float32) float32

	// Float32Slice converts the value to a slice of float32.
	Float32Slice() ([]float32, error)

	// Float32SliceSafe converts the value to a slice of float32, with a fallback on error.
	Float32SliceSafe(fallback []float32) []float32

	// Float64 converts the value to a float64.
	Float64() (float64, error)

	// Float64Safe converts the value to a float64, with a fallback on error.
	Float64Safe(fallback float64) float64

	// Float64Slice converts the value to a slice of float64.
	Float64Slice() ([]float64, error)

	// Float64SliceSafe converts the value to a slice of float64, with a fallback on error.
	Float64SliceSafe(fallback []float64) []float64

	// String converts the value to a string.
	String() (string, error)

	// StringSafe converts the value to a string, with a fallback on error.
	StringSafe(fallback string) string

	// StringSlice converts the value to a slice of string.
	StringSlice() ([]string, error)

	// StringSliceSafe converts the value to a slice of string, with a fallback on error.
	StringSliceSafe(fallback []string) []string
}

Caster provides methods for type casting and conversion.

func NewCaster

func NewCaster(v interface{}) Caster

NewCaster creates a new Caster instance.

type Float32Provider

type Float32Provider interface {
	Float32() (float32, error)
}

Float32Provider defines an interface for providing a float32 value with an error.

type Float32SliceProvider

type Float32SliceProvider interface {
	Float32Slice() ([]float32, error)
}

Float32SliceProvider defines an interface for providing a slice of float32 slice with an error.

type Float64Provider

type Float64Provider interface {
	Float64() (float64, error)
}

Float64Provider defines an interface for providing a float64 value with an error.

type Float64SliceProvider

type Float64SliceProvider interface {
	Float64Slice() ([]float64, error)
}

Float64SliceProvider defines an interface for providing a slice of float64 slice with an error.

type Int16Provider

type Int16Provider interface {
	Int16() (int16, error)
}

Int16Provider defines an interface for providing a int16 value with an error.

type Int16SliceProvider

type Int16SliceProvider interface {
	Int16Slice() ([]int16, error)
}

Int16SliceProvider defines an interface for providing a slice of int16 with an error.

type Int32Provider

type Int32Provider interface {
	Int32() (int32, error)
}

Int32Provider defines an interface for providing a int32 value with an error.

type Int32SliceProvider

type Int32SliceProvider interface {
	Int32Slice() ([]int32, error)
}

Int32SliceProvider defines an interface for providing a slice of int32 with an error.

type Int64Provider

type Int64Provider interface {
	Int64() (int64, error)
}

Int64Provider defines an interface for providing a int64 value with an error..

type Int64SliceProvider

type Int64SliceProvider interface {
	Int64Slice() ([]int64, error)
}

Int64SliceProvider defines an interface for providing a slice of int64 with an error.

type Int8Provider

type Int8Provider interface {
	Int8() (int8, error)
}

Int8Provider defines an interface for providing a int8 value with an error.

type Int8SliceProvider

type Int8SliceProvider interface {
	Int8Slice() ([]int8, error)
}

Int8SliceProvider defines an interface for providing a slice of int8 with an error.

type IntProvider

type IntProvider interface {
	Int() (int, error)
}

IntProvider defines an interface for providing a int value with an error.

type IntSliceProvider

type IntSliceProvider interface {
	IntSlice() ([]int, error)
}

IntSliceProvider defines an interface for providing a slice of int with an error.

type SliceProvider

type SliceProvider interface {
	Slice() ([]interface{}, error)
}

SliceProvider defines an interface for providing a slice of interface{} with an error.

type StringProvider

type StringProvider interface {
	String() (string, error)
}

StringProvider defines an interface for providing a string value with an error.

type StringSliceProvider

type StringSliceProvider interface {
	StringSlice() ([]string, error)
}

StringSliceProvider defines an interface for providing a slice of string with an error.

type Uint16Provider

type Uint16Provider interface {
	Uint16() (uint16, error)
}

Uint16Provider defines an interface for providing a uint16 value with an error.

type Uint16SliceProvider

type Uint16SliceProvider interface {
	Uint16Slice() ([]uint16, error)
}

Uint16SliceProvider defines an interface for providing a slice of uint16 with an error.

type Uint32Provider

type Uint32Provider interface {
	Uint32() (uint32, error)
}

Uint32Provider defines an interface for providing a uint32 value with an error.

type Uint32SliceProvider

type Uint32SliceProvider interface {
	Uint32Slice() ([]uint32, error)
}

Uint32SliceProvider defines an interface for providing a slice of uint32 with an error.

type Uint64Provider

type Uint64Provider interface {
	Uint64() (uint64, error)
}

Uint64Provider defines an interface for providing a uint64 value with an error.

type Uint64SliceProvider

type Uint64SliceProvider interface {
	Uint64Slice() ([]uint64, error)
}

Uint64SliceProvider defines an interface for providing a slice of uint64 with an error.

type Uint8Provider

type Uint8Provider interface {
	Uint8() (uint8, error)
}

Uint8Provider defines an interface for providing a uint8 value with an error.

type Uint8SliceProvider

type Uint8SliceProvider interface {
	Uint8Slice() ([]uint8, error)
}

Uint8SliceProvider defines an interface for providing a slice of uint8 with an error.

type UintProvider

type UintProvider interface {
	Uint() (uint, error)
}

UintProvider defines an interface for providing a uint value with an error.

type UintSliceProvider

type UintSliceProvider interface {
	UintSlice() ([]uint, error)
}

UintSliceProvider defines an interface for providing a slice of uint with an error.

Jump to

Keyboard shortcuts

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