conf

package
v0.0.0-...-aca48a9 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2016 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

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

Index

Constants

View Source
const (
	DEFAULT_SECTION = "DEFAULT"
)

Variables

View Source
var (
	LineBreak = "\n"

	// Write spaces around "=" to look better.
	PrettyFormat = true
)

Functions

func MapTo

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

MapTo maps data sources to given struct.

func MapToWithMapper

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

MapTo maps data sources to given struct with name mapper.

func ReflectFrom

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

ReflectFrom reflects data sources from given struct.

func ReflectFromWithMapper

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

ReflectFrom reflects data sources from given struct with name mapper.

func Version

func Version() string

Types

type File

type File struct {
	// Should make things safe, but sometimes doesn't matter.
	BlockMode bool

	NameMapper
	// contains filtered or unexported fields
}

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

func Empty

func Empty() *File

Empty returns an empty file object.

func Load

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

Load loads and parses from INI data sources. Arguments can be mixed of file name with string type, or raw data in []byte.

func (*File) Append

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

Append appends one or more data sources and reloads automatically.

func (*File) DeleteSection

func (f *File) DeleteSection(name string)

DeleteSection deletes a section.

func (*File) GetSection

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

GetSection returns section by given name.

func (*File) MapTo

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

MapTo maps file to given struct.

func (*File) NewSection

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

NewSection creates a new section.

func (*File) NewSections

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

NewSections creates a list of sections.

func (*File) ReflectFrom

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

ReflectFrom reflects file from given struct.

func (*File) Reload

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

Reload reloads and parses all data sources.

func (*File) SaveTo

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

SaveTo writes content to file system.

func (*File) SaveToIndent

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

SaveToIndent writes content to file system with given value indention.

func (*File) Section

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

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

func (*File) SectionStrings

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

SectionStrings returns list of section names.

func (*File) Sections

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

Section returns list of Section.

func (*File) WriteTo

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

WriteTo writes file content into io.Writer.

func (*File) WriteToIndent

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

WriteToIndent writes file content into io.Writer with given value indention.

type Key

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

Key represents a key under a section.

func (*Key) Bool

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

Bool returns bool type value.

func (*Key) Duration

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

Duration returns time.Duration type value.

func (*Key) Float64

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

Float64 returns float64 type value.

func (*Key) Float64s

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

Float64s returns list of float64 divided by given delimiter.

func (*Key) In

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

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

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

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

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

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

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

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

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

Int returns int type value.

func (*Key) Int64

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

Int64 returns int64 type value.

func (*Key) Int64s

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

Int64s returns list of int64 divided by given delimiter.

func (*Key) Ints

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

Ints returns list of int divided by given delimiter.

func (*Key) MustBool

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

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

func (*Key) MustDuration

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

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

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

func (*Key) MustInt

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

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

func (*Key) MustInt64

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

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

func (*Key) MustString

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

MustString returns default value if key value is empty.

func (*Key) MustTime

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

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

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

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

func (*Key) MustUint64

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

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

func (*Key) Name

func (k *Key) Name() string

Name returns name of key.

func (*Key) RangeFloat64

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

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

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

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

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

func (k *Key) SetValue(v string)

SetValue changes key value.

func (*Key) String

func (k *Key) String() string

String returns string representation of value.

func (*Key) Strings

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

Strings returns list of string divided by given delimiter.

func (*Key) Time

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

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

func (*Key) TimeFormat

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

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

func (*Key) Times

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

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

func (*Key) TimesFormat

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

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

func (*Key) Uint

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

Uint returns uint type valued.

func (*Key) Uint64

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

Uint64 returns uint64 type value.

func (*Key) Uint64s

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

Uint64s returns list of uint64 divided by given delimiter.

func (*Key) Uints

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

Uints returns list of uint divided by given delimiter.

func (*Key) Validate

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

func (k *Key) Value() string

Value returns raw value of key for performance purpose.

type NameMapper

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

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

Section represents a config section.

func (*Section) DeleteKey

func (s *Section) DeleteKey(name string)

DeleteKey deletes a key from section.

func (*Section) GetKey

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

GetKey returns key in section by given name.

func (*Section) HasKey

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

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

func (*Section) HasValue

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

HasValue returns true if section contains given raw value.

func (*Section) Haskey

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

Haskey is a backwards-compatible name for HasKey.

func (*Section) Key

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

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

func (*Section) KeyStrings

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

KeyStrings returns list of key names of section.

func (*Section) Keys

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

Keys returns list of keys of section.

func (*Section) KeysHash

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

KeysHash returns keys hash consisting of names and values.

func (*Section) MapTo

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

MapTo maps section to given struct.

func (*Section) Name

func (s *Section) Name() string

Name returns name of Section.

func (*Section) NewKey

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

NewKey creates a new key to given section.

func (*Section) ReflectFrom

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

ReflectFrom reflects secion from given struct.

Jump to

Keyboard shortcuts

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