Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CountFields for arrays returning correct count #46

Merged
merged 1 commit into from
Jul 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Removed

### Fixed
- Fix (*Config).CountField returning 1 for arrays of any size. #43

## [0.3.0]

Expand Down
4 changes: 4 additions & 0 deletions getset.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ func convertErr(opts *options, v value, err error, to string) Error {
// number of elements for this field. If config value is a list, returns number
// of elements in list
func (c *Config) CountField(name string, opts ...Option) (int, error) {
if name == "" {
return len(c.fields.arr) + len(c.fields.fields), nil
}

if v, ok := c.fields.fields[name]; ok {
return v.Len(makeOptions(opts))
}
Expand Down
11 changes: 11 additions & 0 deletions getset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ func TestSetGetArray(t *testing.T) {
c.SetUint("a", 4, 12)
c.SetChild("a", 5, child)

l, err := c.CountField("a")
assert.NoError(t, err)
assert.Equal(t, 6, l)

a, err := c.Child("a", -1)
assert.NoError(t, err)

l, err = a.CountField("")
assert.NoError(t, err)
assert.Equal(t, 6, l)

b, err := c.Bool("a", 0)
assert.NoError(t, err)
assert.Equal(t, true, b)
Expand Down
10 changes: 9 additions & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@ func (c *cfgString) reflect(*options) (reflect.Value, error) {
func (c *cfgString) reify(*options) (interface{}, error) { return c.s, nil }
func (c *cfgString) typ(*options) (typeInfo, error) { return typeInfo{"string", tString}, nil }

func (cfgSub) Len(*options) (int, error) { return 1, nil }
func (c cfgSub) Context() context { return c.c.ctx }
func (cfgSub) toBool(*options) (bool, error) { return false, ErrTypeMismatch }
func (cfgSub) toString(*options) (string, error) { return "", ErrTypeMismatch }
Expand All @@ -260,6 +259,15 @@ func (cfgSub) toUint(*options) (uint64, error) { return 0, ErrTypeMismatch
func (cfgSub) toFloat(*options) (float64, error) { return 0, ErrTypeMismatch }
func (c cfgSub) toConfig(*options) (*Config, error) { return c.c, nil }

func (c cfgSub) Len(*options) (int, error) {
arr := c.c.fields.arr
if arr != nil {
return len(arr), nil
}

return 1, nil
}

func (c cfgSub) typ(*options) (typeInfo, error) {
return typeInfo{"object", reflect.PtrTo(tConfig)}, nil
}
Expand Down