https://github.com/bborbe/parse
https://github.com/bborbe/parse
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/bborbe/parse
- Owner: bborbe
- License: bsd-2-clause
- Created: 2024-03-06T10:28:53.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2025-12-05T22:37:11.000Z (6 months ago)
- Last Synced: 2025-12-09T12:34:17.321Z (6 months ago)
- Language: Go
- Size: 6.57 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Parse
[](https://github.com/bborbe/parse/actions?query=workflow%3ACI)
[](https://goreportcard.com/report/github.com/bborbe/parse)
[](https://pkg.go.dev/github.com/bborbe/parse)
[](https://coveralls.io/github/bborbe/parse?branch=master)
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
```bash
go get github.com/bborbe/parse
```
## Usage
### Basic String Parsing
```go
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
```go
// 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
```go
// 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
```go
// Parse float64
f, err := parse.ParseFloat64(context.Background(), "3.14")
if err != nil {
// Handle error
}
fmt.Println(f) // 3.14
```
### Array Parsing
```go
// 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
```go
// 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
```go
// 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:
```go
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](https://pkg.go.dev/github.com/bborbe/parse)