浏览代码

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 5 年之前
父节点
当前提交
dae4436d1c
共有 2 个文件被更改,包括 24 次插入0 次删除
  1. 8 0
      api/types/filters/parse.go
  2. 16 0
      daemon/config/builder.go

+ 8 - 0
api/types/filters/parse.go

@@ -36,6 +36,14 @@ func NewArgs(initialArgs ...KeyValuePair) Args {
 	return args
 }
 
+func (args Args) Keys() []string {
+	keys := make([]string, 0, len(args.fields))
+	for k := range args.fields {
+		keys = append(keys, k)
+	}
+	return keys
+}
+
 // MarshalJSON returns a JSON byte representation of the Args
 func (args Args) MarshalJSON() ([]byte, error) {
 	if len(args.fields) == 0 {

+ 16 - 0
daemon/config/builder.go

@@ -2,6 +2,8 @@ package config
 
 import (
 	"encoding/json"
+	"fmt"
+	"sort"
 	"strings"
 
 	"github.com/docker/docker/api/types/filters"
@@ -16,6 +18,20 @@ type BuilderGCRule struct {
 
 type BuilderGCFilter filters.Args
 
+func (x *BuilderGCFilter) MarshalJSON() ([]byte, error) {
+	f := filters.Args(*x)
+	keys := f.Keys()
+	sort.Strings(keys)
+	arr := make([]string, 0, len(keys))
+	for _, k := range keys {
+		values := f.Get(k)
+		for _, v := range values {
+			arr = append(arr, fmt.Sprintf("%s=%s", k, v))
+		}
+	}
+	return json.Marshal(arr)
+}
+
 func (x *BuilderGCFilter) UnmarshalJSON(data []byte) error {
 	var arr []string
 	f := filters.NewArgs()