options

package module
v0.0.0-...-33045df Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2014 License: BSD-3-Clause Imports: 2 Imported by: 1

README

options

This package provides a simple, flexible and convenient way of defining config options in Go.

When we want to specify config options for something, especially when there are a lot of optional options, the naive way of using struct or function with a long list of arguments doesn't work well in a lot of cases. Also, Go is a statically typed language, and doesn't provide a way to specify default arguments in function.

This package uses the power of Go reflections to make that task very simple. This is how you define and use a set of options with the help of this package:

//An option
type WhateverName struct{ Value string }

//An option
type WhateverNum struct{ Value int }

//Another option
type WhateverNum2 struct { Value float32 }

//WhateverSpec specifies the list of options
type WhateverSpec struct {
	Name WhateverName `js:"name"` //You may specify a tag here
	Num  WhateverNum  `js:"num"`
	Num2 WhateverNum2 `js:"num2"`
}

//Example convenience method for creating the master config
func WhateverOptions(opts ...Option) *OptionsProvider {
	return NewOptions(&WhateverSpec{}).Options(opts...)
}

//Example convenience method for retrieving the options
func GetWhateverOptions(o *OptionsProvider) *WhateverSpec {
	return o.Get().(*WhateverSpec)
}

func main() {
	//This is how you use the defined options
	opts := WhateverOptions(
		WhateverName{"n0t9r34t6czn0t9r34t1n49re4tw4y"},
		WhateverNum{99999},
	)

	println(GetWhateverOptions(opts).Name.Value) //Get the Name option value

	//You can export the options to a map
	m := opts.ExportToMapWithTag("js")
	_ = m["name"].(string) //Name exported to key "name", as specified by the tag
	
	opts.IsSet("Num") //returns true
	opts.IsSet("Num2") //returns false
	_, ok := m["num2"] //num2 doesn't appear in the result because it has not been set, returns false in ok
}

###Note:
Rob Pike once described a pattern for solving this problem (link). The client interface is good, it has rollbacks which is nice, but it's really really tedious to have to copy-paste dozens of lines of duplicated code for making the options, repeating the same logic for every options pack we create. It might be good for some, but for me it's unbearable. That's why this simple library is born.

Documentation

Overview

Package options implements a simple, flexible and convenient method for specifying options, with the help of reflections.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option interface{}

Option represents an option. Each option must be a different struct, and should be defined as a struct with a Value field.

For example:

type RouteName struct { Value: string }

type OptionsProvider

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

func NewOptions

func NewOptions(spec Spec) *OptionsProvider

NewOptions creates a new OptionsProvider.

func (*OptionsProvider) ExportToMap

func (o *OptionsProvider) ExportToMap() map[string]interface{}

ExportToMap exports the options to a map

func (*OptionsProvider) ExportToMapWithTag

func (o *OptionsProvider) ExportToMapWithTag(tag string) map[string]interface{}

ExportToMapWithTag exports the options to a map, the key names in the map are determined by a specific struct tag. The tags are set in the options spec. Configs that have not been set don't appear in the map.

func (*OptionsProvider) Get

func (o *OptionsProvider) Get() interface{}

Get returns the options data, which could be casted to the original spec type and the value could be get from there.

func (*OptionsProvider) IsSet

func (o *OptionsProvider) IsSet(field string) bool

Check if the field is set

func (*OptionsProvider) Options

func (o *OptionsProvider) Options(opts ...Option) *OptionsProvider

Options set the options in spec according to the opts passed in.

type Spec

type Spec interface{}

Spec specifies the list of options. A spec should be implemented as a struct which have Option fields, each representing an option. Each Option must be a different struct, multiple fields with the same struct type will cause unwanted behavior (only the last field receives the value).

Jump to

Keyboard shortcuts

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