confmerger

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2021 License: MIT Imports: 6 Imported by: 0

README ΒΆ

confmerger

Documentation

confmerger is a Go package that provides an easy and intuitive way to parse multiple configuration files (both synchronously and asynchronously) and merge them into one! It also provides an API for querying this merged configuration file using a dot seperated path.

Getting Started πŸƒ

To install confmerger, you first need Go installed. You can then use the Go command below.

$ go get -u github.com/AJPutland/confmerger

To use confmerger in your Go code, first import it using the following command.

import "github.com/AJPutland/confmerger"

That's it! You can now use all of the functionality this package provides in your Go program. An example Go program may look like this:

package main

import (
  "github.com/AJPutland/confmerger"
  "fmt"
)

func main() {
  merger := confmerger.New()
  
  config_files := []string{"config.local.json","config.json"}
  err := merger.ReadAsync(config_files, confmerger.JSONFileParser)
  
  if err != nil {
    //handle read error
  }
  
  result, err := merger.Get("database.port")
  
  if err != nil {
    //handle get error
  }
  
  fmt.Println(result)
}

Usage

(include example repository)

Design Choices

confmerger has two main functions:

  • Parsing configuration files and

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

func JSONFileParser ΒΆ

func JSONFileParser(file string) (map[string]interface{}, error)

JSONFileParser provides a simple implementation of the Parser type and simply reads in an parses a JSON file given its file path

Types ΒΆ

type Merger ΒΆ

type Merger struct {
	ConfigMap map[string]interface{}
}

Merger type has one member being a map which it modifies and queries through its public methods

func New ΒΆ

func New() *Merger

New returns a Merger object with an empy config map

func (*Merger) Get ΒΆ

func (m *Merger) Get(path string) (interface{}, error)

Get retrieves parts of the stored config map through a dot seperated path. If the path leads to a final value, this will be returned, if not a map will be returned holding the section pointed to by the path. The path is case sensitive

func (*Merger) Read ΒΆ

func (m *Merger) Read(files []string, parser Parser) error

Read synchronously reads and parses all provided files using the given parser function into the current config map. Returns an error object containing any errors that occured

func (*Merger) ReadAsync ΒΆ

func (m *Merger) ReadAsync(files []string, parser Parser) error

ReadAsync asynchronously reads and parses all provided files using the given parser function into the current config map. Returns an error object containing any errors that occured.

If you pass multiple files which may override one another, the resulting config map stored in the Merger object will be nondeterministic. Whilst it reads in files asynchronously, this function is blocking as it waits for all files to have been read or attempted to have been read.

func (*Merger) Update ΒΆ

func (m *Merger) Update(newMap map[string]interface{})

Update takes in a map to update the currently held config map with. This function is used by the Read functions once they have parsed the files provided. This function was made public so that it is easy to merge config files if you have them in your code already

type Parser ΒΆ

type Parser func(file string) (map[string]interface{}, error)

A Parser is just a function which takes in a string and returns a map, having this as an exposed type allows anyone to create their own Parser function which can be parsed into the Read or ReadAsync functions. Some possible examples are: 1. A parser for a different file type 2. A parser which communicates with an API endpoint to retrieve the map 3. A parser which works over many different file types

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL