etcdmap

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

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

Go to latest
Published: May 2, 2016 License: Apache-2.0 Imports: 9 Imported by: 0

README

EtcdMap

Go package provides methods for interacting with Etcd using struct, map or JSON.

GoDoc

Documentation

etcdmap

import "github.com/mickep76/etcdmap"

Package etcdmap provides methods for interacting with etcd using struct, map or JSON.

func Array

func Array(root *client.Node) []interface{}

Array returns a []interface{} including the directory name inside each entry from a etcd directory.

func ArrayJSON

func ArrayJSON(root *client.Node) ([]byte, error)

JSON returns an etcd directory as JSON []byte.

func ArrayJSONIndent

func ArrayJSONIndent(root *client.Node, indent string) ([]byte, error)

JSONIndent returns an etcd directory as indented JSON []byte.

func Create

func Create(kapi client.KeysAPI, path string, val reflect.Value) error

Create etcd directory structure from a map, slice or struct.

func CreateJSON

func CreateJSON(kapi client.KeysAPI, dir string, j []byte) error

CreateJSON etcd directory structure from JSON.

func JSON

func JSON(root *client.Node) ([]byte, error)

JSON returns an etcd directory as JSON []byte.

func JSONIndent

func JSONIndent(root *client.Node, indent string) ([]byte, error)

JSONIndent returns an etcd directory as indented JSON []byte.

func Map

func Map(root *client.Node) map[string]interface{}

Map returns a map[string]interface{} from a etcd directory.

func Struct

func Struct(root *client.Node, val reflect.Value) error

Documentation

Overview

Package etcdmap provides methods for interacting with etcd using struct, map or JSON.

Example (NestedStruct)

ExampleNestedStruct creates a Etcd directory using a nested Go struct and then gets the directory as JSON.

package main

import (
	"flag"
	"fmt"
	"log"
	"os"
	"reflect"
	"strings"
	"time"

	etcd "github.com/coreos/etcd/client"
	"github.com/mickep76/etcdmap"
	"golang.org/x/net/context"
)

// Env variables.
type Env struct {
	Peers string
}

// User structure.
type User struct {
	Name      string `json:"username" etcd:"name"`
	Age       int    `json:"age" etcd:"age"`
	Male      bool   `json:"male" etcd:"male"`
	FirstName string `json:"first_name" etcd:"first_name"`
	LastName  string `json:"last_name" etcd:"last_name"`
}

// Group structure.
type Group struct {
	Name  string `json:"groupname" etcd:"name"`
	Users []User `json:"users" etcd:"users"`
}

// getEnv variables.
func getEnv() Env {
	env := Env{}
	env.Peers = "http://127.0.0.1:4001,http://127.0.0.1:2379"

	for _, e := range os.Environ() {
		a := strings.Split(e, "=")
		switch a[0] {
		case "ETCD_PEERS":
			env.Peers = a[1]
		}
	}

	return env
}

// ExampleNestedStruct creates a Etcd directory using a nested Go struct and then gets the directory as JSON.
func main() {
	// Get env variables.
	env := getEnv()

	// Options.
	peers := flag.String("peers", env.Peers, "Comma separated list of etcd nodes, can be set with env. variable ETCD_PEERS")
	flag.Parse()

	// Define nested structure.
	g := Group{
		Name: "staff",
		Users: []User{
			User{
				Name:      "jdoe",
				Age:       25,
				Male:      true,
				FirstName: "John",
				LastName:  "Doe",
			},
			User{
				Name:      "lnemoy",
				Age:       62,
				Male:      true,
				FirstName: "Leonard",
				LastName:  "Nimoy",
			},
		},
	}

	// Connect to etcd.
	cfg := etcd.Config{
		Endpoints:               strings.Split(*peers, ","),
		Transport:               etcd.DefaultTransport,
		HeaderTimeoutPerRequest: time.Second,
	}

	client, err := etcd.New(cfg)
	if err != nil {
		log.Fatal(err)
	}

	kapi := etcd.NewKeysAPI(client)

	// Create directory structure based on struct.
	err2 := etcdmap.Create(kapi, "/example", reflect.ValueOf(g))
	if err2 != nil {
		log.Fatal(err2.Error())
	}

	// Get directory structure from Etcd.
	res, err3 := kapi.Get(context.TODO(), "/example", &etcd.GetOptions{Recursive: true})
	if err3 != nil {
		log.Fatal(err3.Error())
	}

	j, err4 := etcdmap.JSON(res.Node)
	if err4 != nil {
		log.Fatal(err4.Error())
	}

	fmt.Println(string(j))

	// Get directory structure from Etcd.
	/*
		res, err5 := kapi.Get(context.TODO(), "/example/users/0", &etcd.GetOptions{Recursive: true})
		if err5 != nil {
			log.Fatal(err5.Error())
		}

		s := User{}
		err6 := etcdmap.Struct(res.Node, reflect.ValueOf(&s))
		if err6 != nil {
			log.Fatal(err6.Error())
		}

		fmt.Println(s)
	*/

}
Output:

{"name":"staff","users":{"0":{"age":"25","first_name":"John","last_name":"Doe","male":"true","name":"jdoe"},"1":{"age":"62","first_name":"Leonard","last_name":"Nimoy","male":"true","name":"lnemoy"}}}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Array

func Array(root *client.Node, dirName string) []interface{}

Array returns a []interface{} including the directory name inside each entry from a etcd directory.

func ArrayJSON

func ArrayJSON(root *client.Node, dirName string) ([]byte, error)

JSON returns an etcd directory as JSON []byte.

func ArrayJSONIndent

func ArrayJSONIndent(root *client.Node, dirName string, indent string) ([]byte, error)

JSONIndent returns an etcd directory as indented JSON []byte.

func Create

func Create(kapi client.KeysAPI, path string, val reflect.Value) error

Create etcd directory structure from a map, slice or struct.

func CreateJSON

func CreateJSON(kapi client.KeysAPI, dir string, j []byte) error

CreateJSON etcd directory structure from JSON.

func JSON

func JSON(root *client.Node) ([]byte, error)

JSON returns an etcd directory as JSON []byte.

func JSONIndent

func JSONIndent(root *client.Node, indent string) ([]byte, error)

JSONIndent returns an etcd directory as indented JSON []byte.

func Map

func Map(root *client.Node) map[string]interface{}

Map returns a map[string]interface{} from a etcd directory.

func Struct

func Struct(root *client.Node, val reflect.Value) error

Types

This section is empty.

Jump to

Keyboard shortcuts

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