https://github.com/0x5a17ed/plainfields
A lightweight, human-friendly configuration format for Go with simple syntax for fields, lists, and maps.
https://github.com/0x5a17ed/plainfields
configuration-language data-serialization dsl dsl-syntax go golang golang-library golang-package lexer parser
Last synced: 10 months ago
JSON representation
A lightweight, human-friendly configuration format for Go with simple syntax for fields, lists, and maps.
- Host: GitHub
- URL: https://github.com/0x5a17ed/plainfields
- Owner: 0x5a17ed
- License: 0bsd
- Created: 2025-04-28T12:59:26.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-04-28T19:14:26.000Z (10 months ago)
- Last Synced: 2025-04-28T19:41:50.220Z (10 months ago)
- Topics: configuration-language, data-serialization, dsl, dsl-syntax, go, golang, golang-library, golang-package, lexer, parser
- Language: Go
- Homepage:
- Size: 14.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PlainFields ๐ฑ
[](https://pkg.go.dev/github.com/0x5a17ed/plainfields)
[](https://opensource.org/licenses/0BSD)
PlainFields is a lightweight, human-friendly configuration language for Go applications. It provides a simple syntax for expressing structured data with fields, lists, and key-value pairs.
```
name=PlainFields, version=1.0.0, ^enabled, !deprecated, features=simple;human-readable;flexible,
settings=theme:dark;indent:4;display:compact
```
## โจ Features
- **๐ค Clean, minimal syntax**
No curly braces, no complex nesting. Friendly to read and write by humans
- **๐ Compact**
Minimal syntax overhead, no deep nesting to navigate
- **๐ Boolean toggles**
Use `^feature` to enable, `!feature` to disable
- **๐ Lists and maps**
Simple syntax for collections: `tags=red;green;blue` `font=family:Arial;size:12`
- **โก Event-based parser**
Efficient parsing and serialization with a streaming parser API
- **๐จ Fluent builder API**
Programmatically create plainfields strings
- **๐งฉ Zero dependencies**
Just pure Go standard library, no `reflect` used
## ๐ฆ Installation
```bash
go get -u github.com/0x5a17ed/plainfields
```
## ๐ Quick Start
### ๐ Parsing PlainFields
```go
package main
import (
"fmt"
"github.com/0x5a17ed/plainfields"
)
func main() {
input := "^enabled, name=john, settings=theme:dark;fontSize:14"
tokens := plainfields.Lex(input)
for event := range plainfields.Parse(tokens) {
switch e := event.(type) {
case plainfields.FieldStartEvent:
fmt.Printf(" Field: %s\n", e.Name)
case plainfields.ValueEvent:
fmt.Printf(" Value: %s\n", e.Value)
case plainfields.MapStartEvent:
fmt.Printf(" Map:\n")
case plainfields.MapKeyEvent:
fmt.Printf(" Key: %s\n", e.Value)
}
}
}
```
### โ๏ธ Creating PlainFields
```go
builder := plainfields.NewBuilder()
result := builder.
Enable("feature").
Field("name", "john").
List("tags", "dev", "prod").
Pairs("settings", "theme", "dark", "fontSize", 14).
String()
fmt.Println(result)
// Output: ^feature,name=john,tags=dev;prod,settings=theme:dark;fontSize:14
```
## ๐ Syntax
PlainFields uses a simple, flat structure:
- **Fields** are comma-separated: `name=john, age=30`
- **Boolean toggles** use prefixes: `^enabled`, `!disabled`
- **Lists** use semicolons: `tags=red;green;blue`
- **Maps** (key-value pairs) use colon and semicolon: `settings=theme:dark;fontSize:14`
- **Blank spaces** are allowed around syntax elements
### ๐งช Data Types
- **Strings**: `name=john` or `message="Hello, world!"` (quotes for special chars)
- **Numbers**: `age=30`, `pi=3.14`, `hex=0xFF`, `binary=0b1010`
- **Booleans**: `active=true` or `valid=false`
- **Lists**: `colors=red;green;blue`
- **Maps**: `settings=theme:dark;fontSize:14`
- **Null**: `value=nil`
## ๐ Detailed Usage
### ๐ ๏ธ Builder with Options
```go
options := plainfields.BuilderOptions{
SpaceAfterFieldSeparator: true,
SpaceAfterListSeparator: true,
SpaceAfterPairsSeparator: true,
SpaceAroundFieldAssignment: true,
}
builder := plainfields.NewBuilderWithOptions(options)
result := builder.
Enable("feature").
Field("name", "john").
List("tags", "dev", "prod").
Pairs("settings", "theme", "dark", "fontSize", 14).
String()
fmt.Println(result)
// Output: ^feature, name = john, tags = dev; prod, settings = theme: dark; fontSize: 14
```
## ๐ License
This project is licensed under the 0BSD Licence โ see the [LICENCE](LICENSE) file for details.
---
Made with โค๏ธ for structured data ๐