-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
struct Parent { | ||
pub mut: | ||
id int | ||
name string | ||
child Child | ||
other Other | ||
} | ||
|
||
struct Child { | ||
pub mut: | ||
id int | ||
age int | ||
} | ||
|
||
struct Other { | ||
pub mut: | ||
id int | ||
name string | ||
} | ||
|
||
interface IdInterface { | ||
mut: | ||
id int | ||
} | ||
|
||
fn insert_ids[T](val T) !T { | ||
mut clone := val | ||
|
||
$for field in T.fields { | ||
$if field.typ is $struct { | ||
clone.$(field.name) = insert_ids(val.$(field.name))! | ||
} | ||
} | ||
$if T is IdInterface { | ||
clone.id = 1 | ||
} $else { | ||
return error('${T.name} does not have an id field!') | ||
} | ||
return clone | ||
} | ||
|
||
fn test_main() { | ||
inserted := insert_ids(Parent{ | ||
name: 'test' | ||
})! | ||
assert inserted.child.id == 1 | ||
assert inserted.other.id == 1 | ||
} |