Browse code

filters: don't encode an empty set. update comments

Docker-DCO-1.1-Signed-off-by: Vincent Batts <vbatts@redhat.com> (github: vbatts)

Vincent Batts authored on 2014/05/31 04:55:40
Showing 2 changed files
... ...
@@ -8,14 +8,12 @@ import (
8 8
 
9 9
 type Args map[string][]string
10 10
 
11
-/*
12
-Parse the argument to the filter flag. Like
13
-
14
-  `docker ps -f 'created=today' -f 'image.name=ubuntu*'`
15
-
16
-If prev map is provided, then it is appended to, and returned. By default a new
17
-map is created.
18
-*/
11
+// Parse the argument to the filter flag. Like
12
+//
13
+//   `docker ps -f 'created=today' -f 'image.name=ubuntu*'`
14
+//
15
+// If prev map is provided, then it is appended to, and returned. By default a new
16
+// map is created.
19 17
 func ParseFlag(arg string, prev Args) (Args, error) {
20 18
 	var filters Args = prev
21 19
 	if prev == nil {
... ...
@@ -37,10 +35,13 @@ func ParseFlag(arg string, prev Args) (Args, error) {
37 37
 
38 38
 var ErrorBadFormat = errors.New("bad format of filter (expected name=value)")
39 39
 
40
-/*
41
-packs the Args into an string for easy transport from client to server
42
-*/
40
+// packs the Args into an string for easy transport from client to server
43 41
 func ToParam(a Args) (string, error) {
42
+	// this way we don't URL encode {}, just empty space
43
+	if len(a) == 0 {
44
+		return "", nil
45
+	}
46
+
44 47
 	buf, err := json.Marshal(a)
45 48
 	if err != nil {
46 49
 		return "", err
... ...
@@ -48,11 +49,12 @@ func ToParam(a Args) (string, error) {
48 48
 	return string(buf), nil
49 49
 }
50 50
 
51
-/*
52
-unpacks the filter Args
53
-*/
51
+// unpacks the filter Args
54 52
 func FromParam(p string) (Args, error) {
55 53
 	args := Args{}
54
+	if len(p) == 0 {
55
+		return args, nil
56
+	}
56 57
 	err := json.Unmarshal([]byte(p), &args)
57 58
 	if err != nil {
58 59
 		return nil, err
... ...
@@ -61,3 +61,18 @@ func TestParam(t *testing.T) {
61 61
 		}
62 62
 	}
63 63
 }
64
+
65
+func TestEmpty(t *testing.T) {
66
+	a := Args{}
67
+	v, err := ToParam(a)
68
+	if err != nil {
69
+		t.Errorf("failed to marshal the filters: %s", err)
70
+	}
71
+	v1, err := FromParam(v)
72
+	if err != nil {
73
+		t.Errorf("%s", err)
74
+	}
75
+	if len(a) != len(v1) {
76
+		t.Errorf("these should both be empty sets")
77
+	}
78
+}