Documentation
¶
Overview ¶
Package convert provides type-safe conversion utilities and format interconversion (JSON / TOML / YAML).
Type Conversion ¶
The ToXxx family of functions convert any value to the target type, returning a zero value and/or error on failure:
ToInt("42") → 42, nil
ToInt("abc") → 0, error
ToString(42) → "42", nil
ToFloat64("3.14") → 3.14, nil
ToBool("true") → true, nil
ToSlice[int]([]any{1,2,3}) → []int{1,2,3}, nil
Format Interconversion ¶
Convert between JSON, TOML, and YAML representations:
json, err := convert.FromYAMLToJSON(yamlBytes) toml, err := convert.FromJSONToTOML(jsonBytes) yaml, err := convert.FromTOMLToYAML(tomlBytes)
All interconversion functions decode the source into a generic map[string]any (or []any for arrays) and re-encode to the target format.
Index ¶
- func Clone[T any](v T) (T, error)
- func Convert(from, to Format, data []byte) ([]byte, error)
- func CopyJSON(v any) (any, error)
- func DeepCopy[T any](v T) T
- func FromJSONToTOML(data []byte) ([]byte, error)
- func FromJSONToYAML(data []byte) ([]byte, error)
- func FromTOMLToJSON(data []byte) ([]byte, error)
- func FromTOMLToYAML(data []byte) ([]byte, error)
- func FromYAMLToJSON(data []byte) ([]byte, error)
- func FromYAMLToTOML(data []byte) ([]byte, error)
- func JSONToMap(data []byte) (map[string]any, error)
- func JSONToSlice(data []byte) ([]any, error)
- func JSONToStruct(data []byte, v any) error
- func Marshal(format Format, v any) ([]byte, error)
- func MustMarshal(format Format, v any) []byte
- func MustUnmarshal(format Format, data []byte, v any)
- func StructToJSON(v any) ([]byte, error)
- func StructToTOML(v any) ([]byte, error)
- func StructToYAML(v any) ([]byte, error)
- func TOMLToStruct(data []byte, v any) error
- func ToBool(v any) (bool, error)
- func ToDuration(v any) (time.Duration, error)
- func ToFloat64(v any) (float64, error)
- func ToInt(v any) (int, error)
- func ToInt64(v any) (int64, error)
- func ToMapStringAny(v any) (map[string]any, error)
- func ToMapStringInt(v any) (map[string]int, error)
- func ToMapStringString(v any) (map[string]string, error)
- func ToSlice[T any](v any) ([]T, error)
- func ToSliceInt(v any) ([]int, error)
- func ToSliceString(v any) ([]string, error)
- func ToString(v any) (string, error)
- func ToUint(v any) (uint, error)
- func Unmarshal(format Format, data []byte, v any) error
- func YAMLToStruct(data []byte, v any) error
- type Format
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Clone ¶
Clone is a type-safe deep copy that returns the same type as input. Uses JSON round-trip, so unexported fields are not preserved.
func Convert ¶
Convert converts data from one format to another. The source is decoded into a generic any and re-encoded to the target format.
func CopyJSON ¶
CopyJSON creates a deep copy of v via JSON round-trip into a new value of the same type. Returns an error if marshaling or unmarshaling fails.
func DeepCopy ¶
func DeepCopy[T any](v T) T
DeepCopy creates a deep copy of v via JSON round-trip. Returns nil if the value cannot be marshaled. This is suitable for structs, maps, slices, and primitives. Unexported fields are NOT copied (JSON does not see them).
func FromJSONToTOML ¶
FromJSONToTOML converts JSON bytes to TOML.
func FromJSONToYAML ¶
FromJSONToYAML converts JSON bytes to YAML.
func FromTOMLToJSON ¶
FromTOMLToJSON converts TOML bytes to JSON.
func FromTOMLToYAML ¶
FromTOMLToYAML converts TOML bytes to YAML.
func FromYAMLToJSON ¶
FromYAMLToJSON converts YAML bytes to JSON.
func FromYAMLToTOML ¶
FromYAMLToTOML converts YAML bytes to TOML.
func JSONToSlice ¶
JSONToSlice converts JSON bytes to []any.
func JSONToStruct ¶
JSONToStruct decodes JSON bytes into a struct.
func MustMarshal ¶
MustMarshal is like Marshal but panics on error. Use only for static data in tests or initialization.
func MustUnmarshal ¶
MustUnmarshal is like Unmarshal but panics on error.
func StructToJSON ¶
StructToJSON encodes a struct to JSON bytes.
func StructToTOML ¶
StructToTOML encodes a struct to TOML bytes.
func StructToYAML ¶
StructToYAML encodes a struct to YAML bytes.
func TOMLToStruct ¶
TOMLToStruct decodes TOML bytes into a struct.
func ToBool ¶
ToBool converts any value to bool. Recognizes: bool, string ("true"/"false"/"1"/"0"/"yes"/"no"/"on"/"off"), int (non-zero = true).
func ToDuration ¶
ToDuration converts any value to time.Duration. Accepts time.Duration, string (e.g. "5s", "100ms"), and numeric types (interpreted as nanoseconds).
func ToInt ¶
ToInt converts any value to int. Supports int variants, float variants, string (parsed), bool (1/0), and json.Number.
func ToMapStringAny ¶
ToMapStringAny converts a map[any]any (common from YAML decoding) to map[string]any. Keys that are not strings are converted via fmt.Sprint.
func ToMapStringInt ¶
ToMapStringInt converts any map to map[string]int. Values are converted via ToInt. Keys are converted via ToString.
func ToMapStringString ¶
ToMapStringString converts any map to map[string]string. Values are converted via ToString. Keys are converted via ToString.
func ToSlice ¶
ToSlice converts any value to a typed slice. If v is already a slice of T, it is returned directly. If v is a []any, each element is converted to T. If v is a single value, a slice containing that one value is returned.
func ToSliceString ¶
ToSliceString converts any value to []string.
func ToString ¶
ToString converts any value to string. Numbers are formatted, bool becomes "true"/"false", nil becomes "".
func YAMLToStruct ¶
YAMLToStruct decodes YAML bytes into a struct.