goini

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

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

Go to latest
Published: Feb 13, 2015 License: BSD-3-Clause Imports: 8 Imported by: 0

README

goini

This is a Go package to interact with arbitrary INI

Our applications have a lot of memory data which are key/value pairs in the form of various separators NOT only '\n'. I have not found any golang open source code suitable for it. So I write it myself.

Build Status Coverage Status

Importing

import github.com/zieckey/goini

Usage

Example 1 : Parsing an INI file

The simplest example code is :

import github.com/zieckey/goini

ini := goini.New()
err := ini.ParseFile(filename)
if err != nil {
	fmt.Printf("parse INI file %v failed : %v\n", filename, err.Error())
	return
}

v, ok := ini.Get("the-key")
//...

Example 2 : Parsing the memory data like the format of INI

raw := []byte("a:av|b:bv||c:cv|||d:dv||||||")
ini := goini.New()
err := ini.Parse(raw, "|", ":")
if err != nil {
	fmt.Printf("parse INI memory data failed : %v\n", err.Error())
	return
}

v, ok := ini.Get("a")
//...

Documentation

Index

Constants

View Source
const (
	DefaultSection           = ""
	DefaultLineSeperator     = "\n"
	DefaultKeyValueSeperator = "="
)
View Source
const (
	InheritedFrom = "inherited_from" // The key of the INI path which will be inherited from
)

Variables

This section is empty.

Functions

func GetPathByRelativePath

func GetPathByRelativePath(relativeFilePath, inheritedPath string) string

GetPathByRelativePath gets the real path according to the relative file path e.g. :

relativeFilePath = /home/goini/conf/common.conf
inheritedPath = app.conf

and then the GetPathByRelativePath(relativeFilePath, inheritedPath) will return /home/goini/conf/app.conf

Types

type INI

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

func LoadInheritedINI

func LoadInheritedINI(filename string) (*INI, error)

LoadInheritedINI loads an INI file which will inherit from another INI e.g:

The common.ini has contents:
	project=common
	ip=192.168.0.1

And the project.ini has contents:
	project=ppp
	combo=ppp
	inherited_from=common.ini

The project.ini has the same configure as below :
	project=ppp
	combo=ppp
	ip=192.168.0.1

func New

func New() *INI

func (*INI) Delete

func (ini *INI) Delete(section, key string)

Delete deletes the key in given section.

func (*INI) Get

func (ini *INI) Get(key string) (value string, ok bool)

Get looks up a value for a key in the default section and returns that value, along with a boolean result similar to a map lookup.

func (*INI) GetAll

func (ini *INI) GetAll() SectionMap

GetAll gets the section map and its key/value pairs.

func (*INI) GetBool

func (ini *INI) GetBool(key string) (value bool, ok bool)

GetBool returns the boolean value represented by the string. It accepts "1", "t", "T", "true", "TRUE", "True", "on", "ON", "On", "yes", "YES", "Yes" as true and "0", "f", "F", "false", "FALSE", "False", "off", "OFF", "Off", "no", "NO", "No" as false Any other value returns false.

func (*INI) GetFloat

func (ini *INI) GetFloat(key string) (value float64, ok bool)

GetFloat get value as float64

func (*INI) GetInt

func (ini *INI) GetInt(key string) (value int, ok bool)

GetInt get value as int

func (*INI) GetKvmap

func (ini *INI) GetKvmap(section string) (kvmap Kvmap, ok bool)

GetKvmap gets all keys under section as a Kvmap (map[string]string). The first return value will get the value that corresponds to the key (or the map’s value type’s zero value if the key isn’t present), and the second will get true(or false if the key isn’t present).

func (*INI) Merge

func (ini *INI) Merge(from *INI, override bool)

Merge merges the data in another INI (from) to this INI (ini), and from INI will not be changed

func (*INI) Parse

func (ini *INI) Parse(data []byte, linesep, kvsep string) error

Parse parse the data to store the data in the INI A successful call returns err == nil

func (*INI) ParseFile

func (ini *INI) ParseFile(filename string) error

ParseFile reads the INI file named by filename and parse the contents to store the data in the INI A successful call returns err == nil

func (*INI) Reset

func (ini *INI) Reset()

Reset clear all the data hold by INI

func (*INI) SectionGet

func (ini *INI) SectionGet(section, key string) (value string, ok bool)

SectionGet looks up a value for a key in a section and returns that value, along with a boolean result similar to a map lookup.

func (*INI) SectionGetBool

func (ini *INI) SectionGetBool(section, key string) (value bool, ok bool)

SectionGetBool get a value as bool. See GetBool for more detail

func (*INI) SectionGetFloat

func (ini *INI) SectionGetFloat(section, key string) (value float64, ok bool)

SectionGetFloat get value as float64

func (*INI) SectionGetInt

func (ini *INI) SectionGetInt(section, key string) (value int, ok bool)

SectionGetInt get value as int

func (*INI) SectionSet

func (ini *INI) SectionSet(section, key, value string)

SectionSet store the section/key/value triple to this INI, creating it if it wasn't already present.

func (*INI) SectionSetBool

func (ini *INI) SectionSetBool(section, key string, value bool)

SectionSetBool store the section/key/value triple to this INI, creating it if it wasn't already present.

func (*INI) SectionSetFloat

func (ini *INI) SectionSetFloat(section, key string, value float64)

SectionSetFloat store the section/key/value triple to this INI, creating it if it wasn't already present.

func (*INI) SectionSetInt

func (ini *INI) SectionSetInt(section, key string, value int)

SectionSetInt store the section/key/value triple to this INI, creating it if it wasn't already present.

func (*INI) Set

func (ini *INI) Set(key, value string)

Set store the key/value pair to the default section of this INI, creating it if it wasn't already present.

func (*INI) SetBool

func (ini *INI) SetBool(key string, value bool)

SetBool store the key/value pair to the default section of this INI, creating it if it wasn't already present.

func (*INI) SetFloat

func (ini *INI) SetFloat(key string, value float64)

SetFloat store the key/value pair to the default section of this INI, creating it if it wasn't already present.

func (*INI) SetInt

func (ini *INI) SetInt(key string, value int)

SetInt store the key/value pair to the default section of this INI, creating it if it wasn't already present.

func (*INI) SetParseSection

func (ini *INI) SetParseSection(parseSection bool)

SetParseSection set INI.parseSection whether process the INI section when parsing

func (*INI) SetSkipCommits

func (ini *INI) SetSkipCommits(skipCommits bool)

SetSkipCommits set INI.skipCommits whether skip commits when parsing

func (*INI) Write

func (ini *INI) Write(w io.Writer) error

Write try to write the INI data into an output.

type Kvmap

type Kvmap map[string]string

type SectionMap

type SectionMap map[string]Kvmap

Directories

Path Synopsis
examples
inifileparsing command

Jump to

Keyboard shortcuts

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