GithubHelp home page GithubHelp logo

go-structparse's Introduction

go-structparse

Documentation GitHub tag Build Status

A (deep) recursive field parser for golang. Basically just a reflection wrapper that finds elements in a struct or map and runs the provided function over them when found. This is useful as a sort of "find and replace" for fields or types in a structure.

It was built to allow overriding of configuration variables in a config file with environmental variables, so that you can have one clear source of truth for configuration that explicitly defines any external requirements.

It is intended that this will be extended with further field / type parsing as required. If you have a need, or an idea, open an issue!

Usage

Sick of passing 4 million command line arguments into your app? Done with configuring unknown / unspecified environmental variables to get your app to work? Well, this might be the project for you!

What if you could have one, human readable, source of truth, that also supported the loading of secrets from the environment in an EXPLICIT manner?

Do we have the solution for you! Try our new EnvironmentMapper which will parse strings looking for a delimiter, and when the delimiter is found replace the value with the environmental variable defined by the prefix and field value. For example, if you configure the EnvironmentMapper with a delimiter of $ and a prefix of APP_, the value $NAME in your configuration file will be replaced with the environmental variable APP_NAME. Now you have a static, explicit configuration file, that supports injection of secrets and other per-instance data. How neat is that!

If you fancy implementing custom parsers, check out the godocs for more information.

Example

Config file

---

# strings beginning with $ will be parsed as environmental variables with the provided prefix
name: $NAME
host: 
  name: $HOSTNAME
  port: $PORT

Code

import (
    "io/ioutil"
    "gopkg.in/yaml.v2"

    "github.com/ryankurte/go-structparse"
)

type Config struct {
    Name string
    Host struct {
        Name string
        Port string
    }
}

func Main() {
    c := Config{}

    // Load (yaml) configuration file
    data, err := ioutil.ReadFile(filename)
    if err != nil {
        return err
    }

    // Unmarshal from yaml to config struct
    err = yaml.Unmarshal(data, c)
    if err != nil {
        return err
    }

    // Parse struct fields
    delimiter := "$"
    prefix := "APP_"
    structparse.Strings(structparse.NewEnvironmentMapper(delimiter, prefix), &c)

    // Do something with the parsed structure
    log.Printf("Config: %+v", c)

}

If you have any questions, comments, or suggestions, feel free to open an issue or a pull request.

go-structparse's People

Contributors

ryankurte avatar jerome-laforge avatar ko80 avatar

Stargazers

Brian Sanders avatar Prashanth Pai avatar Travis Matthews avatar Lionell Yip avatar  avatar Krzysztof Wilczyński avatar Anton Skorochod avatar

Watchers

James Cloos avatar  avatar  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.