File tree 2 files changed +21
-26
lines changed 2 files changed +21
-26
lines changed Original file line number Diff line number Diff line change 1
1
package events
2
2
3
- import (
4
- "encoding/json"
5
- "time"
6
- )
3
+ import "time"
7
4
8
5
type ECSContainerInstanceEvent struct {
9
6
Version string `json:"version"`
@@ -47,24 +44,3 @@ type ECSContainerInstanceEventVersionInfo struct {
47
44
AgentVersion string `json:"agentVersion"`
48
45
DockerVersion string `json:"dockerVersion"`
49
46
}
50
-
51
- // MarshalJSON implements custom marshaling to marshal the struct into JSON format while preserving an empty string slice in `StringSetValue` field.
52
- func (r ECSContainerInstanceEventResource ) MarshalJSON () ([]byte , error ) {
53
- type Alias ECSContainerInstanceEventResource
54
- aux := struct {
55
- StringSetValue json.RawMessage `json:"stringSetValue,omitempty"`
56
- Alias
57
- }{
58
- Alias : (Alias )(r ),
59
- }
60
-
61
- if r .StringSetValue != nil {
62
- b , err := json .Marshal (r .StringSetValue )
63
- if err != nil {
64
- return nil , err
65
- }
66
- aux .StringSetValue = b
67
- }
68
-
69
- return json .Marshal (& aux )
70
- }
Original file line number Diff line number Diff line change @@ -82,7 +82,26 @@ func TestECSContainerInstanceEventMarshaling(t *testing.T) {
82
82
}
83
83
84
84
// 5. check result
85
- assert .JSONEq (t , string (inputJSON ), string (outputJSON ))
85
+ // `registeredResources` key and `remainingResources` key have `stringSetValue` key.
86
+ // if `stringSetValue` key is empty array, it should be removed from outputJSON.
87
+ var result map [string ]interface {}
88
+ json .Unmarshal (outputJSON , & result )
89
+ for _ , resourcesType := range []string {"registeredResources" , "remainingResources" } {
90
+ resources := result ["detail" ].(map [string ]interface {})[resourcesType ].([]interface {})
91
+ for i , resource := range resources {
92
+ resourceMap := resource .(map [string ]interface {})
93
+ if _ , ok := resourceMap ["stringSetValue" ]; ok {
94
+ stringSetValue := resourceMap ["stringSetValue" ].([]interface {})
95
+ if len (stringSetValue ) == 0 {
96
+ delete (resourceMap , "stringSetValue" )
97
+ resources [i ] = resourceMap
98
+ }
99
+ }
100
+ }
101
+ }
102
+ resultJSON , _ := json .Marshal (result )
103
+
104
+ assert .JSONEq (t , string (outputJSON ), string (resultJSON ))
86
105
}
87
106
88
107
func TestECSContainerInstanceMarshalingMalformedJson (t * testing.T ) {
You can’t perform that action at this time.
0 commit comments