kvstructure

package module
v0.1.8 Latest Latest
Warning

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

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

README

kvstructure

GoDoc Build Status Taylor Swift Volkswagen Go Report Card

Go library for transcoding data from KVs supported by libkv to structs, string, int, uint and float32 and vice versa.

Example

transcoder, err := NewTranscoder(
	TranscoderWithKV(kv),
	TranscoderWithPrefix("prefix"),
)
    
if err != nil {
    return err
}

tt := &Example{
	Description: "bar",
	Enabled: true,
}

if err := transcoder.Transcode("foo", &tt) {
    return err
}

License

Apache 2.0

Documentation

Overview

Package kvstructure implements to transcode and transdecode data structures into Key-Value Stores.

See https://github.com/docker/libkv for all supported KV.

Index

Constants

View Source
const Version = "0.1.1"

Variables

This section is empty.

Functions

func Transcode

func Transcode(name string, s interface{}, prefix string, kv store.Store) error

Transcode takes an initialized interface and puts the data in a kv

func TranscoderWithKV

func TranscoderWithKV(kv store.Store) func(o *TranscoderOpts)

TranscoderWithKV ...

func TranscoderWithPrefix

func TranscoderWithPrefix(prefix string) func(o *TranscoderOpts)

TranscoderWithPrefix ...

func Transdecode

func Transdecode(name string, s interface{}, prefix string, kv store.Store) error

Transdecode takes an interface and uses reflection to fill it with data from a kv.

func TransdecoderWithKV

func TransdecoderWithKV(kv store.Store) func(o *TransdecoderOpts)

TransdecoderWithKV ...

func TransdecoderWithPrefix

func TransdecoderWithPrefix(prefix string) func(o *TransdecoderOpts)

TransdecoderWithPrefix ...

Types

type Metadata

type Metadata struct {
	// Keys are the keys of the structure which were successfully decoded
	Keys []string

	// Unused is a slice of keys that were found in the raw value but
	// weren't decoded since there was no matching field in the result interface
	Unused []string
}

Metadata contains information about decoding a structure that is tedious or difficult to get otherwise.

type Transcoder

type Transcoder interface {
	Transcode(string, interface{}) error
}

Transcoder is the interface to a transcoder

func NewTranscoder

func NewTranscoder(opts ...TranscoderOpt) (Transcoder, error)

NewTranscoder returns a new transcoder for the given configuration. Once a transcoder has been returned, the same interface must be used.

 transcoder, err := NewTranscoder(
		TranscoderWithKV(kv),
		TranscoderWithPrefix("prefix"),
	)

 if err != nil {
 	return err
 }

	tt := &Example{
		Description: "bar",
		Enabled: true,
	}

	if err := transcoder.Transcode("foo", &tt) {
  	return err
 }

type TranscoderOpt

type TranscoderOpt func(*TranscoderOpts)

TranscoderOpt ...

type TranscoderOpts

type TranscoderOpts struct {
	// Metadata is the struct that will contain extra metadata about
	// the decoding. If this is nil, then no metadata will be tracked.
	Metadata *Metadata

	// The tag name that kvstructure reads for field names. This
	// defaults to "kvstructure"
	TagName string

	// Prefix is the prefix of the store
	Prefix string

	// KV is the kv used to retrieve the needed infos
	KV store.Store
}

TranscoderOpts is the configuration that is used to create a new transcoder and allows customization of various aspects of decoding.

type Transdecoder

type Transdecoder interface {
	Transdecode(string, interface{}) error
}

Transdecoder is the interface to a transdecoder

func NewTransdecoder

func NewTransdecoder(opts ...TransdecoderOpt) (Transdecoder, error)

NewTransdecoder returns a new transdecoder for the given configuration. Once a transdecoder has been returned, the same configuration must not be used again.

type TransdecoderOpt

type TransdecoderOpt func(*TransdecoderOpts)

TransdecoderOpt ...

type TransdecoderOpts

type TransdecoderOpts struct {
	// ZeroFields, if set to true, will zero fields before writing them.
	// For example, a map will be emptied before decoded values are put in
	// it. If this is false, a map will be merged.
	ZeroFields bool

	// should be done later
	WeaklyTypedInput bool

	// Metadata is the struct that will contain extra metadata about
	// the decoding. If this is nil, then no metadata will be tracked.
	Metadata *Metadata

	// The tag name that kvstructure reads for field names. This
	// defaults to "kvstructure"
	TagName string

	// Prefix is the prefix of the store
	Prefix string

	// KV is the kv used to retrieve the needed infos
	KV store.Store
}

TransdecoderOpts is the configuration that is used to create a new transdecoder and allows customization of various aspects of decoding.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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