Skip to content

Commit

Permalink
up: update some unit tests and README docs
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 16, 2022
1 parent 075202a commit 3d27a8b
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
reporter: github-pr-check
# Report all results. [added,diff_context,file,nofilter].
filter_mode: added
# Exit with 1 when it find at least one finding.
# Exit with 1 when it finds at least one finding.
fail_on_error: true

- name: Run unit tests
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/gookit/properties)](https://github.com/gookit/properties)
[![Go Report Card](https://goreportcard.com/badge/github.com/gookit/properties)](https://goreportcard.com/report/github.com/gookit/properties)
[![Go Reference](https://pkg.go.dev/badge/github.com/gookit/properties.svg)](https://pkg.go.dev/github.com/gookit/properties)
[![Coverage Status](https://coveralls.io/repos/github/gookit/properties/badge.svg?branch=master)](https://coveralls.io/github/gookit/properties?branch=master)

`properties` - Java Properties format contents parse, marshal and unmarshal library.

Expand Down
1 change: 1 addition & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/gookit/properties)](https://github.com/gookit/properties)
[![Go Report Card](https://goreportcard.com/badge/github.com/gookit/properties)](https://goreportcard.com/report/github.com/gookit/properties)
[![Go Reference](https://pkg.go.dev/badge/github.com/gookit/properties.svg)](https://pkg.go.dev/github.com/gookit/properties)
[![Coverage Status](https://coveralls.io/repos/github/gookit/properties/badge.svg?branch=master)](https://coveralls.io/github/gookit/properties?branch=master)

`properties` - Java `Properties` 格式内容的解析器,编码解码库

Expand Down
9 changes: 4 additions & 5 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ type Encoder struct {
buf bytes.Buffer
// TagName for encode a struct. default: properties
TagName string
// comments map data. TODO
// key is path name, value is comments
// comments map[string]string
}

// NewEncoder instance.
Expand Down Expand Up @@ -53,11 +56,7 @@ func (e *Encoder) encode(v any) error {
Result: &mp,
}

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

decoder, _ := mapstructure.NewDecoder(cfg)
if err := decoder.Decode(v); err != nil {
return err
}
Expand Down
28 changes: 28 additions & 0 deletions encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package properties_test
import (
"fmt"
"testing"
"time"

"github.com/gookit/goutil/testutil/assert"
"github.com/gookit/properties"
Expand Down Expand Up @@ -33,3 +34,30 @@ func TestEncode(t *testing.T) {
assert.NoErr(t, err)
assert.Nil(t, bs)
}

func TestEncode_struct(t *testing.T) {
type MyConf struct {
Name string `properties:"name"`
Age int `properties:"age"`
Expire time.Duration `properties:"expire"`
}

myc := &MyConf{
Name: "inhere",
Age: 234,
Expire: time.Second * 3,
}

bs, err := properties.Encode(myc)
assert.NoErr(t, err)
str := string(bs)
assert.StrContains(t, str, "name=inhere")
assert.StrContains(t, str, "age=234")
assert.StrContains(t, str, "expire=3000000000")
}

func TestEncode_error(t *testing.T) {
bs, err := properties.Encode([]int{12, 34})
assert.Nil(t, bs)
assert.ErrMsg(t, err, "only allow encode map and struct data")
}
9 changes: 9 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import (
"github.com/gookit/properties"
)

func TestOptions_basic(t *testing.T) {
opt := &properties.Options{}

fn := properties.WithTagName("config")
fn(opt)

assert.Eq(t, "config", opt.TagName)
}

func TestOptions_InlineComment(t *testing.T) {
text := `
key = value # inline comments
Expand Down

0 comments on commit 3d27a8b

Please sign in to comment.