I didn't originally write the package with that in mind, but mapping `object
Variant --> object type` works without issue. See here 2 unit-tests I just
added to test that:
test """
GIVEN an object variant A and an object type B that share some fields
on the instance
WHEN an instance of A is mapped to an instance of B
THEN it should create an instance of B with all fields having the value
of their name counterparts from A
""":
# Given
type Kind = enum
str, num
type A = object
case kind: Kind
of str: str: string
of num: num: int
type B = object
str: string
num: int
proc map(x: A): B {.map.} = discard
let a = A(
kind: str,
str: "str"
)
# When
let result: B = map(a)
# Then
let expected = B(str: "str")
check result == expected
test """
GIVEN an object type A and an object variant type B that share some
fields on the instance
WHEN two instances of A are mapped to an instance of B
THEN it should create an instance of B with all fields having the value
of their name counterparts from A
""":
# Given
type Kind = enum
str, num
type A = object
case kind: Kind
of str: str: string
of num: num: int
type B = object
str: string
num: int
proc map(x: A, y: A): B {.map.} = discard
let a1 = A(
kind: str,
str: "str"
)
let a2 = A(
kind: num,
num: 5
)
# When
let result: B = map(a1, a2)
# Then
let expected = B(str: "str", num: 5)
check result == expected
Run
Mapping `anything => object Variant` is not yet possible. Syntactically because
the compiler stops you with `Error: parallel 'fields' iterator does not work
for 'case' objects` as well as Conceptually because the only way I could even
contemplate what kind of the output object variant you'd want out is if you
passed in a "kind" parameter explicitly (or you had multiple variants that
share an enum but that's such a special case I don't feel like it's worth
handling separately).
I think I'd need to introduce a new pragma `mapVariant` or so for that to
handle that explicitly and separately from all other usecases.