structparse

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: May 20, 2020 License: MIT Imports: 4 Imported by: 5

README

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(parser, obj interface{})

Parse an object using the provided parser The parser must implement one of more ObjectParser interfaces

func Strings

func Strings(parser StringParser, obj interface{})

Strings reflects over a structure and calls Parse when strings are located

Types

type EnvironmentMapper

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

func NewEnvironmentMapper

func NewEnvironmentMapper(delimiter, prefix string) *EnvironmentMapper

NewEnvironmentMapper creates an environment mapping parser This parses a string looking for a delimiter indicating that the value should be loaded from the environment

func (*EnvironmentMapper) ParseString

func (em *EnvironmentMapper) ParseString(line string) interface{}

type FloatParser

type FloatParser interface {
	ParseFloat(in float64) interface{}
}

FloatParser called on floating point elements

type IntParser

type IntParser interface {
	ParseInt(in int64) interface{}
}

IntParser called on integer elements

type Parsers

type Parsers struct {
	StringParser StringParser
	IntParser    IntParser
	FloatParser  FloatParser
}

Parsers is a container for all possible parser interfaces

type StringParser

type StringParser interface {
	ParseString(in string) interface{}
}

StringParser interface is called on any string elements

Jump to

Keyboard shortcuts

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