consulstruct

package module
v0.0.0-...-23825f4 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2018 License: MIT Imports: 5 Imported by: 0

README

Consulstruct

Decode values from consul into a struct.

Supported types :

  • []string
  • string
  • int
  • bool

package main

import (
	"log"

	"github.com/adelowo/consulstruct"
	"github.com/hashicorp/consul/api"
)

func main() {

	type config struct {
		MemcachedEndPoints []string `consul:"memcached/endpoint" consulSeparator:","`
		Mysql              string   `consul:"database/mysql"`
		Mongo              string   `consul:"database/mongo"`
		IsActive           bool     `consul:"boolean"`
		Count              int      `consul:"counter"`
	}

	conf := new(config)

	client, err := api.NewClient(api.DefaultConfig())
	if err != nil {
		log.Fatalf("an error occurred while trying to instantiate client... %v", err)
	}

	decoder := consulstruct.New(&consulstruct.Config{
		// This prefix would be applied to all elements in the config struct
		// For example -> database/mysql would be evaluated as x/play/database/mysql
		Prefix:    "x/play",
		QueryOpts: nil,
		Store:     client.KV(),
	})

	if err := decoder.Decode(conf); err != nil {
		log.Fatal(err)
	}

	log.Println(conf)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNonPtr    = errors.New("target struct is not a pointer")
	ErrNotStruct = errors.New("target must be a struct")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	Prefix    string
	Store     *api.KV
	QueryOpts *api.QueryOptions
}

type Decoder

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

func New

func New(c *Config) *Decoder

func (*Decoder) Decode

func (d *Decoder) Decode(v interface{}) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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