|
| 1 | +import x.json2.decoder2 as json |
| 2 | + |
| 3 | +struct StruWithJsonAttribute { |
| 4 | + a int |
| 5 | + name2 string @[json: 'name'] |
| 6 | + b int |
| 7 | +} |
| 8 | + |
| 9 | +struct StruWithSkipAttribute { |
| 10 | + a int |
| 11 | + name ?string @[skip] |
| 12 | + b int |
| 13 | +} |
| 14 | + |
| 15 | +struct StruWithJsonSkipAttribute { |
| 16 | + a int |
| 17 | + name ?string @[json: '-'] |
| 18 | + b int |
| 19 | +} |
| 20 | + |
| 21 | +struct StruWithOmitemptyAttribute { |
| 22 | + a int |
| 23 | + name ?string @[omitempty] |
| 24 | + b int |
| 25 | +} |
| 26 | + |
| 27 | +struct StruWithRawAttribute { |
| 28 | + a int |
| 29 | + name string @[raw] |
| 30 | + object string @[raw] |
| 31 | + b int |
| 32 | +} |
| 33 | + |
| 34 | +struct StruWithRequiredAttribute { |
| 35 | + a int |
| 36 | + name string @[required] |
| 37 | + skip_and_required ?string @[required; skip] |
| 38 | + b int |
| 39 | +} |
| 40 | + |
| 41 | +fn test_skip_and_rename_attributes() { |
| 42 | + assert json.decode[StruWithJsonAttribute]('{"name": "hola1", "a": 2, "b": 3}')! == StruWithJsonAttribute{ |
| 43 | + a: 2 |
| 44 | + name2: 'hola1' |
| 45 | + b: 3 |
| 46 | + }, '`json` attribute not working' |
| 47 | + |
| 48 | + assert json.decode[StruWithSkipAttribute]('{"name": "hola2", "a": 2, "b": 3}')! == StruWithSkipAttribute{ |
| 49 | + a: 2 |
| 50 | + name: none |
| 51 | + b: 3 |
| 52 | + }, '`skip` attribute not working' |
| 53 | + |
| 54 | + assert json.decode[StruWithJsonSkipAttribute]('{"name": "hola3", "a": 2, "b": 3}')! == StruWithJsonSkipAttribute{ |
| 55 | + a: 2 |
| 56 | + name: none |
| 57 | + b: 3 |
| 58 | + }, " `json: '-'` skip attribute not working" |
| 59 | + |
| 60 | + assert json.decode[StruWithOmitemptyAttribute]('{"name": "", "a": 2, "b": 3}')! == StruWithOmitemptyAttribute{ |
| 61 | + a: 2 |
| 62 | + name: none |
| 63 | + b: 3 |
| 64 | + }, '`omitempty` attribute not working' |
| 65 | + |
| 66 | + assert json.decode[StruWithOmitemptyAttribute]('{"name": "hola", "a": 2, "b": 3}')! == StruWithOmitemptyAttribute{ |
| 67 | + a: 2 |
| 68 | + name: 'hola' |
| 69 | + b: 3 |
| 70 | + }, '`omitempty` attribute not working' |
| 71 | +} |
| 72 | + |
| 73 | +fn test_raw_attribute() { |
| 74 | + assert json.decode[StruWithRawAttribute]('{"name": "hola", "a": 2, "object": {"c": 4, "d": 5}, "b": 3}')! == StruWithRawAttribute{ |
| 75 | + a: 2 |
| 76 | + name: '"hola"' |
| 77 | + object: '{"c": 4, "d": 5}' |
| 78 | + b: 3 |
| 79 | + }, '`raw` attribute not working' |
| 80 | +} |
| 81 | + |
| 82 | +fn test_required_attribute() { |
| 83 | + assert json.decode[StruWithRequiredAttribute]('{"name": "hola", "a": 2, "skip_and_required": "hola", "b": 3}')! == StruWithRequiredAttribute{ |
| 84 | + a: 2 |
| 85 | + name: 'hola' |
| 86 | + skip_and_required: none |
| 87 | + b: 3 |
| 88 | + }, '`required` attribute not working' |
| 89 | + |
| 90 | + mut has_error := false |
| 91 | + |
| 92 | + json.decode[StruWithRequiredAttribute]('{"name": "hola", "a": 2, "b": 3}') or { |
| 93 | + has_error = true |
| 94 | + assert err.msg() == 'missing required field `skip_and_required`' |
| 95 | + } |
| 96 | + |
| 97 | + assert has_error, '`required` attribute not working. It should have failed' |
| 98 | + has_error = false |
| 99 | +} |
0 commit comments