Documentation
¶
Overview ¶
Package parser is a Parser for parse INI format content to golang data
There are example data:
# comments
name = inhere
age = 28
debug = true
hasQuota1 = 'this is val'
hasQuota2 = "this is val1"
shell = ${SHELL}
noEnv = ${NotExist|defValue}
; array in def section
tags[] = a
tags[] = b
tags[] = c
; comments
[sec1]
key = val0
some = value
stuff = things
; array in section
types[] = x
types[] = y
how to use, please see examples:
Index ¶
- Constants
- Variables
- func Decode(blob []byte, ptr any) error
- func Encode(v any) ([]byte, error)
- func EncodeFull(data map[string]any, defSection ...string) (out []byte, err error)
- func EncodeLite(data map[string]map[string]string, defSection ...string) (out []byte, err error)
- func EncodeSimple(data map[string]map[string]string, defSection ...string) ([]byte, error)
- func EncodeWithDefName(v any, defSection ...string) (out []byte, err error)
- func IgnoreCase(p *Parser)
- func InlineComment(opt *Options)
- func NoDefSection(p *Parser)
- func WithReplaceNl(opt *Options)
- type OptFunc
- type Options
- type Parser
- func (p *Parser) Comments() map[string]string
- func (p *Parser) Decode(ptr any) error
- func (p *Parser) FullData() map[string]any
- func (p *Parser) LiteData() map[string]map[string]string
- func (p *Parser) LiteSection(name string) map[string]string
- func (p *Parser) MapStruct(ptr any) (err error)
- func (p *Parser) ParseBytes(bts []byte) (err error)
- func (p *Parser) ParseFrom(in *bufio.Scanner) (count int64, err error)
- func (p *Parser) ParseReader(r io.Reader) (err error)
- func (p *Parser) ParseString(str string) error
- func (p *Parser) ParsedData() any
- func (p *Parser) Reset()
- func (p *Parser) SimpleData() map[string]map[string]string
- func (p *Parser) Unmarshal(v []byte, ptr any) error
- func (p *Parser) WithOptions(opts ...func(p *Parser)) *Parser
- type SectionMatcher
- type UserCollector
Examples ¶
Constants ¶
const ( ModeFull parseMode = 1 ModeLite parseMode = 2 ModeSimple parseMode = 2 // alias of ModeLite )
mode of parse data
ModeFull - will parse array value and inline array ModeLite/ModeSimple - don't parse array value line
const DefSection = "__default"
DefSection default section key name
const TokSection = textscan.TokComments + 1 + iota
TokSection for mark a section
Variables ¶
var TagName = "ini"
TagName default tag-name of mapping data to struct
Functions ¶
func EncodeFull ¶
EncodeFull full mode data to INI, can set default section name
func EncodeLite ¶
EncodeLite data to INI
func EncodeSimple ¶
EncodeSimple data to INI
func EncodeWithDefName ¶
EncodeWithDefName golang data(map, struct) to INI, can set default section name
func NoDefSection ¶
func NoDefSection(p *Parser)
NoDefSection set don't return DefSection title
Usage:
Parser.NoDefSection()
Types ¶
type Options ¶
type Options struct {
// TagName of mapping data to struct
TagName string
// ParseMode setting. default is ModeLite
ParseMode parseMode
// Ignore case for key name
IgnoreCase bool
// ReplaceNl replace the "\n" to newline
ReplaceNl bool
// default section name. default is "__default"
DefSection string
// NoDefSection setting. only for full parse mode
NoDefSection bool
// InlineComment support parse inline comments. default is false
InlineComment bool
// Collector allow you custom the value collector.
//
// Notice: in lite mode, isSlice always is false.
Collector UserCollector
}
Options for parser
type Parser ¶
type Parser struct {
*Options
// contains filtered or unexported fields
}
Parser definition
func NewFulled ¶
NewFulled create a full mode Parser with some options
Example ¶
p, err := Parse(iniStr, ModeFull)
// p, err := Parse(iniStr, ModeFull, NoDefSection)
if err != nil {
panic(err)
}
fmt.Printf("full parse:\n%#v\n", p.FullData())
func NewSimpled ¶
NewSimpled create a lite mode Parser
Example ¶
// simple mode will ignore all array values
p, err := Parse(iniStr, ModeSimple)
if err != nil {
panic(err)
}
fmt.Printf("simple parse:\n%#v\n", p.SimpleData())
func (*Parser) LiteSection ¶
LiteSection get parsed data by simple parse
func (*Parser) ParseBytes ¶
ParseBytes parse from bytes data
func (*Parser) ParseReader ¶
ParseReader parse from io reader
func (*Parser) ParseString ¶
ParseString parse from string data
func (*Parser) SimpleData ¶
SimpleData get parsed data by simple parse
func (*Parser) WithOptions ¶
WithOptions apply some options
type UserCollector ¶
UserCollector custom data collector.
Notice: in lite mode, isSlice always is false.