| 1 | struct Encoder {} |
| 2 | |
| 3 | struct Writer {} |
| 4 | |
| 5 | struct StructTypePointer[T] { |
| 6 | mut: |
| 7 | val &T |
| 8 | } |
| 9 | |
| 10 | struct StructTypePointerPointer[T] { |
| 11 | mut: |
| 12 | val &&T |
| 13 | } |
| 14 | |
| 15 | pub fn (e &Encoder) encode_value[T](val T, mut wr Writer) ! { |
| 16 | e.encode_struct[T](val, 1, mut wr)! |
| 17 | } |
| 18 | |
| 19 | pub fn (e &Encoder) encode_value_with_level[T](val T, mut wr Writer) ! { |
| 20 | e.encode_struct[T](val, 1, mut wr)! |
| 21 | dump(val) |
| 22 | } |
| 23 | |
| 24 | fn (e &Encoder) encode_struct[U](val U, level int, mut wr Writer) ! { |
| 25 | $for field in U.fields { |
| 26 | $if field.indirections > 0 { |
| 27 | $if field.indirections == 1 { |
| 28 | e.encode_value_with_level(*val.$(field.name), mut wr)! |
| 29 | } |
| 30 | $if field.indirections == 2 { |
| 31 | e.encode_value_with_level(**val.$(field.name), mut wr)! |
| 32 | } |
| 33 | $if field.indirections == 3 { |
| 34 | e.encode_value_with_level(***val.$(field.name), mut wr)! |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | fn main() { |
| 41 | e := Encoder{} |
| 42 | mut sb := Writer{} |
| 43 | |
| 44 | mut string_initialized_with_reference := 'ads' |
| 45 | mut bool_initialized_with_reference := false |
| 46 | |
| 47 | e.encode_value(StructTypePointer[string]{ val: &string_initialized_with_reference }, mut |
| 48 | sb) or {} |
| 49 | e.encode_value(StructTypePointer[bool]{ val: &bool_initialized_with_reference }, mut |
| 50 | sb) or {} |
| 51 | |
| 52 | bool_val := true |
| 53 | ptr_bool := &bool_val |
| 54 | |
| 55 | e.encode_value(StructTypePointerPointer[bool]{ val: &ptr_bool }, mut sb) or {} |
| 56 | } |
| 57 | |