• Stars
    star
    139
  • Rank 262,954 (Top 6 %)
  • Language
    Go
  • License
    MIT License
  • Created about 10 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Config parser for go, modeled after Nginx format, Nice lenient syntax with Comments

Yet another Config Parser for go

This is a config parser most similar to Nginx, supports Json format, but with line-breaks, comments, etc. Also, like Nginx is more lenient.

Uses same syntax as https://github.com/vstakhov/libucl

Code Coverage GoDoc Build Status Go ReportCard

Use SublimeText Nginx Plugin for formatting.

Credit to BurntSushi/Toml and Apcera/Gnatsd from which this was derived.

Other Options

There are a variety of options for config with comments in Go, this project started before some of them which are now probably better options:

Example

# nice, a config with comments!

# support the name = value format
title = "conf Example"
# support json semicolon
title2 : "conf example2"
# support omitting = or : because key starts a line
title3 "conf example"
# note, we do not have to have quotes
title4 = Without Quotes

# for Sections we can use brackets
hand {
  name = "Tyrion"
  organization = "Lannisters"
  bio = "Imp"                 // comments on fields
  dob = 1979-05-27T07:32:00Z  # dates, and more comments on fields
}

// Note, double-slash comment
// section name/value that is quoted and json valid, including commas
address : {
  "street"  : "1 Sky Cell",
  "city"    : "Eyre",
  "region"  : "Vale of Arryn",
  "country" : "Westeros"
}

# sections can omit the colon, equal before bracket 
seenwith {
  # nested section
  # can be spaces or tabs for nesting
  jaime : {
    season = season1
    episode = "episode1"
  }

  cersei = {
    season = season1
    episode = "episode1"
  }

}


# Line breaks are OK when inside arrays
seasons = [
  "season1",
  "season2",
  "season3",
  "season4",
  "???"
]


# long strings can use parens to allow multi-line
description (
    we possibly
    can have
    multi line text with a block paren
    block ends with end paren on new line
)



And the corresponding Go types are:

type Config struct {
	Title       string
	Hand        HandOfKing
	Location    *Address `confl:"address"`
	Seenwith    map[string]Character
	Seasons     []string
	Description string
}

type HandOfKing struct {
	Name     string
	Org      string `json:"organization"`  // Reads either confl, or json attributes
	Bio      string
	DOB      time.Time
	Deceased bool
}

type Address struct {
	Street  string
	City    string
	Region  string
	ZipCode int
}

type Character struct {
	Episode string
	Season  string
}

Note that a case insensitive match will be tried if an exact match can't be found.

A working example of the above can be found in _examples/example.{go,conf}.

Examples

This package works similarly to how the Go standard library handles XML and JSON. Namely, data is loaded into Go values via reflection.

For the simplest example, consider a file as just a list of keys and values:

// Comments in Config
Age = 25
# another comment
Cats = [ "Cauchy", "Plato" ]
# now, using quotes on key
"Pi" = 3.14
Perfection = [ 6, 28, 496, 8128 ]
DOB = 1987-07-05T05:45:00Z

Which could be defined in Go as:

type Config struct {
  Age int
  Cats []string
  Pi float64
  Perfection []int
  DOB time.Time 
}

And then decoded with:

var conf Config
if err := confl.Unmarshal(byteData, &conf); err != nil {
  // handle error
}

You can also use struct tags if your struct field name doesn't map to a confl key value directly:

some_key_NAME = "wat"
type Config struct {
  ObscureKey string `confl:"some_key_NAME"`
}

Using the encoding.TextUnmarshaler interface

Here's an example that automatically parses duration strings into time.Duration values:

song [
	{
		name = "Thunder Road"
		duration = "4m49s"
	},
	{
		name = "Stairway to Heaven"
		duration = "8m03s"
	}
]

Which can be decoded with:

type song struct {
  Name     string
  Duration duration
}
type songs struct {
  Song []song
}
var favorites songs
if err := confl.Unmarshal(blob, &favorites); err != nil {
  log.Fatal(err)
}

for _, s := range favorites.Song {
  fmt.Printf("%s (%s)\n", s.Name, s.Duration)
}

And you'll also need a duration type that satisfies the encoding.TextUnmarshaler interface:

type duration struct {
	time.Duration
}

func (d *duration) UnmarshalText(text []byte) error {
	var err error
	d.Duration, err = time.ParseDuration(string(text))
	return err
}

More Repositories

1

anomalyzer

Probabilistic anomaly detection for time series data
Go
292
star
2

metafora

Distributed long running work system in Go
Go
151
star
3

cloudstorage

Cloud & local storage unified api (s3, google, azure, sftp, local)
Go
80
star
4

multibayes

Multiclass Naive Bayesian Classification
Go
76
star
5

grid

A library for distributed processing for Go
Go
57
star
6

hll

HyperLogLog++ for Go
Go
43
star
7

base62

base62 (ie, url safe) encoding golang lib
Go
42
star
8

escp

Elasticsearch Copier - Copies ES indexes
Go
40
star
9

impact

Lightweight bootstrap testing for detecting causal impact to timeseries in Go.
Go
17
star
10

lytics

Lytics Command Line Utility
Go
15
star
11

dfa

Deterministic Finite Automata to define computation with labeled states and explicit transitions
Go
13
star
12

cache

In memory concurrent cache data structure for go (golang)
Go
9
star
13

sereno

Sereno is a Go library of recipes for Etcd. Inspired by Netflix's curator for Zookeeper.
Go
9
star
14

toolbucket

A selection of small Go tool kits for anyone to use.
Go
8
star
15

squaredance

Simple task coordination
Go
7
star
16

slackhook

Simple Go client for Slack's Incoming WebHook API
Go
7
star
17

sshtail

Simple utility to multiplex logs from multiple servers over SSH
Go
6
star
18

sample

Weighted sampling in Go
Go
6
star
19

estail

Elasticsearch/Logstash Tailing Tool
Go
5
star
20

qlbridge

A golang expression evaluator & Library to build SQL query engine based functionality.
Go
5
star
21

gowrapmx4j

Golang wrapper for accesssing MX4J HTTP data
Go
5
star
22

go-lytics

Lytics SDK for Go (Golang)
Go
5
star
23

collector-ios

Ios Collector SDK for lytics.io
Objective-C
5
star
24

datemath

Simple library for evaluating ElasticSearch style date expressions.
Go
4
star
25

ordpool

An order-preserving parallel worker pool library for Go
Go
4
star
26

retry

Retry Library for Go
Go
3
star
27

wherefore

Wherefore art thy transferring via network?
Go
3
star
28

inflight

inflight provides primitives for managing sets of inflight messages that are being processed in parallel
Go
3
star
29

LogspoutLoges

A gliderlabs/logspout module for shipping logs straight to Elasticsearch to bypass the necessity for Logstash.
Go
3
star
30

informant

A drop-in solution for visualizing metrics
JavaScript
3
star
31

gentleman

Full-featured, plugin-oriented, composable HTTP client toolkit for Go
Go
3
star
32

pathforajs

Web personalization SDK
JavaScript
2
star
33

lifecycle

Go package that helps with managing service states and shutdown requests
Go
2
star
34

flo

Pre Alpha
Go
2
star
35

gobyairship

Go client for Urban Airship
Go
2
star
36

skewer

Dumb tool for detecting skew between dumb cloud clocks
Go
2
star
37

rgcs

Google Cloud Storage Wrapper for R
R
2
star
38

pathforacss

Boilerplate for generating custom PathforaJS styles.
CSS
2
star
39

analyst

A simple data API abstraction layer
JavaScript
1
star
40

lytics-js

Interact with the Lytics REST API from JavaScript
TypeScript
1
star
41

pathforadocs

HTML
1
star
42

saltfiles

Public Salt-States for lytics, and custom grains, modules, etc
Python
1
star
43

toomanysecrets

Private gist cleaner
Go
1
star
44

pathforajs-examples

1
star
45

quickstart

Quickstart For getting up and running building Visualizations using Lytics.io
JavaScript
1
star
46

etcdlog

Logs etcd events
Go
1
star
47

ghmoveproject

CLI app to move github project from a repo to an org
Go
1
star
48

segml

A dashboard for creating and visualizing Lytics SegmentML models
R
1
star