helper

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddFormField

func AddFormField(writer *multipart.Writer, parent string, child string, v any) error

AddFormField fills a form field with a key defined by parent[child] or child if parent is empty and writes them into multipart.Writer. Accepted values: string, []byte, func() (MultipartFile, error), and map[string]any

func CreateReadCloser

func CreateReadCloser(b []byte) io.ReadCloser

CreateReadCloser constructs an io.ReadCloser from a byte slice.

func Maps

func Maps(p map[string]any, s ...map[string]any) map[string]any

Maps recursively merges multiple maps of type map[string]any.

func Must

func Must[T any](v T, err error) T

Must returns the provided value if err is nil; otherwise, it panics with the error. Useful for writing tests to reduce error handling boilerplate.

func MustStringSlice

func MustStringSlice(v any) []string

MustStringSlice takes a value of type any and converts them into a string slice. Panics on error.

func Sign

func Sign(key string, message []byte) (string, error)

Sign encodes a message into HMAC-SHA-512 hashed with a base64-encoded key.

func StringSlice

func StringSlice(v any) (slice []string, err error)

StringSlice takes a value of type any and converts them into a string slice.

func StructToMap

func StructToMap(s any) (map[string]any, error)

StructToMap converts a struct into map[string]any.

func ToJSON

func ToJSON(v any) string

ToJSON returns the JSON string of v or panics.

func ToJSONIndent

func ToJSONIndent(v any) string

ToJSONIndent returns the JSON string of v with indents or panics.

func ToURLValues

func ToURLValues(val any) (values url.Values, err error)

ToURLValues converts v into url.Values.

func Traverse

func Traverse[V any](m any, keys ...any) (*V, error)

Traverse returns the value in a nested structure based on a sequence of keys. Iteration-based map indexes are supported. However, they are non deterministic as Go maps are unordered.

func UUID

func UUID() string

UUID creates a new random UUID and returns it as a string or panics

Types

type DirectReflection

type DirectReflection struct {
	Value reflect.Value
	Type  reflect.Type
}

DirectReflection contains the result of GetDirectReflection.

func GetDirectReflection

func GetDirectReflection(s any) DirectReflection

GetDirectReflection returns the direct reflect.Value of an object without the pointer.

type Form

type Form struct {
	*bytes.Buffer
	*multipart.Writer
}

Form is a combined representation of bytes.Buffer and multipart.Writer.

func NewForm

func NewForm(m map[string]any) (*Form, error)

NewForm constructs a Form from the given map[string]any. See AddFormField for accepted values.

type GetDirectReflectionFunction

type GetDirectReflectionFunction func(s any) DirectReflection

GetDirectReflectionFunction returns the direct reflect.Value of an object without the pointer.

type MarshalFunction

type MarshalFunction func(opts MarshalOptions) (result MarshalResult, err error)

MarshalFunction encodes the provided object according to the media type.

type MarshalOptions

type MarshalOptions struct {
	MediaType string
	Object    any
}

MarshalOptions contains the parameters for MarshalFunction.

type MarshalResult

type MarshalResult struct {
	Data        []byte
	ContentType string
}

MarshalResult contains the result of MarshalFunction.

func Marshal

func Marshal(opts MarshalOptions) (result MarshalResult, err error)

Marshal implements MarshalFunction by encoding the provided object according to the media type:

- "multipart/form-data": expects object to be a map[string]any and sets it as a multipart form.

- "application/x-www-form-urlencoded": expects object to be a map[string]any and encodes it as form values.

- "application/json": marshals object as JSON.

Returns an error if the media type is not among the list or the data doesn't match the expected format.

type MultipartFile

type MultipartFile interface {
	Name() string
	io.ReadCloser
}

MultipartFile is an interface to attach a file into the multipart form. os.File implements this interface.

type StructToMapFunction

type StructToMapFunction func(s any) (map[string]any, error)

StructToMapFunction converts a struct into map[string]any.

Jump to

Keyboard shortcuts

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