config

package
v0.0.0-...-93368d4 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2015 License: MIT Imports: 5 Imported by: 3

Documentation

Overview

Package config defines a parser and a handler for minero's simple configuration file format.

Properties:

  • Less verbose than JSON.
  • Simpler than YAML.
  • Easy parsing.
  • Indentation based.

Notes on Indentation:

  • Spaces and tabs are equivalent here. Examples: " \t" == "\t\t " // true " " == "\t\t\t" // true
  • You can mix both, although it's not recomended.
  • Indentation level is computed using: level = num_tabs + num_spaces.

Example input:

a:
 b:
  c: 2
  d: 3
 e:
  f: 5
g:
 h: 7

After parsing produces:

var config = Map{
   "a.b.c": "2",
   "a.b.d": "3",
   "a.e.f": "5",
   "g.h": "7"
}

Online config tester: http://play.golang.org/p/FP9hHDBjnN

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrEmpty = errors.New("Empty config file")

Functions

func PrettyMap

func PrettyMap(m Map) string

PrettyMap sorts a Map by its keys and returns a pretty-printed string version.

Example
package main

import (
	"fmt"

	"github.com/minero/minero/config"
)

func main() {
	c := config.New()
	c.Parse("a:\n b:2\n c:3\nd:4")

	m := c.Copy()
	fmt.Println(config.PrettyMap(m))
}
Output:

"a.b": 2
"a.c": 3
"d": 4

Types

type Config

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

Config wraps an unexported Map with methods for parsing and type conversion.

func New

func New() *Config

New creates and initializes a new Config.

func NewFrom

func NewFrom(m Map) *Config

NewFrom creates and initializes a new Config using m as its initial contents.

func (Config) Copy

func (c Config) Copy() (m Map)

Copy copies this Config's underlying Map and returns that copy.

func (Config) Get

func (c Config) Get(k string) string

Get retrieves a pair value by its key name.

func (Config) Len

func (c Config) Len() int

Len returns the number of entries in this Config

func (Config) Parse

func (c Config) Parse(s string) error

Parse parses a string into a config.

func (Config) ParseFile

func (c Config) ParseFile(f string) error

Parse parses a file's contents into a config.

func (Config) Save

func (c Config) Save(file string) error

Save saves a config into a file.

func (Config) Set

func (c Config) Set(k, v string)

Set sets a key/value pair. Setting an existing key overwrites it.

func (Config) String

func (c Config) String() string

String returns the pretty-printed contents of this Config's underlying Map sorted by key name.

type Map

type Map map[string]string

Map holds all key/value pairs within a Config.

func (Map) String

func (m Map) String() string

Jump to

Keyboard shortcuts

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