@@ -108,7 +108,7 @@ defmodule PolymorphicEmbed do
108108 type: type_name ,
109109 module: Keyword . fetch! ( type_opts , :module ) ,
110110 identify_by_fields:
111- type_opts |> Keyword . get ( :identify_by_fields , [ ] ) |> Enum . map ( & to_string / 1 )
111+ Keyword . get ( type_opts , :identify_by_fields , [ ] ) |> Enum . map ( & to_string / 1 )
112112 }
113113 end )
114114
@@ -563,19 +563,26 @@ defmodule PolymorphicEmbed do
563563 end
564564
565565 defp get_polymorphic_module_from_map ( % { } = attrs , type_field_name , types_metadata ) do
566- attrs = attrs |> convert_map_keys_to_string ( )
567- type_field_name_as_string = to_string ( type_field_name )
568-
569- if type = Map . get ( attrs , type_field_name_as_string ) do
566+ if type = Attrs . get ( attrs , type_field_name ) do
570567 get_polymorphic_module_for_type ( type , types_metadata )
571568 else
572569 # check if one list is contained in another
573570 # Enum.count(contained -- container) == 0
574571 # contained -- container == []
575- types_metadata
576- |> Enum . filter ( & ( [ ] != & 1 . identify_by_fields ) )
577- |> Enum . find ( & ( [ ] == & 1 . identify_by_fields -- Map . keys ( attrs ) ) )
578- |> ( & ( & 1 && Map . fetch! ( & 1 , :module ) ) ) . ( )
572+
573+ types_metadata =
574+ types_metadata
575+ |> Enum . filter ( & ( [ ] != & 1 . identify_by_fields ) )
576+
577+ if types_metadata != [ ] do
578+ keys = Map . keys ( attrs ) |> Enum . map ( & to_string / 1 )
579+
580+ types_metadata
581+ |> Enum . find ( & ( [ ] == & 1 . identify_by_fields -- keys ) )
582+ |> ( & ( & 1 && Map . fetch! ( & 1 , :module ) ) ) . ( )
583+ else
584+ nil
585+ end
579586 end
580587 end
581588
@@ -647,9 +654,6 @@ defmodule PolymorphicEmbed do
647654 end
648655 end
649656
650- defp convert_map_keys_to_string ( % { } = map ) ,
651- do: for ( { key , val } <- map , into: % { } , do: { to_string ( key ) , val } )
652-
653657 defp raise_cannot_infer_type_from_data ( data ) ,
654658 do: raise ( "could not infer polymorphic embed from data #{ inspect ( data ) } " )
655659
0 commit comments