-
Notifications
You must be signed in to change notification settings - Fork 132
/
hardware_wrapper.go
96 lines (91 loc) · 3.35 KB
/
hardware_wrapper.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package pkg
import (
"encoding/json"
"github.com/tinkerbell/tink/protos/hardware"
)
// HardwareWrapper is a wrapper for the Hardware type generated from the protos
// to allow for custom marshalling/unmarshalling of the Hardware type
//
// Why we need this:
// Since the metadata field is a json formatted string, when it's being marshaled
// along with the rest of the Hardware object to json format, the double quotes
// inside the metadata string would be automatically escaped, making it hard to read/consume.
//
// The point of the custom marshal/unmarshal is to manually convert the metadata field
// between a string (for creating a hardware object) and a map (for printing) as a workaround
// for this problem.
type HardwareWrapper struct {
*hardware.Hardware
}
// MarshalJSON marshals the HardwareWrapper object, with the metadata field represented as a map
// as the final result
//
// Since the Hardware object's metadata field is of type string, to convert the field into a map
// we need to solve somewhat of a water jug problem.
//
// 1. Create an empty map A
// 2. Marshal Hardware object to then be unmarshaled into map A
// 3. Create another empty map B to unmarshal metadata string into
// 4. Set map B as map A's metadata field
// 5. Marshal map A.
func (h HardwareWrapper) MarshalJSON() ([]byte, error) {
tmp := make(map[string]interface{}) // map (A) to hold metadata as a map (as opposed to string)
hwByte, err := json.Marshal(h.Hardware) // marshal hardware object
if err != nil {
return nil, err
}
err = json.Unmarshal(hwByte, &tmp) // fill out the tmp map
if err != nil {
return nil, err
}
if h.Metadata != "" {
metadata := make(map[string]interface{}) // map (B) to unmarshal metadata string into
err = json.Unmarshal([]byte(h.Metadata), &metadata) // fill metadata map
if err != nil {
return nil, err
}
tmp["metadata"] = metadata // set hw metadata field to the metadata map
}
tmpByte, err := json.Marshal(tmp) // marshal hw map into []byte
if err != nil {
return nil, err
}
return tmpByte, err
}
// UnmarshalJSON unmarshals the HardwareWrapper object, with the metadata field represented as a string
// as the final result
//
// Since the incoming []byte b represents the metadata as a map, we can't directly unmarshal it into
// a Hardware object, cue water jug problem.
//
// 1. Create empty map
// 2. Unmarshal []byte b into map
// 3. Marshal metadata field
// 4. Set metadata string as map's metadata field
// 5. Marshal map to then be unmarshaled into Hardware object.
func (h *HardwareWrapper) UnmarshalJSON(b []byte) error {
type hwWrapper HardwareWrapper // intermediary type to avoid infinite recursion
tmp := make(map[string]interface{}) // map to hold metadata as a string (as well as all the hardware data)
err := json.Unmarshal(b, &tmp) // unmarshal []byte b into map
if err != nil {
return err
}
if _, ok := tmp["metadata"]; ok { // check if there's metadata
metadata, err := json.Marshal(tmp["metadata"]) // marshal metadata field
if err != nil {
return err
}
tmp["metadata"] = string(metadata) // set tmp metadata field to the metadata string
}
tmpByte, err := json.Marshal(tmp) // marshal map tmp
if err != nil {
return err
}
var hw hwWrapper
err = json.Unmarshal(tmpByte, &hw) // unmarshal tmpByte into hardware object
if err != nil {
return err
}
*h = HardwareWrapper(hw)
return nil
}