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 85733620eb)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Tibor Vass 2019-10-03 00:09:50 +00:00 committed by Sebastiaan van Stijn
parent 1e26b431c9
commit dae4436d1c
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
2 changed files with 24 additions and 0 deletions

View file

@ -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 {

View file

@ -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()