parser

package
v2.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2022 License: MIT Imports: 9 Imported by: 7 modules, 9 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

INI Parser

This is a parser for parse INI format content to golang data

parser is ref the project: https://github.com/dombenson/go-ini, Thank you very much

Feature

  • Support comments start with ; #

TODO

  • multi line text '''text'''
  • inline comments

Ref

License

MIT

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:

Example (FullParse)
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())
Example (SimpleParse)
// 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())

Index

Examples

Constants

View Source
const (
	MultiLineValMarkS = "'''"
	MultiLineValMarkD = `"""`
)

special chars consts

View Source
const (
	TokMLValMarkS = 'm' // multi line value by single quotes: ”'
	TokMLValMarkD = 'M' // multi line value by double quotes: """
)

token consts

View Source
const (
	ModeFull   parseMode = 1
	ModeLite   parseMode = 2
	ModeSimple parseMode = 2
)

mode of parse data

ModeFull   - will parse inline array
ModeLite/ModeSimple - don't parse array value
View Source
const DefSection = "__default"

DefSection default section key name

Variables

View Source
var TagName = "ini"

TagName default tag-name of mapping data to struct

Functions

func Decode

func Decode(blob []byte, ptr interface{}) error

Decode INI content to golang data

func Encode

func Encode(v interface{}) ([]byte, error)

Encode golang data(map, struct) to INI string.

func EncodeFull

func EncodeFull(data map[string]interface{}, defSection ...string) (out []byte, err error)

EncodeFull full mode data to INI, can set default section name

func EncodeSimple

func EncodeSimple(data map[string]map[string]string, defSection ...string) (out []byte, err error)

EncodeSimple data to INI

func EncodeWithDefName added in v2.1.2

func EncodeWithDefName(v interface{}, defSection ...string) (out []byte, err error)

EncodeWithDefName golang data(map, struct) to INI, can set default section name

func IgnoreCase

func IgnoreCase(p *Parser)

IgnoreCase set ignore-case

func NoDefSection

func NoDefSection(p *Parser)

NoDefSection set don't return DefSection title

Usage:

Parser.NewFulled(ini.ParseEnv)

Types

type Parser

type Parser struct {

	// TagName of mapping data to struct
	TagName string
	// Ignore case for key name
	IgnoreCase bool
	// default section name. default is "__default"
	DefSection string
	// only for full parse mode
	NoDefSection bool
	// you can custom data collector
	Collector UserCollector
	// contains filtered or unexported fields
}

Parser definition

func New added in v2.1.2

func New(fns ...func(*Parser)) *Parser

New a full mode Parser with some options

func NewFulled

func NewFulled(fns ...func(*Parser)) *Parser

NewFulled create a full mode Parser with some options

func NewSimpled

func NewSimpled(opts ...func(*Parser)) *Parser

NewSimpled create a simple mode Parser

func Parse

func Parse(data string, mode parseMode, opts ...func(*Parser)) (p *Parser, err error)

Parse a INI data string to golang

func (*Parser) FullData

func (p *Parser) FullData() map[string]interface{}

FullData get parsed data by full parse

func (*Parser) LiteSection added in v2.1.2

func (p *Parser) LiteSection(name string) map[string]string

LiteSection get parsed data by simple parse

func (*Parser) MapStruct added in v2.0.6

func (p *Parser) MapStruct(ptr interface{}) (err error)

MapStruct mapping the parsed data to struct ptr

func (*Parser) ParseBytes

func (p *Parser) ParseBytes(bts []byte) (err error)

ParseBytes parse from bytes data

func (*Parser) ParseFrom

func (p *Parser) ParseFrom(in *bufio.Scanner) (int64, error)

ParseFrom a data scanner

func (*Parser) ParseMode

func (p *Parser) ParseMode() uint8

ParseMode get current mode

func (*Parser) ParseString

func (p *Parser) ParseString(str string) error

ParseString parse from string data

func (*Parser) ParsedData

func (p *Parser) ParsedData() interface{}

ParsedData get parsed data

func (*Parser) Reset

func (p *Parser) Reset()

Reset parser, clear parsed data

func (*Parser) SimpleData

func (p *Parser) SimpleData() map[string]map[string]string

SimpleData get parsed data by simple parse

func (*Parser) WithOptions

func (p *Parser) WithOptions(opts ...func(*Parser)) *Parser

WithOptions apply some options

type UserCollector

type UserCollector func(section, key, val string, isSlice bool)

UserCollector custom data collector.

Notice: in simple mode, isSlice always is false.

Jump to

Keyboard shortcuts

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