From 85733620ebea3da75abe7d732043354aa0883f8a Mon Sep 17 00:00:00 2001 From: Tibor Vass Date: Thu, 3 Oct 2019 00:09:50 +0000 Subject: [PATCH] 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 --- api/types/filters/parse.go | 8 ++++++++ daemon/config/builder.go | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/api/types/filters/parse.go b/api/types/filters/parse.go index 2e24e769c1..4c5aff6616 100644 --- a/api/types/filters/parse.go +++ b/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 { diff --git a/daemon/config/builder.go b/daemon/config/builder.go index 8559a4f2d1..8d2d12b671 100644 --- a/daemon/config/builder.go +++ b/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()