goini

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

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

Go to latest
Published: Jun 19, 2021 License: BSD-3-Clause Imports: 8 Imported by: 0

README

goini

Build Status Coverage Status

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.

goini is designed to be simple, flexible, efficient. It

  1. Supports the standard INI format
  2. Supports section
  3. Supports parsing INI file from local disk
  4. Supports parsing INI configuration data from memory
  5. Supports parsing data which are key/value pairs in the form of various separators NOT only \n
  6. Supports UTF8 encoding
  7. Supports comments which has a leading character ; or #
  8. Supports cascading inheritance
  9. Only depends standard Golang libraries
  10. Has 100% test coverage

Importing

import github.com/zieckey/goini

Usage

Example 1 : Parses 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 : Parses the memory data with similar 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
}

key := "a"
v, ok := ini.Get(key)
if ok {
    fmt.Printf("The value of %v is [%v]\n", key, v) // Output : The value of a is [av]
}

key = "c"
v, ok = ini.Get(key)
if ok {
    fmt.Printf("The value of %v is [%v]\n", key, v) // Output : The value of c is [cv]
}

Example 3 : Parses an inherited INI file

Assume we have a large project which has several production environments. Each production environment has its own configuration. But there is tiny difference between these configurations of the standalone production environments. So we use a common INI configuration to store the common configurations. And each production environment inherits from this common INI configuration.

The common.ini is bellow:

product=common
combo=common
debug=0

version=0.0.0.0
encoding=0

[sss]
a = aval
b = bval

The project1.ini is the configuration of project #1 as below:

inherited_from=common.ini

;the following config will override the values inherited from common.ini
product=project1
combo=test
debug=1

local=0
mid=c4ca4238a0b923820dcc509a6f75849b

[sss]
a = project1-aval
c = project1-cval

If we use goini.LoadInheritedINI("project1.ini") is the same as we have the following INI configuration:

product=project1
combo=test
debug=1

local=0
mid=c4ca4238a0b923820dcc509a6f75849b

version=0.0.0.0
encoding=0

[sss]
a = project1-aval
c = project1-cval

The value of the key product has been overwritten by value project1. The value of the key a in section sss has been overwritten by value project1-aval.

Documentation

Index

Constants

View Source
const (
	DefaultSection           = ""
	DefaultLineSeparator     = "\n"
	DefaultKeyValueSeparator = "="
)
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 inherits 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) (string, 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) (bool, 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) (float64, bool)

GetFloat gets value as float64

func (*INI) GetInt

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

GetInt gets 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) GetSections

func (ini *INI) GetSections() []string

Added by UndeadIndustries to simply get sections.

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 parses 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) ParseFrom

func (ini *INI) ParseFrom(r io.Reader, lineSep, kvSep string) error

ParseFrom reads all the data from reader r 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 clears 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) (bool, bool)

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

func (*INI) SectionGetFloat

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

SectionGetFloat gets value as float64

func (*INI) SectionGetInt

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

SectionGetInt gets value as int

func (*INI) SectionSet

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

SectionSet stores 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 stores 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 stores 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 stores 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 stores 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 stores 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 stores 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 sets INI.parseSection whether to process the INI section when parsing

func (*INI) SetSkipCommits

func (ini *INI) SetSkipCommits(skipCommits bool)

SetSkipCommits sets INI.skipCommits whether to skip commits when parsing

func (*INI) SetTrimQuotes

func (ini *INI) SetTrimQuotes(v bool)

SetTrimQuotes sets INI.trimQuotes whether to trim quotation marks of the value when parsing

func (*INI) Write

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

Write tries 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

Jump to

Keyboard shortcuts

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