Skip to content
/ tommy Public

Tommy is a toml parsing library for golang, focused in simplicity and speed.

License

Notifications You must be signed in to change notification settings

jean0t/tommy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tommy

A simple and lightweight TOML parser for Go, designed for easy configuration file parsing.

Features

  • Simple API - Parse TOML with a single function call
  • Numbers - Supports integers and floats (all parsed as float64)
  • Strings - Full string support with proper escaping
  • Lists - Homogeneous lists of numbers or strings
  • Sections - Organize configuration into logical sections
  • Comments - Lines starting with # are ignored
  • 🚧 Dictionaries - Coming soon
  • 🚧 Dates - Coming soon

Installation

go get github.com/jean0t/tommy

Usage

Basic Example

package main

import (
    "fmt"
    "log"
    
    "github.com/jean0t/tommy"
)

func main() {
    input := `
# Global configuration
name = "john"
age = 22

[profession]
experience = 4
wage = 8900.56

[relationships]
wife = "amanda"
children = ["james", "peter"]
`

    // its type is tommy.ParsedToml
    // as input you send the file contents as string
    result, err := tommy.Parse(input)
    if err != nil {
        log.Fatal(err)
    }

    // Access global variables (empty string key)
    fmt.Println("Name:", result[""]["name"])           // john
    fmt.Println("Age:", result[""]["age"])             // 22

    // Access section variables
    fmt.Println("Experience:", result["profession"]["experience"])   // 4
    fmt.Println("Wage:", result["profession"]["wage"])               // 8900.56
    
    // Access lists
    children := result["relationships"]["children"].([]string)
    fmt.Println("Children:", children)                 // [james peter]
}

Data Structure

Tommy returns a map[string]map[string]any where:

  • Outer key: Section name (empty string "" for global scope)
  • Inner key: Variable name
  • Value: The parsed value (float64, string, []float64, or []string)
result := map[string]map[string]any{
    "": {                    // Global scope
        "name": "john",
        "age": 22.0,
    },
    "profession": {          // Section scope
        "experience": 4.0,
        "wage": 8900.56,
    },
}

Supported Syntax

Numbers

All numbers are parsed as float64:

integer = 42
float = 3.14
negative = -10

Strings

Strings must be quoted:

name = "john"
path = "/usr/local/bin"

Lists

Lists must contain elements of the same type:

# Number lists
ports = [8080, 8081, 8082]
prices = [19.99, 29.99, 39.99]

# String lists
users = ["admin", "guest"]

Sections

Group related configuration:

# Global variables
app_name = "MyApp"

[database]
host = "localhost"
port = 5432

[server]
timeout = 30

Comments

Lines starting with # are ignored:

# This is a comment
name = "john"  # Inline comments are not supported yet

Type Assertions

Since values are stored as any, you'll need type assertions:

// For numbers
age := result[""]["age"].(float64)

// For strings
name := result[""]["name"].(string)

// For number lists
ports := result["server"]["ports"].([]float64)

// For string lists
users := result["database"]["users"].([]string)

// Safe type assertion
if age, ok := result[""]["age"].(float64); ok {
    fmt.Println("Age:", age)
}

Limitations

  • No inline tables - person = { name = "john", age = 22 } not supported yet
  • No dates - Date/time types coming soon
  • Homogeneous lists - Lists cannot mix types
  • No multiline strings - Triple-quoted strings not supported yet

Roadmap

  • Dictionary/table support
  • Date and time parsing
  • Inline tables
  • Multiline strings
  • Boolean type

Contributing

Contributions are welcome! Feel free to:

  • Report bugs
  • Suggest features
  • Submit pull requests

License

MIT License - see LICENSE file for details

Example Configuration File

# Application Configuration
app_name = "MyApp"
version = 1.5
debug_mode = 0

[database]
host = "localhost"
port = 5432
max_connections = 100
users = ["admin", "readonly"]

[server]
host = "0.0.0.0"
ports = [8080, 8081, 8082]
timeout = 30.5
workers = 4

[logging]
level = "info"
outputs = ["stdout", "file"]
max_size = 10.5

Why Tommy?

Tommy is designed to be simple and straightforward. If you need a full-featured TOML parser with all the bells and whistles, check out other Go TOML libraries. Tommy is perfect for:

  • Simple configuration files
  • Learning about parsers
  • Projects where you control the TOML format
  • Lightweight parsing needs

Made with ❤️ by jean0t

About

Tommy is a toml parsing library for golang, focused in simplicity and speed.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages