Skip to content

Commit f9ac9df

Browse files
author
wafuwafu13
committed
fix: remove MarshalJSON function
1 parent 7316ef1 commit f9ac9df

File tree

2 files changed

+21
-26
lines changed

2 files changed

+21
-26
lines changed

events/ecs_container_instance.go

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package events
22

3-
import (
4-
"encoding/json"
5-
"time"
6-
)
3+
import "time"
74

85
type ECSContainerInstanceEvent struct {
96
Version string `json:"version"`
@@ -47,24 +44,3 @@ type ECSContainerInstanceEventVersionInfo struct {
4744
AgentVersion string `json:"agentVersion"`
4845
DockerVersion string `json:"dockerVersion"`
4946
}
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-
}

events/ecs_container_instance_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,26 @@ func TestECSContainerInstanceEventMarshaling(t *testing.T) {
8282
}
8383

8484
// 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))
86105
}
87106

88107
func TestECSContainerInstanceMarshalingMalformedJson(t *testing.T) {

0 commit comments

Comments
 (0)