configo

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

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

Go to latest
Published: Aug 29, 2017 License: BSD-3-Clause Imports: 5 Imported by: 0

README

configo

import "github.com/kimor79/configo"

Package configo provides functions to handle configuration items.

Example:

type Config struct {
  SomeThing *SomeThing
  Mysql  MysqlConfig `toml:"mysql"`
  Port  *Int `default:"8080"`
}

type MysqlConfig struct {
  Dsn *string
}

mydb := "/mydb"

config := Config{
  Mysql: MysqlConfig{
    Dsn: &mydb,
  },
}

err := configo.UnmarshalFile("/path/to/config.toml", &config)
// error check

fmt.Printf("Listening on port %d", *config.Port)

func FromDefaults

func FromDefaults(v interface{}) error

FromDefaults sets pointer v based on default values of v.

A field's value will be determined based on the following order:

  1. If v already contains a value for the field, it will be used.
  2. If a "default" tag exists for a field, its value will be used, subject to type casting.
  3. The field will be initialized to its zero value (i.e., "" for string, 0 for int, etc).

func FromEnv

func FromEnv(v interface{}) error

FromEnv sets pointer v based on the environment.

A field's value will be determined based on the following order:

  1. If an "env" tag exists for a field and an environment variable matching the tag's value exists, the environment variable's value will be used, subject to type casting.
  2. If v already contains a value for the field, it will be used.

func FromTOML

func FromTOML(f string, v interface{}) error

FromTOML decodes the contents of the file f in TOML format into a pointer v.

A field's value will be determined based on the following order:

  1. If the field exists in the file, its value will be used. The toml tag may be used to map TOML keys to fields that don't match the key name exactly.
  2. If v already contains a value for the field, it will be used.

func UnmarshalFile

func UnmarshalFile(f string, v interface{}) error

UnmarshalFile decodes the contents of the file f in TOML format into a pointer v. If v contains data, that data will be used as "defaults".

A field's value will be determined based on the following order:

  1. If an "env" tag exists for a field and an environment variable matching the tag's value exists, the environment variable's value will be used, subject to type casting.
  2. If the field exists in the file, its value will be used. The toml tag may be used to map TOML keys to fields that don't match the key name exactly.
  3. If v already contains a value for the field, it will be used.
  4. If a "default" tag exists for a field, its value will be used, subject to type casting.
  5. The field will be initialized to its zero value (i.e., "" for string, 0 for int, etc).

Generated by godoc2md

Documentation

Overview

Package configo provides functions to handle configuration items.

Example:

type Config struct {
  SomeThing *SomeThing
  Mysql  MysqlConfig `toml:"mysql"`
  Port  *Int `default:"8080"`
}

type MysqlConfig struct {
  Dsn *string
}

mydb := "/mydb"

config := Config{
  Mysql: MysqlConfig{
    Dsn: &mydb,
  },
}

err := configo.UnmarshalFile("/path/to/config.toml", &config)
// error check

fmt.Printf("Listening on port %d", *config.Port)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromDefaults

func FromDefaults(v interface{}) error

FromDefaults sets pointer `v` based on default values of `v`.

A field's value will be determined based on the following order:

1. If `v` already contains a value for the field, it will be used. 2. If a "default" tag exists for a field, its value will be used, subject to type casting. 3. The field will be initialized to its zero value (i.e., "" for string, 0 for int, etc).

func FromEnv

func FromEnv(v interface{}) error

FromEnv sets pointer `v` based on the environment.

A field's value will be determined based on the following order:

1. If an "env" tag exists for a field and an environment variable matching the tag's value exists, the environment variable's value will be used, subject to type casting. 2. If `v` already contains a value for the field, it will be used.

func FromTOML

func FromTOML(f string, v interface{}) error

FromTOML decodes the contents of the file `f` in TOML format into a pointer `v`.

A field's value will be determined based on the following order:

1. If the field exists in the file, its value will be used. The `toml` tag may be used to map TOML keys to fields that don't match the key name exactly. 2. If `v` already contains a value for the field, it will be used.

func UnmarshalFile

func UnmarshalFile(f string, v interface{}) error

UnmarshalFile decodes the contents of the file `f` in TOML format into a pointer `v`. If `v` contains data, that data will be used as "defaults".

A field's value will be determined based on the following order:

1. If an "env" tag exists for a field and an environment variable matching the tag's value exists, the environment variable's value will be used, subject to type casting. 2. If the field exists in the file, its value will be used. The `toml` tag may be used to map TOML keys to fields that don't match the key name exactly. 3. If `v` already contains a value for the field, it will be used. 4. If a "default" tag exists for a field, its value will be used, subject to type casting. 5. The field will be initialized to its zero value (i.e., "" for string, 0 for int, etc).

Types

This section is empty.

Jump to

Keyboard shortcuts

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