Fin/README.md
2025-03-04 19:16:36 -06:00

153 lines
4.0 KiB
Markdown

# Go Config Parser
A lightweight, intuitive configuration parser for Go applications with a clean, readable syntax.`
## Features
- Simple, human-readable configuration format
- Strong typing with automatic type conversion
- Nested structures with dot notation access
- Support for arrays and maps
- Inline and block comments
- Fast parsing with no dependencies
- Full test coverage
## Configuration Format
This parser uses a clean, minimal syntax that's easy to read and write:
```
host "localhost"
port 8080
debug true
allowed_ips {
"192.168.1.1"
"192.168.1.2"
"10.0.0.1"
}
database {
host "db.example.com"
port 5432
credentials {
username "admin"
password "secure123"
}
}
-- This is a line comment
--[[ This is a
block comment spanning
multiple lines ]]
```
## Installation
```bash
go get git.sharkk.net/Go/Config
```
## Usage
### Basic Usage
```go
package main
import (
"fmt"
"os"
config "git.sharkk.net/Go/Config"
)
func main() {
// Load configuration from file
file, err := os.Open("config.conf")
if err != nil {
panic(err)
}
defer file.Close()
cfg, err := config.Load(file)
if err != nil {
panic(err)
}
// Access values with type conversion
host, err := cfg.GetString("database.host")
port, err := cfg.GetInt("database.port")
debug, err := cfg.GetBool("debug")
// Use default values for missing keys
timeout := cfg.GetOr("timeout", 30).(int64)
}
```
### Type Conversion
The parser automatically converts values to appropriate types:
```go
// These will return properly typed values
boolValue, err := cfg.GetBool("feature.enabled")
intValue, err := cfg.GetInt("server.port")
floatValue, err := cfg.GetFloat("threshold")
stringValue, err := cfg.GetString("app.name")
// For complex types
arrayValue, err := cfg.GetArray("allowed_ips")
mapValue, err := cfg.GetMap("database")
// Generic getter (returns interface{})
value, err := cfg.Get("some.key")
```
### Accessing Arrays and Maps
Access array elements and nested map values using dot notation:
```go
// Access the first element in the array
firstIP, err := cfg.GetString("allowed_ips.0")
// Access deeply nested values
username, err := cfg.GetString("database.credentials.username")
```
## Performance
This parser provides competitive performance compared to popular formats like JSON, YAML, and TOML:
| Benchmark | Operations | Time (ns/op) | Memory (B/op) | Allocations (allocs/op) |
|-----------|----------:|-------------:|--------------:|------------------------:|
| **Small Config Files** |
| Config | 718,893 | 1,611 | 5,256 | 25 |
| JSON | 1,000,000 | 1,170 | 1,384 | 23 |
| YAML | 213,438 | 5,668 | 8,888 | 82 |
| TOML | 273,586 | 4,505 | 4,520 | 67 |
| **Medium Config Files** |
| Config | 138,517 | 8,777 | 11,247 | 114 |
| JSON | 241,069 | 4,996 | 5,344 | 89 |
| YAML | 47,695 | 24,183 | 21,577 | 347 |
| TOML | 66,411 | 17,709 | 16,349 | 208 |
| **Large Config Files** |
| Config | 33,177 | 35,591 | 31,791 | 477 |
| JSON | 66,384 | 18,066 | 18,138 | 297 |
| YAML | 12,482 | 95,248 | 65,574 | 1,208 |
| TOML | 17,594 | 67,928 | 66,038 | 669 |
*Benchmarked on AMD Ryzen 9 7950X 16-Core Processor*
## Why Choose This Parser?
- **Readability**: Simple syntax that's easy for humans to read and write
- **Flexibility**: Supports various data types and nested structures
- **Performance**: Fast parsing with minimal overhead
- **Type Safety**: Strong typing with automatic conversion
- **Simplicity**: No external dependencies required
## License
MIT