togglr

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

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

Go to latest
Published: Nov 3, 2015 License: MIT Imports: 7 Imported by: 0

README

togglr Build Status GoDoc

A simple implementation of the Feature Toggles pattern for Golang

Feature list

  • Configure from environment variables
  • Configure from JSON file
  • Randomly enabled feature
  • Time base configuration
  • Percentage based configuration
  • Week day based configuration

Getting Started

At first create a struct containing the features you want to be able to toggle:

type MyFeatures struct{
  SimpleFeature togglr.Feature
  EnabledOnOrBefore togglr.Feature
  EnabledOnOrAfter togglr.Feature
  FiftyFifty togglr.Feature
  OnlyOnWeekend togglr.Feature
  FeatureFromEnv togglr.Feature `env:"FEATURE"`
}

Then create a json file configuring thoses feature by thier names:

{
  "SimpleFeature": true,
  "EnabledOnOrBefore": { "enabledOnOrBefore": "2006-01-02 15:04:05" },
  "EnabledOnOrAfter": { "enabledOnOrAfter": "2016-01-02" },
  "FiftyFifty": { "percent": 50 },
  "OnlyOnWeekend" : { "days": "Saturday | Sunday" }
}

Almost done. Now just fil the feature sruct and use it

package main

import "github.com/mouk/togglr"

func main() {
  features := MyFeatures{}
  togglr.Init("feature_configuration.json")
  togglr.Read(&features)

  if(features.EnabledOnOrBefore.IsEnabled()){
    // Do somthing
  }
}

Export feature snapshot

A static overview of all features can be exported as a dictionary. This should be used only for debugging and reporting puposes, but not to change the control flow.

package main

import "github.com/mouk/togglr"

type MyFeatures struct{
  Feature1 togglr.Feature
  //....
}

func main() {
  togglr.Init("features.json")
  features := MyFeatures{}
  togglr.Read(&features)

  snapshot = togglr.GetFeatureSpanshots(&features)

  for feature, enabled := snapshot m {
    fmt.Println("Feature:", feature, "Enabled:", enabled)
  }
}

Documentation

Index

Constants

View Source
const (
	EnabledOnOrAfterKey  = "enabledOnOrAfter"
	EnabledOnOrBeforeKey = "enabledOnOrBefore"
	WeekdaysKey          = "days"
	PercentKey           = "percent"
)

Variables

This section is empty.

Functions

func GetFeatureSpanshots

func GetFeatureSpanshots(obj interface{}) map[string]bool

func Init

func Init(path string)

func InjectFeatureFactory

func InjectFeatureFactory(factory FeatureFactory)

Add new FeatureFactory

func Read

func Read(obj interface{})

Populate the passed object with feature using the already specified ConfigSources and FeatureFactories

Types

type ConfigSource

type ConfigSource interface {
	GetConfig(name string, tag reflect.StructTag) (interface{}, bool)
}

This tinterface represents a pluggable configuration source. You can plug in your own sources or use one of the built-in.

func NewJsonConfigSource

func NewJsonConfigSource(path string) ConfigSource

type Feature

type Feature interface {
	IsEnabled() bool
}

func DefaulFactory

func DefaulFactory(val interface{}) (Feature, bool)

func NewStaticFeature

func NewStaticFeature(val bool) Feature

Create a new Feature with a static value that cannot be changed afterwards

type FeatureFactory

type FeatureFactory (func(data interface{}) (Feature, bool))

Jump to

Keyboard shortcuts

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