Skip to content

Commit 43a4e09

Browse files
authored
Merge pull request #2586 from thaJeztah/19.03_backport_context_dont_loose_additional_fields
[19.03 backport] Don't loose additional metadata fields
2 parents 6051b36 + cfa1fd9 commit 43a4e09

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

cli/command/context.go

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,56 @@
11
package command
22

33
import (
4+
"encoding/json"
45
"errors"
56

67
"github.com/docker/cli/cli/context/store"
78
)
89

910
// DockerContext is a typed representation of what we put in Context metadata
1011
type DockerContext struct {
11-
Description string `json:",omitempty"`
12-
StackOrchestrator Orchestrator `json:",omitempty"`
12+
Description string
13+
StackOrchestrator Orchestrator
14+
AdditionalFields map[string]interface{}
15+
}
16+
17+
// MarshalJSON implements custom JSON marshalling
18+
func (dc DockerContext) MarshalJSON() ([]byte, error) {
19+
s := map[string]interface{}{}
20+
if dc.Description != "" {
21+
s["Description"] = dc.Description
22+
}
23+
if dc.StackOrchestrator != "" {
24+
s["StackOrchestrator"] = dc.StackOrchestrator
25+
}
26+
if dc.AdditionalFields != nil {
27+
for k, v := range dc.AdditionalFields {
28+
s[k] = v
29+
}
30+
}
31+
return json.Marshal(s)
32+
}
33+
34+
// UnmarshalJSON implements custom JSON marshalling
35+
func (dc *DockerContext) UnmarshalJSON(payload []byte) error {
36+
var data map[string]interface{}
37+
if err := json.Unmarshal(payload, &data); err != nil {
38+
return err
39+
}
40+
for k, v := range data {
41+
switch k {
42+
case "Description":
43+
dc.Description = v.(string)
44+
case "StackOrchestrator":
45+
dc.StackOrchestrator = Orchestrator(v.(string))
46+
default:
47+
if dc.AdditionalFields == nil {
48+
dc.AdditionalFields = make(map[string]interface{})
49+
}
50+
dc.AdditionalFields[k] = v
51+
}
52+
}
53+
return nil
1354
}
1455

1556
// GetDockerContext extracts metadata from stored context metadata

cli/command/context_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package command
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
7+
"gotest.tools/v3/assert"
8+
)
9+
10+
func TestDockerContextMetadataKeepAdditionalFields(t *testing.T) {
11+
c := DockerContext{
12+
Description: "test",
13+
StackOrchestrator: OrchestratorSwarm,
14+
AdditionalFields: map[string]interface{}{
15+
"foo": "bar",
16+
},
17+
}
18+
jsonBytes, err := json.Marshal(c)
19+
assert.NilError(t, err)
20+
assert.Equal(t, `{"Description":"test","StackOrchestrator":"swarm","foo":"bar"}`, string(jsonBytes))
21+
22+
var c2 DockerContext
23+
assert.NilError(t, json.Unmarshal(jsonBytes, &c2))
24+
assert.Equal(t, c2.AdditionalFields["foo"], "bar")
25+
assert.Equal(t, c2.StackOrchestrator, OrchestratorSwarm)
26+
assert.Equal(t, c2.Description, "test")
27+
}

0 commit comments

Comments
 (0)