parse

package module
v1.10.1 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: BSD-2-Clause Imports: 13 Imported by: 2

README

Parse

CI Go Report Card Go Reference Coverage Status

A robust Go utility library for parsing and converting various data types from interface{} values with comprehensive fallback mechanisms and error handling.

Features

  • Type-safe parsing with context support
  • Fallback values via ParseXDefault functions
  • Subtype handling for custom types derived from basic types
  • Interface support for fmt.Stringer implementations
  • Comprehensive error handling with structured errors
  • Zero dependencies for runtime (only github.com/bborbe/errors)

Installation

go get github.com/bborbe/parse

Usage

Basic String Parsing
import (
    "context"
    "github.com/bborbe/parse"
)

// Parse with error handling
value, err := parse.ParseString(context.Background(), 42)
if err != nil {
    // Handle error
}
fmt.Println(value) // "42"

// Parse with default fallback
value := parse.ParseStringDefault(context.Background(), invalidValue, "default")
fmt.Println(value) // "default"
Integer Parsing
// Parse int
num, err := parse.ParseInt(context.Background(), "123")
if err != nil {
    // Handle error
}
fmt.Println(num) // 123

// Parse int64 with default
num64 := parse.ParseInt64Default(context.Background(), "invalid", 0)
fmt.Println(num64) // 0
Boolean Parsing
// Parse bool
flag, err := parse.ParseBool(context.Background(), "true")
if err != nil {
    // Handle error
}
fmt.Println(flag) // true

// Parse with default
flag := parse.ParseBoolDefault(context.Background(), "invalid", false)
fmt.Println(flag) // false
Float Parsing
// Parse float64
f, err := parse.ParseFloat64(context.Background(), "3.14")
if err != nil {
    // Handle error
}
fmt.Println(f) // 3.14
Array Parsing
// Parse string array
strs, err := parse.ParseStrings(context.Background(), []interface{}{"a", "b", "c"})
if err != nil {
    // Handle error
}
fmt.Println(strs) // ["a", "b", "c"]

// Parse int array with default
ints := parse.ParseIntArrayDefault(context.Background(), "invalid", []int{1, 2, 3})
fmt.Println(ints) // [1, 2, 3]
Time Parsing
// Parse time with custom format
t, err := parse.ParseTime(context.Background(), "2023-12-25", "2006-01-02")
if err != nil {
    // Handle error
}
fmt.Println(t) // 2023-12-25 00:00:00 +0000 UTC
ASCII Conversion
// Convert to ASCII (removes diacritics)
ascii, err := parse.ParseASCII(context.Background(), "café")
if err != nil {
    // Handle error
}
fmt.Println(ascii) // "cafe"
Custom Types

The library supports custom types derived from basic types:

type MyString string

value, err := parse.ParseString(context.Background(), MyString("hello"))
// Works seamlessly with custom types

API Reference

Core Functions
  • ParseString(ctx, value) (string, error) - Parse to string
  • ParseInt(ctx, value) (int, error) - Parse to int
  • ParseInt64(ctx, value) (int64, error) - Parse to int64
  • ParseBool(ctx, value) (bool, error) - Parse to bool
  • ParseFloat64(ctx, value) (float64, error) - Parse to float64
  • ParseTime(ctx, value, format) (time.Time, error) - Parse to time.Time
  • ParseASCII(ctx, value) (string, error) - Convert to ASCII
Array Functions
  • ParseStrings(ctx, value) ([]string, error) - Parse to string array
  • ParseIntArray(ctx, value) ([]int, error) - Parse to int array
  • ParseInt64Array(ctx, value) ([]int64, error) - Parse to int64 array
Default Functions

All parse functions have corresponding ParseXDefault variants that return a fallback value on error:

  • ParseStringDefault(ctx, value, defaultValue) string
  • ParseIntDefault(ctx, value, defaultValue) int
  • ParseBoolDefault(ctx, value, defaultValue) bool
  • etc.

Full API documentation: pkg.go.dev/github.com/bborbe/parse

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidType = stderrors.New("invalid type")

ErrInvalidType is returned when a value cannot be converted to the requested type.

Functions

func ParseASCII added in v1.8.0

func ParseASCII(ctx context.Context, value interface{}) (string, error)

ParseASCII returns a the given string converted to ascii

func ParseAscii deprecated

func ParseAscii(ctx context.Context, value interface{}) (string, error)

ParseAscii returns a the given string converted to ascii

Deprecated: Use ParseASCII instead.

func ParseBool

func ParseBool(ctx context.Context, value interface{}) (bool, error)

ParseBool converts an interface{} value to a bool. Supported types: bool, string (case-insensitive "true"/"false"), fmt.Stringer. String values are converted to lowercase and compared against "true" and "false". Returns an error if the value cannot be converted to bool.

func ParseBoolDefault

func ParseBoolDefault(ctx context.Context, value interface{}, defaultValue bool) bool

ParseBoolDefault converts an interface{} value to a bool, returning defaultValue on error. This is a convenience wrapper around ParseBool that never returns an error.

func ParseFloat64

func ParseFloat64(ctx context.Context, value interface{}) (float64, error)

ParseFloat64 converts an interface{} value to a float64. Supported types: int, int32, int64, float32, float64, string, fmt.Stringer. String values are parsed using strconv.ParseFloat. Returns an error if the value cannot be converted to float64.

func ParseFloat64Default

func ParseFloat64Default(ctx context.Context, value interface{}, defaultValue float64) float64

ParseFloat64Default converts an interface{} value to a float64, returning defaultValue on error. This is a convenience wrapper around ParseFloat64 that never returns an error.

func ParseInt

func ParseInt(ctx context.Context, value interface{}) (int, error)

ParseInt converts an interface{} value to an int. Supported types: int, int32, int64, float32, float64, string, fmt.Stringer. Float values are rounded to the nearest integer. String values are parsed using strconv.Atoi. Returns an error if the value cannot be converted to int.

func ParseInt64

func ParseInt64(ctx context.Context, value interface{}) (int64, error)

ParseInt64 converts an interface{} value to an int64. Supported types: int64, int32, int, float32, float64, string. Float values are rounded to the nearest integer. String values are parsed using strconv.ParseInt. Returns an error if the value cannot be converted to int64.

func ParseInt64Array

func ParseInt64Array(ctx context.Context, value interface{}) ([]int64, error)

ParseInt64Array converts an interface{} value to an int64 slice. Supported types: []int64, []interface{}, []int, []int32, []float32, []float64, []string. Each element is converted using ParseInt64. Returns an error if the value cannot be converted to []int64.

func ParseInt64ArrayDefault

func ParseInt64ArrayDefault(ctx context.Context, value interface{}, defaultValue []int64) []int64

ParseInt64ArrayDefault converts an interface{} value to an int64 slice, returning defaultValue on error. This is a convenience wrapper around ParseInt64Array that never returns an error.

func ParseInt64ArrayFromInterfaces

func ParseInt64ArrayFromInterfaces(ctx context.Context, values []interface{}) ([]int64, error)

ParseInt64ArrayFromInterfaces converts a slice of interface{} values to an int64 slice. Each element is converted using ParseInt64. Returns an error if any element cannot be converted to int64.

func ParseInt64Default

func ParseInt64Default(ctx context.Context, value interface{}, defaultValue int64) int64

ParseInt64Default converts an interface{} value to an int64, returning defaultValue on error. This is a convenience wrapper around ParseInt64 that never returns an error.

func ParseIntArray

func ParseIntArray(ctx context.Context, value interface{}) ([]int, error)

ParseIntArray converts an interface{} value to an int slice. Supported types: []int, []interface{}, []int32, []int64, []float32, []float64, []string. Each element is converted using ParseInt. Returns an error if the value cannot be converted to []int.

func ParseIntArrayDefault

func ParseIntArrayDefault(ctx context.Context, value interface{}, defaultValue []int) []int

ParseIntArrayDefault converts an interface{} value to an int slice, returning defaultValue on error. This is a convenience wrapper around ParseIntArray that never returns an error.

func ParseIntArrayFromInterfaces

func ParseIntArrayFromInterfaces(ctx context.Context, values []interface{}) ([]int, error)

ParseIntArrayFromInterfaces converts a slice of interface{} values to an int slice. Each element is converted using ParseInt. Returns an error if any element cannot be converted to int.

func ParseIntDefault

func ParseIntDefault(ctx context.Context, value interface{}, defaultValue int) int

ParseIntDefault converts an interface{} value to an int, returning defaultValue on error. This is a convenience wrapper around ParseInt that never returns an error.

func ParseString

func ParseString(ctx context.Context, value interface{}) (string, error)

ParseString converts an interface{} value to a string. Supported types: string, bool, int, int32, int64, uint, uint32, uint64, float32, float64, fmt.Stringer. Custom types derived from string are automatically detected and handled. Returns an error if the value cannot be converted to string.

func ParseStringDefault

func ParseStringDefault(ctx context.Context, value interface{}, defaultValue string) string

ParseStringDefault converts an interface{} value to a string, returning defaultValue on error. This is a convenience wrapper around ParseString that never returns an error.

func ParseStrings

func ParseStrings(ctx context.Context, value interface{}) ([]string, error)

ParseStrings converts an interface{} value to a string slice. Supported types: []string, []interface{}, []float64, []bool, []int, []int32, []int64, string, HasStrings interface, HasString interface, slices of string subtypes (e.g., []Direction where type Direction string), and slices of types implementing String() string method. A single string value is returned as a slice with one element. Returns nil for nil input. Returns an error if the value cannot be converted to []string.

func ParseStringsDefault

func ParseStringsDefault(ctx context.Context, value interface{}, defaultValue []string) []string

ParseStringsDefault converts an interface{} value to a string slice, returning defaultValue on error. This is a convenience wrapper around ParseStrings that never returns an error.

func ParseTime

func ParseTime(ctx context.Context, value interface{}, format string) (time.Time, error)

ParseTime converts an interface{} value to a time.Time using the specified format. The value is first converted to a string using ParseString, then parsed using time.Parse. Format should follow Go's time format layout (e.g., "2006-01-02", "2006-01-02T15:04:05Z07:00"). Returns an error if the value cannot be converted to time.Time.

func ParseTimeDefault

func ParseTimeDefault(
	ctx context.Context,
	value interface{},
	format string,
	defaultValue time.Time,
) time.Time

ParseTimeDefault converts an interface{} value to a time.Time using the specified format, returning defaultValue on error. This is a convenience wrapper around ParseTime that never returns an error.

func ToInterfaceList

func ToInterfaceList[T any](values []T) []interface{}

ToInterfaceList converts a typed slice to a slice of interface{}. This is a generic helper function used internally for type conversion.

Types

type HasString added in v1.9.0

type HasString interface {
	String() string
}

HasString interface is implemented by types that can provide a string representation. This is similar to fmt.Stringer but used specifically for ParseStrings conversion.

type HasStrings added in v1.9.0

type HasStrings interface {
	Strings() []string
}

HasStrings interface is implemented by types that can provide a string slice representation.

Jump to

Keyboard shortcuts

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