Skip to content

Commit 491aba9

Browse files
authored
Merge pull request #13 from kenshin579/feat/#12-empty
[#12] not to cache if all value of fields are empty
2 parents 6ff89a9 + fa98473 commit 491aba9

File tree

3 files changed

+103
-10
lines changed

3 files changed

+103
-10
lines changed

cache.go

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,15 +296,49 @@ func isAllFieldsEmpty(inter any) bool {
296296
return true
297297
}
298298

299-
if val.Kind() != reflect.Struct {
300-
return false
301-
}
299+
switch val.Kind() {
300+
case reflect.Struct:
301+
for i := 0; i < val.NumField(); i++ {
302+
field := val.Field(i)
303+
zeroValue := reflect.Zero(field.Type())
304+
if reflect.DeepEqual(field.Interface(), zeroValue.Interface()) {
305+
continue
306+
}
307+
return false
308+
}
309+
case reflect.Slice:
310+
var dataMap map[string]any
302311

303-
for i := 0; i < val.NumField(); i++ {
304-
field := val.Field(i)
305-
if !field.IsZero() {
312+
if err := json.Unmarshal(inter.([]byte), &dataMap); err != nil {
313+
fmt.Printf("fail to unmarshal json. err:%v\n", err)
306314
return false
307315
}
316+
return isMapEmpty(dataMap)
317+
}
318+
319+
return true
320+
}
321+
322+
func isMapEmpty(m map[string]any) bool {
323+
for _, v := range m {
324+
switch val := v.(type) {
325+
case string:
326+
if val != "" {
327+
return false
328+
}
329+
case int:
330+
if val != 0 {
331+
return false
332+
}
333+
case float64:
334+
if val != 0 {
335+
return false
336+
}
337+
case bool:
338+
if val == false {
339+
return false
340+
}
341+
}
308342
}
309343
return true
310344
}

cache_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,44 @@ func Test_sortURLParams(t *testing.T) {
279279
})
280280
}
281281
}
282+
283+
func Test_isAllFieldsEmpty(t *testing.T) {
284+
type person struct {
285+
Name string `json:"name"`
286+
Age int `json:"age"`
287+
Address struct {
288+
City string `json:"city"`
289+
Zip int `json:"zip"`
290+
} `json:"address"`
291+
}
292+
293+
p1 := person{
294+
Address: struct {
295+
City string `json:"city"`
296+
Zip int `json:"zip"`
297+
}{
298+
City: "seoul",
299+
},
300+
}
301+
302+
p2 := person{}
303+
p3 := person{
304+
Name: "",
305+
Age: 0,
306+
Address: struct {
307+
City string `json:"city"`
308+
Zip int `json:"zip"`
309+
}{
310+
City: "",
311+
Zip: 0,
312+
},
313+
}
314+
315+
assert.False(t, isAllFieldsEmpty(p1))
316+
assert.True(t, isAllFieldsEmpty(p2))
317+
assert.True(t, isAllFieldsEmpty(p3))
318+
assert.False(t, isAllFieldsEmpty([]byte(`{"a":"","b":"","c":1}`)))
319+
assert.False(t, isAllFieldsEmpty([]byte(`{"a":"","b":"b","c":0}`)))
320+
assert.True(t, isAllFieldsEmpty([]byte(`{"a":"","b":"","c":0}`)))
321+
assert.True(t, isAllFieldsEmpty([]byte(`{"a":"","b":"","c":0.0}`)))
322+
}

redis_test.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,14 @@ func (suite *cacheRedisStoreTestSuite) Test_Echo_CacheWithConfig() {
7474
return c.String(http.StatusOK, "test")
7575
})
7676

77-
suite.echo.GET("/empty", func(c echo.Context) error {
77+
suite.echo.GET("/empty/string", func(c echo.Context) error {
7878
return c.String(http.StatusOK, "")
7979
})
8080

81+
suite.echo.GET("/empty/json", func(c echo.Context) error {
82+
return c.String(http.StatusOK, `{"symbolId":"","type":"","price":0.0}`)
83+
})
84+
8185
suite.Run("GET /test with non empty body", func() {
8286
req := httptest.NewRequest(http.MethodGet, "/test", nil)
8387
rec := httptest.NewRecorder()
@@ -97,16 +101,30 @@ func (suite *cacheRedisStoreTestSuite) Test_Echo_CacheWithConfig() {
97101
suite.Equal("test", string(cacheResponse.Value))
98102
})
99103

100-
suite.Run("GET /empty", func() {
101-
req := httptest.NewRequest(http.MethodGet, "/empty", nil)
104+
suite.Run("GET /empty/string", func() {
105+
req := httptest.NewRequest(http.MethodGet, "/empty/string", nil)
102106
rec := httptest.NewRecorder()
103107

104108
suite.echo.ServeHTTP(rec, req)
105109

106110
suite.Equal(http.StatusOK, rec.Code)
107111
suite.Equal("", rec.Body.String())
108112

109-
key := generateKey(http.MethodGet, "/empty")
113+
key := generateKey(http.MethodGet, "/empty/string")
114+
_, ok := suite.cacheStore.Get(key)
115+
suite.False(ok)
116+
})
117+
118+
suite.Run("GET /empty/json", func() {
119+
req := httptest.NewRequest(http.MethodGet, "/empty/json", nil)
120+
rec := httptest.NewRecorder()
121+
122+
suite.echo.ServeHTTP(rec, req)
123+
124+
suite.Equal(http.StatusOK, rec.Code)
125+
suite.Equal(`{"symbolId":"","type":"","price":0.0}`, rec.Body.String())
126+
127+
key := generateKey(http.MethodGet, "/empty2")
110128
_, ok := suite.cacheStore.Get(key)
111129
suite.False(ok)
112130
})

0 commit comments

Comments
 (0)