Skip to content

Commit d5096dd

Browse files
committed
Remove new/0, new/1 and new!/1
These functions have been deprecated since Protobuf v0.12
1 parent 451133b commit d5096dd

9 files changed

Lines changed: 4 additions & 284 deletions

File tree

lib/protobuf.ex

Lines changed: 1 addition & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,6 @@ defmodule Protobuf do
6464

6565
@behaviour Protobuf
6666

67-
@deprecated "Build the struct by hand with %MyMessage{...} or use struct/1"
68-
@impl unquote(__MODULE__)
69-
def new() do
70-
Protobuf.Builder.new(__MODULE__)
71-
end
72-
73-
@deprecated "Build the struct by hand with %MyMessage{...} or use struct/2"
74-
@impl unquote(__MODULE__)
75-
def new(attrs) do
76-
Protobuf.Builder.new(__MODULE__, attrs)
77-
end
78-
79-
@deprecated "Build the struct by hand with %MyMessage{...} or use struct!/2"
80-
@impl unquote(__MODULE__)
81-
def new!(attrs) do
82-
Protobuf.Builder.new!(__MODULE__, attrs)
83-
end
84-
8567
@impl unquote(__MODULE__)
8668
def transform_module() do
8769
nil
@@ -98,59 +80,6 @@ defmodule Protobuf do
9880
end
9981
end
10082

101-
@doc """
102-
Builds a blank struct with default values.
103-
104-
> #### Deprecated {: .warning}
105-
>
106-
> This is deprecated in favor of building the struct with `%MyMessage{...}` or using
107-
> `struct/1`.
108-
109-
"""
110-
@callback new() :: struct()
111-
112-
@doc """
113-
Builds and updates the struct with passed fields.
114-
115-
This function will:
116-
117-
* Recursively call `c:new/1` for embedded fields
118-
* Create structs using `struct/2` for keyword lists and maps
119-
* Create the correct struct if passed the wrong struct
120-
* Call `c:new/1` for each element in the list for repeated fields
121-
122-
> #### Deprecated {: .warning}
123-
> This is deprecated in favor of building the struct with `%MyMessage{...}` or using
124-
> `struct/2`.
125-
126-
## Examples
127-
128-
MyMessage.new(field1: "foo")
129-
#=> %MyMessage{field1: "foo", ...}
130-
131-
MyMessage.new(field1: [field2: "foo"])
132-
#=> %MyMessage{field1: %MySubMessage{field2: "foo"}}
133-
134-
MyMessage.new(field1: %WrongStruct{field2: "foo"})
135-
#=> %MyMessage{field1: %MySubMessage{field2: "foo"}}
136-
137-
MyMessage.new(repeated: [%{field1: "foo"}, %{field1: "bar"}])
138-
#=> %MyMessage{repeated: [%MyRepeated{field1: "foo"}, %MyRepeated{field1: "bar"}]}
139-
140-
"""
141-
@callback new(attributes :: Enum.t()) :: struct
142-
143-
@doc """
144-
Similar to `c:new/1`, but use `struct!/2` to build the struct, so
145-
errors will be raised if unknown keys are passed.
146-
147-
> #### Deprecated {: .warning}
148-
> This is deprecated in favor of building the struct with `%MyMessage{...}` or using
149-
> `struct!/2` directly.
150-
151-
"""
152-
@callback new!(attributes :: Enum.t()) :: struct()
153-
15483
@doc """
15584
Encodes the given struct into to a Protobuf binary.
15685
@@ -253,7 +182,7 @@ defmodule Protobuf do
253182
254183
You encode this:
255184
256-
payload = Protobuf.encode(User.new!(email: "user@example.com"))
185+
payload = Protobuf.encode(%User{email: "user@example.com"})
257186
#=> <<...>>
258187
259188
Now, you try to decode this payload using this schema instead:

lib/protobuf/builder.ex

Lines changed: 0 additions & 80 deletions
This file was deleted.

lib/protobuf/transform_module.ex

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ defmodule Protobuf.TransformModule do
55
By defining a `transform_module/0` function on your protobuf message module
66
you can add custom encoding and decoding logic for your message.
77
8-
The `c:Protobuf.new/1` function will not be called for structs that have a transform module, if
9-
you still want to emulate this behavior you can use `Protobuf.TransformModule.InferFieldsFromEnum`.
10-
118
As an example we can use this to implement a message that will be decoded as a string value:
129
1310
defmodule StringMessage do

lib/protobuf/transform_module/infer_fields_from_enum.ex

Lines changed: 0 additions & 15 deletions
This file was deleted.

test/protobuf/builder_test.exs

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Protobuf.BuilderTest do
22
use ExUnit.Case, async: true
33

4-
alias TestMsg.{Foo, Foo2, ContainsTransformModule, Proto3Optional}
4+
alias TestMsg.{Foo, Foo2, Proto3Optional}
55

66
describe "default values for structs" do
77
test "for proto3" do
@@ -29,60 +29,5 @@ defmodule Protobuf.BuilderTest do
2929
assert %Foo{e: %Foo.Bar{a: 1}}.e == %Foo.Bar{a: 1}
3030
assert %Foo{h: [%Foo.Bar{a: 1}]}.h == [%Foo.Bar{a: 1}]
3131
end
32-
33-
test "new/2 doesn't build embedded messages for nil and maps" do
34-
assert %Foo{e: nil}.e == nil
35-
assert %Foo{}.e == nil
36-
assert %Foo{l: %{"k" => 1}}.l == %{"k" => 1}
37-
end
38-
end
39-
40-
# TODO: remove these tests once we remove new/2.
41-
describe "new/2" do
42-
test "doesn't process struct" do
43-
msg = %Foo{}
44-
assert msg == Foo.new(msg)
45-
end
46-
47-
test "raises an error for non-list repeated embedded msgs" do
48-
# TODO: remove conditional once we support only Elixir 1.18+
49-
message =
50-
if System.version() >= "1.18.0" do
51-
~r/protocol Enumerable not implemented for type TestMsg.Foo.Ba/
52-
else
53-
~r/protocol Enumerable not implemented for %TestMsg.Foo.Bar/
54-
end
55-
56-
assert_raise Protocol.UndefinedError, message, fn ->
57-
Foo.new(h: Foo.Bar.new())
58-
end
59-
end
60-
61-
test "builds correct message for non matched struct" do
62-
foo = Foo.new(Foo2.new(non_matched: 1))
63-
64-
assert_raise Protobuf.EncodeError, fn ->
65-
Foo.encode(foo)
66-
end
67-
end
68-
69-
test "ignores structs with transform modules" do
70-
assert ContainsTransformModule.new(field: 123) == %ContainsTransformModule{field: 123}
71-
end
72-
end
73-
74-
# TODO: remove these tests once we remove new!/2.
75-
describe "new!/2" do
76-
test "raises for fields that don't exist in the schema" do
77-
assert_raise KeyError, fn ->
78-
Foo.new!(nonexisting_field: "foo")
79-
end
80-
end
81-
82-
test "raises for non-matched struct" do
83-
assert_raise ArgumentError, fn ->
84-
Foo.new!(%Foo2{})
85-
end
86-
end
8732
end
8833
end

test/protobuf/dsl_test.exs

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -190,38 +190,6 @@ defmodule Protobuf.DSLTest do
190190
refute msg_props.field_props[11].embedded?
191191
end
192192

193-
# TODO: remove once we remove new/0 and new/1.
194-
test "generates new/0 and new/1 functions" do
195-
assert %Foo{
196-
a: 0,
197-
c: "",
198-
d: +0.0,
199-
e: nil,
200-
f: 0,
201-
g: [],
202-
h: [],
203-
i: [],
204-
j: :UNKNOWN,
205-
k: false,
206-
l: %{}
207-
} = Foo.new()
208-
209-
assert %Foo{
210-
a: 1,
211-
b: 42,
212-
c: "abc",
213-
d: +0.0,
214-
e: %Foo.Bar{a: 2, b: "asd"},
215-
f: 0,
216-
g: [],
217-
h: [],
218-
i: [],
219-
j: :UNKNOWN,
220-
k: false,
221-
l: %{}
222-
} = Foo.new(%{a: 1, b: 42, c: "abc", e: Foo.Bar.new(a: 2, b: "asd")})
223-
end
224-
225193
test "set oneof of message props" do
226194
msg_props = TestMsg.Oneof.__message_props__()
227195
assert %{oneof: [{:first, 0}, {:second, 1}]} = msg_props

test/protobuf/encoder_test.exs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -335,14 +335,4 @@ defmodule Protobuf.EncoderTest do
335335
assert Encoder.encode(msg) == <<>>
336336
assert TestMsg.ContainsTransformModule.decode(Encoder.encode(msg)) == msg
337337
end
338-
339-
test "NewTransform calls new/2 before encoding" do
340-
msg = %TestMsg.ContainsNewTransformModule{field: [field: 123]}
341-
assert msg == %TestMsg.ContainsNewTransformModule{field: [field: 123]}
342-
343-
assert TestMsg.ContainsNewTransformModule.decode(Encoder.encode(msg)) ==
344-
%TestMsg.ContainsNewTransformModule{
345-
field: %TestMsg.WithNewTransformModule{field: 123}
346-
}
347-
end
348338
end

test/protobuf/protoc/cli_integration_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ defmodule Protobuf.Protoc.CLIIntegrationTest do
4545
protoc!([
4646
"--proto_path=#{tmp_dir}",
4747
"--elixir_out=#{tmp_dir}",
48-
"--elixir_opt=transform_module=Protobuf.TransformModule.InferFieldsFromEnum",
48+
"--elixir_opt=transform_module=TestMsg.TransformModule",
4949
"--plugin=./protoc-gen-elixir",
5050
proto_path
5151
])
5252

5353
assert [mod] = compile_file_and_clean_modules_on_exit("#{tmp_dir}/user.pb.ex")
5454
assert mod == Foo.User
5555

56-
assert mod.transform_module() == Protobuf.TransformModule.InferFieldsFromEnum
56+
assert mod.transform_module() == TestMsg.TransformModule
5757
end
5858

5959
test "gen_descriptors option", %{tmp_dir: tmp_dir, proto_path: proto_path} do

test/support/test_msg.ex

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -296,20 +296,6 @@ defmodule TestMsg do
296296
field :field, 1, type: WithTransformModule
297297
end
298298

299-
defmodule WithNewTransformModule do
300-
use Protobuf, syntax: :proto3
301-
302-
field :field, 1, type: :int32
303-
304-
def transform_module(), do: Protobuf.TransformModule.InferFieldsFromEnum
305-
end
306-
307-
defmodule ContainsNewTransformModule do
308-
use Protobuf, syntax: :proto3
309-
310-
field :field, 1, type: WithNewTransformModule
311-
end
312-
313299
defmodule TransformIntegerStrings do
314300
@behaviour Protobuf.TransformModule
315301

0 commit comments

Comments
 (0)