diff --git a/lib/protobuf/dsl.ex b/lib/protobuf/dsl.ex index 68cfb853..4474c1cb 100644 --- a/lib/protobuf/dsl.ex +++ b/lib/protobuf/dsl.ex @@ -57,6 +57,9 @@ defmodule Protobuf.DSL do end) if !found, do: raise "The first enum value must be zero in proto3" end + mapping = Enum.reduce(props, %{}, fn {_, %{fnum: fnum, name_atom: name_atom}}, acc -> + Map.put(acc, name_atom, fnum) + end) Enum.map(props, fn {_, %{fnum: fnum, name_atom: name_atom}} -> quote do def value(unquote(name_atom)), do: unquote(fnum) @@ -70,7 +73,11 @@ defmodule Protobuf.DSL do quote do def key(unquote(fnum)), do: unquote(name_atom) end - end) + end) ++ [ + quote do + def mapping(), do: unquote(Macro.escape(mapping)) + end + ] end defp def_enum_functions(_), do: nil diff --git a/test/protobuf/dsl_test.exs b/test/protobuf/dsl_test.exs index 85c30388..eef8079e 100644 --- a/test/protobuf/dsl_test.exs +++ b/test/protobuf/dsl_test.exs @@ -137,6 +137,7 @@ defmodule Protobuf.DSLTest do assert TestMsg.EnumFoo.key(1) == :A assert TestMsg.EnumFoo.key(2) == :B assert TestMsg.EnumFoo.key(4) == :C + assert TestMsg.EnumFoo.mapping() == %{UNKNOWN: 0, A: 1, B: 2, C: 4} assert %FieldProps{fnum: 11, type: {:enum, TestMsg.EnumFoo}, wire_type: 0} = Foo.__message_props__().field_props[11]