ecto_morph
ecto_morph copied to clipboard
Create map keys functionality
Would be cool if we could map between structs, specifying which key in the one struct maps to the key in the other struct.
EctoMorph.translate(%MyStruct{field: "yis"}, %YouKnow{}, [field: :other])
# => %YouKnow{other: "yis"}
EctoMorph.translate(%MyStruct{field: "yis"}, %YouKnow{}, [field: :other])
Or something. Probably could do with a better API for it and naming, potentially with map_values function to?
Cast to struct will translate values for us, when the keys match... but would be cool if we can specify a key becoming another key. Need to be carful cast_to_struct doesn't become mental
EctoMorph.cast_and_translate_to_struct(%MyStruct{field: "yis"}, %YouKnow{}, [:field], [field: :other])
# => %YouKnow{other: "yis"}
In fact when you think about it, all the rest falls out of this. Like cast to struct is a translate, where the keys are just equivalent.
EctoMorph.translate(%MyStruct{field: "yis"}, %YouKnow{}, [field: :field], )
# => %YouKnow{field: "yis"}
EctoMorph.cast_to_struct(%MyStruct{field: "yis"}, %YouKnow{}, [:field])
# => %YouKnow{field: "yis"}
Names
It's not really a map, as it doesnt preserve the original struct, is more like a reduce. translate coerce cast cast_with_* cast_to_struct - just have extra options cast_keys