goconfig

package
v0.0.0-...-c5e6d41 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2014 License: MIT Imports: 9 Imported by: 0

README

goconfig

Build Status

About

goconfig is a easy-use, comments-support configuration file parser for the Go Programming Language which provides a structure similar to what you would find on Microsoft Windows INI files.

The configuration file consists of sections, led by a "[section]" header and followed by "name:value" entries; "name=value" is also accepted. Note that leading whitespace is removed from values. The optional values can contain format strings which refer to other values in the same section, or values in a special DEFAULT section. Comments are indicated by ";" or "#"; comments may begin anywhere on a single line.

Features

  • It simplified operation processes, easy to use and undersatnd; therefore, there are less chances to have errors.
  • It uses exactly the same way to access a configuration file as you use windows APIs, so you don't need to change your code style.
  • It supports read recursion sections.
  • It supports auto increment of key.
  • It supports configuration file with comments each section or key which all the other parsers don't support!!!!!!!
  • It supports get value through type bool, float64, int, int64 and string, methods that start with "Must" means ignore errors and get zero-value if error occurs.

Example(Comments Support!!!!)

config.ini
; Google
google=www.google.com
search: http://%(google)s

; Here are Comments
; Second line
[Demo]
# This symbol can also make this line to be comments
key1=Let's us GoConfig!!!
key2=test data
key3=this is based on key2:%(key2)s

[What's this?]
; Not Enough Comments!!
name=try one more value ^-^

[parent]
name=john
relation=father
sex=male
age=32

[parent.child]
age=3

[parent.child.child]

; Auto increment by setting key to "-"
[auto increment]
-:hello
-:go
-=config
Code Fragment (GoConfig_test.go)
	c, err := LoadConfigFile("config.ini")
	if err != nil {
		t.Error(err)
	}

	// GetValue
	value, _ := c.GetValue("Demo", "key1") // return "Let's use GoConfig!!!"
	if value != "Let's us GoConfig!!!" {
		t.Error("Error occurs when GetValue of key1")
	}

	// GetComments
	comments := c.GetKeyComments("Demo", "key1") // return "# This symbol can also make this line to be comments"
	if comments != "# This symbol can also make this line to be comments" {
		t.Error("Error occurs when GetKeyComments")
	}

	// SetValue
	c.SetValue("What's this?", "name", "Do it!") // Now name's value is "Do it!"
	search, _ := c.GetValue(DEFAULT_SECTION, "search")
	c.SetValue(DEFAULT_SECTION, "path", search)
	key3, _ := c.GetValue("Demo", "key3")
	c.SetValue("Demo", "key3", key3)

	// You can even edit comments in your code
	c.SetKeyComments("Demo", "key1", "")
	c.SetKeyComments("Demo", "key2", "comments by code without symbol")
	c.SetKeyComments("Demo", "key3", "# comments by code with symbol")

	// Don't need that key any more? Pass empty string "" to remove! that's all!'
	c.SetValue("What's this?", "name", "") // If your key was removed, its comments will be removed too!
	c.SetValue("What's this?", "name_test", "added by test")

	// Support for recursion sections.
	age, _ := c.GetValue("parent.child", "age")
	if age != "3" {
		t.Errorf("Recursion section: should have %d but get %s.", 3, age) // 3, not 32.
	}
	name, _ := c.GetValue("parent.child", "name")
	if name != "john" {
		t.Errorf("Recursion section: should have %s but get %s.", "john", name) // "john", not empty.
	}
	name, _ = c.GetValue("parent.child.child", "name")
	if name != "john" {
		t.Errorf("Recursion section2: should have %s but get %s.", "john", name) // "john", not empty.
	}

	// GetSection and auto increment.
	se, _ := c.GetSection("auto increment")
	if len(se) != 3 {
		t.Errorf("GetSection: should have %d of map elements but get %d.", 3,
			len(se)) // 3
	}

	hello, _ := c.GetValue("auto increment", "#1")
	if hello != "hello" {
		t.Error("Error occurs when GetValue of auto increment: " + hello) // "hello", not empty.
	}

	// Finally, you need to save it
	SaveConfigFile(c, "config_test.ini")

Installation

go get github.com/Unknwon/goconfig

More Information

  • All characters are CASE SENSITIVE, BE CAREFULL!
  • If you use other operation systems instead of windows, you may want to change global variable [ LineBreak ] in conf.go, replace it with suitable characters, default value "\r\n" is for windows only. You can also use "\n" in all operation systems because I use "\n" as line break, it may look strange when you open with Notepad.exe in windows, but it works anyway.
  • API documentation: Go Walker.

Known issues

  • Map is not thread-safe.

References

Documentation

Overview

goconfig is a easy-use comments-support configuration file parser.

Index

Constants

View Source
const (
	// Default section name.
	DEFAULT_SECTION = "DEFAULT"

	// Get Errors.
	SectionNotFound = iota
	KeyNotFound
	// Read Errors.
	BlankSection
	// Get and Read Errors.
	CouldNotParse
)

Variables

View Source
var (
	// Line break
	LineBreak = "\r\n"
)

Functions

func SaveConfigFile

func SaveConfigFile(c *ConfigFile, filename string) (err error)

SaveConfigFile writes configuration file to local file system

Types

type ConfigFile

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

ConfigFile is the representation of configuration settings. The public interface is entirely through methods.

func LoadConfigFile

func LoadConfigFile(filename string) (c *ConfigFile, err error)

LoadConfigFile reads a file and returns a new configuration representation. This representation can be queried with GetValue.

func (*ConfigFile) Bool

func (c *ConfigFile) Bool(section, key string) (bool, error)

Bool returns bool type value.

func (*ConfigFile) Float64

func (c *ConfigFile) Float64(section, key string) (float64, error)

Float64 returns float64 type value.

func (*ConfigFile) GetKeyComments

func (c *ConfigFile) GetKeyComments(section, key string) (comments string)

GetKeyComments returns the comments of key in the given section. It returns an empty string(0 length) if the comments do not exist

func (*ConfigFile) GetSection

func (c *ConfigFile) GetSection(section string) (map[string]string, error)

GetSection returns key-value pairs in given section. It section does not exist, returns nil and error.

func (*ConfigFile) GetSectionComments

func (c *ConfigFile) GetSectionComments(section string) (comments string)

GetSectionComments returns the comments in the given section. It returns an empty string(0 length) if the comments do not exist

func (*ConfigFile) GetValue

func (c *ConfigFile) GetValue(section, key string) (string, error)

GetValue returns the value of key available in the given section. If the value needs to be unfolded (see e.g. %(google)s example in the GoConfig_test.go), then String does this unfolding automatically, up to _DEPTH_VALUES number of iterations. It returns an error if the section or (default)key does not exist and empty string value.

func (*ConfigFile) Int

func (c *ConfigFile) Int(section, key string) (int, error)

Int returns int type value.

func (*ConfigFile) Int64

func (c *ConfigFile) Int64(section, key string) (int64, error)

Int64 returns int64 type value.

func (*ConfigFile) MustBool

func (c *ConfigFile) MustBool(section, key string) bool

MustBool always returns value without error, it returns false if error occurs.

func (*ConfigFile) MustFloat64

func (c *ConfigFile) MustFloat64(section, key string) float64

MustFloat64 always returns value without error, it returns 0.0 if error occurs.

func (*ConfigFile) MustInt

func (c *ConfigFile) MustInt(section, key string) int

MustInt always returns value without error, it returns 0 if error occurs.

func (*ConfigFile) MustInt64

func (c *ConfigFile) MustInt64(section, key string) int64

MustInt64 always returns value without error, it returns 0 if error occurs.

func (*ConfigFile) MustValue

func (c *ConfigFile) MustValue(section, key string) string

MustValue always returns value without error, it returns empty string if error occurs.

func (*ConfigFile) SetKeyComments

func (c *ConfigFile) SetKeyComments(section, key, comments string) bool

SetKeyComments adds new section-key comments to the configuration. If comments are empty(0 length), it will remove its section-key comments! It returns true if the comments were inserted or removed, and false if the comments were overwritten. If the section does not exist in advance, it is created.

func (*ConfigFile) SetSectionComments

func (c *ConfigFile) SetSectionComments(section, comments string) bool

SetSectionComments adds new section comments to the configuration. If comments are empty(0 length), it will remove its section comments! It returns true if the comments were inserted or removed, and false if the comments were overwritten.

func (*ConfigFile) SetValue

func (c *ConfigFile) SetValue(section, key, value string) bool

SetValue adds a new section-key-value to the configuration. If value is an empty string(0 length), it will remove its section-key and its comments! It returns true if the key and value were inserted or removed, and false if the value was overwritten. If the section does not exist in advance, it is created.

type ReadError

type ReadError struct {
	Reason  int    // Error reason
	Content string // Line content
}

ReadError occurs when read configuration file with wrong format

func (ReadError) Error

func (err ReadError) Error() string

Implement Error method

Jump to

Keyboard shortcuts

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