| 1 | import json |
| 2 | |
| 3 | pub struct DocumentFindFilter[T] { |
| 4 | pub: |
| 5 | selector ?T |
| 6 | selector2 ?[]T |
| 7 | selector3 ?[1]T |
| 8 | limit ?int |
| 9 | skip ?int |
| 10 | fields ?[]string // This breaks the compiler when encoding to JSON |
| 11 | conflicts ?bool |
| 12 | read_quorum ?int @[json: r] |
| 13 | update ?bool |
| 14 | stable ?bool |
| 15 | stale ?string |
| 16 | execution_stats ?bool |
| 17 | } |
| 18 | |
| 19 | fn test_string() { |
| 20 | t := DocumentFindFilter[string]{ |
| 21 | selector: 'aa' |
| 22 | selector2: ['a', 'b'] |
| 23 | selector3: ['z']! |
| 24 | } |
| 25 | |
| 26 | assert json.encode(t) == '{"selector":"aa","selector2":["a","b"],"selector3":["z"]}' |
| 27 | assert json.decode(DocumentFindFilter[string], |
| 28 | '{"selector":"aa","selector2":["a","b"],"selector3":["z"]}')! == t |
| 29 | } |
| 30 | |
| 31 | fn test_int() ! { |
| 32 | t := DocumentFindFilter[int]{ |
| 33 | selector: 1 |
| 34 | selector2: [1, 2] |
| 35 | selector3: [3]! |
| 36 | } |
| 37 | |
| 38 | assert json.encode(t) == '{"selector":1,"selector2":[1,2],"selector3":[3]}' |
| 39 | assert json.decode(DocumentFindFilter[int], '{"selector":1,"selector2":[1,2],"selector3":[3]}')! == t |
| 40 | } |
| 41 | |
| 42 | fn test_none() ! { |
| 43 | t := DocumentFindFilter[int]{} |
| 44 | |
| 45 | assert json.encode(t) == '{}' |
| 46 | assert json.decode(DocumentFindFilter[int], '{}')! == t |
| 47 | } |
| 48 | |