pongo

package module
v0.0.0-...-7722d08 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2019 License: MIT Imports: 11 Imported by: 0

README

PonGo

PonGo (Properties on Go) is a package that converts properties files in complex structures.

The file with the following content, for example:

map.a=11
map.b=22
name=myname
age=33

loaded in struct

struct {
	TheName string `pongo:"name"`
	TheAge  int    `pongo:"age"`
	Map     map[string]int
}

becomes

{TheName:"myname" TheAge:33 Map:map[a:11 b:22]}

PonGo supports the following tags:

timeformat: specifies a time parsing format
inline:		the value is intended as an csv inline array 

Special Types

Time

A time.Time field is parsed with the following timeformat 2006-01-02 15:04:05, unless the timeformat tag is specified.

Map

A map includes every property that starts with its prefix. The struct

struct {
	Map map[string]string
}

with the file

map.a = asd
map.b = lol

will become

{["a":"asd" "b":"lol"]}
Slices

A slice element without inline tag finds the properties with numbers after the slice prefix. The struct

struct {
	Array []string
}

with the file

array.1 = asd
array.2 = lol

will become

{["asd" "lol"]}

Environments

PonGo offers an environment feature: it works by simply adding @environment at the end of the property name. Pongo will search for the property with the environment appendix first, if nothing is found it will search the value in the simple property.

Take the following file:

sampleprop@env1 = sampleValue
sampleprop = anotherValue
anotherprop@env2 = a property
anotherprop = the value

Loading the properties with the environment env1 will set the properties to:

sampleprop = sampleValue
anotherprop = the value

Using env2:

sampleprop = anotherValue
anotherprop = a property

And without any environment

sampleprop = anotherValue
anotherprop = the value

See documentation for help.

Documentation

Overview

PonGo (Properties on Go) is a package that converts properties files in complex structures.

The file with the following content, for example

map.a=11
map.b=22
name=myname
age=33
arr=1,2,3,4
t=2014-03-18

loaded in struct

struct {
	TheName string     `pongo:"name"`
	TheAge  int        `pongo:"age"`
	Map     map[string]int
	Array   []int      `pongo:"arr,inline"`
	T       *time.Time `pongo:",timeformat=2006-01-02`
}

becomes

{TheName:"myname" TheAge:33 Map:map[a:11 b:22] Array:[1 2 3 4] T:2014-03-18 00:00:00 +0000 UTC}

PonGo supports the following tags:

timeformat: specifies a time parsing format
inline: the string value is intended as an inline array splitted with the decoder separator

A prefix is a string used in the decoding phase to complete the properties names.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsDataLeft

func IsDataLeft(err error) ([]string, bool)

Checks if err is a ErrDataLeft and returns the unread keys.

func Unmarshal

func Unmarshal(b []byte, v interface{}, prefix string, readAll bool) error

Unmarshal parses the properties-encoded data and stores the result in the value pointed to by v. It uses the default decoder.

Types

type Decoder

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

A Decoder reads and decodes properties into struct from an input stream.

func NewDecoder

func NewDecoder(r io.Reader, sep, env string) (*Decoder, error)

NewDecoder returns a new decoder that reads from r, env is the preferred environment, and sep is the regex used to split inline array.

func (*Decoder) Decode

func (d *Decoder) Decode(v interface{}, prefix string, readAll bool) error

Decode populate v with the values extraced from decode's reader.

type ErrDataLeft

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

Some data in the reader has not been assigned.

func (*ErrDataLeft) Error

func (err *ErrDataLeft) Error() string

Jump to

Keyboard shortcuts

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