Siberia-configuration-properties is a library which provides a convenient way of parsing a configuration file with replacing environment variables with real and separation a parsed data by small configs
When I parse a configuration file I want to see 2 things:
- I can simply type a declaration of an environment variable in the file and I will be sure this variable will be parsed automatically
- I can use only a part of a configuration
When I dived into the internet for looking something similar, I found everything but not what I wanted to see: Or it was 1 structure for all configuration, or environment variables were not parsed, or I was forced to provide additional structures which were useless, or the stars didn't align well etc
Based on the mentioned above this library has the right to life
john@doe-pc:~$ go get github.com/siberia-projects/siberia-configuration-properties
Comparing with other libraries it may look a bit complicated, but it's not
All you need is to create a couple of tools, declare your data model and parse the config into the model using the tools:
- Declare a structure in a way it implements the Properties interface (it provides a starting point where your data really begins (could be empty - which literally means "all"))
- Read your configuration file a ReadConfiguration(ConfigurationPath) method
- (Optional) Create a SimpleSeparator
- Create an instance of your data model
- Call Configuration.WriteTo(Properties) method or use the Separator.Separate(Configuration, Properties)
my:
custom:
properties:
headers:
- key: Cache-Control
value: ${CACHE_CONTROL:no-cache}
- key: Content-Type
value: application/json
not:
my:
properties:
supportsHttps: true
package main
import (
"fmt"
"github.com/siberia-projects/siberia-configuration-properties/pkg/configuration"
)
const (
configFilepath = "path/to/config.yaml"
)
type CustomProperties struct {
Headers []Header
}
type Header struct {
Key string
Value string
}
func (properties *CustomProperties) GetPrefix() string {
return "my.custom.properties"
}
func (properties *CustomProperties) String() string {
return fmt.Sprintf("%#v", properties)
}
type NotMyProperties struct {
SupportsHttps bool
}
func (properties *NotMyProperties) GetPrefix() string {
return "not.my.properties"
}
func (properties *NotMyProperties) String() string {
return fmt.Sprintf("%#v", properties)
}
func main() {
configurationInstance, err := configuration.ReadConfiguration(configFilepath)
if err != nil {
panic(err.Error())
}
customProperties := &CustomProperties{}
notMyProperties := &NotMyProperties{}
err = configurationInstance.WriteTo(customProperties)
if err != nil {
panic(err.Error())
}
err = configurationInstance.WriteTo(notMyProperties)
if err != nil {
panic(err.Error())
}
customPropertiesString := customProperties.String()
notMyPropertiesString := notMyProperties.String()
println(customPropertiesString)
println(notMyPropertiesString)
}
Result:
&main.CustomProperties{Headers:[]main.Header{main.Header{Key:"Cache-Control", Value:"no-cache"}, main.Header{Key:"Content-Type", Value:"application/json"}}}
&main.NotMyProperties{SupportsHttps:false}