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 ¶
var ( ErrMissingLineSeparator = errors.New("missing line separator") ErrMissingKeyValueSeparator = errors.New("missing key-value separator") )
Functions ¶
func Read ¶
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 ¶
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.