Skip to content

Commit e2be257

Browse files
author
José Valim
committed
Ensure both strings and atoms go through the same lookup
1 parent f9ec6cf commit e2be257

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* [IEx] IEx now respects signals sent from the Ctrl+G menu
66
* [Kernel] Allow documentation for types with `@typedoc`
77
* [Mix] Allow apps to be selected in umbrella projects
8+
* [Record] Generated record functions `new` and `update` also take options with strings as keys
89
* [Stream] Add `Stream.unfold/1`
910

1011
* Bug fixes

lib/elixir/lib/record.ex

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -630,18 +630,8 @@ defmodule Record do
630630
# an ordered dict of options (opts) and it will try to fetch
631631
# the given key from the ordered dict, falling back to the
632632
# default value if one does not exist.
633-
atom_selective = lc { k, v } inlist values do
634-
quote do: Keyword.get(opts, unquote(k), unquote(v))
635-
end
636-
string_selective = lc { k, v } inlist values do
637-
k = atom_to_binary(k)
638-
quote do
639-
case :lists.keyfind(unquote(k), 1, opts) do
640-
false -> unquote(v)
641-
{_, v} -> v
642-
end
643-
end
644-
end
633+
atom_selective = lc { k, v } inlist values, do: initialize_lookup(k, v)
634+
string_selective = lc { k, v } inlist values, do: initialize_lookup(atom_to_binary(k), v)
645635
646636
quote do
647637
@doc false
@@ -654,6 +644,15 @@ defmodule Record do
654644
end
655645
end
656646
647+
defp initialize_lookup(k, v) do
648+
quote do
649+
case :lists.keyfind(unquote(k), 1, opts) do
650+
false -> unquote(v)
651+
{_, v} -> v
652+
end
653+
end
654+
end
655+
657656
# Define converters method(s). For a declaration like:
658657
#
659658
# defrecord FileInfo, atime: nil, mtime: nil
@@ -741,23 +740,11 @@ defmodule Record do
741740
# Define an updater method that receives a
742741
# keyword list and updates the record.
743742
defp updater(values) do
744-
atom_fields =
745-
lc {key, _default} inlist values do
746-
index = find_index(values, key, 1)
747-
quote do: Keyword.get(keywords, unquote(key), elem(record, unquote(index)))
748-
end
743+
atom_fields =
744+
lc {key, _default} inlist values, do: updater_lookup(key, key, values)
749745
750746
string_fields =
751-
lc {key, _default} inlist values do
752-
index = find_index(values, key, 1)
753-
key = atom_to_binary(key)
754-
quote do
755-
case :lists.keyfind(unquote(key), 1, keywords) do
756-
false -> elem(record, unquote(index))
757-
{_, value} -> value
758-
end
759-
end
760-
end
747+
lc {key, _default} inlist values, do: updater_lookup(atom_to_binary(key), key, values)
761748
762749
atom_contents = quote do: { __MODULE__, unquote_splicing(atom_fields) }
763750
string_contents = quote do: { __MODULE__, unquote_splicing(string_fields) }
@@ -777,6 +764,17 @@ defmodule Record do
777764
end
778765
end
779766
767+
defp updater_lookup(k, key, values) do
768+
v = find_index(values, key, 1)
769+
770+
quote do
771+
case :lists.keyfind(unquote(k), 1, keywords) do
772+
false -> elem(record, unquote(v))
773+
{_, value} -> value
774+
end
775+
end
776+
end
777+
780778
defp record_optimizable do
781779
quote do
782780
@record_optimized true

0 commit comments

Comments
 (0)