Giter VIP home page Giter VIP logo

binder's Introduction

Binder for Go

Binder is a configuration reader that parses different types of configurations and adds the possibility to bind them to one or many typed instances.

It can read configuration values from files, environment variables, remote URLs, Kubernetes volumes, and is flexible enough to enable custom configuration parsers. Binder is also able to listen for file changes/volume changes, and re-bind configurations when a backing file or backing volume has been updated.

Example:

package main

import "github.com/ourstudio-se/binder"

type MyFirst struct {
    KeyOne string `config:"external_key_one"`
    KeyTwo int `config:"my_int"`
}

type MySecond struct {
    AnotherKey bool `config:"boolean_value"`
}

func main() {
    bnd := binder.New(
        binder.WithFile("../values.conf"),
        binder.WithEnv("Prefix_"),
        binder.WithWatch("../values.conf"))
    defer bnd.Close()
    
    var fst MyFirst
    var snd MySecond
    bnd.Bind(&fst, &snd);

    fmt.Printf("KeyOne: %s\n", fst.KeyOne)
    fmt.Printf("KeyTwo: %d\n", fst.KeyTwo)
    fmt.Printf("AnotherKey: %t\n", snd.AnotherKey)
}

It's also possible to use binder without binding to any instances, however there's no way to re-bind with a watch when using this pattern:

package main

import (
    "fmt"
    "github.com/ourstudio-se/binder"
)

func main() {
    bnd := binder.New(
        binder.WithFile("../values.conf"),
        binder.WithEnv("Prefix_"),
        binder.WithWatch("../values.conf"))
    defer bnd.Close()
    
    values := bnd.Values() // WithWatch has no effect on the configuration values here

    keyOne, ok := values.Get("external_key_one")
    if !ok {
        panic("no such key")
    }

    fmt.Println("KeyOne: %s\n", keyOne)
}

To listen for any errors, which might come from any parser, or when binding, or from the file watcher, there's a chan available:

package main

import (
    "fmt"
    "log"
    "github.com/ourstudio-se/binder"
)

func main() {
    bnd := binder.New(
        binder.WithFile("../values.conf"),
        binder.WithFile("non-existent-file.conf")
        binder.WithWatch("../values.conf"))
    defer bnd.Close()
    
    go func() {
        for {
            select {
                case err := <- bnd.Errors()
                    if err != nil {
                        log.Errorf("error occurred: %v", err)
                    }
                default:
                    continue
            }
        }
    }()

    var cfg MyConfig
    bnd.Bind(&cfg)
}

If a rebind happens, the implemented bound instance will get a notification if it implements a Notify() method:

package main

import (
    "fmt"
    "log"
    "github.com/ourstudio-se/binder"
)

type MyConfig struct {
    Property string `config:"property"`
}

func (cfg *MyConfig) Notify() {
    fmt.Println("I get called when my `Property` changes!")
}

func main() {
    bnd := binder.New(
        binder.WithFile("../values.conf"),
        binder.WithWatch("../values.conf"))
    defer bnd.Close()

    var cfg MyConfig
    bnd.Bind(&cfg)

}

binder's People

Contributors

ourbjorn avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.