mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
filters: don't encode an empty set. update comments
Docker-DCO-1.1-Signed-off-by: Vincent Batts <vbatts@redhat.com> (github: vbatts)
This commit is contained in:
parent
89a15fa235
commit
3a4e3ca327
2 changed files with 31 additions and 14 deletions
|
@ -8,14 +8,12 @@ import (
|
||||||
|
|
||||||
type Args map[string][]string
|
type Args map[string][]string
|
||||||
|
|
||||||
/*
|
// Parse the argument to the filter flag. Like
|
||||||
Parse the argument to the filter flag. Like
|
//
|
||||||
|
// `docker ps -f 'created=today' -f 'image.name=ubuntu*'`
|
||||||
`docker ps -f 'created=today' -f 'image.name=ubuntu*'`
|
//
|
||||||
|
// If prev map is provided, then it is appended to, and returned. By default a new
|
||||||
If prev map is provided, then it is appended to, and returned. By default a new
|
// map is created.
|
||||||
map is created.
|
|
||||||
*/
|
|
||||||
func ParseFlag(arg string, prev Args) (Args, error) {
|
func ParseFlag(arg string, prev Args) (Args, error) {
|
||||||
var filters Args = prev
|
var filters Args = prev
|
||||||
if prev == nil {
|
if prev == nil {
|
||||||
|
@ -37,10 +35,13 @@ func ParseFlag(arg string, prev Args) (Args, error) {
|
||||||
|
|
||||||
var ErrorBadFormat = errors.New("bad format of filter (expected name=value)")
|
var ErrorBadFormat = errors.New("bad format of filter (expected name=value)")
|
||||||
|
|
||||||
/*
|
// packs the Args into an string for easy transport from client to server
|
||||||
packs the Args into an string for easy transport from client to server
|
|
||||||
*/
|
|
||||||
func ToParam(a Args) (string, error) {
|
func ToParam(a Args) (string, error) {
|
||||||
|
// this way we don't URL encode {}, just empty space
|
||||||
|
if len(a) == 0 {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
buf, err := json.Marshal(a)
|
buf, err := json.Marshal(a)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
@ -48,11 +49,12 @@ func ToParam(a Args) (string, error) {
|
||||||
return string(buf), nil
|
return string(buf), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// unpacks the filter Args
|
||||||
unpacks the filter Args
|
|
||||||
*/
|
|
||||||
func FromParam(p string) (Args, error) {
|
func FromParam(p string) (Args, error) {
|
||||||
args := Args{}
|
args := Args{}
|
||||||
|
if len(p) == 0 {
|
||||||
|
return args, nil
|
||||||
|
}
|
||||||
err := json.Unmarshal([]byte(p), &args)
|
err := json.Unmarshal([]byte(p), &args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -61,3 +61,18 @@ func TestParam(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEmpty(t *testing.T) {
|
||||||
|
a := Args{}
|
||||||
|
v, err := ToParam(a)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to marshal the filters: %s", err)
|
||||||
|
}
|
||||||
|
v1, err := FromParam(v)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%s", err)
|
||||||
|
}
|
||||||
|
if len(a) != len(v1) {
|
||||||
|
t.Errorf("these should both be empty sets")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue