Skip to content

Commit

Permalink
add new method for ini parser
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 21, 2020
1 parent 2ff9a25 commit cba1194
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
21 changes: 3 additions & 18 deletions parser/decode_encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import (
"fmt"
"reflect"
"sort"

"github.com/mitchellh/mapstructure"
)

// TagName of mapping data to struct
// TagName default tag-name of mapping data to struct
var TagName = "ini"

// Decode INI content to golang data
Expand All @@ -29,20 +27,7 @@ func Decode(blob []byte, ptr interface{}) error {
return err
}

mapConf := &mapstructure.DecoderConfig{
Metadata: nil,
Result: ptr,
TagName: TagName,
// will auto convert string to int/uint
WeaklyTypedInput: true,
}

decoder, err := mapstructure.NewDecoder(mapConf)
if err != nil {
return err
}

return decoder.Decode(p.fullData)
return p.MapStruct(ptr)
}

// Encode golang data to INI
Expand Down Expand Up @@ -97,6 +82,7 @@ func EncodeFull(data map[string]interface{}, defSection ...string) (out []byte,
return
}
}
// case map[string]string: // is section
case map[string]interface{}: // is section
if key != defSecName {
secBuf.WriteString("[" + key + "]\n")
Expand Down Expand Up @@ -138,7 +124,6 @@ func buildSectionBuffer(data map[string]interface{}, buf *bytes.Buffer) (err err
continue
}
}

return
}

Expand Down
37 changes: 36 additions & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import (
"fmt"
"regexp"
"strings"

"github.com/mitchellh/mapstructure"
)

// errSyntax is returned when there is a syntax error in an INI file.
Expand Down Expand Up @@ -91,7 +93,11 @@ type Parser struct {
simpleData map[string]map[string]string
// parsed bool
parseMode parseMode
// -- options ---

// ---- options ----

// TagName of mapping data to struct
TagName string
// Ignore case for key name
IgnoreCase bool
// default section name. default is "__default"
Expand All @@ -105,6 +111,7 @@ type Parser struct {
// NewFulled create a full mode Parser with some options
func NewFulled(opts ...func(*Parser)) *Parser {
p := &Parser{
TagName: TagName,
DefSection: DefSection,
parseMode: ModeFull,
fullData: make(map[string]interface{}),
Expand All @@ -116,6 +123,7 @@ func NewFulled(opts ...func(*Parser)) *Parser {
// NewSimpled create a simple mode Parser
func NewSimpled(opts ...func(*Parser)) *Parser {
p := &Parser{
TagName: TagName,
DefSection: DefSection,
parseMode: ModeSimple,
simpleData: make(map[string]map[string]string),
Expand Down Expand Up @@ -374,6 +382,33 @@ func (p *Parser) Reset() {
}
}

// MapStruct mapping the parsed data to struct ptr
func (p *Parser) MapStruct(ptr interface{}) (err error) {
if p.parseMode == ModeFull {
err = mapStruct(p.TagName, p.fullData, ptr)
} else {
err = mapStruct(p.TagName, p.simpleData, ptr)
}
return
}

func mapStruct(tagName string, data interface{}, ptr interface{}) error {
mapConf := &mapstructure.DecoderConfig{
Metadata: nil,
Result: ptr,
TagName: tagName,
// will auto convert string to int/uint
WeaklyTypedInput: true,
}

decoder, err := mapstructure.NewDecoder(mapConf)
if err != nil {
return err
}

return decoder.Decode(data)
}

func trimWithQuotes(inputVal string) (filtered string) {
filtered = strings.TrimSpace(inputVal)
groups := quotesRegex.FindStringSubmatch(filtered)
Expand Down

0 comments on commit cba1194

Please sign in to comment.