diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 22c09d4..6fe7d54 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -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 diff --git a/README.md b/README.md index f63f6fb..e613279 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/README.zh-CN.md b/README.zh-CN.md index 265679b..c85c5e3 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -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` 格式内容的解析器,编码解码库 diff --git a/encoder.go b/encoder.go index 6583aa0..01fa143 100644 --- a/encoder.go +++ b/encoder.go @@ -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. @@ -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 } diff --git a/encoder_test.go b/encoder_test.go index 1b941b8..edbdbe7 100644 --- a/encoder_test.go +++ b/encoder_test.go @@ -3,6 +3,7 @@ package properties_test import ( "fmt" "testing" + "time" "github.com/gookit/goutil/testutil/assert" "github.com/gookit/properties" @@ -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") +} diff --git a/options_test.go b/options_test.go index 95617ae..454271c 100644 --- a/options_test.go +++ b/options_test.go @@ -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