Documentation
¶
Overview ¶
Example ¶
package main
import (
"fmt"
"github.com/aileron-projects/go-properties"
)
func main() {
data := `
server.port = 8080
server.tls.cert = /tmp/cert.pem
server.tls.key = /tmp/key.pem
`
props, err := properties.Parse([]byte(data))
if err != nil {
panic(err)
}
fmt.Println("server.port:", props["server.port"])
fmt.Println("server.tls.cert:", props["server.tls.cert"])
fmt.Println("server.tls.key:", props["server.tls.key"])
}
Output: server.port: 8080 server.tls.cert: /tmp/cert.pem server.tls.key: /tmp/key.pem
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotFound = errors.New("not found") ErrConvert = errors.New("cannot convert data type from string") )
Functions ¶
func Load ¶
Load loads properties from files. Duplicated keys are always overwritten. See Parse for file formats and [Resolve] for file syntax.
func LoadReaders ¶
LoadReaders loads envirproperties from files. Duplicated keys are always overwritten.
func Parse ¶
Parse parses properties from the given bytes. Typically Parse parses key-values from files such as .properties file. Parse resolves embedded environmental variables in the b.
Input specifications:
Key syntax:
# 'a'-'z', 'A'-'Z', '0'-'9', '_', '.', '-', are allowed for keys.
# Whitespaces are allowed around the seperator '='.
foo = ok
foo=ok
foo_bar=ok
foo.bar=ok
foo-bar=ok
Single line:
# Single quotes and double quotes are removed if entire value is enclosed.
foo=bar >> bar
foo="bar" >> bar
foo='bar' >> bar
foo='b"r' >> b"r
foo="b'r" >> b'r
Multiple lines:
# The "foo" will be "barbaz".
# Line breaks of LF and CRLF are removed.
# BOTH single quotes and double quotes can be used to enclose multiple lines.
foo="
bar
baz
"
Comments:
# Sharp '#' can be used for commenting.
# It must not be in the scope of single quotes and double quotes.
# It must have at least 1 white space before '#' if the comment is inlined.
# comment >> Comment is appropriately parsed.
FOO=BAR # comment >> Comment is appropriately parsed.
FOO=BAR# comment >> '#' is not parsed as comment. It is considered as a part of the value.
Escapes:
# '\\' can be used for escaping characters by following the 3 rules.
# 1. '\\' always escapes special character of ', ", \\, #
# 2. '\\' is ignored when it is not in the scope of single quotes or double quotes.
# 3. '\\'n or "\n" is replaced to LF, '\\'r or "\r" to CR, '\\'t or "\t" to TAB.
foo=ba\"r >> ba"r
foo=ba\'r >> ba'r
foo="ba\"r" >> ba"r
foo=ba\r >> bar ('\' is not in a scope of single or double quotes.)
foo="ba\nr" >> ba<LF>r (\n is, if in a scope of quotes, converted into a line break.)
foo="ba\rr" >> ba<CR>r (\r is, if in a scope of quotes, converted into a carriage return.)
foo="ba\tr" >> ba<TAB>r (\t is, if in a scope of quotes, converted into a tab space.)
Referencing:
# Other properties and environmental variables can be referenced by ${} with following steps.
# 1. Lookup environmental variable.
# 2. Convert the key to environmental variable and lookup environmental variable.
# 3. Lookup property.
# For example, with the following example, value will be resolved by the order of
# LookupEnv("bar.baz") > LookupEnv(ToEnvName("bar.baz")) > LookupProperty("bar.baz").
# Here, ToEnvName transforms the key to upper cases and replaces '-' and '.' to '_'.
FOO=${bar.baz}
Example (ReferenceEnv) ¶
package main
import (
"fmt"
"os"
"github.com/aileron-projects/go-properties"
)
func main() {
os.Setenv("EXAMPLE", "example")
defer func() { os.Unsetenv("EXAMPLE") }()
data := `reference = ${EXAMPLE}`
props, err := properties.Parse([]byte(data))
if err != nil {
panic(err)
}
// Environmental variable is prior to the property in file.
fmt.Println("reference:", props["reference"])
}
Output: reference: example
Example (ReferenceProperty) ¶
package main
import (
"fmt"
"github.com/aileron-projects/go-properties"
)
func main() {
data := `
foo.bar = baz
reference = ${foo.bar}
`
props, err := properties.Parse([]byte(data))
if err != nil {
panic(err)
}
fmt.Println("reference:", props["reference"])
}
Output: reference: baz
Example (ReferencePropertyEnv) ¶
package main
import (
"fmt"
"os"
"github.com/aileron-projects/go-properties"
)
func main() {
os.Setenv("FOO_BAR", "BAZ")
defer func() { os.Unsetenv("FOO_BAR") }()
data := `
foo.bar = baz
reference = ${foo.bar}
`
props, err := properties.Parse([]byte(data))
if err != nil {
panic(err)
}
// Environmental variable is prior to the property in file.
fmt.Println("reference:", props["reference"])
}
Output: reference: BAZ
Types ¶
type Error ¶
type Error struct {
Inner error // Inner is the inner error.
Type string // Type is the error type.
Msg string // Msg is the error message.
}
Error is the property related error.
type Properties ¶
type Properties struct {
// contains filtered or unexported fields
}
Properties provides priperties.
Example ¶
package main
import (
"fmt"
"github.com/aileron-projects/go-properties"
)
func main() {
data := `
server.port = 8080
server.tls.cert = /tmp/cert.pem
server.tls.key = /tmp/key.pem
debug.enabled = true
`
mapProps, err := properties.Parse([]byte(data))
if err != nil {
panic(err)
}
props := properties.NewProperties(mapProps)
// Set more properties
props.Set("server.listen", "0.0.0.0")
// Get properties
fmt.Println(props.Get("server.listen"))
fmt.Println(props.GetAs[int]("server.port"))
fmt.Println(props.GetAs[string]("server.tls.cert"))
fmt.Println(props.GetAs[string]("server.tls.key"))
fmt.Println(props.GetAs[bool]("debug.enabled"))
}
Output: 0.0.0.0 <nil> 8080 <nil> /tmp/cert.pem <nil> /tmp/key.pem <nil> true <nil>
func NewProperties ¶
func NewProperties(props map[string]string) *Properties
NewProperties creates new instance of Properties. If props is not empty, properties will be set to the returned Properties.
func (*Properties) Get ¶
func (p *Properties) Get(key string) (any, error)
Get returns a property value if exists. Get returns ErrNotFound if the key was not found.