doozerconfig

package module
v0.0.0-...-b4182a3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 11, 2013 License: MIT Imports: 6 Imported by: 0

README

doozerconfig

What Is It?

doozerconfig is a Go package for managing json-encoded configuration in doozer. Configuration in doozer is directly mapped to a Go struct. Configuration changes made in doozer are automatically reflected in the struct. For details, see the example below.

API documentation

Installation

$ go get github.com/ActiveState/doozerconfig

Example

var Config struct {
    MaxItems  int                `doozer:"config/max_items"`
    DbUri     string             `doozer:"config/db_uri"`
    EnvVars   map[string]string  `doozer:"config/envvars"`
    Verbose   bool   // not in doozer
}

func init() {
    doozer, _ := doozer.Dial("localhost:8046")
    rev, _ := doozer.Rev()
    
    // Map Config fields to "/myapp/" + the struct tag above.
    // eg: MaxItems will be mapped to /myapp/config/max_items
    //     EnvVars will be mapped to /myapp/config/envvars/*
    cfg := doozerconfig.New(doozer, rev, &Config, "/myapp/")

    // Load config values from doozer and assign to Config fields
    _ = cfg.Load()  

    // Watch for live changes to doozer config, and automatically
    // update the struct fields. The callback function can be used to
    // handle errors, and to get notified of changes.
    go cfg.Monitor("/myapp/config/*", func(change *Change, err error) {
        fmt.Printf("config changed in doozer; %+v\n", change)            
    })
}

Notes

  • If a file is deleted from doozer, the config struct is not updated (unless it a map entry). Perhaps we should update the value to a default value (as specified in a default struct tag).

  • Writing configuration back to doozer is not supported yet.

Changes

  • Oct 22, 2012:

    • Support for loading map types

    • API: Load now takes doozer revision as a mandatory argument.

    • API: Monitor doesn't take a revision argument anymore. Instead it uses the one passed to Load.

    • API: Monitor's callback function accepts a new Change struct.

  • Sep 25, 2012:

    • Initial public release.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Change

type Change struct {
	FieldName string // Field name that was changed
	Type      ChangeType
	Key       string      // If field is map, key value that changed
	Value     interface{} // New value; if map, value is slotted at changed key
}

Change represents a change to the config struct

type ChangeType

type ChangeType uint

A Change type represents the kind of change that happened

const (
	SET ChangeType = iota
	DELETE
)

type DoozerConfig

type DoozerConfig struct {
	// contains filtered or unexported fields
}

func New

func New(conn *doozer.Conn, loadRev int64, configStruct interface{}, prefix string) *DoozerConfig

New returns a new DoozerConfig given doozer connection, struct object and doozer path prefix.

func (*DoozerConfig) Load

func (c *DoozerConfig) Load() error

Load populates the struct with config values from doozer.

func (*DoozerConfig) Monitor

func (c *DoozerConfig) Monitor(glob string, callback func(*Change, error))

Monitor listens for config changes in doozer and updates the struct. A callback function can be passed to get notified of the changes made and errors.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL