|
1 | 1 | package jsonry_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "errors" |
5 | 6 |
|
6 | 7 | "code.cloudfoundry.org/jsonry" |
@@ -209,50 +210,102 @@ var _ = Describe("Marshal", func() { |
209 | 210 | }) |
210 | 211 |
|
211 | 212 | Describe("omitempty", func() { |
212 | | - It("omits zero values of basic types", func() { |
| 213 | + It("reads the `omitempty` field from JSON and JSONry tags with and without names", func() { |
213 | 214 | s := struct { |
214 | 215 | A string `json:",omitempty"` |
215 | 216 | B string `json:"bee,omitempty"` |
216 | 217 | C string `jsonry:",omitempty"` |
217 | 218 | D string `jsonry:"dee,omitempty"` |
218 | | - E string |
219 | 219 | }{} |
220 | | - expectToMarshal(s, `{"E":""}`) |
| 220 | + expectToMarshal(s, `{}`) |
| 221 | + }) |
| 222 | + |
| 223 | + // and any empty array, slice, map, or string. |
| 224 | + |
| 225 | + It("omits false", func() { |
| 226 | + s := struct { |
| 227 | + A bool `jsonry:",omitempty"` |
| 228 | + }{} |
| 229 | + expectToMarshal(s, `{}`) |
| 230 | + }) |
| 231 | + |
| 232 | + It("omits 0", func() { |
| 233 | + s := struct { |
| 234 | + A int `jsonry:",omitempty"` |
| 235 | + B uint `jsonry:",omitempty"` |
| 236 | + C float64 `jsonry:",omitempty"` |
| 237 | + }{} |
| 238 | + expectToMarshal(s, `{}`) |
221 | 239 | }) |
222 | 240 |
|
223 | | - It("omits zero value structs", func() { |
| 241 | + It("omits nil pointers", func() { |
224 | 242 | type t struct{ A string } |
225 | 243 | s := struct { |
226 | | - B t `jsonry:",omitempty"` |
| 244 | + A *string `jsonry:",omitempty"` |
| 245 | + B *t `jsonry:",omitempty"` |
| 246 | + C *[]string `jsonry:",omitempty"` |
| 247 | + D *map[int]int `jsonry:",omitempty"` |
| 248 | + E *interface{} `jsonry:",omitempty"` |
227 | 249 | }{} |
228 | 250 | expectToMarshal(s, `{}`) |
229 | 251 | }) |
230 | 252 |
|
231 | | - It("omits empty lists", func() { |
| 253 | + It("omits nil interface values", func() { |
232 | 254 | s := struct { |
233 | | - A []string `jsonry:",omitempty"` |
234 | | - D [0]string `jsonry:",omitempty"` |
| 255 | + A interface{} `jsonry:",omitempty"` |
| 256 | + B json.Marshaler `jsonry:",omitempty"` |
235 | 257 | }{} |
236 | 258 | expectToMarshal(s, `{}`) |
237 | 259 | }) |
238 | 260 |
|
| 261 | + It("omits empty arrays", func() { |
| 262 | + s := struct { |
| 263 | + A [0]int `jsonry:",omitempty"` |
| 264 | + }{A: [0]int{}} |
| 265 | + expectToMarshal(s, `{}`) |
| 266 | + }) |
| 267 | + |
| 268 | + It("omits empty slices", func() { |
| 269 | + s := struct { |
| 270 | + A []int `jsonry:",omitempty"` |
| 271 | + B []int `jsonry:",omitempty"` |
| 272 | + C []int `jsonry:",omitempty"` |
| 273 | + }{ |
| 274 | + B: []int{}, |
| 275 | + C: make([]int, 0, 1), |
| 276 | + } |
| 277 | + expectToMarshal(s, `{}`) |
| 278 | + }) |
| 279 | + |
239 | 280 | It("omits empty maps", func() { |
240 | 281 | s := struct { |
241 | 282 | A map[interface{}]interface{} `jsonry:",omitempty"` |
242 | 283 | D map[int]int `jsonry:",omitempty"` |
243 | | - }{} |
| 284 | + }{ |
| 285 | + D: make(map[int]int), |
| 286 | + } |
244 | 287 | expectToMarshal(s, `{}`) |
245 | 288 | }) |
246 | 289 |
|
247 | | - It("omits nil pointers", func() { |
| 290 | + It("omits empty strings", func() { |
248 | 291 | s := struct { |
249 | | - A *string `json:",omitempty"` |
250 | | - B *string `json:"bee,omitempty"` |
251 | | - C *string `jsonry:",omitempty"` |
252 | | - D *string `jsonry:"dee,omitempty"` |
253 | | - E *string |
| 292 | + A string `jsonry:",omitempty"` |
| 293 | + B string `jsonry:",omitempty"` |
| 294 | + }{ |
| 295 | + B: "", |
| 296 | + } |
| 297 | + expectToMarshal(s, `{}`) |
| 298 | + }) |
| 299 | + |
| 300 | + // For consistency with json.Marshal, see https://github.com/golang/go/issues/11939 |
| 301 | + It("does not omit empty structs", func() { |
| 302 | + type t struct { |
| 303 | + A string `jsonry:",omitempty"` |
| 304 | + } |
| 305 | + s := struct { |
| 306 | + B t `jsonry:",omitempty"` |
254 | 307 | }{} |
255 | | - expectToMarshal(s, `{"E":null}`) |
| 308 | + expectToMarshal(s, `{"B":{}}`) |
256 | 309 | }) |
257 | 310 | }) |
258 | 311 |
|
|
0 commit comments