| 1 | struct EmptyStruct { |
| 2 | b []string |
| 3 | pub: |
| 4 | a int |
| 5 | } |
| 6 | |
| 7 | fn encode_struct[T](val T) { |
| 8 | mut result := map[string][]string{} |
| 9 | $for field in T.fields { |
| 10 | result[field.name] = []string{} |
| 11 | $if field.is_array == false { |
| 12 | result[field.name] << '> is not array' |
| 13 | } $else { |
| 14 | result[field.name] << '> is array' |
| 15 | } |
| 16 | $if !field.is_array { |
| 17 | result[field.name] << '>> is not array' |
| 18 | } $else $if field.is_pub { |
| 19 | result[field.name] << '>> is public' |
| 20 | } $else { |
| 21 | result[field.name] << '>> is array' |
| 22 | } |
| 23 | $if field.is_array { |
| 24 | result[field.name] << '>>> is array' |
| 25 | } $else { |
| 26 | result[field.name] << '>>> is not array' |
| 27 | } |
| 28 | $if field.is_shared { |
| 29 | result[field.name] << '>>>> is shared' |
| 30 | } $else $if field.is_pub { |
| 31 | result[field.name] << '>>>> is pub' |
| 32 | } |
| 33 | } |
| 34 | result['bool'] = []string{} |
| 35 | $if !false { |
| 36 | result['bool'] << '1' |
| 37 | } |
| 38 | $if !true { |
| 39 | result['bool'] << '2' |
| 40 | } |
| 41 | $if true { |
| 42 | result['bool'] << '3' |
| 43 | } |
| 44 | $if false { |
| 45 | result['bool'] << '4' |
| 46 | } |
| 47 | |
| 48 | assert result['a'].len == 4 |
| 49 | assert result['b'].len == 3 |
| 50 | assert result['bool'].len == 2 |
| 51 | |
| 52 | assert result['a'][0] == '> is not array' |
| 53 | assert result['a'][1] == '>> is not array' |
| 54 | assert result['a'][2] == '>>> is not array' |
| 55 | assert result['a'][3] == '>>>> is pub' |
| 56 | |
| 57 | assert result['b'][0] == '> is array' |
| 58 | assert result['b'][1] == '>> is array' |
| 59 | assert result['b'][2] == '>>> is array' |
| 60 | |
| 61 | assert result['bool'][0] == '1' |
| 62 | assert result['bool'][1] == '3' |
| 63 | } |
| 64 | |
| 65 | fn test_main() { |
| 66 | encode_struct(EmptyStruct{}) |
| 67 | } |
| 68 | |