EZConf

I whipped up a tiny package called EZConf over the last couple of days, it does really dumb configuration parsing for Go.

I wanted something like this to create a higher level abstraction over parsing a JSON configuration file and then transforming some elements of it (say, reading ENV variables). In part because Go needs better higher level solutions like this - although writing low level code is fun, it’s important to keep on shipping.

So EZConf can read JSON files into structs:

type myStruct struct {
  Foo string
  Baz string
}

// ...

config := ezconf.Read(filename, &myStruct)

amazing!

Where it actually does something useful however is being able to use Filters and TransformFunc’s to transform the values of the struct. A TransformFunc is simply a function that takes an interface{} value and returns it’s replacement interface{} value.

func fooBar(val interface{}) interface{} {
  return val.(string) + "Bar"
}

A Filter specifies which elements of the struct should be operated on, either by Type:

typeFilter := ezconf.Filter{Type: ezconf.String}

or by name:

keyFilter := ezconf.Filter{KeyFields: []{"Foo"}}

They can be registered with a Parser, which can then transform an input config struct, returning the transformed copy.

p := NewParser()
p.RegisterTransformer(fooBar, typeFilter)
config = p.Transform(config)
fmt.Println(config.Foo)
// "FooBar"

This can be used to easily do things like replacing values matching "ENV['SOMEVAR']" with the resulting ENV variable automatically, or parsing particular config values out.

EZConf is a work in progress, extra Types need implementing, and more importantly a series of common transforms added.