ini

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2018 License: MIT Imports: 9 Imported by: 35

README

Overview

This is a golang library for reading/writing the .ini format file. The description on .ini file can be found at https://en.wikipedia.org/wiki/INI_file

Supported .ini format

A .ini file contains one or more sections and each section contains one or more key/value pair. Following is an example of .ini file

# this is a comment line
; this is also a comment line

[section1]

key1 = value1

[section2]

key2 = value2

Comments

Comments line

A comments line is started with char '#' or ';' and it will be ignored when processing the .ini file.


# this is a comment line
; this is also a comment line

inline comments

A comment can be appended in a tail of line. The inline comments must be started with ';' or '#' and its previous char must be a space.

[section1]
key1 = value1 ;this is a inline comment
key2 = value2;this is not a inline comment

Multiline value

if a value is multiple line value, the value can be put between """ and """, an example:


[section1]

multi-line-key = """this is a multi-line example,
multiple line can be put in a value,
this is multiple line is just for test"""

single-line-key = this is a normal value

Continuation line

If a line is too long, user can devide one line to multiple line and on the end of line the char '\' should be put:

[section1]
key1 = this line is too long, \
we need to write it to multiple line, \
but actually it is one line from the point of user

Escape char

This library supports the escape char, the escape char is started with char \

Common escape sequences Sequence Meaning
\\ \ (a single backslash, escaping the escape character)
\0 Null character
\a Bell/Alert/Audible
\b Backspace, Bell character for some applications
\t Tab character
\r Carriage return
\n Line feed
\; Semicolon
\# Number sign
\= Equals sign
\: Colon
\x???? Unicode character with hexadecimal code point

Environemnt variable support

Environment variable can be embeded in the value of the key and the environment variable will be replaced. For example:

[section1]
key1 = this value has env ${HOME}
key2 = this value has env with default ${SOME_ENV:-test},hihi

In the above example, the environment variable HOME is in the value of key1. So if the value of environment variable HOME is "/home/test", the value of key1 is "this value has env /home/test".

For the key2, the environemnt SOME_ENV is included and if the environment variable SOME_ENV does not exist, its value will be "test" otherwise it will be the value of SOME_ENV environment variable.

API

import the library

The go-ini library should be imported before using this library:

import (
  ini "github.com/ochinchina/go-ini"
)

Load .ini file

.ini format file or string can be loaded by the method:

Load from a file
//Load the .ini from a file
ini := ini.Load( "fileName" )

Load from a string or byte array in .ini format
ini_str := `[section1]
key1 = value1
key2 = value 2
`

ini := ini.Load( ini_str )
//load from a byte array

ini = ini.Load( []byte(ini_str) )

Load from a io.Reader

var reader io.Reader = ...

ini := ini.Load( reader )

Load .ini from multiple source

The Load() method can load .ini from multiple mixed sources.

//load multiple sources: fileName, string, reader and byte array in one statement

ini := ini.Load( "fileName", ini_str, reader )
Load the .ini in Ini object

The Ini class also provide a method named Load(), this method can be called multiple times and the later loaded .ini will be appended to the Ini object.

//first load the .ini from a file
ini := ini.Load( "fileName" )

//append the .ini from string to the ini object
ini_str := `[section1]
key1 = value1
key2 = value 2
`
ini.Load( ini_str )

//append the .ini from a reader to the ini object
var reader io.Reader = ...
ini.Load( reader )

Access the value of key in the .ini file

After loading the .ini from a file/string/reader, we can access a keya under a section. This library provides three level API to access the value of a key in a section.

Access the value of key in Ini class level

The value of key can be accessed in Ini class level.

ini := ini.Load(...)

value, err := ini.GetValue( "section1", "key1")

// if err is nil, the value is ok
if err == nil {
  //the value exists and DO something according to the value
}

Sometimes we need to provide a default value if the key in the section does not exist, at this time the user can provide a default value by GetValueWithDefault() method.

ini := ini.Load(...)

//if the section1 or key1 does not exist, return a default value(empty string)
value := ini.GetValueWithDefault( "section1", "key1", "" )
Access the value of key in Section class level

Call the GetSection() method by the section name on the Ini object at frist, and then call GetValue() on the section to get the value of key.

ini := ini.Load(...)

section, err := ini.GetSection( "section1" )

if err == nil {
  value, err := section.GetValue( "key1" )
  if err == nil {
    //the value of key1 exists
  }
}

The method GetValueWithDefault() ask user provide a default value if the key under section does not exist, the user provided default value will be returned.

ini := ini.Load(...)

section, err := ini.GetSection( "section1" )

if err == nil {
  //get the value of key1 and if the key1 does not exists, return the default empty string
  value := section.GetValueWithDefault("key1", "" )
}
Access the value of key in Key class level

The value of a key can be acccessed in the Key class level also. The method Key() on the section with keyname can be called even if the key does not exist. After getting a Key object, user can call Value() method to get the value of key.

ini := ini.Load(...)

section, err := ini.GetSection( "section1" )
if err == nil {
  //the Key() method always returns a Key object even if the key does not exist
  value, err := section.Key( "key1" ).Value()
  if err == nul {
    //the value in key1 exists
  }
}

User can provide a default value to method ValueWithDefault() on the Key object to get the value of key and if the key does not exist the default value will be returned.

ini := ini.Load(...)

section, err := ini.GetSection( "section1" )
if err == nil {
  //the Key() method always returns a Key object even if the key does not exist
  value:= section.Key( "key1" ).ValueWithDefault("")
}

Convert the string value to desired types

Except for getting a string value of a key, you can also ask the library convert the string to one of following types:

  • bool
  • int
  • int64
  • uint64
  • float32
  • float64

For each data type, this library provides two methods GetXXX() and GetXXXWithDefault() on the Ini&Section class level where the XXX stands for the Bool, Int, Int64, Uint64, Float32, Float64.

An example to ask the library convert the key to a int data type in Ini level:


ini := ini.Load(...)

value, err := ini.GetInt( "section1", "key1" )

if err == nil {
  //at this time, the value of key1 exists and can be converted to integer
}

value = ini.GetIntWithDefault( "section1", "key1", 0 )

An example to ask the library convert the key to a int data type in Section level:


ini := ini.Load(...)

section, err := ini.GetSection( "section1" )

if err == nil {
  value, err = section.GetInt( "key1" )
  if err == nil {
    //at this time the key1 exists and its value can be converted to int
  }
  
  value = section.GetIntWithDefault("key1", 0 )
}

An example to ask the library convert the key to a int data type in Key level:


ini := ini.Load(...)
section, err := ini.GetSection( "section1" )
if err == nil {
  value, err := section.Key( "key1" ).Int()
  if err == nil {
    //at this time the key1 exists and its value can be converted to int
  }
  
  //get with default value
  value = section.Key( "key1" ).IntWithDefault( 0 )
}

Add the key&value to .ini file

This library also provides API to add key&value to the .ini file.


ini := ini.NewIni()

section := ini.NewSection( "section1" )
section.Add( "key1", "value1" )

Save the .ini to the file

User can call the Write() method on Ini object to write the .ini contents to a io.Writer


ini := ini.NewIni()
section := ini.NewSection( "section1" )
section.Add( "key1", "value1" )

buf := bytes.NewBufferString("")
ini.Write( buf )

If want to write to the file, there is a convinent API WriteToFile() with filename on the Ini object to write the .ini content to the file.


ini := ini.NewIni()
section := ini.NewSection( "section1" )
section.Add( "key1", "value1" )

ini.WriteToFile( "test.ini" )

Documentation

Overview

A golang implemented library to read/write .ini format files.

With this library, you can load the .ini file a string, a byte array, a file and a io.Reader.

import (
    ini "github.com/ochinchina/go-ini"
)

func main() {
    //load from .ini file
    ini := ini.Load( "myfile.ini")
    //load from .ini format string
    str_data := "[section1]\nkey1=value1\n[section2]\nkey2=value2"
    ini = ini.Load( str_data )

    //load .ini format byte array
    ini = ini.Load( []byte(str_data) )

    //load from io.Reader
    var reader io.Reader = ...

    ini = ini.Load( reader )

    //load from multiple source in one Load method
    ini = ini.Load( "myfile.ini", reader, str_data, bytes_data )
}

The loaded Ini includes sections, you can access section:

//get all the sections in the .ini
var sections []*Section = ini.Sections()

//get a section by Name
var section *Section = ini.GetSection( sectionName )

Then the key in a section can be accessed by method GetXXX() and GetXXXWithDefault(defValue):

//get the value of key
value, err := section.GetValue( "key1")
value = section.GetValueWithDefault("key1", "")

//get value of key as int
i, err := section.GetInt( "key2" )
i = section.GetIntWithDefault( "key2" )

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetDefaultSectionName

func SetDefaultSectionName(defSectionName string)

Types

type Ini

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

manage all the sections and their key values defined in the .ini file

func Load

func Load(sources ...interface{}) *Ini

Load the .ini from one of following resource:

  • file
  • string in .ini format
  • byte array in .ini format
  • io.Reader a reader to load .ini content

One or more source can be provided in this Load method, such as:

var reader1 io.Reader = ...
var reader2 io.Reader = ...
ini.Load( "./my.ini", "[section]\nkey=1", "./my2.ini", reader1, reader2 )

func NewIni

func NewIni() *Ini

func (*Ini) AddSection

func (ini *Ini) AddSection(section *Section)

add a section to the .ini file and overwrite the exist section with same name

func (*Ini) GetBool

func (ini *Ini) GetBool(sectionName, key string) (bool, error)

get the value of key in section as bool. return true if the value of the key is one of following(case insensitive):

  • true
  • yes
  • t
  • y
  • 1

return false for all other values

func (*Ini) GetBoolWithDefault

func (ini *Ini) GetBoolWithDefault(sectionName, key string, defValue bool) bool

get the value of key as bool and return the default value if the section in the .ini file or key in the section does not exist

func (*Ini) GetDefaultSectionName

func (ini *Ini) GetDefaultSectionName() string

func (*Ini) GetFloat32

func (ini *Ini) GetFloat32(sectionName, key string) (float32, error)

get the value of key in the section as float32

func (*Ini) GetFloat32WithDefault

func (ini *Ini) GetFloat32WithDefault(sectionName, key string, defValue float32) float32

get the value of key in the section as float32 and return defValue if the section in the .ini file or key in the section does not exist

func (*Ini) GetFloat64

func (ini *Ini) GetFloat64(sectionName, key string) (float64, error)

get the value of key in the section as float64

func (*Ini) GetFloat64WithDefault

func (ini *Ini) GetFloat64WithDefault(sectionName, key string, defValue float64) float64

get the value of key in the section as float64 and return defValue if the section in the .ini file or key in the section does not exist

func (*Ini) GetInt

func (ini *Ini) GetInt(sectionName, key string) (int, error)

get the value of key in the section as int

func (*Ini) GetInt64

func (ini *Ini) GetInt64(sectionName, key string) (int64, error)

get the value of key in the section as int64

func (*Ini) GetInt64WithDefault

func (ini *Ini) GetInt64WithDefault(sectionName, key string, defValue int64) int64

get the value of key in the section as int64 and return defValue if the section in the .ini file or key in the section does not exist

func (*Ini) GetIntWithDefault

func (ini *Ini) GetIntWithDefault(sectionName, key string, defValue int) int

get the value of key in the section as int and return defValue if the section in the .ini file or key in the section does not exist

func (*Ini) GetSection

func (ini *Ini) GetSection(name string) (*Section, error)

get section by section name

return: section or nil

func (*Ini) GetUint

func (ini *Ini) GetUint(sectionName, key string) (uint, error)

get the value of key in the section as uint

func (*Ini) GetUint64

func (ini *Ini) GetUint64(sectionName, key string) (uint64, error)

get the value of key in the section as uint64

func (*Ini) GetUint64WithDefault

func (ini *Ini) GetUint64WithDefault(sectionName, key string, defValue uint64) uint64

get the value of key in the section as uint64 and return defValue if the section in the .ini file or key in the section does not exist

func (*Ini) GetUintWithDefault

func (ini *Ini) GetUintWithDefault(sectionName, key string, defValue uint) uint

get the value of key in the section as int and return defValue if the section in the .ini file or key in the section does not exist

func (*Ini) GetValue

func (ini *Ini) GetValue(sectionName, key string) (string, error)

get the value of key in section

func (*Ini) GetValueWithDefault

func (ini *Ini) GetValueWithDefault(sectionName, key string, defValue string) string

get the value of the key in section if the key does not exist, return the defValue

func (*Ini) HasKey

func (ini *Ini) HasKey(sectionName, key string) bool

check if a key exists or not in the Ini

return true if the key in section exists

func (*Ini) HasSection

func (ini *Ini) HasSection(name string) bool

return true if the section with name exists return false if the section with name does not exist

func (*Ini) Load

func (ini *Ini) Load(sources ...interface{})

Load from the sources, the source can be one of:

  • fileName
  • a string includes .ini
  • io.Reader the reader to load the .ini contents
  • byte array incldues .ini content

func (*Ini) LoadBytes

func (ini *Ini) LoadBytes(content []byte)

load .ini from a byte array which contains the .ini formated content

func (*Ini) LoadFile

func (ini *Ini) LoadFile(fileName string)

Load ini file from file named fileName

func (*Ini) LoadReader

func (ini *Ini) LoadReader(reader io.Reader)

Explicitly loads .ini from a reader

func (*Ini) LoadString

func (ini *Ini) LoadString(content string)

load ini from the content which contains the .ini formated string

func (*Ini) NewSection

func (ini *Ini) NewSection(name string) *Section

create a new section if the section with name does not exist or return the exist one if the section with name already exists

func (*Ini) Sections

func (ini *Ini) Sections() []*Section

Get all the section name in the ini

return all the section names

func (*Ini) SetDefaultSectionName

func (ini *Ini) SetDefaultSectionName(defSectionName string)

func (*Ini) String

func (ini *Ini) String() string

func (*Ini) Write

func (ini *Ini) Write(writer io.Writer) error

write the content of the .ini in the .ini file format, e.g. in following format:

[section1]
key1 = value1
key2 = value2
[section2]
key3 = value3
key4 = value4

func (*Ini) WriteToFile

func (ini *Ini) WriteToFile(fileName string) error

Write the conents of ini to a file

type Key

type Key interface {
	// get name of the key
	Name() string

	// get value of the key
	Value() (string, error)

	//get the value of key and return defValue if
	//the value does not exist
	ValueWithDefault(defValue string) string

	// get the value as bool
	// return true if the value is one of following(case insensitive):
	// - true
	// - yes
	// - T
	// - Y
	// - 1
	// Any other value will return false
	Bool() (bool, error)

	// get the value as bool and return the defValue if the
	// value of the key does not exist
	BoolWithDefault(defValue bool) bool
	// get the value as int
	Int() (int, error)

	// get value as int and return defValue if the
	// value of the key does not exist
	IntWithDefault(defValue int) int

	//get value as uint
	Uint() (uint, error)

	//get value as uint and return defValue if the
	//key does not exist or it is not uint format
	UintWithDefault(defValue uint) uint

	// get the value as int64
	Int64() (int64, error)

	// get the value as int64 and return defValue
	// if the value of the key does not exist
	Int64WithDefault(defValue int64) int64

	// get the value as uint64
	Uint64() (uint64, error)

	// get the value as uint64 and return defValue
	// if the value of the key does not exist
	Uint64WithDefault(defValue uint64) uint64

	// get the value as float32
	Float32() (float32, error)

	// get the value as float32 and return defValue
	// if the value of the key does not exist
	Float32WithDefault(defValue float32) float32

	// get the value as float64
	Float64() (float64, error)

	// get the value as the float64 and return defValue
	// if the value of the key does not exist
	Float64WithDefault(defValue float64) float64

	// return a string as "key=value" format
	// and if no value return empty string
	String() string
}

represents the <key, value> pair stored in the section of the .ini file

type Properties

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

func NewProperties

func NewProperties() *Properties

func (*Properties) GetBool

func (p *Properties) GetBool(key string) (bool, error)

func (*Properties) GetBoolWithDefault

func (p *Properties) GetBoolWithDefault(key string, defValue bool) bool

func (*Properties) GetFloat32

func (p *Properties) GetFloat32(key string) (float32, error)

func (*Properties) GetFloat32WithDefault

func (p *Properties) GetFloat32WithDefault(key string, defValue float32) float32

func (*Properties) GetFloat64

func (p *Properties) GetFloat64(key string) (float64, error)

func (*Properties) GetFloat64WithDefault

func (p *Properties) GetFloat64WithDefault(key string, defValue float64) float64

func (*Properties) GetInt

func (p *Properties) GetInt(key string) (int, error)

func (*Properties) GetInt64

func (p *Properties) GetInt64(key string) (int64, error)

func (*Properties) GetInt64WithDefault

func (p *Properties) GetInt64WithDefault(key string, defValue int64) int64

func (*Properties) GetIntWithDefault

func (p *Properties) GetIntWithDefault(key string, defValue int) int

func (*Properties) GetProperty

func (p *Properties) GetProperty(key string) (string, error)

func (*Properties) GetPropertyWithDefault

func (p *Properties) GetPropertyWithDefault(key string, defValue string) string

func (*Properties) GetUint

func (p *Properties) GetUint(key string) (uint, error)

func (*Properties) GetUint64

func (p *Properties) GetUint64(key string) (uint64, error)

func (*Properties) GetUint64WithDefault

func (p *Properties) GetUint64WithDefault(key string, defValue uint64) uint64

func (*Properties) GetUintWithDefault

func (p *Properties) GetUintWithDefault(key string, defValue uint) uint

func (*Properties) Load

func (p *Properties) Load(sources ...interface{})

type Section

type Section struct {
	//Name of the section
	Name string
	// contains filtered or unexported fields
}

manages all the key/value defined in the .ini file format

func NewSection

func NewSection(name string) *Section

construct a new section with section name

func (*Section) Add

func (section *Section) Add(key, value string)

add key/value to the section and overwrite the old one

func (*Section) GetBool

func (section *Section) GetBool(key string) (bool, error)

Get the value of key as bool, it will return true if the value of the key is one of following( case insensitive):

  • true
  • yes
  • t
  • y
  • 1

func (*Section) GetBoolWithDefault

func (section *Section) GetBoolWithDefault(key string, defValue bool) bool

Get the value of key as bool and if the key does not exist, return the default value

func (*Section) GetFloat32

func (section *Section) GetFloat32(key string) (float32, error)

Get the value of the key as float32

func (*Section) GetFloat32WithDefault

func (section *Section) GetFloat32WithDefault(key string, defValue float32) float32

Get the value of the key as float32 and if the key does not exist return the default value

func (*Section) GetFloat64

func (section *Section) GetFloat64(key string) (float64, error)

Get the value of the key as float64

func (*Section) GetFloat64WithDefault

func (section *Section) GetFloat64WithDefault(key string, defValue float64) float64

Get the value of the key as float64 and if the key does not exist return the default value

func (*Section) GetInt

func (section *Section) GetInt(key string) (int, error)

Get the value of the key as int

func (*Section) GetInt64

func (section *Section) GetInt64(key string) (int64, error)

Get the value of the key as int64

func (*Section) GetInt64WithDefault

func (section *Section) GetInt64WithDefault(key string, defValue int64) int64

Get the value of the key as int64 and if the key does not exist return the default value

func (*Section) GetIntWithDefault

func (section *Section) GetIntWithDefault(key string, defValue int) int

Get the value of the key as int and if the key does not exist return the default value

func (*Section) GetUint

func (section *Section) GetUint(key string) (uint, error)

Get the value of the key as uint

func (*Section) GetUint64

func (section *Section) GetUint64(key string) (uint64, error)

Get the value of the key as uint64

func (*Section) GetUint64WithDefault

func (section *Section) GetUint64WithDefault(key string, defValue uint64) uint64

Get the value of the key as uint64 and if the key does not exist return the default value

func (*Section) GetUintWithDefault

func (section *Section) GetUintWithDefault(key string, defValue uint) uint

Get the value of the key as int and if the key does not exist return the default value

func (*Section) GetValue

func (section *Section) GetValue(key string) (string, error)

Get value of key as string

func (*Section) GetValueWithDefault

func (section *Section) GetValueWithDefault(key string, defValue string) string

Get value of key and if the key does not exist, return the defValue

func (*Section) HasKey

func (section *Section) HasKey(key string) bool

check if the key is in the section

return true if the section contains the key

func (*Section) Key

func (section *Section) Key(key string) Key

Get the key.

This method can be called even if the key is not in the section.

func (*Section) Keys

func (section *Section) Keys() []Key

Get all the keys in the section

return: all keys in the section

func (*Section) String

func (section *Section) String() string

convert the section content to the .ini section format, so the section content will be converted to following format:

[sectionx]
key1 = value1
key2 = value2

func (*Section) Write

func (section *Section) Write(writer io.Writer) error

write the section content to the writer with .ini section format.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳