/
marshaledwithhumandetails.go
35 lines (29 loc) · 1.28 KB
/
marshaledwithhumandetails.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package output
import (
"errors"
)
// MarshaledWithHumanDetails implements output.Command for a return value that is displayed as raw marshaled in JSON and YAML or as human details, such as UUID or IP address, eg. most create commands.
type MarshaledWithHumanDetails struct {
Value interface{}
Details []DetailRow
}
// MarshalJSON implements json.Marshaler and output.Output
func (d MarshaledWithHumanDetails) MarshalJSON() ([]byte, error) {
return OnlyMarshaled{Value: d.Value}.MarshalJSON()
}
// MarshalYAML implements output.Output, it marshals the value and returns the YAML as []byte
// nb. does *not* implement yaml.Marshaler
func (d MarshaledWithHumanDetails) MarshalYAML() ([]byte, error) {
return OnlyMarshaled{Value: d.Value}.MarshalYAML()
}
// MarshalHuman implements output.Output
// For MarshaledWithHumanDetails outputs, we return *only* the details part in humanized output
func (d MarshaledWithHumanDetails) MarshalHuman() ([]byte, error) {
return Details{Sections: []DetailSection{
{Rows: d.Details},
}}.MarshalHuman()
}
// MarshalRawMap implements output.Output
func (d MarshaledWithHumanDetails) MarshalRawMap() (map[string]interface{}, error) {
return nil, errors.New("MarshaledWithHumanDetails output should not be used as part of multiple output, raw output is undefined")
}