Skip to content

Commit

Permalink
chore: update readme and update some code style
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 11, 2022
1 parent aeb318c commit 0a42acf
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 44 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ INI data parse by golang. INI config data management tool library.
- Support for rebinding data to structure
- Support data override merge
- Support parse ENV variable
- Support comments start with `;` `#`
- Complete unit test(coverage > 90%)
- Support variable reference, default compatible with Python's configParser format `%(VAR)s`

Expand Down Expand Up @@ -266,11 +267,6 @@ go test ./... -cover
golint ./...
```

## Refer

- [go-ini/ini](https://github.com/go-ini/ini) ini parser and config manage
- [dombenson/go-ini](https://github.com/dombenson/go-ini) ini parser and config manage

## Gookit packages

- [gookit/ini](https://github.com/gookit/ini) Go config management, use INI files
Expand All @@ -286,6 +282,11 @@ golint ./...
- [gookit/goutil](https://github.com/gookit/goutil) Some utils for the Go: string, array/slice, map, format, cli, env, filesystem, test and more
- More, please see https://github.com/gookit

## Related

- [go-ini/ini](https://github.com/go-ini/ini) ini parser and config manage
- [dombenson/go-ini](https://github.com/dombenson/go-ini) ini parser and config manage

## License

**MIT**
11 changes: 6 additions & 5 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- 支持数据覆盖合并
- 支持将数据重新绑定到结构体
- 支持解析 ENV 变量名
- 支持使用 `;` `#` 进行注释一行
- 支持变量参考,默认兼容Python的configParser格式 `%(VAR)s`
- 完善的单元测试(coverage > 90%)

Expand Down Expand Up @@ -258,11 +259,6 @@ go test ./... -cover
golint ./...
```

## 参考

- [go-ini/ini](https://github.com/go-ini/ini) ini parser and config manage
- [dombenson/go-ini](https://github.com/dombenson/go-ini) ini parser and config manage

## Gookit packages

- [gookit/ini](https://github.com/gookit/ini) Go config management, use INI files
Expand All @@ -278,6 +274,11 @@ golint ./...
- [gookit/goutil](https://github.com/gookit/goutil) Some utils for the Go: string, array/slice, map, format, cli, env, filesystem, test and more
- More, please see https://github.com/gookit

## 相关项目参考

- [go-ini/ini](https://github.com/go-ini/ini) ini parser and config manage
- [dombenson/go-ini](https://github.com/dombenson/go-ini) ini parser and config manage

## License

**MIT**
4 changes: 2 additions & 2 deletions dotenv/dotenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (
// DefaultName default file name
DefaultName = ".env"

// OnlyLoadExists load on file exists
// OnlyLoadExists only load on file exists
OnlyLoadExists bool

// save original Env data
Expand Down Expand Up @@ -49,6 +49,7 @@ func DontUpperEnvKey() {
}

// Load parse .env file data to os ENV.
//
// Usage:
// dotenv.Load("./", ".env")
func Load(dir string, filenames ...string) (err error) {
Expand Down Expand Up @@ -149,7 +150,6 @@ func loadFile(file string) (err error) {
// open file
fd, err := os.Open(file)
if err != nil {
// skip not exist file
if os.IsNotExist(err) && OnlyLoadExists {
return nil
}
Expand Down
9 changes: 8 additions & 1 deletion ini.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func New() *Ini {
}

// NewWithOptions new a instance and with some options
//
// Usage:
// ini.NewWithOptions(ini.ParseEnv, ini.Readonly)
func NewWithOptions(opts ...func(*Options)) *Ini {
Expand Down Expand Up @@ -130,6 +131,7 @@ func (c *Ini) ensureInit() {
*************************************************************/

// newDefaultOptions create a new default Options
//
// Notice:
// Cannot use package var instead it. That will allow multiple instances to use the same Options
func newDefaultOptions() *Options {
Expand All @@ -146,20 +148,23 @@ func newDefaultOptions() *Options {
}

// Readonly setting
//
// Usage:
// ini.NewWithOptions(ini.Readonly)
func Readonly(opts *Options) {
opts.Readonly = true
}

// ParseVar on get value
//
// Usage:
// ini.WithOptions(ini.ParseVar)
func ParseVar(opts *Options) {
opts.ParseVar = true
}

// ParseEnv will parse ENV key on get value
//
// Usage:
// ini.WithOptions(ini.ParseEnv)
func ParseEnv(opts *Options) {
Expand All @@ -172,12 +177,14 @@ func IgnoreCase(opts *Options) {
}

// GetOptions get options info.
//
// Notice: return is value. so, cannot change Ini instance
func GetOptions() Options {
return dc.Options()
}

// Options get options info.
//
// Notice: return is value. so, cannot change options
func (c *Ini) Options() Options {
return *c.opts
Expand All @@ -191,7 +198,7 @@ func WithOptions(opts ...func(*Options)) {
// WithOptions apply some options
func (c *Ini) WithOptions(opts ...func(*Options)) {
if !c.IsEmpty() {
panic("ini: Cannot set options after data has been load")
panic("ini: cannot set options after data has been load")
}

// apply options
Expand Down
4 changes: 4 additions & 0 deletions parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ 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'''`
Expand Down
16 changes: 8 additions & 8 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ var (
const DefSection = "__default"

// mode of parse data
// ModeFull - will parse array
// ModeSimple - don't parse array value
//
// ModeFull - will parse array
// ModeSimple - don't parse array value
const (
ModeFull parseMode = 1
ModeSimple parseMode = 2
Expand Down Expand Up @@ -111,7 +112,7 @@ type Parser struct {
// NewFulled create a full mode Parser with some options
func NewFulled(opts ...func(*Parser)) *Parser {
p := &Parser{
TagName: TagName,
TagName: TagName,
DefSection: DefSection,
parseMode: ModeFull,
fullData: make(map[string]interface{}),
Expand All @@ -123,7 +124,7 @@ func NewFulled(opts ...func(*Parser)) *Parser {
// NewSimpled create a simple mode Parser
func NewSimpled(opts ...func(*Parser)) *Parser {
p := &Parser{
TagName: TagName,
TagName: TagName,
DefSection: DefSection,
parseMode: ModeSimple,
simpleData: make(map[string]map[string]string),
Expand All @@ -133,6 +134,7 @@ func NewSimpled(opts ...func(*Parser)) *Parser {
}

// NoDefSection set don't return DefSection title
//
// Usage:
// Parser.NewWithOptions(ini.ParseEnv)
func NoDefSection(p *Parser) {
Expand Down Expand Up @@ -203,7 +205,7 @@ func (p *Parser) parse(in *bufio.Scanner) (bytes int64, err error) {
for readLine = in.Scan(); readLine; readLine = in.Scan() {
line := in.Text()

bytes++
bytes++ // newline
bytes += int64(len(line))

lineNum++
Expand Down Expand Up @@ -251,11 +253,10 @@ func (p *Parser) parse(in *bufio.Scanner) (bytes int64, err error) {
}
}

err = in.Err()
if bytes < 0 {
bytes = 0
}

err = in.Err()
return
}

Expand Down Expand Up @@ -353,7 +354,6 @@ func (p *Parser) ParsedData() interface{} {
if p.parseMode == ModeFull {
return p.fullData
}

return p.simpleData
}

Expand Down
50 changes: 27 additions & 23 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"testing"

"github.com/gookit/goutil/dump"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -106,15 +107,15 @@ func TestNewSimpled(t *testing.T) {
}

func TestNewFulled(t *testing.T) {
st := assert.New(t)
is := assert.New(t)

p := NewFulled()
st.Equal(ModeFull.Unit8(), p.ParseMode())
st.False(p.IgnoreCase)
st.False(p.NoDefSection)
is.Equal(ModeFull.Unit8(), p.ParseMode())
is.False(p.IgnoreCase)
is.False(p.NoDefSection)

err := p.ParseString("invalid string")
st.Error(err)
is.Error(err)

err = p.ParseString(`
[__default]
Expand All @@ -124,39 +125,42 @@ newKey = val5
[newSec]
key = val0
`)
st.Nil(err)

// fmt.Printf("%#v\n", p.ParsedData())
is.Nil(err)
dump.P(p.ParsedData())

p.Reset()
err = p.ParseString(iniStr)
st.Nil(err)
is.Nil(err)

v := p.ParsedData()
st.NotEmpty(v)
is.NotEmpty(v)

// options: ignore case
p = NewFulled(IgnoreCase)
st.True(p.IgnoreCase)
is.True(p.IgnoreCase)
err = p.ParseString(iniStr)
st.Nil(err)
is.Nil(err)

v = p.ParsedData()
st.NotEmpty(v)
is.NotEmpty(v)

data := p.FullData()
str := fmt.Sprintf("%v", data)
st.Contains(str, "hasquota2:")
st.NotContains(str, "hasQuota1:")
is.Contains(str, "hasquota2:")
is.NotContains(str, "hasQuota1:")
}

func TestNewFulled_NoDefSection(t *testing.T) {
is := assert.New(t)

// options: NoDefSection
p = NewFulled(NoDefSection)
st.Equal(ModeFull.Unit8(), p.ParseMode())
st.False(p.IgnoreCase)
st.True(p.NoDefSection)
p := NewFulled(NoDefSection)
is.Equal(ModeFull.Unit8(), p.ParseMode())
is.False(p.IgnoreCase)
is.True(p.NoDefSection)

err = p.ParseString(iniStr)
st.Nil(err)
err := p.ParseString(iniStr)
is.Nil(err)

p.Reset()
err = p.ParseString(`
Expand All @@ -172,6 +176,6 @@ arr[] = val1
key1 = val1
arr[] = val2
`)
st.Nil(err)
// fmt.Printf("%#v\n", p.ParsedData())
is.Nil(err)
dump.P(p.ParsedData())
}

0 comments on commit 0a42acf

Please sign in to comment.