From 10eba75f3673bf0c707a29b8ee0ca275831b1bd5 Mon Sep 17 00:00:00 2001 From: Alexandre Hamez Date: Fri, 15 Nov 2024 22:01:41 +0100 Subject: [PATCH] chore: remove deprecated defs/0 generated function BREAKING CHANGE: changes in generated API --- CHANGELOG | 3 ++ lib/mix/tasks/protox/generate.ex | 3 +- lib/protox/define_message.ex | 47 -------------------------------- test/code_generation_test.exs | 7 ----- test/protox_test.exs | 3 +- 5 files changed, 5 insertions(+), 58 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 833aa685..0eba6197 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,6 @@ +# 2.0.0 (unreleased) + - BREAKING CHANGE: Remove generated defs/0 function + # 1.7.8 - Fix warnings of unknown JSON modules when a JSON library is not installed diff --git a/lib/mix/tasks/protox/generate.ex b/lib/mix/tasks/protox/generate.ex index bb395934..f2bcf039 100644 --- a/lib/mix/tasks/protox/generate.ex +++ b/lib/mix/tasks/protox/generate.ex @@ -25,8 +25,7 @@ defmodule Mix.Tasks.Protox.Generate do include_path: :keep, namespace: :string, multiple_files: :boolean, - keep_unknown_fields: :boolean, - generate_defs_funs: :boolean + keep_unknown_fields: :boolean ] @impl Mix.Task diff --git a/lib/protox/define_message.ex b/lib/protox/define_message.ex index c53549e3..a6c8739d 100644 --- a/lib/protox/define_message.ex +++ b/lib/protox/define_message.ex @@ -5,7 +5,6 @@ defmodule Protox.DefineMessage do def define(messages, opts \\ []) do keep_unknown_fields = Keyword.get(opts, :keep_unknown_fields, true) - generate_defs_funs = Keyword.get(opts, :generate_defs_funs, true) for msg = %Protox.Message{} <- messages do fields = Enum.sort(msg.fields, &(&1.tag < &2.tag)) @@ -16,7 +15,6 @@ defmodule Protox.DefineMessage do unknown_fields_funs = make_unknown_fields_funs(unknown_fields, keep_unknown_fields) required_fields_fun = make_required_fields_fun(required_fields) - defs_funs = make_defs_funs(fields, generate_defs_funs) fields_access_funs = make_fields_access_funs(fields) json_funs = make_json_funs(msg.name) default_fun = make_default_funs(fields) @@ -35,7 +33,6 @@ defmodule Protox.DefineMessage do unquote(encoder) unquote(decoder) unquote(json_funs) - unquote(defs_funs) unquote(fields_access_funs) unquote(unknown_fields_funs) unquote(required_fields_fun) @@ -96,27 +93,6 @@ defmodule Protox.DefineMessage do end end - defp make_defs_funs(_fields, false = _generate_defs_funs), do: [] - - defp make_defs_funs(fields, true = _generate_defs_funs) do - fields_map = make_fields_map(fields) - fields_by_name_map = make_fields_by_name_map(fields) - - quote do - @deprecated "Use fields_defs()/0 instead" - @spec defs() :: %{ - required(non_neg_integer) => {atom, Protox.Types.kind(), Protox.Types.type()} - } - def defs(), do: unquote(fields_map) - - @deprecated "Use fields_defs()/0 instead" - @spec defs_by_name() :: %{ - required(atom) => {non_neg_integer, Protox.Types.kind(), Protox.Types.type()} - } - def defs_by_name(), do: unquote(fields_by_name_map) - end - end - defp make_json_funs(msg_name) do quote do @spec json_decode(iodata(), keyword()) :: {:ok, struct()} | {:error, any()} @@ -318,27 +294,4 @@ defmodule Protox.DefineMessage do quote(do: [unquote(specs)]) end - - # Generate a map used to store a message's definitions. - defp make_fields_map(fields) do - fields - |> Enum.reduce(%{}, fn %Field{tag: tag, name: name, kind: kind, type: type}, acc -> - Map.put(acc, tag, {name, kind, make_type_field(kind, type)}) - end) - |> Macro.escape() - end - - defp make_fields_by_name_map(fields) do - fields - |> Enum.reduce(%{}, fn %Field{tag: tag, name: name, kind: kind, type: type}, acc -> - Map.put(acc, name, {tag, kind, make_type_field(kind, type)}) - end) - |> Macro.escape() - end - - defp make_type_field(:map, {key_type, {:message, msg}}), do: {key_type, {:message, msg}} - defp make_type_field(:map, {key_type, {:enum, enum}}), do: {key_type, {:enum, enum}} - defp make_type_field(_, {:enum, enum}), do: {:enum, enum} - defp make_type_field(_, {:message, enum}), do: {:message, enum} - defp make_type_field(_, ty), do: ty end diff --git a/test/code_generation_test.exs b/test/code_generation_test.exs index e2e5a082..52a222da 100644 --- a/test/code_generation_test.exs +++ b/test/code_generation_test.exs @@ -34,13 +34,6 @@ defmodule Protox.CodeGenerationTest do launch(path, protox_path, "single_without_unknown_fields.ex", ["--keep-unknown-fields=false"]) end - test "Generate single file, without defs funs", %{ - code_generation_path: path, - protox_path: protox_path - } do - launch(path, protox_path, "single_without_defs_funs.ex", ["--generate-defs-funs=false"]) - end - test "Generate multiple files, with unknown fields", %{ code_generation_path: path, protox_path: protox_path diff --git a/test/protox_test.exs b/test/protox_test.exs index b2b6ce08..bd86a334 100644 --- a/test/protox_test.exs +++ b/test/protox_test.exs @@ -102,8 +102,7 @@ defmodule ProtoxTest do message NoDefsFuns { } - """, - generate_defs_funs: false + """ use Protox, schema: """