Skip to content

Commit 5a144c4

Browse files
committed
pkg/streamformatter: reduce jsonmessage.JSONMessage dependency
This package depends on jsonformatter.JSONProgress and jsonmessage.JSONMessage, and it looks like it requires some of those for their stringer interface. Signed-off-by: Derek McGowan <[email protected]> Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent f4127d7 commit 5a144c4

3 files changed

Lines changed: 52 additions & 41 deletions

File tree

pkg/streamformatter/streamformatter.go

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@ import (
1212
"github.com/moby/moby/api/types/jsonstream"
1313
)
1414

15+
// jsonMessage defines a message struct. It describes
16+
// the created time, where it from, status, ID of the
17+
// message. It's used for docker events.
18+
//
19+
// It is a reduced set of [jsonmessage.JSONMessage].
20+
type jsonMessage struct {
21+
Stream string `json:"stream,omitempty"`
22+
Status string `json:"status,omitempty"`
23+
Progress *jsonmessage.JSONProgress `json:"progressDetail,omitempty"` // TODO(thaJeztah): can this be a [jsonstream.Progress]?
24+
ID string `json:"id,omitempty"`
25+
Error *jsonstream.Error `json:"errorDetail,omitempty"`
26+
Aux *json.RawMessage `json:"aux,omitempty"` // Aux contains out-of-band data, such as digests for push signing and image id after building.
27+
28+
// ErrorMessage contains errors encountered during the operation.
29+
//
30+
// Deprecated: this field is deprecated since docker v0.6.0 / API v1.4. Use [Error.Message] instead. This field will be omitted in a future release.
31+
ErrorMessage string `json:"error,omitempty"` // deprecated
32+
}
33+
1534
const streamNewline = "\r\n"
1635

1736
type jsonProgressFormatter struct{}
@@ -23,7 +42,7 @@ func appendNewline(source []byte) []byte {
2342
// FormatStatus formats the specified objects according to the specified format (and id).
2443
func FormatStatus(id, format string, a ...interface{}) []byte {
2544
str := fmt.Sprintf(format, a...)
26-
b, err := json.Marshal(&jsonmessage.JSONMessage{ID: id, Status: str})
45+
b, err := json.Marshal(&jsonMessage{ID: id, Status: str})
2746
if err != nil {
2847
return FormatError(err)
2948
}
@@ -36,7 +55,7 @@ func FormatError(err error) []byte {
3655
if !ok {
3756
jsonError = &jsonstream.Error{Message: err.Error()}
3857
}
39-
if b, err := json.Marshal(&jsonmessage.JSONMessage{Error: jsonError, ErrorMessage: err.Error()}); err == nil {
58+
if b, err := json.Marshal(&jsonMessage{Error: jsonError, ErrorMessage: err.Error()}); err == nil {
4059
return appendNewline(b)
4160
}
4261
return []byte(`{"error":"format error"}` + streamNewline)
@@ -47,9 +66,9 @@ func (sf *jsonProgressFormatter) formatStatus(id, format string, a ...interface{
4766
}
4867

4968
// formatProgress formats the progress information for a specified action.
50-
func (sf *jsonProgressFormatter) formatProgress(id, action string, progress *jsonmessage.JSONProgress, aux interface{}) []byte {
69+
func (sf *jsonProgressFormatter) formatProgress(id, action string, progress *jsonstream.Progress, aux interface{}) []byte {
5170
if progress == nil {
52-
progress = &jsonmessage.JSONProgress{}
71+
progress = &jsonstream.Progress{}
5372
}
5473
var auxJSON *json.RawMessage
5574
if aux != nil {
@@ -60,12 +79,11 @@ func (sf *jsonProgressFormatter) formatProgress(id, action string, progress *jso
6079
auxJSON = new(json.RawMessage)
6180
*auxJSON = auxJSONBytes
6281
}
63-
b, err := json.Marshal(&jsonmessage.JSONMessage{
64-
Status: action,
65-
ProgressMessage: progress.String(),
66-
Progress: progress,
67-
ID: id,
68-
Aux: auxJSON,
82+
b, err := json.Marshal(&jsonMessage{
83+
Status: action,
84+
Progress: &jsonmessage.JSONProgress{Progress: *progress},
85+
ID: id,
86+
Aux: auxJSON,
6987
})
7088
if err != nil {
7189
return nil
@@ -79,15 +97,16 @@ func (sf *rawProgressFormatter) formatStatus(id, format string, a ...interface{}
7997
return []byte(fmt.Sprintf(format, a...) + streamNewline)
8098
}
8199

82-
func (sf *rawProgressFormatter) formatProgress(id, action string, progress *jsonmessage.JSONProgress, aux interface{}) []byte {
100+
func (sf *rawProgressFormatter) formatProgress(id, action string, progress *jsonstream.Progress, aux interface{}) []byte {
83101
if progress == nil {
84-
progress = &jsonmessage.JSONProgress{}
102+
progress = &jsonstream.Progress{}
85103
}
86104
endl := "\r"
87-
if progress.String() == "" {
105+
out := (&jsonmessage.JSONProgress{Progress: *progress}).String()
106+
if out == "" {
88107
endl += "\n"
89108
}
90-
return []byte(action + " " + progress.String() + endl)
109+
return []byte(action + " " + out + endl)
91110
}
92111

93112
// NewProgressOutput returns a progress.Output object that can be passed to
@@ -104,7 +123,7 @@ func NewJSONProgressOutput(out io.Writer, newLines bool) progress.Output {
104123

105124
type formatProgress interface {
106125
formatStatus(id, format string, a ...interface{}) []byte
107-
formatProgress(id, action string, progress *jsonmessage.JSONProgress, aux interface{}) []byte
126+
formatProgress(id, action string, progress *jsonstream.Progress, aux interface{}) []byte
108127
}
109128

110129
type progressOutput struct {
@@ -120,13 +139,11 @@ func (out *progressOutput) WriteProgress(prog progress.Progress) error {
120139
if prog.Message != "" {
121140
formatted = out.sf.formatStatus(prog.ID, prog.Message)
122141
} else {
123-
jsonProgress := jsonmessage.JSONProgress{
124-
Progress: jsonstream.Progress{
125-
Current: prog.Current,
126-
Total: prog.Total,
127-
HideCounts: prog.HideCounts,
128-
Units: prog.Units,
129-
},
142+
jsonProgress := jsonstream.Progress{
143+
Current: prog.Current,
144+
Total: prog.Total,
145+
HideCounts: prog.HideCounts,
146+
Units: prog.Units,
130147
}
131148
formatted = out.sf.formatProgress(prog.ID, prog.Action, &jsonProgress, prog.Aux)
132149
}
@@ -159,7 +176,7 @@ func (sf *AuxFormatter) Emit(id string, aux interface{}) error {
159176
}
160177
auxJSON := new(json.RawMessage)
161178
*auxJSON = auxJSONBytes
162-
msgJSON, err := json.Marshal(&jsonmessage.JSONMessage{ID: id, Aux: auxJSON})
179+
msgJSON, err := json.Marshal(&jsonMessage{ID: id, Aux: auxJSON})
163180
if err != nil {
164181
return err
165182
}

pkg/streamformatter/streamformatter_test.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@ func TestRawProgressFormatterFormatStatus(t *testing.T) {
2323

2424
func TestRawProgressFormatterFormatProgress(t *testing.T) {
2525
sf := rawProgressFormatter{}
26-
jsonProgress := &jsonmessage.JSONProgress{
27-
Progress: jsonstream.Progress{
28-
Current: 15,
29-
Total: 30,
30-
Start: 1,
31-
},
26+
jsonProgress := &jsonstream.Progress{
27+
Current: 15,
28+
Total: 30,
29+
Start: 1,
3230
}
3331
res := sf.formatProgress("id", "action", jsonProgress, nil)
3432
out := string(res)
@@ -58,25 +56,23 @@ func TestFormatJSONError(t *testing.T) {
5856

5957
func TestJsonProgressFormatterFormatProgress(t *testing.T) {
6058
sf := &jsonProgressFormatter{}
61-
jsonProgress := &jsonmessage.JSONProgress{
62-
Progress: jsonstream.Progress{
63-
Current: 15,
64-
Total: 30,
65-
Start: 1,
66-
},
59+
jsonProgress := &jsonstream.Progress{
60+
Current: 15,
61+
Total: 30,
62+
Start: 1,
6763
}
6864
aux := "aux message"
6965
res := sf.formatProgress("id", "action", jsonProgress, aux)
70-
msg := &jsonmessage.JSONMessage{}
66+
msg := &jsonMessage{}
7167

7268
assert.NilError(t, json.Unmarshal(res, msg))
7369

7470
rawAux := json.RawMessage(`"` + aux + `"`)
75-
expected := &jsonmessage.JSONMessage{
71+
expected := &jsonMessage{
7672
ID: "id",
7773
Status: "action",
7874
Aux: &rawAux,
79-
Progress: jsonProgress,
75+
Progress: &jsonmessage.JSONProgress{Progress: *jsonProgress},
8076
}
8177
assert.DeepEqual(t, msg, expected, cmpJSONMessageOpt())
8278
}

pkg/streamformatter/streamwriter.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package streamformatter
33
import (
44
"encoding/json"
55
"io"
6-
7-
"github.com/docker/docker/pkg/jsonmessage"
86
)
97

108
type streamWriter struct {
@@ -22,7 +20,7 @@ func (sw *streamWriter) Write(buf []byte) (int, error) {
2220
}
2321

2422
func (sw *streamWriter) format(buf []byte) []byte {
25-
msg := &jsonmessage.JSONMessage{Stream: sw.lineFormat(buf)}
23+
msg := &jsonMessage{Stream: sw.lineFormat(buf)}
2624
b, err := json.Marshal(msg)
2725
if err != nil {
2826
return FormatError(err)

0 commit comments

Comments
 (0)