optional

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

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

Go to latest
Published: Sep 2, 2015 License: BSD-3-Clause Imports: 4 Imported by: 0

README

go-optional

A Cascading option resolver designed for numerious sources of configuration information

GoDoc

Documentation

Overview

Package optional provides a cascading option resolver for cli applications

Usage: Create a resolver by calling New(&config). Config should be a struct Add sources by calling AddSource.

Example
type Config struct {
	ConfigPath string `default:"/etc/test/config.json" arg:"c" arg:"conf-path"`
	Name       string `arg:"n" arg:"name" default:"New User"`
	Goodbye    bool   `default:"false" arg:"b" arg:"!h" arg:"goodbye"`
}

config := Config{}
opts := New(&config)
opts.AddSourceFast(0, opts.StructTags("default")) // Fast because we dont read until another AddSource
opts.AddSource(2, opts.Arguments(
	[]string{"optional_test", "-n", "Jamie", "--conf-path", "c:/test.conf"},
)) //normaly opts.AddSource(opts.Arguments(os.Args))

c, err := ioutil.ReadFile(config.ConfigPath) //Open JSON on disk
if err != nil {
	fmt.Println(err.Error())
} else {
	m := make(map[string]interface{})
	json.Unmarshal(c, &m)
	opts.AddSource(1, Wrapped{m})
}
//Config should be set as:
// ConfigPath: "D:/test.conf" (from Arguments)
// Name:       "Jamie"        (from Arguments)
// Goodbye:    false          (from Defaults)
h := "Hello "
if config.Goodbye {
	h = "Goodbye "
}
fmt.Println(h + config.Name)
fmt.Println(config.ConfigPath)
//Output will be: Hello Jamie (Unless you changed the conf)
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func StrToType

func StrToType(str string, t reflect.Type) interface{}

StrToType converts a string to a type and puts it in a interface{}

Types

type ArgumentsOptionSource

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

ArgumentsOptionSource implements OptionSource, pulling data from the passed argument array

func (ArgumentsOptionSource) Get

func (s ArgumentsOptionSource) Get(key string) (interface{}, bool)

Get implements OptionSource

type OptionSource

type OptionSource interface {
	//Get will be given a path, period deliminated and should return the value related to it
	Get(key string) (value interface{}, ok bool)
}

A OptionSource can be passed to AddSource to add a source for values

type Opts

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

Opts holds cascaded sources

func New

func New(i interface{}) *Opts

New creates a new Opts structure for the interface passed

func (*Opts) AddSource

func (o *Opts) AddSource(i int, s OptionSource)

AddSource adds a source to the opt, 'i' being the priority level (higher number = higher priority)

func (*Opts) AddSourceFast

func (o *Opts) AddSourceFast(i int, s OptionSource)

AddSourceFast adds a source to the opt without updating the parent struct

Example
type Config struct {
	A string `default:"default"`
	B string `default:"default" someOtherDefault:"OtherDefault"`
	C string `default:"default"`
}

config := Config{
	A: "Allocate", // Will be overwritten
}
opts := New(&config) // Does not change struct
fmt.Println(config)
//{Allocate  }

opts.AddSource(1, opts.StructTags("default"))
opts.AddSourceFast(2, opts.StructTags("someOtherDefault"))
fmt.Println(config)
//{default default default}
opts.Recalc()
fmt.Println(config)
//{default OtherDefault default}
Output:

{Allocate  }
{default default default}
{default OtherDefault default}

func (*Opts) Arguments

func (o *Opts) Arguments(s []string) ArgumentsOptionSource

Arguments creates a (Arguments)OptionSource pulling data from the passed argument array.

Parser rules
shorthand (1 char) has a single dash
longhand (1+char ) has two dashes
shorthand bools can be strung together -abcd will set a,b,c, and d to true
shorthand bools with names begining with a '!' will be inverted ie: Fast bool `args:"Fast" args:"f" args:"!s"`
'--' stops parsing

func (*Opts) Get

func (o *Opts) Get(key string) (interface{}, bool)

Get will return the current value for a path

func (*Opts) Recalc

func (o *Opts) Recalc()

Recalc sets all of the values in the struct to the correct values This is called automatically after AddSource

func (*Opts) StructTags

func (o *Opts) StructTags(key string) StructTagsOptionSource

StructTags creates a (StructTags)OptionSource pulling from struct's tags

type StructTagsOptionSource

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

StructTagsOptionSource implements OptionSource, pulling data from Struct tags

func (StructTagsOptionSource) Get

func (s StructTagsOptionSource) Get(key string) (interface{}, bool)

Get implements OptionSource

type Wrapped

type Wrapped struct {
	V interface{}
}

Wrapped wraps a struct or map implementing OptionSource

func (Wrapped) Get

func (w Wrapped) Get(key string) (value interface{}, ok bool)

Get implements OptionSource

Jump to

Keyboard shortcuts

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