ini.v1: gopkg.in/go-ini/ini.v1 Index | Files

package ini

import "gopkg.in/go-ini/ini.v1"

Package ini provides INI file read and write functionality in Go.

Index

Package Files

error.go file.go ini.go key.go parser.go section.go struct.go

Constants

const (
    // DefaultSection is the name of default section. You can use this constant or the string literal.
    // In most of cases, an empty string is all you need to access the section.
    DefaultSection = "DEFAULT"
    // Deprecated: Use "DefaultSection" instead.
    DEFAULT_SECTION = DefaultSection
)

Variables

var (
    // LineBreak is the delimiter to determine or compose a new line.
    // This variable will be changed to "\r\n" automatically on Windows at package init time.
    LineBreak = "\n"

    // DefaultFormatLeft places custom spaces on the left when PrettyFormat and PrettyEqual are both disabled.
    DefaultFormatLeft = ""
    // DefaultFormatRight places custom spaces on the right when PrettyFormat and PrettyEqual are both disabled.
    DefaultFormatRight = ""

    // PrettyFormat indicates whether to align "=" sign with spaces to produce pretty output
    // or reduce all possible spaces for compact format.
    PrettyFormat = true

    // PrettyEqual places spaces around "=" sign even when PrettyFormat is false.
    PrettyEqual = false

    // DefaultHeader explicitly writes default section header.
    DefaultHeader = false

    // PrettySection indicates whether to put a line between sections.
    PrettySection = true
)

func IsErrDelimiterNotFound Uses

func IsErrDelimiterNotFound(err error) bool

IsErrDelimiterNotFound returns true if the given error is an instance of ErrDelimiterNotFound.

func MapTo Uses

func MapTo(v, source interface{}, others ...interface{}) error

MapTo maps data sources to given struct.

func MapToWithMapper Uses

func MapToWithMapper(v interface{}, mapper NameMapper, source interface{}, others ...interface{}) error

MapToWithMapper maps data sources to given struct with name mapper.

func ReflectFrom Uses

func ReflectFrom(cfg *File, v interface{}) error

ReflectFrom reflects data sources from given struct.

func ReflectFromWithMapper Uses

func ReflectFromWithMapper(cfg *File, v interface{}, mapper NameMapper) error

ReflectFromWithMapper reflects data sources from given struct with name mapper.

func StrictMapTo Uses

func StrictMapTo(v, source interface{}, others ...interface{}) error

StrictMapTo maps data sources to given struct in strict mode, which returns all possible error including value parsing error.

func StrictMapToWithMapper Uses

func StrictMapToWithMapper(v interface{}, mapper NameMapper, source interface{}, others ...interface{}) error

StrictMapToWithMapper maps data sources to given struct with name mapper in strict mode, which returns all possible error including value parsing error.

func Version Uses

func Version() string

Version returns current package version literal.

type ErrDelimiterNotFound Uses

type ErrDelimiterNotFound struct {
    Line string
}

ErrDelimiterNotFound indicates the error type of no delimiter is found which there should be one.

func (ErrDelimiterNotFound) Error Uses

func (err ErrDelimiterNotFound) Error() string

type File Uses

type File struct {

    // Should make things safe, but sometimes doesn't matter.
    BlockMode bool

    NameMapper
    ValueMapper
    // contains filtered or unexported fields
}

File represents a combination of a or more INI file(s) in memory.

func Empty Uses

func Empty() *File

Empty returns an empty file object.

func InsensitiveLoad Uses

func InsensitiveLoad(source interface{}, others ...interface{}) (*File, error)

InsensitiveLoad has exactly same functionality as Load function except it forces all section and key names to be lowercased.

func Load Uses

func Load(source interface{}, others ...interface{}) (*File, error)

Load loads and parses from INI data sources. Arguments can be mixed of file name with string type, or raw data in []byte. It will return error if list contains nonexistent files.

func LoadSources Uses

func LoadSources(opts LoadOptions, source interface{}, others ...interface{}) (_ *File, err error)

LoadSources allows caller to apply customized options for loading from data source(s).

func LooseLoad Uses

func LooseLoad(source interface{}, others ...interface{}) (*File, error)

LooseLoad has exactly same functionality as Load function except it ignores nonexistent files instead of returning error.

func ShadowLoad Uses

func ShadowLoad(source interface{}, others ...interface{}) (*File, error)

ShadowLoad has exactly same functionality as Load function except it allows have shadow keys.

func (*File) Append Uses

func (f *File) Append(source interface{}, others ...interface{}) error

Append appends one or more data sources and reloads automatically.

func (*File) ChildSections Uses

func (f *File) ChildSections(name string) []*Section

ChildSections returns a list of child sections of given section name.

func (*File) DeleteSection Uses

func (f *File) DeleteSection(name string)

DeleteSection deletes a section.

func (*File) GetSection Uses

func (f *File) GetSection(name string) (*Section, error)

GetSection returns section by given name.

func (*File) MapTo Uses

func (f *File) MapTo(v interface{}) error

MapTo maps file to given struct.

func (*File) NewRawSection Uses

func (f *File) NewRawSection(name, body string) (*Section, error)

NewRawSection creates a new section with an unparseable body.

func (*File) NewSection Uses

func (f *File) NewSection(name string) (*Section, error)

NewSection creates a new section.

func (*File) NewSections Uses

func (f *File) NewSections(names ...string) (err error)

NewSections creates a list of sections.

func (*File) ReflectFrom Uses

func (f *File) ReflectFrom(v interface{}) error

ReflectFrom reflects file from given struct.

func (*File) Reload Uses

func (f *File) Reload() (err error)

Reload reloads and parses all data sources.

func (*File) SaveTo Uses

func (f *File) SaveTo(filename string) error

SaveTo writes content to file system.

func (*File) SaveToIndent Uses

func (f *File) SaveToIndent(filename, indent string) error

SaveToIndent writes content to file system with given value indention.

func (*File) Section Uses

func (f *File) Section(name string) *Section

Section assumes named section exists and returns a zero-value when not.

func (*File) SectionStrings Uses

func (f *File) SectionStrings() []string

SectionStrings returns list of section names.

func (*File) Sections Uses

func (f *File) Sections() []*Section

Sections returns a list of Section stored in the current instance.

func (*File) StrictMapTo Uses

func (f *File) StrictMapTo(v interface{}) error

StrictMapTo maps file to given struct in strict mode, which returns all possible error including value parsing error.

func (*File) WriteTo Uses

func (f *File) WriteTo(w io.Writer) (int64, error)

WriteTo writes file content into io.Writer.

func (*File) WriteToIndent Uses

func (f *File) WriteToIndent(w io.Writer, indent string) (int64, error)

WriteToIndent writes content into io.Writer with given indention. If PrettyFormat has been set to be true, it will align "=" sign with spaces under each section.

type Key Uses

type Key struct {
    Comment string
    // contains filtered or unexported fields
}

Key represents a key under a section.

func (*Key) AddNestedValue Uses

func (k *Key) AddNestedValue(val string) error

AddNestedValue adds a nested value to the key.

func (*Key) AddShadow Uses

func (k *Key) AddShadow(val string) error

AddShadow adds a new shadow key to itself.

func (*Key) Bool Uses

func (k *Key) Bool() (bool, error)

Bool returns bool type value.

func (*Key) Duration Uses

func (k *Key) Duration() (time.Duration, error)

Duration returns time.Duration type value.

func (*Key) Float64 Uses

func (k *Key) Float64() (float64, error)

Float64 returns float64 type value.

func (*Key) Float64s Uses

func (k *Key) Float64s(delim string) []float64

Float64s returns list of float64 divided by given delimiter. Any invalid input will be treated as zero value.

func (*Key) In Uses

func (k *Key) In(defaultVal string, candidates []string) string

In always returns value without error, it returns default value if error occurs or doesn't fit into candidates.

func (*Key) InFloat64 Uses

func (k *Key) InFloat64(defaultVal float64, candidates []float64) float64

InFloat64 always returns value without error, it returns default value if error occurs or doesn't fit into candidates.

func (*Key) InInt Uses

func (k *Key) InInt(defaultVal int, candidates []int) int

InInt always returns value without error, it returns default value if error occurs or doesn't fit into candidates.

func (*Key) InInt64 Uses

func (k *Key) InInt64(defaultVal int64, candidates []int64) int64

InInt64 always returns value without error, it returns default value if error occurs or doesn't fit into candidates.

func (*Key) InTime Uses

func (k *Key) InTime(defaultVal time.Time, candidates []time.Time) time.Time

InTime always parses with RFC3339 format and returns value without error, it returns default value if error occurs or doesn't fit into candidates.

func (*Key) InTimeFormat Uses

func (k *Key) InTimeFormat(format string, defaultVal time.Time, candidates []time.Time) time.Time

InTimeFormat always parses with given format and returns value without error, it returns default value if error occurs or doesn't fit into candidates.

func (*Key) InUint Uses

func (k *Key) InUint(defaultVal uint, candidates []uint) uint

InUint always returns value without error, it returns default value if error occurs or doesn't fit into candidates.

func (*Key) InUint64 Uses

func (k *Key) InUint64(defaultVal uint64, candidates []uint64) uint64

InUint64 always returns value without error, it returns default value if error occurs or doesn't fit into candidates.

func (*Key) Int Uses

func (k *Key) Int() (int, error)

Int returns int type value.

func (*Key) Int64 Uses

func (k *Key) Int64() (int64, error)

Int64 returns int64 type value.

func (*Key) Int64s Uses

func (k *Key) Int64s(delim string) []int64

Int64s returns list of int64 divided by given delimiter. Any invalid input will be treated as zero value.

func (*Key) Ints Uses

func (k *Key) Ints(delim string) []int

Ints returns list of int divided by given delimiter. Any invalid input will be treated as zero value.

func (*Key) MustBool Uses

func (k *Key) MustBool(defaultVal ...bool) bool

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

func (*Key) MustDuration Uses

func (k *Key) MustDuration(defaultVal ...time.Duration) time.Duration

MustDuration always returns value without error, it returns zero value if error occurs.

func (*Key) MustFloat64 Uses

func (k *Key) MustFloat64(defaultVal ...float64) float64

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

func (*Key) MustInt Uses

func (k *Key) MustInt(defaultVal ...int) int

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

func (*Key) MustInt64 Uses

func (k *Key) MustInt64(defaultVal ...int64) int64

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

func (*Key) MustString Uses

func (k *Key) MustString(defaultVal string) string

MustString returns default value if key value is empty.

func (*Key) MustTime Uses

func (k *Key) MustTime(defaultVal ...time.Time) time.Time

MustTime always parses with RFC3339 format and returns value without error, it returns zero value if error occurs.

func (*Key) MustTimeFormat Uses

func (k *Key) MustTimeFormat(format string, defaultVal ...time.Time) time.Time

MustTimeFormat always parses with given format and returns value without error, it returns zero value if error occurs.

func (*Key) MustUint Uses

func (k *Key) MustUint(defaultVal ...uint) uint

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

func (*Key) MustUint64 Uses

func (k *Key) MustUint64(defaultVal ...uint64) uint64

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

func (*Key) Name Uses

func (k *Key) Name() string

Name returns name of key.

func (*Key) NestedValues Uses

func (k *Key) NestedValues() []string

NestedValues returns nested values stored in the key. It is possible returned value is nil if no nested values stored in the key.

func (*Key) RangeFloat64 Uses

func (k *Key) RangeFloat64(defaultVal, min, max float64) float64

RangeFloat64 checks if value is in given range inclusively, and returns default value if it's not.

func (*Key) RangeInt Uses

func (k *Key) RangeInt(defaultVal, min, max int) int

RangeInt checks if value is in given range inclusively, and returns default value if it's not.

func (*Key) RangeInt64 Uses

func (k *Key) RangeInt64(defaultVal, min, max int64) int64

RangeInt64 checks if value is in given range inclusively, and returns default value if it's not.

func (*Key) RangeTime Uses

func (k *Key) RangeTime(defaultVal, min, max time.Time) time.Time

RangeTime checks if value with RFC3339 format is in given range inclusively, and returns default value if it's not.

func (*Key) RangeTimeFormat Uses

func (k *Key) RangeTimeFormat(format string, defaultVal, min, max time.Time) time.Time

RangeTimeFormat checks if value with given format is in given range inclusively, and returns default value if it's not.

func (*Key) SetValue Uses

func (k *Key) SetValue(v string)

SetValue changes key value.

func (*Key) StrictFloat64s Uses

func (k *Key) StrictFloat64s(delim string) ([]float64, error)

StrictFloat64s returns list of float64 divided by given delimiter or error on first invalid input.

func (*Key) StrictInt64s Uses

func (k *Key) StrictInt64s(delim string) ([]int64, error)

StrictInt64s returns list of int64 divided by given delimiter or error on first invalid input.

func (*Key) StrictInts Uses

func (k *Key) StrictInts(delim string) ([]int, error)

StrictInts returns list of int divided by given delimiter or error on first invalid input.

func (*Key) StrictTimes Uses

func (k *Key) StrictTimes(delim string) ([]time.Time, error)

StrictTimes parses with RFC3339 format and returns list of time.Time divided by given delimiter or error on first invalid input.

func (*Key) StrictTimesFormat Uses

func (k *Key) StrictTimesFormat(format, delim string) ([]time.Time, error)

StrictTimesFormat parses with given format and returns list of time.Time divided by given delimiter or error on first invalid input.

func (*Key) StrictUint64s Uses

func (k *Key) StrictUint64s(delim string) ([]uint64, error)

StrictUint64s returns list of uint64 divided by given delimiter or error on first invalid input.

func (*Key) StrictUints Uses

func (k *Key) StrictUints(delim string) ([]uint, error)

StrictUints returns list of uint divided by given delimiter or error on first invalid input.

func (*Key) String Uses

func (k *Key) String() string

String returns string representation of value.

func (*Key) Strings Uses

func (k *Key) Strings(delim string) []string

Strings returns list of string divided by given delimiter.

func (*Key) StringsWithShadows Uses

func (k *Key) StringsWithShadows(delim string) []string

StringsWithShadows returns list of string divided by given delimiter. Shadows will also be appended if any.

func (*Key) Time Uses

func (k *Key) Time() (time.Time, error)

Time parses with RFC3339 format and returns time.Time type value.

func (*Key) TimeFormat Uses

func (k *Key) TimeFormat(format string) (time.Time, error)

TimeFormat parses with given format and returns time.Time type value.

func (*Key) Times Uses

func (k *Key) Times(delim string) []time.Time

Times parses with RFC3339 format and returns list of time.Time divided by given delimiter. Any invalid input will be treated as zero value (0001-01-01 00:00:00 +0000 UTC).

func (*Key) TimesFormat Uses

func (k *Key) TimesFormat(format, delim string) []time.Time

TimesFormat parses with given format and returns list of time.Time divided by given delimiter. Any invalid input will be treated as zero value (0001-01-01 00:00:00 +0000 UTC).

func (*Key) Uint Uses

func (k *Key) Uint() (uint, error)

Uint returns uint type valued.

func (*Key) Uint64 Uses

func (k *Key) Uint64() (uint64, error)

Uint64 returns uint64 type value.

func (*Key) Uint64s Uses

func (k *Key) Uint64s(delim string) []uint64

Uint64s returns list of uint64 divided by given delimiter. Any invalid input will be treated as zero value.

func (*Key) Uints Uses

func (k *Key) Uints(delim string) []uint

Uints returns list of uint divided by given delimiter. Any invalid input will be treated as zero value.

func (*Key) ValidFloat64s Uses

func (k *Key) ValidFloat64s(delim string) []float64

ValidFloat64s returns list of float64 divided by given delimiter. If some value is not float, then it will not be included to result list.

func (*Key) ValidInt64s Uses

func (k *Key) ValidInt64s(delim string) []int64

ValidInt64s returns list of int64 divided by given delimiter. If some value is not 64-bit integer, then it will not be included to result list.

func (*Key) ValidInts Uses

func (k *Key) ValidInts(delim string) []int

ValidInts returns list of int divided by given delimiter. If some value is not integer, then it will not be included to result list.

func (*Key) ValidTimes Uses

func (k *Key) ValidTimes(delim string) []time.Time

ValidTimes parses with RFC3339 format and returns list of time.Time divided by given delimiter.

func (*Key) ValidTimesFormat Uses

func (k *Key) ValidTimesFormat(format, delim string) []time.Time

ValidTimesFormat parses with given format and returns list of time.Time divided by given delimiter.

func (*Key) ValidUint64s Uses

func (k *Key) ValidUint64s(delim string) []uint64

ValidUint64s returns list of uint64 divided by given delimiter. If some value is not 64-bit unsigned integer, then it will not be included to result list.

func (*Key) ValidUints Uses

func (k *Key) ValidUints(delim string) []uint

ValidUints returns list of uint divided by given delimiter. If some value is not unsigned integer, then it will not be included to result list.

func (*Key) Validate Uses

func (k *Key) Validate(fn func(string) string) string

Validate accepts a validate function which can return modifed result as key value.

func (*Key) Value Uses

func (k *Key) Value() string

Value returns raw value of key for performance purpose.

func (*Key) ValueWithShadows Uses

func (k *Key) ValueWithShadows() []string

ValueWithShadows returns raw values of key and its shadows if any.

type LoadOptions Uses

type LoadOptions struct {
    // Loose indicates whether the parser should ignore nonexistent files or return error.
    Loose bool
    // Insensitive indicates whether the parser forces all section and key names to lowercase.
    Insensitive bool
    // IgnoreContinuation indicates whether to ignore continuation lines while parsing.
    IgnoreContinuation bool
    // IgnoreInlineComment indicates whether to ignore comments at the end of value and treat it as part of value.
    IgnoreInlineComment bool
    // SkipUnrecognizableLines indicates whether to skip unrecognizable lines that do not conform to key/value pairs.
    SkipUnrecognizableLines bool
    // AllowBooleanKeys indicates whether to allow boolean type keys or treat as value is missing.
    // This type of keys are mostly used in my.cnf.
    AllowBooleanKeys bool
    // AllowShadows indicates whether to keep track of keys with same name under same section.
    AllowShadows bool
    // AllowNestedValues indicates whether to allow AWS-like nested values.
    // Docs: http://docs.aws.amazon.com/cli/latest/topic/config-vars.html#nested-values
    AllowNestedValues bool
    // AllowPythonMultilineValues indicates whether to allow Python-like multi-line values.
    // Docs: https://docs.python.org/3/library/configparser.html#supported-ini-file-structure
    // Relevant quote:  Values can also span multiple lines, as long as they are indented deeper
    // than the first line of the value.
    AllowPythonMultilineValues bool
    // SpaceBeforeInlineComment indicates whether to allow comment symbols (\# and \;) inside value.
    // Docs: https://docs.python.org/2/library/configparser.html
    // Quote: Comments may appear on their own in an otherwise empty line, or may be entered in lines holding values or section names.
    // In the latter case, they need to be preceded by a whitespace character to be recognized as a comment.
    SpaceBeforeInlineComment bool
    // UnescapeValueDoubleQuotes indicates whether to unescape double quotes inside value to regular format
    // when value is surrounded by double quotes, e.g. key="a \"value\"" => key=a "value"
    UnescapeValueDoubleQuotes bool
    // UnescapeValueCommentSymbols indicates to unescape comment symbols (\# and \;) inside value to regular format
    // when value is NOT surrounded by any quotes.
    // Note: UNSTABLE, behavior might change to only unescape inside double quotes but may noy necessary at all.
    UnescapeValueCommentSymbols bool
    // UnparseableSections stores a list of blocks that are allowed with raw content which do not otherwise
    // conform to key/value pairs. Specify the names of those blocks here.
    UnparseableSections []string
    // KeyValueDelimiters is the sequence of delimiters that are used to separate key and value. By default, it is "=:".
    KeyValueDelimiters string
    // PreserveSurroundedQuote indicates whether to preserve surrounded quote (single and double quotes).
    PreserveSurroundedQuote bool
}

LoadOptions contains all customized options used for load data source(s).

type NameMapper Uses

type NameMapper func(string) string

NameMapper represents a ini tag name mapper.

var (
    // AllCapsUnderscore converts to format ALL_CAPS_UNDERSCORE.
    AllCapsUnderscore NameMapper = func(raw string) string {
        newstr := make([]rune, 0, len(raw))
        for i, chr := range raw {
            if isUpper := 'A' <= chr && chr <= 'Z'; isUpper {
                if i > 0 {
                    newstr = append(newstr, '_')
                }
            }
            newstr = append(newstr, unicode.ToUpper(chr))
        }
        return string(newstr)
    }
    // TitleUnderscore converts to format title_underscore.
    TitleUnderscore NameMapper = func(raw string) string {
        newstr := make([]rune, 0, len(raw))
        for i, chr := range raw {
            if isUpper := 'A' <= chr && chr <= 'Z'; isUpper {
                if i > 0 {
                    newstr = append(newstr, '_')
                }
                chr -= ('A' - 'a')
            }
            newstr = append(newstr, chr)
        }
        return string(newstr)
    }
)

Built-in name getters.

type Section Uses

type Section struct {
    Comment string
    // contains filtered or unexported fields
}

Section represents a config section.

func (*Section) Body Uses

func (s *Section) Body() string

Body returns rawBody of Section if the section was marked as unparseable. It still follows the other rules of the INI format surrounding leading/trailing whitespace.

func (*Section) ChildSections Uses

func (s *Section) ChildSections() []*Section

ChildSections returns a list of child sections of current section. For example, "[parent.child1]" and "[parent.child12]" are child sections of section "[parent]".

func (*Section) DeleteKey Uses

func (s *Section) DeleteKey(name string)

DeleteKey deletes a key from section.

func (*Section) GetKey Uses

func (s *Section) GetKey(name string) (*Key, error)

GetKey returns key in section by given name.

func (*Section) HasKey Uses

func (s *Section) HasKey(name string) bool

HasKey returns true if section contains a key with given name.

func (*Section) HasValue Uses

func (s *Section) HasValue(value string) bool

HasValue returns true if section contains given raw value.

func (*Section) Haskey Uses

func (s *Section) Haskey(name string) bool

Deprecated: Use "HasKey" instead.

func (*Section) Key Uses

func (s *Section) Key(name string) *Key

Key assumes named Key exists in section and returns a zero-value when not.

func (*Section) KeyStrings Uses

func (s *Section) KeyStrings() []string

KeyStrings returns list of key names of section.

func (*Section) Keys Uses

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

Keys returns list of keys of section.

func (*Section) KeysHash Uses

func (s *Section) KeysHash() map[string]string

KeysHash returns keys hash consisting of names and values.

func (*Section) MapTo Uses

func (s *Section) MapTo(v interface{}) error

MapTo maps section to given struct.

func (*Section) Name Uses

func (s *Section) Name() string

Name returns name of Section.

func (*Section) NewBooleanKey Uses

func (s *Section) NewBooleanKey(name string) (*Key, error)

NewBooleanKey creates a new boolean type key to given section.

func (*Section) NewKey Uses

func (s *Section) NewKey(name, val string) (*Key, error)

NewKey creates a new key to given section.

func (*Section) ParentKeys Uses

func (s *Section) ParentKeys() []*Key

ParentKeys returns list of keys of parent section.

func (*Section) ReflectFrom Uses

func (s *Section) ReflectFrom(v interface{}) error

ReflectFrom reflects secion from given struct.

func (*Section) SetBody Uses

func (s *Section) SetBody(body string)

SetBody updates body content only if section is raw.

func (*Section) StrictMapTo Uses

func (s *Section) StrictMapTo(v interface{}) error

StrictMapTo maps section to given struct in strict mode, which returns all possible error including value parsing error.

type ValueMapper Uses

type ValueMapper func(string) string

ValueMapper represents a mapping function for values, e.g. os.ExpandEnv

Package ini imports 15 packages (graph) and is imported by 3 packages. Updated 2019-08-05. Refresh now. Tools for package owners.