mirror of https://github.com/docker/cli.git
Merge pull request #2572 from simonferquel/context-dont-loose-additional-fields
Don't loose additional metadata fields when read/writing contexts metadata
This commit is contained in:
commit
af2c31c4a7
|
@ -1,6 +1,7 @@
|
||||||
package command
|
package command
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/docker/cli/cli/context/store"
|
"github.com/docker/cli/cli/context/store"
|
||||||
|
@ -8,8 +9,48 @@ import (
|
||||||
|
|
||||||
// DockerContext is a typed representation of what we put in Context metadata
|
// DockerContext is a typed representation of what we put in Context metadata
|
||||||
type DockerContext struct {
|
type DockerContext struct {
|
||||||
Description string `json:",omitempty"`
|
Description string
|
||||||
StackOrchestrator Orchestrator `json:",omitempty"`
|
StackOrchestrator Orchestrator
|
||||||
|
AdditionalFields map[string]interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalJSON implements custom JSON marshalling
|
||||||
|
func (dc DockerContext) MarshalJSON() ([]byte, error) {
|
||||||
|
s := map[string]interface{}{}
|
||||||
|
if dc.Description != "" {
|
||||||
|
s["Description"] = dc.Description
|
||||||
|
}
|
||||||
|
if dc.StackOrchestrator != "" {
|
||||||
|
s["StackOrchestrator"] = dc.StackOrchestrator
|
||||||
|
}
|
||||||
|
if dc.AdditionalFields != nil {
|
||||||
|
for k, v := range dc.AdditionalFields {
|
||||||
|
s[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return json.Marshal(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON implements custom JSON marshalling
|
||||||
|
func (dc *DockerContext) UnmarshalJSON(payload []byte) error {
|
||||||
|
var data map[string]interface{}
|
||||||
|
if err := json.Unmarshal(payload, &data); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for k, v := range data {
|
||||||
|
switch k {
|
||||||
|
case "Description":
|
||||||
|
dc.Description = v.(string)
|
||||||
|
case "StackOrchestrator":
|
||||||
|
dc.StackOrchestrator = Orchestrator(v.(string))
|
||||||
|
default:
|
||||||
|
if dc.AdditionalFields == nil {
|
||||||
|
dc.AdditionalFields = make(map[string]interface{})
|
||||||
|
}
|
||||||
|
dc.AdditionalFields[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDockerContext extracts metadata from stored context metadata
|
// GetDockerContext extracts metadata from stored context metadata
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
package command
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"gotest.tools/v3/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDockerContextMetadataKeepAdditionalFields(t *testing.T) {
|
||||||
|
c := DockerContext{
|
||||||
|
Description: "test",
|
||||||
|
StackOrchestrator: OrchestratorSwarm,
|
||||||
|
AdditionalFields: map[string]interface{}{
|
||||||
|
"foo": "bar",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
jsonBytes, err := json.Marshal(c)
|
||||||
|
assert.NilError(t, err)
|
||||||
|
assert.Equal(t, `{"Description":"test","StackOrchestrator":"swarm","foo":"bar"}`, string(jsonBytes))
|
||||||
|
|
||||||
|
var c2 DockerContext
|
||||||
|
assert.NilError(t, json.Unmarshal(jsonBytes, &c2))
|
||||||
|
assert.Equal(t, c2.AdditionalFields["foo"], "bar")
|
||||||
|
assert.Equal(t, c2.StackOrchestrator, OrchestratorSwarm)
|
||||||
|
assert.Equal(t, c2.Description, "test")
|
||||||
|
}
|
Loading…
Reference in New Issue