utils

package
v0.9.9 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2025 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidType           = E.New("invalid type")
	ErrNilValue              = E.New("nil")
	ErrUnsettable            = E.New("unsettable")
	ErrUnsupportedConversion = E.New("unsupported conversion")
	ErrUnknownField          = E.New("unknown field")
)
View Source
var ErrValidationError = E.New("validation error")

Functions

func Convert

func Convert(src reflect.Value, dst reflect.Value) E.Error

Convert attempts to convert the src to dst.

If src is a map, it is deserialized into dst. If src is a slice, each of its elements are converted and stored in dst. For any other type, it is converted using the reflect.Value.Convert function (if possible).

If dst is not settable, an error is returned. If src cannot be converted to dst, an error is returned. If any error occurs during conversion (e.g. deserialization), it is returned.

Returns:

  • error: the error occurred during conversion, or nil if no error occurred.

func ConvertString

func ConvertString(src string, dst reflect.Value) (convertible bool, convErr E.Error)

func Copy

func Copy(dst *ContextWriter, src *ContextReader) (err error)

Copyright 2009 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style This is a copy of io.Copy with context handling Author: yusing <yusing@6uo.me>.

func Copy2

func Copy2(ctx context.Context, dst io.Writer, src io.Reader) error

func Deserialize

func Deserialize(src SerializedObject, dst any) (err E.Error)

Deserialize takes a SerializedObject and a target value, and assigns the values in the SerializedObject to the target value. Deserialize ignores case differences between the field names in the SerializedObject and the target.

The target value must be a struct or a map[string]any. If the target value is a struct , and implements the MapUnmarshaller interface, the UnmarshalMap method will be called.

If the target value is a struct, but does not implements the MapUnmarshaller interface, the SerializedObject will be deserialized into the struct fields and validate if needed.

If the target value is a map[string]any the SerializedObject will be deserialized into the map.

The function returns an error if the target value is not a struct or a map[string]any, or if there is an error during deserialization.

func DeserializeYAML

func DeserializeYAML[T any](data []byte, target *T) E.Error

func DeserializeYAMLMap

func DeserializeYAMLMap[V any](data []byte) (_ functional.Map[string, V], err E.Error)

func Intersect

func Intersect[T comparable, Slice ~[]T](slice1 Slice, slice2 Slice) Slice

Intersect returns a new slice containing the elements that are present in both input slices. This provides a more efficient solution than using two nested loops.

func ListFiles

func ListFiles(dir string, maxDepth int, hideHidden ...bool) ([]string, error)

Recursively lists all files in a directory until `maxDepth` is reached Returns a slice of file paths relative to `dir`.

func LoadJSONIfExist

func LoadJSONIfExist[T any](path string, dst *T) error

func MustRegisterValidation

func MustRegisterValidation(tag string, fn validator.Func)

func NearestField

func NearestField(input string, s any) string

func New

func New(t reflect.Type) reflect.Value

func RegisterDefaultValueFactory

func RegisterDefaultValueFactory[T any](factory func() *T)

func SaveJSON

func SaveJSON[T any](path string, src *T, perm os.FileMode) error

func Slice

func Slice[T any](slice []T, n int) []T

Slice returns a slice of the first n elements in slice like javascript's slice.

func ValidateWithCustomValidator

func ValidateWithCustomValidator(v reflect.Value) E.Error

func ValidateWithFieldTags

func ValidateWithFieldTags(s any) E.Error

func Validator

func Validator() *validator.Validate

Types

type BidirectionalPipe

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

TODO: move to "utils/io".

func (BidirectionalPipe) Start

func (p BidirectionalPipe) Start() E.Error

type ContextReader

type ContextReader struct {
	io.Reader
	// contains filtered or unexported fields
}

TODO: move to "utils/io".

func (*ContextReader) Read

func (r *ContextReader) Read(p []byte) (int, error)

type ContextWriter

type ContextWriter struct {
	io.Writer
	// contains filtered or unexported fields
}

TODO: move to "utils/io".

func (*ContextWriter) Write

func (w *ContextWriter) Write(p []byte) (int, error)

type CustomValidator

type CustomValidator interface {
	Validate() E.Error
}

type FileReader

type FileReader struct {
	Path string
}

TODO: move to "utils/io".

type MapUnmarshaller

type MapUnmarshaller interface {
	UnmarshalMap(m map[string]any) E.Error
}

type NoCopy

type NoCopy struct{}

empty struct that implements Locker interface for hinting that no copy should be performed.

func (*NoCopy) Lock

func (*NoCopy) Lock()

func (*NoCopy) Unlock

func (*NoCopy) Unlock()

type Pipe

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

TODO: move to "utils/io".

func NewPipe

func NewPipe(ctx context.Context, r io.ReadCloser, w io.WriteCloser) *Pipe

func (*Pipe) Start

func (p *Pipe) Start() (err error)

type RefCount

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

func NewRefCounter

func NewRefCounter() *RefCount

func (*RefCount) Add

func (rc *RefCount) Add()

func (*RefCount) Sub

func (rc *RefCount) Sub()

func (*RefCount) Zero

func (rc *RefCount) Zero() <-chan struct{}

type SerializedObject

type SerializedObject = map[string]any

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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