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>
This commit is contained in:
Tibor Vass 2019-10-03 00:09:50 +00:00
parent fbdd437d29
commit 85733620eb
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()