Configuration file syntax and parsing for golang
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
Brett Langdon 3baf4c309c fix comment for NewFileParser 11 years ago
example add support for negative numbers 11 years ago
token huge refactor 11 years ago
LICENSE Add license 11 years ago
Makefile add makefile and linting helper script 11 years ago
README.md fix readme 11 years ago
example_test.go fix up examples 11 years ago
forge.go add support for negative numbers 11 years ago
lint.sh add makefile and linting helper script 11 years ago
parser.go fix comment for NewFileParser 11 years ago
primative.go add comments to primative.go 11 years ago
scanner.go add support for negative numbers 11 years ago
section.go add back in AddComment/AddInclude 11 years ago
value.go add comments to value.go 11 years ago

README.md

forge

GoDoc

Forge is a configuration syntax and parser.

Installation

git get github.com/brettlangdon/forge

Documentation

Documentation can be viewed on godoc: https://godoc.org/github.com/brettlangdon/forge

Example

You can see example usage in the example folder.

# example.cfg

# Global directives
global = "global value";
# Primary section
primary {
  string = "primary string value";
  integer = 500;
  float = 80.80;
  boolean = true;
  negative = FALSE;
  nothing = NULL;
  # Include external files
  include "./include*.cfg";
  # Primary-sub section
  sub {
      key = "primary sub key value";
  }
}

# Secondary section
secondary {
  another = "secondary another value";
  global_reference = global;
  primary_sub_key = primary.sub.key;
  another_again = .another;  # References secondary.another
  _under = 50;
}
package main

import (
	"fmt"
	"json"

	"github.com/brettlangdon/forge"
)

func main() {
	// Parse a `SectionValue` from `example.cfg`
	settings, err := forge.ParseFile("example.cfg")
	if err != nil {
		panic(err)
	}

	// Get a single value
	if settings.Contains("global") {
		// Get `global` casted as `StringValue`
		value := settings.GetString("global")
		fmt.Printf("global = \"%s\"\r\n", value.GetValue())
	}

	// Get a nested value
	value, err := settings.Resolve("primary.included_setting")
	fmt.Printf("primary.included_setting = \"%s\"\r\n", value.GetValue())

	// You can also traverse down the sections
	primary, err := settings.GetSection("primary")
	value, err := primary.GetString("included_setting")
	fmt.Printf("primary.included_setting = \"%s\"\r\n", value.GetValue())

	// Convert settings to a map
	settingsMap, err := settings.ToMap()
	fmt.Printf("global = \"%s\"\r\n", settingsMap["global"])

	// Convert settings to JSON
	jsonBytes, err := settings.ToJSON()
	fmt.Printf("\r\n\r\n%s\r\n", string(jsonBytes))
}

Issues/Requests?

Please feel free to open a github issue for any issues you have or any feature requests.