Browse code

daemon/config: add MarshalJSON for future proofing

If anything marshals the daemon config now or in the future
this commit ensures the correct canonical form for the builder
GC policies' filters.

Signed-off-by: Tibor Vass <tibor@docker.com>
(cherry picked from commit 85733620ebea3da75abe7d732043354aa0883f8a)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Tibor Vass authored on 2019/10/03 09:09:50
Showing 2 changed files
... ...
@@ -36,6 +36,14 @@ func NewArgs(initialArgs ...KeyValuePair) Args {
36 36
 	return args
37 37
 }
38 38
 
39
+func (args Args) Keys() []string {
40
+	keys := make([]string, 0, len(args.fields))
41
+	for k := range args.fields {
42
+		keys = append(keys, k)
43
+	}
44
+	return keys
45
+}
46
+
39 47
 // MarshalJSON returns a JSON byte representation of the Args
40 48
 func (args Args) MarshalJSON() ([]byte, error) {
41 49
 	if len(args.fields) == 0 {
... ...
@@ -2,6 +2,8 @@ package config
2 2
 
3 3
 import (
4 4
 	"encoding/json"
5
+	"fmt"
6
+	"sort"
5 7
 	"strings"
6 8
 
7 9
 	"github.com/docker/docker/api/types/filters"
... ...
@@ -16,6 +18,20 @@ type BuilderGCRule struct {
16 16
 
17 17
 type BuilderGCFilter filters.Args
18 18
 
19
+func (x *BuilderGCFilter) MarshalJSON() ([]byte, error) {
20
+	f := filters.Args(*x)
21
+	keys := f.Keys()
22
+	sort.Strings(keys)
23
+	arr := make([]string, 0, len(keys))
24
+	for _, k := range keys {
25
+		values := f.Get(k)
26
+		for _, v := range values {
27
+			arr = append(arr, fmt.Sprintf("%s=%s", k, v))
28
+		}
29
+	}
30
+	return json.Marshal(arr)
31
+}
32
+
19 33
 func (x *BuilderGCFilter) UnmarshalJSON(data []byte) error {
20 34
 	var arr []string
21 35
 	f := filters.NewArgs()