utils

package
v0.0.0-...-64e30f5 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2024 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CommaSeperatedList

func CommaSeperatedList(s string) []string

func Convert

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

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.NestedError)

func Copy

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

func Copy2

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

func Deserialize

func Deserialize(src SerializedObject, dst any) E.NestedError

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, the SerializedObject will be deserialized into the struct fields. 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 DeserializeJSON

func DeserializeJSON(j map[string]string, target any) E.NestedError

func ExtractPort

func ExtractPort(fullURL string) (int, error)

func FormatDuration

func FormatDuration(d time.Duration) string

func FormatTime

func FormatTime(t time.Time) string

func GetSchema

func GetSchema(path string) *jsonschema.Schema

func ListFiles

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

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

func LoadJSON

func LoadJSON[T any](path string, pointer *T) E.NestedError

func ParseBool

func ParseBool(s string) bool

func PortString

func PortString(port uint16) string

func SaveJSON

func SaveJSON[T any](path string, pointer *T, perm os.FileMode) E.NestedError

func Title

func Title(s string) string

func ToLowerNoSnake

func ToLowerNoSnake(s string) string

func ValidateYaml

func ValidateYaml(schema *jsonschema.Schema, data []byte) E.NestedError

Types

type BidirectionalPipe

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

TODO: move to "utils/io".

func (BidirectionalPipe) Start

func (p BidirectionalPipe) Start() 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 Converter

type Converter interface {
	ConvertFrom(value any) (any, E.NestedError)
}

type FileReader

type FileReader struct {
	Path string
}

TODO: move to "utils/io".

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 SerializedObject

type SerializedObject = map[string]any

func Serialize

func Serialize(data any) (SerializedObject, E.NestedError)

Serialize converts the given data into a map[string]any representation.

It uses reflection to inspect the data type and handle different kinds of data. For a struct, it extracts the fields using the json tag if present, or the field name if not. For an embedded struct, it recursively converts its fields into the result map. For any other type, it returns an error.

Parameters: - data: The data to be converted into a map.

Returns: - result: The resulting map[string]any representation of the data. - error: An error if the data type is unsupported or if there is an error during conversion.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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