config

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: May 19, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

package config implements methods for reading config files encoded in json using path expressions and default fallback values thanks to https://github.com/creamdog/gonfig

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config interface {
	Get(key string, defaultValue interface{}) (interface{}, error)
	GetString(key string, defaultValue interface{}) (string, error)
	GetInt(key string, defaultValue interface{}) (int, error)
	GetFloat(key string, defaultValue interface{}) (float64, error)
	GetBool(key string, defaultValue interface{}) (bool, error)
	GetAs(key string, target interface{}) error
}

func FromJson

func FromJson(reader io.Reader) (Config, error)

FromJson reads the contents from the supplied reader. The content is parsed as json into a map[string]interface{}. It returns a JsonConfig struct pointer and any error encountered

func FromJsonText added in v1.0.1

func FromJsonText(text string) (Config, error)

type JsonConfig

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

Config implementation Implements the Config interface

func (*JsonConfig) Get

func (jconfig *JsonConfig) Get(key string, defaultValue interface{}) (interface{}, error)

Get attempts to retreive the value behind the supplied key. It returns a interface{} with either the retreived value or the default value and any error encountered. If supplied key is not found and defaultValue is set to nil it returns a KeyNotFoundError If supplied key path goes deeper into a non-map type (string, int, bool) it returns a UnexpectedValueTypeError

func (*JsonConfig) GetAs

func (jconfig *JsonConfig) GetAs(key string, target interface{}) error

GetAs uses Get to fetch the value behind the supplied key. The value is serialized into json and deserialized into the supplied target interface. It returns any error encountered.

func (*JsonConfig) GetBool

func (jconfig *JsonConfig) GetBool(key string, defaultValue interface{}) (bool, error)

GetBool uses Get to fetch the value behind the supplied key. It returns a bool with either the retreived value or the default value and any error encountered. If value is not a bool it returns a UnexpectedValueTypeError

func (*JsonConfig) GetFloat

func (jconfig *JsonConfig) GetFloat(key string, defaultValue interface{}) (float64, error)

GetFloat uses Get to fetch the value behind the supplied key. It returns a float with either the retreived value or the default value and any error encountered. It returns a bool with either the retreived value or the default value and any error encountered. If value is not a float it returns a UnexpectedValueTypeError

func (*JsonConfig) GetInt

func (jconfig *JsonConfig) GetInt(key string, defaultValue interface{}) (int, error)

GetInt uses Get to fetch the value behind the supplied key. It returns a int with either the retreived value or the default value and any error encountered. If value is not a int it returns a UnexpectedValueTypeError

func (*JsonConfig) GetString

func (jconfig *JsonConfig) GetString(key string, defaultValue interface{}) (string, error)

GetString uses Get to fetch the value behind the supplied key. It returns a string with either the retreived value or the default value and any error encountered. If value is not a string it returns a UnexpectedValueTypeError

type KeyNotFoundError

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

func (*KeyNotFoundError) Error

func (err *KeyNotFoundError) Error() string

type UnexpectedValueTypeError

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

func (*UnexpectedValueTypeError) Error

func (err *UnexpectedValueTypeError) Error() string

Jump to

Keyboard shortcuts

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