nutrition

package
v0.0.0-...-f2f6987 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2014 License: Apache-2.0, MIT Imports: 9 Imported by: 0

README

Nutrition Build Status Build Status Coverage Status

Package nutrition provides decoding of different sources based on user defined struct. Source is the stream of lines in 'key=value' form. Environment, file and raw io.Reader sources are supported. Package can decode types boolean, numeric, and string types as well as time.Duration and time.Time. The later could be customized with formats.

Documentation

The Nutrition API reference is available on GoDoc.

Installation

Install Nutrition using the go get command:

go get -u github.com/dmotylev/nutrition

Example

Feed struct from environment only:

// Try:
//
// APP_TIMEOUT=1h2m3s APP_DAY=2013-12-13 APP_NUMWORKERS=5 APP_WORKERNAME=Hulk go run p.go
//
package main

import (
	"fmt"
	"time"

	"github.com/dmotylev/nutrition"
)

func main() {
	var conf struct {
		Timeout    time.Duration
		Day        time.Time `time,format:"2006-01-02"`
		WorkerName string
		NumWorkers int
	}

	err := nutrition.Env("APP_").Feed(&conf)

	fmt.Printf("err=%v\n", err)
	fmt.Printf("timeout=%s\n", conf.Timeout)
	fmt.Printf("day=%s\n", conf.Day.Format(time.UnixDate))
	fmt.Printf("worker=%s\n", conf.WorkerName)
	fmt.Printf("workers=%d\n", conf.NumWorkers)
}

Feed struct from stdin only:

// Try:
//
// echo -e "timeout=1h2m3s\nday=2013-12-13\nnumWorkers=5\nworkerName=Hulk" | go run p.go
//
package main

import (
	"fmt"
	"time"
	"os"

	"github.com/dmotylev/nutrition"
)

func main() {
	var conf struct {
		Timeout    time.Duration
		Day        time.Time `time,format:"2006-01-02"`
		WorkerName string
		NumWorkers int
	}

	err := nutrition.Reader(os.Stdin).Feed(&conf)

	fmt.Printf("err=%v\n", err)
	fmt.Printf("timeout=%s\n", conf.Timeout)
	fmt.Printf("day=%s\n", conf.Day.Format(time.UnixDate))
	fmt.Printf("worker=%s\n", conf.WorkerName)
	fmt.Printf("workers=%d\n", conf.NumWorkers)
}

Feed struct from file only:

// Try:
//
// echo -e "timeout=1h2m3s\nday=2013-12-13\nnumWorkers=5\nworkerName=Hulk" > env && go run p.go
//
package main

import (
	"fmt"
	"time"

	"github.com/dmotylev/nutrition"
)

func main() {
	var conf struct {
		Timeout    time.Duration
		Day        time.Time `time,format:"2006-01-02"`
		WorkerName string
		NumWorkers int
	}

	err := nutrition.Reader("env").Feed(&conf)

	fmt.Printf("err=%v\n", err)
	fmt.Printf("timeout=%s\n", conf.Timeout)
	fmt.Printf("day=%s\n", conf.Day.Format(time.UnixDate))
	fmt.Printf("worker=%s\n", conf.WorkerName)
	fmt.Printf("workers=%d\n", conf.NumWorkers)
}

All together:

// Try:
//
// echo -e "timeout=1h2m3s\nday=2013-12-13\nnumWorkers=5\nworkerName=Hulk" > env && echo -e "numWorkers=10000\nworkerName=Fido" | APP_WORKERNAME=Pinnoccio go run p.go
//
package main

import (
	"fmt"
	"os"
	"time"

	"github.com/dmotylev/nutrition"
)

func main() {
	var conf struct {
		Timeout    time.Duration
		Day        time.Time `time,format:"2006-01-02"`
		WorkerName string
		NumWorkers int
	}

	// You can change the order. First source with value will be used
	err := nutrition.Env("APP_").Reader(os.Stdin).File("env").Feed(&conf)

	fmt.Printf("err=%v\n", err)
	fmt.Printf("timeout=%s\n", conf.Timeout)
	fmt.Printf("day=%s\n", conf.Day.Format(time.UnixDate))
	fmt.Printf("worker=%s\n", conf.WorkerName)
	fmt.Printf("workers=%d\n", conf.NumWorkers)
}

License

The package available under LICENSE.

Documentation

Overview

Package nutrition provides decoding of different sources based on user defined struct. Source is the stream of lines in 'key=value' form. Environment, file and raw io.Reader sources are supported. Package can decode types boolean, numeric, and string types as well as time.Duration and time.Time. The later could be customized with formats (default is time.UnixDate).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Nutrition

type Nutrition interface {
	// Feeds given struct with decoded values from provided sources
	Feed(v interface{}) error
	// Add environment as source for values
	Env(prefix string) Nutrition
	// Add Reader as source for values
	Reader(reader io.Reader) Nutrition
	// Add File as source for values
	File(filename string) Nutrition
}

func Env

func Env(prefix string) Nutrition

Build Nutrition instance that decodes environment variables. Given prefix used for looking values in environment

func File

func File(filename string) Nutrition

Build Nutrition instance that decodes given File. Do nothing if file does not exists.

func Reader

func Reader(reader io.Reader) Nutrition

Build Nutrition instance that decodes given Reader

type NutritionError

type NutritionError struct {
	// Raw value as it was read from source
	Value string
	// Name of the user struct
	Struct string
	// Name of the field in the struct
	Field string
	// Type of the field
	FieldType string
	// Cause why the field was not feed with value
	Cause error
}

func (*NutritionError) Error

func (e *NutritionError) Error() string

Jump to

Keyboard shortcuts

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