gonfig

package module
v0.0.0-...-70bc6c1 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2020 License: Apache-2.0 Imports: 8 Imported by: 0

README

gonfig

a config parser for GO with support for path selectors

##package documentation

https://godoc.org/github.com/creamdog/gonfig

##overview

a loaded config gonfig.FromJson("path/to/config.json") exposes the following methods

  • GetString("path/to/key", "default value / can be nil") (string, error)
  • GetInt("path/to/key", 0) (string, error)
  • GetFloat("path/to/key", nil) (string, error)
  • GetBool("path/to/key", false) (string, error)
  • GetAs("path/to/key", &myCustomStruct) error

##usage example

###example config file

{
  "services" : {
    "facebook" : {
      "host" : "facebook.com",
      "port" : 443,
      "message" : {
        "template" : {
          "subject" : "this is my default subject",
          "message" : "whoops, looks like I forgot to write a body!"
        }
      }
    }
  },
  "credentials" : {
    "facebook" : {
      "username" : "jane32",
      "password" : "supersecret"
    }
  }
}

###example main.go

import(
  "github.com/creamdog/gonfig"
  "os"
)

type ExampleMessageStruct struct {
  Message string
  Subject string
}

func main() {
  f, err := os.Open("myconfig.json")
  if err != nil {
    // TODO: error handling
  }
  defer f.Close();
  config, err := gonfig.FromJson(f)
  if err != nil {
    // TODO: error handling
  }
  
  
  username, err := config.GetString("credentials/facebook/username", "scooby")
  if err != nil {
    // TODO: error handling
  }
  password, err := config.GetString("credentials/facebook/password", "123456")
  if err != nil {
    // TODO: error handling
  }
  host, err := config.GetString("services/facebook/host", "localhost")
  if err != nil {
    // TODO: error handling
  }
  port, err := config.GetInt("services/facebook/port", 80)
  if err != nil {
    // TODO: error handling
  }
  
  // TODO: example something
  // login(host, port, username, password)
  
  var template ExampleMessageStruct
  if err := config.GetAs("services/facebook/message/template", &template); err != nil {
    // TODO: error handling
  }
  
  /// TODO: example something
  // template.Message = "I just want to say, oh my!"
  // sendMessage(host, port, template)
  
}

Documentation

Overview

Package gonfig implements methods for reading config files encoded in json using path expressions and default fallback values

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Gonfig

type Gonfig 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
	ConfigKeySeparator(separator string)
}

func FromJson

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

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

func FromYml

func FromYml(reader io.Reader) (Gonfig, error)

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

type JsonGonfig

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

Gonfig implementation Implements the Gonfig interface

func (*JsonGonfig) ConfigKeySeparator

func (jgonfig *JsonGonfig) ConfigKeySeparator(separator string)

func (*JsonGonfig) Get

func (jgonfig *JsonGonfig) 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 (*JsonGonfig) GetAs

func (jgonfig *JsonGonfig) 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 (*JsonGonfig) GetBool

func (jgonfig *JsonGonfig) 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 (*JsonGonfig) GetFloat

func (jgonfig *JsonGonfig) 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 (*JsonGonfig) GetInt

func (jgonfig *JsonGonfig) 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 (*JsonGonfig) GetString

func (jgonfig *JsonGonfig) 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