Skip to content

Siberia-configuration-properties is a library which allows you write 1 config file which could be divided on small configs with env variables expansion

License

Notifications You must be signed in to change notification settings

siberia-projects/siberia-configuration-properties

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Siberia Configuration Properties

Author Source Code Version Coverage Status

What is it?

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

Why?

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

How to download?

john@doe-pc:~$ go get github.com/siberia-projects/siberia-configuration-properties

How to use?

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)

Examples

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}

About

Siberia-configuration-properties is a library which allows you write 1 config file which could be divided on small configs with env variables expansion

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages