| 1 | pub type DesiredCapabilities = FireFox | Edge |
| 2 | |
| 3 | struct FireFox { |
| 4 | browser_name string = 'firefox' |
| 5 | accept_insecure_certs bool = true |
| 6 | moz_debugger_address bool = true |
| 7 | } |
| 8 | |
| 9 | struct Edge { |
| 10 | browser_name string = 'MicrosoftEdge' |
| 11 | } |
| 12 | |
| 13 | fn struct_values[T](s T) map[string]string { |
| 14 | mut res := map[string]string{} |
| 15 | $if T is $struct { |
| 16 | $for field in T.fields { |
| 17 | res[field.name] = s.$(field.name).str() |
| 18 | } |
| 19 | } |
| 20 | return res |
| 21 | } |
| 22 | |
| 23 | fn useit(dc DesiredCapabilities) string { |
| 24 | $for v in dc.variants { |
| 25 | if dc is v { |
| 26 | $if v is $struct { |
| 27 | result := struct_values(dc) |
| 28 | return result.str() |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | return '' |
| 33 | } |
| 34 | |
| 35 | fn test_main() { |
| 36 | assert useit(Edge{}) == "{'browser_name': 'MicrosoftEdge'}" |
| 37 | assert useit(FireFox{}) == "{'browser_name': 'firefox', 'accept_insecure_certs': 'true', 'moz_debugger_address': 'true'}" |
| 38 | } |
| 39 | |