Skip to content

Commit

Permalink
add pflag value interface implementation to flagext
Browse files Browse the repository at this point in the history
  • Loading branch information
casualjim committed Dec 19, 2016
1 parent 14b161b commit ce2fd6a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ secrets.yml
coverage.out
*.cov
*.out
playground
2 changes: 1 addition & 1 deletion client/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (r *request) SetHeaderParam(name string, values ...string) error {
// when there is only 1 value provided for the varargs, it will set it.
// when there are several values provided for the varargs it will add it (no overriding)
func (r *request) SetQueryParam(name string, values ...string) error {
if r.header == nil {
if r.query == nil {
r.query = make(url.Values)
}
r.query[name] = values
Expand Down
10 changes: 10 additions & 0 deletions flagext/byte_size.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@ func (b *ByteSize) UnmarshalFlag(value string) error {
*b = ByteSize(int(sz))
return nil
}

// String method for a bytesize (pflag value and stringer interface)
func (b ByteSize) String() string {
return units.HumanSize(float64(b))
}

// Set the value of this bytesize (pflag value interfaces)
func (b *ByteSize) Set(value string) error {
return b.UnmarshalFlag(value)
}
16 changes: 16 additions & 0 deletions flagext/byte_size_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ func TestMarshalBytesize(t *testing.T) {
}
}

func TestStringBytesize(t *testing.T) {
v := ByteSize(2048).String()
assert.Equal(t, "2.048 kB", v)
}

func TestUnmarshalBytesize(t *testing.T) {
var b ByteSize
err := b.UnmarshalFlag("notASize")
Expand All @@ -20,3 +25,14 @@ func TestUnmarshalBytesize(t *testing.T) {
assert.Equal(t, ByteSize(1000000), b)
}
}

func TestSetBytesize(t *testing.T) {
var b ByteSize
err := b.Set("notASize")
assert.Error(t, err)

err = b.Set("2MB")
if assert.NoError(t, err) {
assert.Equal(t, ByteSize(2000000), b)
}
}

0 comments on commit ce2fd6a

Please sign in to comment.