Skip to content

Commit 68b8a2e

Browse files
authored
Remove deprecated support for own t/0 type and defstruct (#412)
1 parent 35a5017 commit 68b8a2e

File tree

2 files changed

+84
-123
lines changed

2 files changed

+84
-123
lines changed

lib/protobuf/dsl.ex

Lines changed: 11 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -174,58 +174,17 @@ defmodule Protobuf.DSL do
174174
unquote(Macro.escape(msg_props))
175175
end
176176

177-
cond do
178-
# If this is an enum and "@type t()" is already called, it's fine because it's likely
179-
# the old code generated by this library.
180-
unquote(msg_props.enum?) and unquote(defines_t_type?) ->
181-
IO.warn("""
182-
Since v0.10.0 of the :protobuf library, the t/0 type in Protobuf enum modules \
183-
is automatically generated by "use Protobuf". Remove your explicit definition or \
184-
regenerate the files with a newer version of the protoc-gen-elixir escript. \
185-
This warning will become an error in version 0.11.0+ of this library.
186-
""")
187-
188-
# If both "defstruct" and "@type t()" are called, it's probably okay because it's the code
189-
# we used to generated before from this library, but we want to get rid of it, so we warn.
190-
unquote(defines_defstruct?) and unquote(defines_t_type?) ->
191-
IO.warn("""
192-
Since v0.10.0 of the :protobuf library, the t/0 type and the struct are automatically \
193-
generated for modules that call "use Protobuf" if they are Protobuf enums or messages. \
194-
Remove your explicit definition of both of these or regenerate the files with the \
195-
latest version of the protoc-gen-elixir plugin. This warning will become an error \
196-
in version 0.11.0+ of the :protobuf library.\
197-
""")
198-
199-
# If users defined only "defstruct" OR "@type t()", it means either they didn't generate
200-
# the code through this library or they modified the generated files. In either case,
201-
# let's raise here since we could have inconsistencies between the user-defined spec/type
202-
# and our type/spec, respectively.
203-
unquote(not msg_props.enum?) and (unquote(defines_defstruct?) or unquote(defines_t_type?)) ->
204-
what = if unquote(defines_defstruct?), do: "defstruct/1", else: "@type t() :: ..."
205-
206-
raise """
207-
since v0.10.0 of the :protobuf library, the t/0 type and the struct are automatically \
208-
generated for modules that call "use Protobuf" if they are Protobuf enums or messages. \
209-
This was the case for #{inspect(unquote(__MODULE__))}, where you call #{what}. \
210-
This could cause inconsistencies with the type or struct generated by the library. \
211-
You can either:
212-
213-
* make sure that you define both the t/0 type as well as the struct, but that will
214-
become an error in later versions of the Protobuf library
215-
216-
* remove both the t/0 type definition as well as the struct definition and let the
217-
library define both
218-
219-
* regenerate the file from the Protobuf source definition with the latest version
220-
of the protoc-gen-elixir plugin, which won't generate the struct or the t/0 type
221-
definition
222-
223-
"""
224-
225-
# Newest version of this library generate both the t/0 type as well as the struct.
226-
true ->
227-
unquote(def_t_typespec(msg_props, extension_props, transform_module_ast))
228-
unquote(gen_defstruct(msg_props))
177+
if unquote(defines_defstruct?) or unquote(defines_t_type?) do
178+
raise Protobuf.InvalidError, """
179+
since v0.10.0 of the :protobuf library, the t/0 type and the struct are automatically \
180+
generated for modules that call "use Protobuf" if they are Protobuf enums or messages, \
181+
and their usage was deprecated. Since v0.15.0, they are errors. Remove your explicit \
182+
definition of both of these or regenerate the files with the latest version of the \
183+
protoc-gen-elixir plugin.\
184+
"""
185+
else
186+
unquote(def_t_typespec(msg_props, extension_props, transform_module_ast))
187+
unquote(gen_defstruct(msg_props))
229188
end
230189

231190
unquote(msg_props.enum? && Protobuf.DSL.Enum.quoted_enum_functions(msg_props))

test/protobuf/dsl_test.exs

Lines changed: 73 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
defmodule Protobuf.DSLTest do
22
use ExUnit.Case, async: true
33

4-
import ExUnit.CaptureIO
5-
64
alias Protobuf.{FieldProps, MessageProps}
75
alias TestMsg.{Foo, Foo2, Proto3Optional}
86

@@ -205,88 +203,92 @@ defmodule Protobuf.DSLTest do
205203
assert TestMsg.WithTransformModule.transform_module() == TestMsg.TransformModule
206204
end
207205

208-
test "emits a warning if there is already a definition for the t/0 type for an enum" do
209-
output =
210-
capture_io(:stderr, fn ->
211-
Code.eval_quoted(
212-
quote do
213-
defmodule MessageWithWarning do
214-
use Protobuf, syntax: :proto3, enum: true
215-
216-
@type t() :: integer() | :FOO
217-
218-
field :FOO, 0, type: :bool
219-
end
220-
end
221-
)
222-
end)
223-
224-
assert output =~ "the t/0 type in Protobuf enum modules is automatically generated"
206+
test "raises a compilation error if there is already a definition for the t/0 type for an enum" do
207+
assert_raise Protobuf.InvalidError,
208+
~r{t/0 type and the struct are automatically generated},
209+
fn ->
210+
Code.eval_quoted(
211+
quote do
212+
defmodule MessageWithWarning do
213+
use Protobuf, syntax: :proto3, enum: true
214+
215+
@type t() :: integer() | :FOO
216+
217+
field :FOO, 0, type: :bool
218+
end
219+
end
220+
)
221+
end
225222
end
226223

227-
test "emits a warning if there is already a call to defstruct/1 and a definition for the t/0 type" do
228-
output =
229-
capture_io(:stderr, fn ->
230-
Code.eval_quoted(
231-
quote do
232-
defmodule MessageWithWarning do
233-
use Protobuf, syntax: :proto3
224+
test "raises a compilation error if there is already a call to defstruct/1 and a definition for the t/0 type" do
225+
assert_raise Protobuf.InvalidError,
226+
~r{t/0 type and the struct are automatically generated},
227+
fn ->
228+
Code.eval_quoted(
229+
quote do
230+
defmodule MessageWithWarning do
231+
use Protobuf, syntax: :proto3
234232

235-
@type t() :: %__MODULE__{foo: boolean()}
233+
@type t() :: %__MODULE__{foo: boolean()}
236234

237-
defstruct [:foo]
235+
defstruct [:foo]
238236

239-
field :foo, 1, type: :bool
240-
end
241-
end
242-
)
243-
end)
244-
245-
assert output =~ "t/0 type and the struct are automatically generated"
237+
field :foo, 1, type: :bool
238+
end
239+
end
240+
)
241+
end
246242
end
247243

248244
test "raises a compilation error if there is already a call to defstruct/1 but no definition for the t/0 type" do
249-
assert_raise RuntimeError, ~r{t/0 type and the struct are automatically generated}, fn ->
250-
Code.eval_quoted(
251-
quote do
252-
defmodule MessageWithDefstructError do
253-
use Protobuf, syntax: :proto3
254-
255-
defstruct [:foo]
256-
257-
field :foo, 1, type: :bool
258-
end
259-
end
260-
)
261-
end
245+
assert_raise Protobuf.InvalidError,
246+
~r{t/0 type and the struct are automatically generated},
247+
fn ->
248+
Code.eval_quoted(
249+
quote do
250+
defmodule MessageWithDefstructError do
251+
use Protobuf, syntax: :proto3
252+
253+
defstruct [:foo]
254+
255+
field :foo, 1, type: :bool
256+
end
257+
end
258+
)
259+
end
262260
end
263261

264262
test "raises a compilation error if there is already a definition for the t/0 type but no defstruct" do
265-
assert_raise RuntimeError, ~r{the t/0 type and the struct are automatically generated}, fn ->
266-
Code.eval_quoted(
267-
quote do
268-
defmodule MessageWithTTypeError do
269-
use Protobuf, syntax: :proto3
270-
271-
@type t() :: %__MODULE__{foo: boolean()}
272-
273-
field :foo, 1, type: :bool
274-
end
275-
end
276-
)
277-
end
263+
assert_raise Protobuf.InvalidError,
264+
~r{the t/0 type and the struct are automatically generated},
265+
fn ->
266+
Code.eval_quoted(
267+
quote do
268+
defmodule MessageWithTTypeError do
269+
use Protobuf, syntax: :proto3
270+
271+
@type t() :: %__MODULE__{foo: boolean()}
272+
273+
field :foo, 1, type: :bool
274+
end
275+
end
276+
)
277+
end
278278
end
279279

280280
test "raises a compilation error if syntax is proto3 and the first enum has tag other than 0" do
281-
assert_raise RuntimeError, "the first enum value must have tag 0 in proto3, got: 1", fn ->
282-
Code.eval_quoted(
283-
quote do
284-
defmodule MessageWithProto3BadEnumTag do
285-
use Protobuf, syntax: :proto3, enum: true
286-
field :NOT_ZERO, 1
287-
end
288-
end
289-
)
290-
end
281+
assert_raise RuntimeError,
282+
"the first enum value must have tag 0 in proto3, got: 1",
283+
fn ->
284+
Code.eval_quoted(
285+
quote do
286+
defmodule MessageWithProto3BadEnumTag do
287+
use Protobuf, syntax: :proto3, enum: true
288+
field :NOT_ZERO, 1
289+
end
290+
end
291+
)
292+
end
291293
end
292294
end

0 commit comments

Comments
 (0)