1 // Copyright 2017 Frank Schroeder. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package properties provides functions for reading and writing
6 // ISO-8859-1 and UTF-8 encoded .properties files and has
7 // support for recursive property expansion.
9 // Java properties files are ISO-8859-1 encoded and use Unicode
10 // literals for characters outside the ISO character set. Unicode
11 // literals can be used in UTF-8 encoded properties files but
14 // To load a single properties file use MustLoadFile():
16 // p := properties.MustLoadFile(filename, properties.UTF8)
18 // To load multiple properties files use MustLoadFiles()
19 // which loads the files in the given order and merges the
20 // result. Missing properties files can be ignored if the
21 // 'ignoreMissing' flag is set to true.
23 // Filenames can contain environment variables which are expanded
26 // f1 := "/etc/myapp/myapp.conf"
27 // f2 := "/home/${USER}/myapp.conf"
28 // p := MustLoadFiles([]string{f1, f2}, properties.UTF8, true)
30 // All of the different key/value delimiters ' ', ':' and '=' are
31 // supported as well as the comment characters '!' and '#' and
34 // ! this is a comment
37 // # the following expressions are equal
46 // Properties stores all comments preceding a key and provides
47 // GetComments() and SetComments() methods to retrieve and
48 // update them. The convenience functions GetComment() and
49 // SetComment() allow access to the last comment. The
50 // WriteComment() method writes properties files including
51 // the comments and with the keys in the original order.
52 // This can be used for sanitizing properties files.
54 // Property expansion is recursive and circular references
55 // and malformed expressions are not allowed and cause an
56 // error. Expansion of environment variables is supported.
58 // # standard property
61 // # property expansion: key2 = value
64 // # recursive expansion: key3 = value
67 // # circular reference (error)
70 // # malformed expression (error)
73 // # refers to the users' home dir
76 // # local key takes precendence over env var: u = foo
80 // The default property expansion format is ${key} but can be
81 // changed by setting different pre- and postfix values on the
84 // p := properties.NewProperties()
88 // Properties provides convenience functions for getting typed
89 // values with default values if the key does not exist or the
90 // type conversion failed.
92 // # Returns true if the value is either "1", "on", "yes" or "true"
93 // # Returns false for every other value and the default value if
94 // # the key does not exist.
95 // v = p.GetBool("key", false)
97 // # Returns the value if the key exists and the format conversion
98 // # was successful. Otherwise, the default value is returned.
99 // v = p.GetInt64("key", 999)
100 // v = p.GetUint64("key", 999)
101 // v = p.GetFloat64("key", 123.0)
102 // v = p.GetString("key", "def")
103 // v = p.GetDuration("key", 999)
105 // As an alterantive properties may be applied with the standard
106 // library's flag implementation at any time.
108 // # Standard configuration
109 // v = flag.Int("key", 999, "help message")
112 // # Merge p into the flag set
113 // p.MustFlag(flag.CommandLine)
115 // Properties provides several MustXXX() convenience functions
116 // which will terminate the app if an error occurs. The behavior
117 // of the failure is configurable and the default is to call
118 // log.Fatal(err). To have the MustXXX() functions panic instead
119 // of logging the error set a different ErrorHandler before
120 // you use the Properties package.
122 // properties.ErrorHandler = properties.PanicHandler
124 // # Will panic instead of logging an error
125 // p := properties.MustLoadFile("config.properties")
127 // You can also provide your own ErrorHandler function. The only requirement
128 // is that the error handler function must exit after handling the error.
130 // properties.ErrorHandler = func(err error) {
135 // # Will write to stdout and then exit
136 // p := properties.MustLoadFile("config.properties")
138 // Properties can also be loaded into a struct via the `Decode`
142 // A string `properties:"a,default=foo"`
143 // D time.Duration `properties:"timeout,default=5s"`
144 // E time.Time `properties:"expires,layout=2006-01-02,default=2015-01-01"`
147 // See `Decode()` method for the full documentation.
149 // The following documents provide a description of the properties
152 // http://en.wikipedia.org/wiki/.properties
154 // http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html#load%28java.io.Reader%29