generic

package
v2.0.0-...-6ba723b Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2025 License: MIT Imports: 12 Imported by: 6

README

generic Library

The generic library provides powerful utilities for working with generic values and type conversions in Go. It allows you to wrap any value and easily convert it between different types, access properties, and perform various operations regardless of the underlying type.

Installation

import "github.com/getevo/evo/v2/lib/generic"

Features

  • Type Conversion: Convert between different types (string, int, float, bool, time, etc.)
  • Property Access: Access and manipulate properties of structs and maps
  • Type Checking: Check types and compare types between values
  • Size Utilities: Convert between different size formats (KB, MB, GB, etc.)
  • Serialization: JSON and YAML marshaling/unmarshaling
  • Database Integration: Implements database/sql interfaces
  • Reflection Utilities: Work with reflection in a simplified way

Usage Examples

Basic Usage
package main

import (
    "fmt"
    "github.com/getevo/evo/v2/lib/generic"
)

func main() {
    // Parse any value
    value := generic.Parse("123")
    
    // Convert to different types
    fmt.Println(value.Int())    // 123
    fmt.Println(value.String()) // "123"
    fmt.Println(value.Bool())   // true
    
    // Parse a size string
    size := generic.Parse("5MB")
    fmt.Println(size.SizeInBytes()) // 5242880
    
    // Format bytes as human-readable size
    bytes := generic.Parse(5242880)
    fmt.Println(bytes.ByteCount()) // "5.0 MB"
}
Working with Structs and Maps
package main

import (
    "fmt"
    "github.com/getevo/evo/v2/lib/generic"
)

type Person struct {
    Name    string
    Age     int
    Address Address
}

type Address struct {
    City    string
    Country string
}

func main() {
    // Create a struct
    person := Person{
        Name: "John",
        Age:  30,
        Address: Address{
            City:    "New York",
            Country: "USA",
        },
    }
    
    // Access properties
    p := generic.Parse(person)
    fmt.Println(p.Prop("Name").String())                // "John"
    fmt.Println(p.Prop("Age").Int())                    // 30
    fmt.Println(p.Prop("Address").Prop("City").String()) // "New York"
    
    // Modify properties
    personPtr := &Person{}
    generic.Parse(personPtr).SetProp("Name", "Alice")
    generic.Parse(personPtr).SetProp("Age", 25)
    fmt.Println(personPtr.Name) // "Alice"
    fmt.Println(personPtr.Age)  // 25
    
    // Work with maps
    m := map[string]interface{}{
        "name": "Bob",
        "age":  40,
    }
    
    mp := generic.Parse(m)
    fmt.Println(mp.Prop("name").String()) // "Bob"
    fmt.Println(mp.Prop("age").Int())     // 40
    
    // Set map property
    generic.Parse(&m).SetProp("city", "London")
    fmt.Println(m["city"]) // "London"
}
Type Checking
package main

import (
    "fmt"
    "github.com/getevo/evo/v2/lib/generic"
    "reflect"
)

func main() {
    // Check types
    str := generic.Parse("hello")
    num := generic.Parse(123)
    
    fmt.Println(str.IsAny(reflect.String))        // true
    fmt.Println(num.IsAny(reflect.Int, reflect.String)) // true
    
    // Check if types are the same
    fmt.Println(str.SameAs("world"))  // true
    fmt.Println(num.SameAs(int64(0))) // false
    
    // Check if value is nil or empty
    nilVal := generic.Parse(nil)
    fmt.Println(nilVal.IsNil())   // true
    fmt.Println(nilVal.IsEmpty()) // true
}
Time and Duration Handling
package main

import (
    "fmt"
    "github.com/getevo/evo/v2/lib/generic"
    "time"
)

func main() {
    // Parse time strings
    timeStr := generic.Parse("2023-01-15T15:30:45Z")
    t, _ := timeStr.Time()
    fmt.Println(t.Year()) // 2023
    
    // Parse duration strings
    durStr := generic.Parse("1h30m")
    d, _ := durStr.Duration()
    fmt.Println(d.Minutes()) // 90
}

How It Works

The generic library works by wrapping any value in a Value struct, which provides methods to convert the value to different types and perform various operations. It uses reflection to inspect and manipulate the underlying value, making it easy to work with values of unknown or dynamic types.

The library handles type conversions automatically, attempting to convert between compatible types and providing sensible defaults when conversion isn't possible. It also provides utilities for working with structs and maps, allowing you to access and modify properties regardless of the underlying structure.

For more detailed information, please refer to the source code and comments within the library.

Documentation

Index

Constants

View Source
const (
	Invalid reflect.Kind = iota
	Bool
	Int
	Int8
	Int16
	Int32
	Int64
	Uint
	Uint8
	Uint16
	Uint32
	Uint64
	Uintptr
	Float32
	Float64
	Complex64
	Complex128
	Array
	Chan
	Func
	Interface
	Map
	Ptr
	Slice
	String
	Struct
	UnsafePointer
)

Variables

This section is empty.

Functions

func ToString

func ToString(v any) string

ToString cast anything to string

@param v
@return string

Types

type Type

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

Type lib structure to keep input and its type

func TypeOf

func TypeOf(input any) *Type

TypeOf return type of input

@param input
@return *Type

func (*Type) Indirect

func (t *Type) Indirect() *Type

Indirect get type of object considering if it is pointer

@receiver t
@return *Type

func (*Type) Is

func (t *Type) Is(input any) bool

Is checks if input and given type are equal

@receiver t
@param input
@return bool

type Value

type Value struct {
	Input any
}

Value wraps over interface

func Parse

func Parse(i any) Value

Parse parse input

@param i
@return Value

func (Value) Bool

func (v Value) Bool() bool

Bool return value as bool

@receiver v
@return bool

func (Value) ByteCount

func (v Value) ByteCount() string

func (Value) Cast

func (v Value) Cast(dst any) error

func (Value) Duration

func (v Value) Duration() (time.Duration, error)

Duration return value as time.Duration

@receiver v
@return time.Duration
@return error

func (Value) FieldNames

func (v Value) FieldNames() []string

func (Value) Float

func (v Value) Float() float64

Float return value as float64

@receiver v
@return float64

func (Value) Float32

func (v Value) Float32() float32

func (Value) Float64

func (v Value) Float64() float64

func (Value) GetName

func (v Value) GetName(name string) string

func (Value) HasProp

func (v Value) HasProp(name string) bool

func (Value) Indirect

func (v Value) Indirect() reflect.Value

func (Value) IndirectType

func (v Value) IndirectType() reflect.Type

func (Value) Int

func (v Value) Int() int

Int return value as integer

@receiver v
@return int

func (Value) Int16

func (v Value) Int16() int16

func (Value) Int32

func (v Value) Int32() int32

func (Value) Int64

func (v Value) Int64() int64

Int64 return value as int64

@receiver v
@return int64

func (Value) Int8

func (v Value) Int8() int8

func (Value) Is

func (v Value) Is(s string) bool

func (Value) IsAny

func (v Value) IsAny(s ...any) bool

func (Value) IsEmpty

func (v Value) IsEmpty() bool

func (Value) IsNil

func (v Value) IsNil() bool

IsNil returns if the value is nil

@receiver v
@return bool

func (*Value) MarshalJSON

func (v *Value) MarshalJSON() ([]byte, error)

func (*Value) MarshalYAML

func (v *Value) MarshalYAML() ([]byte, error)

func (Value) New

func (v Value) New() Value

func (Value) ParseJSON

func (v Value) ParseJSON(in any) error

ParseJSON parse json value into struct

@receiver v
@param in
@return error

func (Value) Prop

func (v Value) Prop(property string) Value

func (Value) PropByTag

func (v Value) PropByTag(tag string) Value

func (Value) Props

func (v Value) Props() []reflect.StructField

func (Value) SameAs

func (v Value) SameAs(s any) bool

func (*Value) Scan

func (v *Value) Scan(value any) error

func (Value) SetProp

func (v Value) SetProp(property string, value any) error

func (Value) SizeInBytes

func (v Value) SizeInBytes() uint64

func (Value) String

func (v Value) String() string

String return value as string

@receiver v
@return string

func (Value) Time

func (v Value) Time() (time.Time, error)

Time return value as time.Time

@receiver v
@return time.Time
@return error

func (Value) Uint

func (v Value) Uint() uint

func (Value) Uint16

func (v Value) Uint16() uint16

func (Value) Uint32

func (v Value) Uint32() uint32

func (Value) Uint64

func (v Value) Uint64() uint64

Uint64 return value as uint64

@receiver v
@return uint64

func (Value) Uint8

func (v Value) Uint8() uint8

func (*Value) UnmarshalJSON

func (v *Value) UnmarshalJSON(data []byte) error

func (*Value) UnmarshalYAML

func (v *Value) UnmarshalYAML(data []byte) error

func (Value) Value

func (v Value) Value() (driver.Value, error)

Value return drive.Value value, implement driver.Valuer interface of gorm

Jump to

Keyboard shortcuts

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