Documentation
¶
Overview ¶
Package properties provides a thread-safe properties management system with file loading, hot-reloading, and change notification support.
The package offers a simple key-value store for application configuration with support for loading from properties files, command-line overrides, and dynamic updates with file watching.
Key features:
- Load properties from files (key=value format)
- Command-line property overrides via -P flag
- File watching with automatic hot-reloading
- Thread-safe operations with read-write locks
- Change listeners for reactive configuration
- Type-safe accessors for common data types
Basic usage:
// Load properties from file
err := properties.LoadFile("app.properties")
if err != nil {
log.Fatal(err)
}
// Get property values with defaults
host := properties.String("localhost", "server.host")
port := properties.Int(8080, "server.port")
debug := properties.On(false, "debug.enabled", "debug")
timeout := properties.Duration(30*time.Second, "request.timeout")
// Set properties dynamically
properties.Set("api.key", "secret-key")
// Register change listener
properties.RegisterListener(func(p *properties.Properties) {
log.Println("Properties updated")
})
Command-line usage:
./app -P db.host=localhost -P db.port=5432
Properties file format:
# Comments start with # server.host=localhost server.port=8080 debug.enabled=true request.timeout=30s
The package maintains a global instance for convenience, but you can also create isolated Properties instances for different configuration contexts.
Index ¶
- Variables
- func BindFlags(flags *pflag.FlagSet)
- func BusinessHours() (timeinterval.TimeIntervals, error)
- func Bytes(def int, key string) int
- func Choice(def string, options []string, keys ...string) string
- func Duration(def time.Duration, keys ...string) time.Duration
- func Get(key string) string
- func Int(def int, key string) int
- func LogLevel(def string, options []string, keys ...string) string
- func On(def bool, keys ...string) bool
- func ParseBytes(value string) (int64, error)
- func RegisterListener(fn func(*Properties))
- func Set(key string, value any)
- func String(def string, keys ...string) string
- func TimeIntervals(keys ...string) (timeinterval.TimeIntervals, error)
- func Update(props map[string]string)
- type Properties
- func (p *Properties) Bytes(def int, key string) int
- func (p *Properties) Choice(def string, options []string, keys ...string) string
- func (p *Properties) Duration(def time.Duration, keys ...string) time.Duration
- func (p *Properties) Get(key string) string
- func (p *Properties) GetAll() map[string]string
- func (p *Properties) Int(def int, key string) int
- func (p *Properties) List() []Property
- func (p *Properties) LoadFile(filename string) error
- func (p *Properties) LogLevel(def string, options []string, keys ...string) string
- func (p *Properties) On(def bool, keys ...string) bool
- func (p *Properties) RegisterListener(fn func(*Properties))
- func (p *Properties) Set(key string, value any)
- func (p *Properties) String(def string, keys ...string) string
- func (p *Properties) TimeIntervals(keys ...string) (timeinterval.TimeIntervals, error)
- func (p *Properties) Update(props map[string]string)
- func (p *Properties) Watch() func()
- type Property
- type PropertySource
- type PropertyType
Constants ¶
This section is empty.
Variables ¶
var Global = &Properties{ m: make(map[string]string), }
Global is the default properties instance used by package-level functions. It's automatically initialized and ready to use.
var LoadFile = func(filename string) error { return Global.LoadFile(filename) }
LoadFile is a convenience function that loads properties from a file into the global properties instance. It's equivalent to Global.LoadFile(filename).
Functions ¶
func BindFlags ¶ added in v1.29.0
BindFlags binds the -P/--properties flag to the given flag set, allowing properties to be set via command line.
Example:
flags := pflag.NewFlagSet("app", pflag.ContinueOnError)
properties.BindFlags(flags)
flags.Parse(os.Args[1:])
// Now you can use: ./app -P key1=value1 -P key2=value2
func BusinessHours ¶ added in v1.44.0
func BusinessHours() (timeinterval.TimeIntervals, error)
func ParseBytes ¶ added in v1.59.0
ParseBytes parses a byte-size value: a decimal integer with an optional, case-insensitive size suffix. Accepted suffixes are B, KB/MB/GB/TB (powers of 1000) and KiB/MiB/GiB/TiB (powers of 1024); the bare forms K/M/G/T and Ki/Mi/Gi/Ti are accepted too. Whitespace between the number and the suffix is ignored, so "1MiB", "1 MiB" and "1048576" all parse to 1048576.
func RegisterListener ¶
func RegisterListener(fn func(*Properties))
func TimeIntervals ¶ added in v1.44.0
func TimeIntervals(keys ...string) (timeinterval.TimeIntervals, error)
Types ¶
type Properties ¶
type Properties struct {
Reload func() // Function to manually trigger reload
// contains filtered or unexported fields
}
Properties represents a thread-safe key-value store for application configuration. It supports loading from files, dynamic updates, file watching, and change notifications.
func (*Properties) Bytes ¶ added in v1.58.0
func (p *Properties) Bytes(def int, key string) int
Bytes returns the byte size configured under key, falling back to def when the property is unset or does not parse. Values use the ParseBytes syntax: a decimal integer with an optional size suffix, e.g. "1048576", "1MiB" or "4MB". Only unset and unparseable values fall back to def; a value that parses to 0 or less is returned as-is so callers can use it to disable a cap. Callers that require a positive limit must reject non-positive results themselves, as logger.HTTPLogResponseBodyLength does.
func (*Properties) Choice ¶ added in v1.58.0
func (p *Properties) Choice(def string, options []string, keys ...string) string
func (*Properties) Get ¶
func (p *Properties) Get(key string) string
func (*Properties) GetAll ¶
func (p *Properties) GetAll() map[string]string
func (*Properties) List ¶ added in v1.58.0
func (p *Properties) List() []Property
func (*Properties) LoadFile ¶
func (p *Properties) LoadFile(filename string) error
func (*Properties) LogLevel ¶ added in v1.58.0
func (p *Properties) LogLevel(def string, options []string, keys ...string) string
func (*Properties) RegisterListener ¶
func (p *Properties) RegisterListener(fn func(*Properties))
func (*Properties) Set ¶
func (p *Properties) Set(key string, value any)
func (*Properties) TimeIntervals ¶ added in v1.44.0
func (p *Properties) TimeIntervals(keys ...string) (timeinterval.TimeIntervals, error)
TimeIntervals returns the parsed time intervals from the specified property keys. Properties are looked up under "time_interval.<key>" prefix. If no keys are provided, defaults to "business_hours". The property value should be a JSON array in alertmanager time interval format. Example: [{"weekdays":["monday:friday"],"times":[{"start_time":"09:00","end_time":"17:00"}]}]
func (*Properties) Update ¶
func (p *Properties) Update(props map[string]string)
func (*Properties) Watch ¶ added in v1.27.0
func (p *Properties) Watch() func()
type Property ¶ added in v1.58.0
type Property struct {
Key string
Value string
Default string
HasDefault bool
Type PropertyType
Options []string
Source PropertySource
ReadOnly bool
}
type PropertySource ¶ added in v1.58.0
type PropertySource string
const ( PropertySourceDefault PropertySource = "default" PropertySourceRuntime PropertySource = "runtime" PropertySourceCommandLine PropertySource = "command-line" PropertySourceUnset PropertySource = "unset" )
type PropertyType ¶ added in v1.58.0
type PropertyType string
const ( PropertyTypeString PropertyType = "string" PropertyTypeBool PropertyType = "bool" PropertyTypeInt PropertyType = "int" PropertyTypeBytes PropertyType = "bytes" PropertyTypeDuration PropertyType = "duration" PropertyTypeChoice PropertyType = "choice" PropertyTypeLogLevel PropertyType = "log-level" PropertyTypeTimeIntervals PropertyType = "time-intervals" )