parser

package
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2023 License: MIT Imports: 12 Imported by: 0

README

INI Parser

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

Feature

  • Support parse section, array value.
  • Support comments start with ; #
  • Support multi line comments /* .. */
  • Support multi line value with """ or '''

Install

go get github.com/gookit/ini/v2/parser

Usage

package main

import (
	"github.com/gookit/goutil"
	"github.com/gookit/goutil/dump"
	"github.com/gookit/ini/v2/parser"
)

func main() {
	p := parser.New()

	err := p.ParseString(`
# comments 1
name = inhere
age = 28

; comments 2
[sec1]
key = val0
some = value
`)

	goutil.PanicErr(err)
	// dump parsed data and collected comments map
	dump.P(p.ParsedData(), p.Comments())
}

Output:

map[string]map[string]string { #len=2
  "__default": map[string]string { #len=7
    "name": string("inhere"), #len=6
    "age": string("28"), #len=2
  },
  "sec1": map[string]string { #len=3
    "key": string("val0"), #len=4
    "some": string("value"), #len=5
  },
},
# collected comments
map[string]string { #len=2
  "_sec_sec1": string("; comments 2"), #len=12
  "__default_name": string("# comments 1"), #len=12
},

Functions API

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 func(opt *Options)
    func WithDefSection(name string) OptFunc
    func WithParseMode(mode parseMode) OptFunc
    func WithTagName(name string) OptFunc
type Options struct{ ... }
    func NewOptions(fns ...OptFunc) *Options
type Parser struct{ ... }
    func New(fns ...OptFunc) *Parser
    func NewFulled(fns ...func(*Parser)) *Parser
    func NewLite(fns ...OptFunc) *Parser
    func NewSimpled(fns ...func(*Parser)) *Parser
    func Parse(data string, mode parseMode, opts ...func(*Parser)) (p *Parser, err error)

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:

Index

Examples

Constants

View Source
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
View Source
const DefSection = "__default"

DefSection default section key name

View Source
const TokSection = textscan.TokComments + 1 + iota

TokSection for mark a section

Variables

View Source
var TagName = "ini"

TagName default tag-name of mapping data to struct

Functions

func Decode

func Decode(blob []byte, ptr any) error

Decode INI content to golang data

func Encode

func Encode(v any) ([]byte, error)

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

func EncodeFull

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

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

func EncodeLite

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

EncodeLite data to INI

func EncodeSimple

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

EncodeSimple data to INI

func EncodeWithDefName

func EncodeWithDefName(v any, 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 InlineComment

func InlineComment(opt *Options)

InlineComment for parse

func NoDefSection

func NoDefSection(p *Parser)

NoDefSection set don't return DefSection title

Usage:

Parser.NoDefSection()

func WithReplaceNl

func WithReplaceNl(opt *Options)

WithReplaceNl for parse

Types

type OptFunc

type OptFunc func(opt *Options)

OptFunc define

func WithDefSection

func WithDefSection(name string) OptFunc

WithDefSection name for parse

func WithParseMode

func WithParseMode(mode parseMode) OptFunc

WithParseMode name for parse

func WithTagName

func WithTagName(name string) OptFunc

WithTagName for decode data

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

func NewOptions

func NewOptions(fns ...OptFunc) *Options

NewOptions instance

type Parser

type Parser struct {
	*Options
	// contains filtered or unexported fields
}

Parser definition

func New

func New(fns ...OptFunc) *Parser

New a lite mode Parser with some options

func NewFulled

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

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())
Output:

func NewLite

func NewLite(fns ...OptFunc) *Parser

NewLite create a lite mode Parser. alias of New()

func NewSimpled

func NewSimpled(fns ...func(*Parser)) *Parser

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())
Output:

func Parse

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

Parse a INI data string to golang

func (*Parser) Comments

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

Comments get

func (*Parser) Decode

func (p *Parser) Decode(ptr any) error

Decode the parsed data to struct ptr

func (*Parser) FullData

func (p *Parser) FullData() map[string]any

FullData get parsed data by full parse

func (*Parser) LiteData

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

LiteData get parsed data by simple parse

func (*Parser) LiteSection

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

LiteSection get parsed data by simple parse

func (*Parser) MapStruct

func (p *Parser) MapStruct(ptr any) (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) (count int64, err error)

ParseFrom a data scanner

func (*Parser) ParseReader

func (p *Parser) ParseReader(r io.Reader) (err error)

ParseReader parse from io reader

func (*Parser) ParseString

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

ParseString parse from string data

func (*Parser) ParsedData

func (p *Parser) ParsedData() any

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) Unmarshal

func (p *Parser) Unmarshal(v []byte, ptr any) error

Unmarshal parse ini text and decode to struct

func (*Parser) WithOptions

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

WithOptions apply some options

type SectionMatcher

type SectionMatcher struct{}

SectionMatcher match section line: [section]

func (*SectionMatcher) Match

func (m *SectionMatcher) Match(text string, _ textscan.Token) (textscan.Token, error)

Match section line: [section]

type UserCollector

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

UserCollector custom data collector.

Notice: in lite mode, isSlice always is false.

Jump to

Keyboard shortcuts

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