flexiconfig

package module
v0.0.0-...-ba51320 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2019 License: MIT Imports: 8 Imported by: 1

README

FlexiConfig

GoDoc

FlexiConfig is a library for those of us tired of just using a single static JSON or YAML file. It does this by introducing a way to load a lua file as part of the config. The hierarchical nature of FlexiConfig allows you to easily split your config into several files, and even load one as a way of setting defaults. FlexiConfig allows easy access to the values by giving you the ability to access individual sections of the config through a path-like string. This path can be used to set values, retrieve individual values, or even allowing you to pass in a struct to have the config unmarshaled into.

FlexiConfig's featureset is really defined by what I need in my projects. Various features such as file reloading on change and other programming languages are out of scope for this project. Flexiconfig is really aimed at being simple, hierarchical, and allow you to use a scripting language as a config format.

Try it out

Say you want to load a config like this:

{
    "Components": {
        "Server": {
            "Port": 2512,
            "Worlds": [
                {
                    "WorldSeed": 241555,
                    "WorldName": "My World"
                },
                {
                    "WorldSeed": 01189998819991197253,
                    "WorldName": "Crowded World"
                }
            ]
        },
        "FileSystem": {
            "Base": "/dev/null"
        }
    }
}

Into a go struct like this:

type WorldConfig struct {
    WorldSeed int
    WorldName string
}

type Config struct {
    Port int
    Worlds []WorldConfig
}

You can use this code:

// Create a new Settings object
settings := flexiconfig.NewSettings()

// Load JSON file. LoadFile tries to detect what loader to use based on file extension. You can force it with LoadJSONFile
if err := settings.LoadFile("./test.json"); err != nil {
    panic(err)
}

// Or load a JSON byte array:
if err := settings.LoadJSON(json); err != nil {
    panic(err)
}

// Get our WorldConfig struct:
worldConfig := WorldConfig{}
err := settings.Get("Components:Server", &worldConfig)

// Get our filesystem setting, default it to "~"
fsBase, _ := settings.GetString("FileSystem:Base", "~")

But what if we want to make this a lua config file?

function world(seed, name)
    return {
        WorldSeed = seed,
        WorldName = name
    }
end

return {
    Components = {
        Server = {
            Port = 2512,
            Worlds = {
                world(241555, "My World"), -- We can use helper functions!
                world(01189998819991197253, "Crowded World"),  
            }
        },
        FileSystem = {
            Base = "/dev/null", -- Imagine that, using a trailing comma and not having a syntax error??
        }
    }
}

If we wanted to load multiple config files we could just run LoadFile (or similar Load function) multiple times. Read the godoc for more info. Feel free to read through the example. It covers pretty much the entire library.

Contributing

Go for it! Note that I'm hesitant to add more features especially if they cause bloat to the library. For instance I don't want to depend on the V8 engine, don't add Javascript support! Feel free to check out the issue tracker, I put things there that I want to get to later.

Documentation

Overview

Package flexiconfig is a configuration package with the goal of being powerful but not be more complex than a configuration package should be.

FlexiConfig is a hierarchical system that will merge configs together based on the order that they are loaded. Later config loads will replace earlier settings if they overlap.

A core part of this package is the ability to load lua files. This gives you the ability to run a sub program in order to generate your config.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LuaLoader

type LuaLoader func(L *lua.LState) int

LuaLoader is the type representing the function signature used to load custom lua modules.

type Settings

type Settings struct {
	// contains filtered or unexported fields
}

Settings is the main type that holds the config and loads new configuration files.

func NewSettings

func NewSettings() Settings

NewSettings creates a new empty settings struct.

func (*Settings) AddLuaLoader

func (this *Settings) AddLuaLoader(name string, loader lua.LGFunction)

AddLuaLoader can be used to add a custom lua module to each lua-state that is used. Note that flexiconfig currently creates a new lua instance for every lua config file loaded.

func (Settings) Get

func (this Settings) Get(path string, target interface{}) error

Get will retrieve the path and store it inside the interface the best it can.

func (Settings) GetBool

func (this Settings) GetBool(path string, defaultValue bool) (bool, error)

GetBool returns a bool stored in the path. If the the path isn't defined it will return the defaultValue and an error.

func (Settings) GetFloat

func (this Settings) GetFloat(path string, defaultValue float64) (float64, error)

GetFloat returns a float stored in the path. If the the path isn't defined it will return the defaultValue and an error.

func (Settings) GetInt

func (this Settings) GetInt(path string, defaultValue int64) (int64, error)

GetInt returns a int stored in the path. If the the path isn't defined it will return the defaultValue and an error.

func (Settings) GetJSON

func (this Settings) GetJSON() []byte

GetJSON returns the json representation of the current config. This is useful to retain a static copy of the settings for later.

func (Settings) GetPrettyJSON

func (this Settings) GetPrettyJSON(prefix, indent string) []byte

GetPrettyJSON returns a pretty formatted json of the current config

func (Settings) GetString

func (this Settings) GetString(path string, defaultValue string) (string, error)

GetString returns a string stored in the path. If the the path isn't defined it will return the defaultValue and an error.

func (*Settings) LoadFile

func (this *Settings) LoadFile(path string) error

LoadFile takes a path and attempts to load it with the proper loader based on extension.

func (*Settings) LoadJSON

func (this *Settings) LoadJSON(b []byte) error

LoadJSON takes a byte slice, dejsonifys it, then stores the contents in the Settings object.

func (*Settings) LoadJSONFile

func (this *Settings) LoadJSONFile(path string) error

LoadJSON takes a path to a .json file and loads it into the Settings object.

func (*Settings) LoadLuaFile

func (this *Settings) LoadLuaFile(path string) error

LoadLuaFile is used to load a lua config file from a specified path

func (*Settings) LoadLuaString

func (this *Settings) LoadLuaString(code string) error

LoadLuaString is used to load a config file from a lua string.

func (*Settings) MergeSettings

func (this *Settings) MergeSettings(newSettings map[string]interface{}) error

MergeSettings takes a new map[string]interface{} of settings and merges it into the existing one recursively.

func (Settings) Print

func (this Settings) Print()

Print is a utility function to print out the settings as JSON

func (Settings) RawGet

func (this Settings) RawGet(path string) (interface{}, error)

RawGet will return the interface{} of the value at a specific path, and error if the value cannot be found.

func (Settings) RawSet

func (this Settings) RawSet(timid bool, path string, value interface{}) error

RawSet will set the value of the config at a specific path. "timid" is used to help describe how to treat values that are part of the path but not the proper type. For instance, given this config:given the path of

{"root": {"intermediate": 22}},

and this function call:

settings.RawSet(timid, "root:intermediate:value", "Hello World")

timid == 0 will replace "itermediate" with the required map timid == 1 will instead throw an error claiming to not be able to find the path.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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