toggles

package
v4.2.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2019 License: Apache-2.0 Imports: 3 Imported by: 4

Documentation

Overview

Package toggles defines a standard way to define, list, and use feature toggles in the service broker.

It mimics Go's `flags` package, but uses Viper as a backing store to abstract out how a particular flag is set.

Index

Examples

Constants

This section is empty.

Variables

View Source
var Compatibility = NewToggleSet("compatibility.")

Compatibility is the default set of flags for enabling compatibility modes.

View Source
var Feature = NewToggleSet("feature.")

Feature is the default set of flags for enabling or disabling features.

Functions

This section is empty.

Types

type Toggle

type Toggle struct {
	Name        string
	Default     bool
	Description string
	// contains filtered or unexported fields
}

Toggle represents a single feature that the user can enable or disable.

func (Toggle) EnvironmentVariable

func (toggle Toggle) EnvironmentVariable() string

EnvironmentVariable gets the environment variable used to control the toggle.

Example
ts := NewToggleSet("foo.")
toggle := ts.Toggle("bar", true, "bar gets a default of true")

fmt.Println(toggle.EnvironmentVariable())
Output:

GSB_FOO_BAR

func (Toggle) IsActive

func (toggle Toggle) IsActive() bool

IsActive returns true if the toggle is enabled and false if it isn't.

Example
ts := NewToggleSet("foo.")
toggle := ts.Toggle("bar", true, "bar gets a default of true")

fmt.Println(toggle.IsActive())
viper.Set("foo.bar", "false")
defer viper.Reset()

fmt.Println(toggle.IsActive())
Output:

true
false

type ToggleSet

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

A ToggleSet represents a set of defined toggles. The zero value of a ToggleSet has no property prefix.

func NewToggleSet

func NewToggleSet(propertyPrefix string) *ToggleSet

NewFlagSet returns a new, empty toggle set with the specified property prefix. The property prefix will be prepended to any toggles exactly as-is. You MUST specify a trailing period if you want your properties to be namespaced.

func (*ToggleSet) Toggle

func (set *ToggleSet) Toggle(name string, value bool, description string) Toggle

Toggle creates a new toggle with the given name, default value, label and description. It also adds the toggle to an internal registry and initializes the default value in viper.

func (*ToggleSet) Toggles

func (set *ToggleSet) Toggles() []Toggle

Toggles returns a list of all registered toggles sorted lexicographically by their property name.

Example
ts := NewToggleSet("foo.")

//  add some toggles
ts.Toggle("z", true, "a toggle")
ts.Toggle("a", false, "another toggle")
ts.Toggle("b", true, "a third toggle")

for _, tgl := range ts.Toggles() {
	fmt.Printf("name: %s, var: %s, description: %q, default: %v\n", tgl.Name, tgl.EnvironmentVariable(), tgl.Description, tgl.Default)
}
Output:

name: a, var: GSB_FOO_A, description: "another toggle", default: false
name: b, var: GSB_FOO_B, description: "a third toggle", default: true
name: z, var: GSB_FOO_Z, description: "a toggle", default: true

Jump to

Keyboard shortcuts

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