properties

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2019 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package properties includes functions for reading and writing from properties files. See the examples below for usage.

Reference:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrMissingLineSeparator     = errors.New("missing line separator")
	ErrMissingKeyValueSeparator = errors.New("missing key-value separator")
)

Functions

func Read

func Read(input *ReadInput) (interface{}, error)

Read parses properties from the given reader and returns a map of the properties, and error if any.

Example (Comment)

This example shows you can read from properties text with comments

in := `
  a=1
  b:2
  # ignore this comment
  c true
  d=nil
  # and ignore this one too
  e=
  `

out, err := Read(&ReadInput{
	Type:            reflect.TypeOf(map[string]string{}),
	Reader:          strings.NewReader(in),
	LineSeparator:   []byte("\n")[0],
	Comment:         "#",
	Trim:            true,
	UnescapeSpace:   false,
	UnescapeEqual:   false,
	UnescapeColon:   false,
	UnescapeNewLine: false,
})
if err != nil {
	panic(err)
}
fmt.Println(out)
Output:
map[a:1 b:2 c:true d:nil e:]
Example (Map)

This example shows you can write a map into properties text.

in := `
  a=1
  b:2
  c true
  d=nil
  e=
  `

out, err := Read(&ReadInput{
	Type:            reflect.TypeOf(map[string]string{}),
	Reader:          strings.NewReader(in),
	LineSeparator:   []byte("\n")[0],
	Comment:         "",
	Trim:            true,
	UnescapeSpace:   false,
	UnescapeEqual:   false,
	UnescapeColon:   false,
	UnescapeNewLine: false,
})
if err != nil {
	panic(err)
}
fmt.Println(out)
Output:
map[a:1 b:2 c:true d:nil e:]
Example (Semicolon)

This example shows you can use a custom line separator to read from a series of semi-colon separated key-value pairs.

in := `a=1;b:2;c true;d=nil;e=`

out, err := Read(&ReadInput{
	Type:            reflect.TypeOf(map[string]string{}),
	Reader:          strings.NewReader(in),
	LineSeparator:   []byte(";")[0],
	Comment:         "",
	Trim:            true,
	UnescapeSpace:   false,
	UnescapeEqual:   false,
	UnescapeColon:   false,
	UnescapeNewLine: false,
})
if err != nil {
	panic(err)
}
fmt.Println(out)
Output:
map[a:1 b:2 c:true d:nil e:]

func Write

func Write(input *WriteInput) error

Write writes the given rows as separated values.

Example (Map)

This example shows you can write a map into properties text.

obj := map[string]interface{}{
	"a": 1,
	"b": 2,
	"c": 3,
}
buf := new(bytes.Buffer)
err := Write(&WriteInput{
	Writer:            buf,
	KeyValueSeparator: "=",
	LineSeparator:     "\n",
	Object:            obj,
	KeySerializer:     stringify.NewStringer("", false, false, false),
	ValueSerializer:   stringify.NewStringer("", false, false, false),
	Sorted:            true,
	EscapePrefix:      "\\",
	EscapeSpace:       true,
	EscapeEqual:       true,
	EscapeColon:       false,
	EscapeNewLine:     false,
})
if err != nil {
	panic(err)
}
fmt.Println(buf.String())
Output:
a=1
b=2
c=3
Example (Semicolon)

This example shows you can write a map into properties text using a custom key-value separator, in this case a semicolon.

obj := map[string]interface{}{
	"a": 1,
	"b": 2,
	"c": 3,
}
buf := new(bytes.Buffer)
err := Write(&WriteInput{
	Writer:            buf,
	KeyValueSeparator: ";", // specify the separator for each key-value pair
	LineSeparator:     "\n",
	Object:            obj,
	KeySerializer:     stringify.NewStringer("", false, false, false),
	ValueSerializer:   stringify.NewStringer("", false, false, false),
	Sorted:            true,
	EscapePrefix:      "\\",
	EscapeSpace:       true,
	EscapeEqual:       true,
	EscapeColon:       false,
	EscapeNewLine:     false,
})
if err != nil {
	panic(err)
}
fmt.Println(buf.String())
Output:
a;1
b;2
c;3
Example (ValueSerializer)

This example shows you can write a map into properties text using a custom value serializer. In this case, nil values are written as a dash.

obj := map[string]interface{}{
	"a": 1,
	"b": 2,
	"c": nil,
}
buf := new(bytes.Buffer)
err := Write(&WriteInput{
	Writer:            buf,
	KeyValueSeparator: "=", // specify the separator for each key-value pair
	LineSeparator:     "\n",
	Object:            obj,
	KeySerializer:     stringify.NewStringer("", false, false, false),
	ValueSerializer:   stringify.NewStringer("-", false, false, false), // specify the no-data value
	Sorted:            true,
	EscapePrefix:      "\\",
	EscapeSpace:       true,
	EscapeEqual:       true,
	EscapeColon:       false,
	EscapeNewLine:     false,
})
if err != nil {
	panic(err)
}
fmt.Println(buf.String())
Output:
a=1
b=2
c=-

Types

type ErrInvalidKind

type ErrInvalidKind struct {
	Value    reflect.Type
	Expected []reflect.Kind
}

func (ErrInvalidKind) Error

func (e ErrInvalidKind) Error() string

Error returns the error formatted as a string.

type ReadInput

type ReadInput struct {
	Type            reflect.Type // the output type
	Reader          io.Reader    // the underlying reader
	LineSeparator   byte         // the newline byte
	DropCR          bool         // drop carriage return
	Comment         string       // the comment prefix
	Trim            bool         // trim lines
	EscapePrefix    string       // escape prefix
	UnescapeSpace   bool         // unescape spaces
	UnescapeEqual   bool         // unescape =
	UnescapeColon   bool         // unescape :
	UnescapeNewLine bool         // unescape \n
}

ReadInput provides the input for the Read function.

type WriteInput

type WriteInput struct {
	Writer            io.Writer          // the underlying writer
	LineSeparator     string             // the newline byte
	KeyValueSeparator string             // the separator for key-value pairs
	Object            interface{}        // the object to write
	KeySerializer     stringify.Stringer // serializer for object properties
	ValueSerializer   stringify.Stringer // serializer for object properties
	Sorted            bool               // sort output
	Reversed          bool               // if sorted, sort in reverse alphabetical order
	EscapePrefix      string             // escape prefix, if empty then doesn't escape
	EscapeSpace       bool               // escape spaces
	EscapeEqual       bool               // escape =
	EscapeColon       bool               // escape :
	EscapeNewLine     bool               // escape \n
}

WriteInput provides the input for the Write function.

Jump to

Keyboard shortcuts

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