utilx

package
v0.7.10 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2025 License: MIT Imports: 6 Imported by: 18

Documentation

Overview

Package utilx provides additional utility functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func And

func And[T comparable](a, b T) T

And works similar to "&&" in other languages.

func Empty

func Empty[T comparable](value T) bool

Empty returns true of the given value is equal to the default empty value.

func Equal added in v0.7.6

func Equal[T comparable](a, b T) bool

Equal returns true of the given values are equal.

func IfElse

func IfElse[T any](condition bool, v1, v2 T) T

IfElse works similar to "?:", "if-else" in other languages.

func IsNil added in v0.7.8

func IsNil(value any) bool

IsNil returns true of the given value is nil.

func NotEmpty

func NotEmpty[T comparable](value T) bool

NotEmpty returns true of the given value is not equal to the default empty value.

func NotEqual added in v0.7.10

func NotEqual[T comparable](a, b T) bool

NotEqual returns true of the given values are not equal.

func NotNil added in v0.7.10

func NotNil(value any) bool

NotNil returns true of the given value is not nil.

func Or

func Or[T comparable](a, b T) T

Or works similar to "||" in other languages.

func SetDefaults

func SetDefaults(variable interface{})

SetDefaults sets the default values for the fields of the given struct.

Usage

type ExampleBasic struct {
    Foo bool   `default:"true"`
    Bar string `default:"33"`
    Qux int8
    Dur time.Duration `default:"2m3s"`
}

 foo := &ExampleBasic{}
 SetDefaults(foo)

Types

type FieldData

type FieldData struct {
	Field    reflect.StructField
	Value    reflect.Value
	TagValue string
	Parent   *FieldData
}

FieldData contains the information of a field of a struct.

type Filler

type Filler struct {
	FuncByName map[string]FillerFunc
	FuncByType map[TypeHash]FillerFunc
	FuncByKind map[reflect.Kind]FillerFunc
	Tag        string
}

Filler contains all the functions to fill any struct field with any type allowing to define function by Kind, Type of field name.

func (*Filler) Fill

func (f *Filler) Fill(variable interface{})

Fill apply all the functions contained on Filler, setting all the possible values.

func (*Filler) GetFieldsFromValue

func (f *Filler) GetFieldsFromValue(valueObject reflect.Value, parent *FieldData) []*FieldData

GetFieldsFromValue returns all the fields of a struct.

func (*Filler) SetDefaultValue

func (f *Filler) SetDefaultValue(field *FieldData)

SetDefaultValue sets the default value for a field.

func (*Filler) SetDefaultValues

func (f *Filler) SetDefaultValues(fields []*FieldData)

SetDefaultValues sets the default values for all the fields.

type FillerFunc

type FillerFunc func(field *FieldData)

FillerFunc is a function that fills a field with a specific.

type TypeHash

type TypeHash string

TypeHash is a string representing a reflect.Type following the next pattern: <package.name>.<type.name>.

func GetTypeHash

func GetTypeHash(t reflect.Type) TypeHash

GetTypeHash returns the TypeHash for a given reflect.Type.

Jump to

Keyboard shortcuts

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