Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 1 addition & 72 deletions lib/protobuf.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,6 @@ defmodule Protobuf do

@behaviour Protobuf

@deprecated "Build the struct by hand with %MyMessage{...} or use struct/1"
@impl unquote(__MODULE__)
def new() do
Protobuf.Builder.new(__MODULE__)
end

@deprecated "Build the struct by hand with %MyMessage{...} or use struct/2"
@impl unquote(__MODULE__)
def new(attrs) do
Protobuf.Builder.new(__MODULE__, attrs)
end

@deprecated "Build the struct by hand with %MyMessage{...} or use struct!/2"
@impl unquote(__MODULE__)
def new!(attrs) do
Protobuf.Builder.new!(__MODULE__, attrs)
end

@impl unquote(__MODULE__)
def transform_module() do
nil
Expand All @@ -98,59 +80,6 @@ defmodule Protobuf do
end
end

@doc """
Builds a blank struct with default values.

> #### Deprecated {: .warning}
>
> This is deprecated in favor of building the struct with `%MyMessage{...}` or using
> `struct/1`.

"""
@callback new() :: struct()

@doc """
Builds and updates the struct with passed fields.

This function will:

* Recursively call `c:new/1` for embedded fields
* Create structs using `struct/2` for keyword lists and maps
* Create the correct struct if passed the wrong struct
* Call `c:new/1` for each element in the list for repeated fields

> #### Deprecated {: .warning}
> This is deprecated in favor of building the struct with `%MyMessage{...}` or using
> `struct/2`.

## Examples

MyMessage.new(field1: "foo")
#=> %MyMessage{field1: "foo", ...}

MyMessage.new(field1: [field2: "foo"])
#=> %MyMessage{field1: %MySubMessage{field2: "foo"}}

MyMessage.new(field1: %WrongStruct{field2: "foo"})
#=> %MyMessage{field1: %MySubMessage{field2: "foo"}}

MyMessage.new(repeated: [%{field1: "foo"}, %{field1: "bar"}])
#=> %MyMessage{repeated: [%MyRepeated{field1: "foo"}, %MyRepeated{field1: "bar"}]}

"""
@callback new(attributes :: Enum.t()) :: struct

@doc """
Similar to `c:new/1`, but use `struct!/2` to build the struct, so
errors will be raised if unknown keys are passed.

> #### Deprecated {: .warning}
> This is deprecated in favor of building the struct with `%MyMessage{...}` or using
> `struct!/2` directly.

"""
@callback new!(attributes :: Enum.t()) :: struct()

@doc """
Encodes the given struct into to a Protobuf binary.

Expand Down Expand Up @@ -253,7 +182,7 @@ defmodule Protobuf do

You encode this:

payload = Protobuf.encode(User.new!(email: "user@example.com"))
payload = Protobuf.encode(%User{email: "user@example.com"})
#=> <<...>>

Now, you try to decode this payload using this schema instead:
Expand Down
80 changes: 0 additions & 80 deletions lib/protobuf/builder.ex

This file was deleted.

3 changes: 0 additions & 3 deletions lib/protobuf/transform_module.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ defmodule Protobuf.TransformModule do
By defining a `transform_module/0` function on your protobuf message module
you can add custom encoding and decoding logic for your message.

The `c:Protobuf.new/1` function will not be called for structs that have a transform module, if
you still want to emulate this behavior you can use `Protobuf.TransformModule.InferFieldsFromEnum`.

As an example we can use this to implement a message that will be decoded as a string value:

defmodule StringMessage do
Expand Down
15 changes: 0 additions & 15 deletions lib/protobuf/transform_module/infer_fields_from_enum.ex

This file was deleted.

57 changes: 1 addition & 56 deletions test/protobuf/builder_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Protobuf.BuilderTest do
use ExUnit.Case, async: true

alias TestMsg.{Foo, Foo2, ContainsTransformModule, Proto3Optional}
alias TestMsg.{Foo, Foo2, Proto3Optional}

describe "default values for structs" do
test "for proto3" do
Expand Down Expand Up @@ -29,60 +29,5 @@ defmodule Protobuf.BuilderTest do
assert %Foo{e: %Foo.Bar{a: 1}}.e == %Foo.Bar{a: 1}
assert %Foo{h: [%Foo.Bar{a: 1}]}.h == [%Foo.Bar{a: 1}]
end

test "new/2 doesn't build embedded messages for nil and maps" do
assert %Foo{e: nil}.e == nil
assert %Foo{}.e == nil
assert %Foo{l: %{"k" => 1}}.l == %{"k" => 1}
end
end

# TODO: remove these tests once we remove new/2.
describe "new/2" do
test "doesn't process struct" do
msg = %Foo{}
assert msg == Foo.new(msg)
end

test "raises an error for non-list repeated embedded msgs" do
# TODO: remove conditional once we support only Elixir 1.18+
message =
if System.version() >= "1.18.0" do
~r/protocol Enumerable not implemented for type TestMsg.Foo.Ba/
else
~r/protocol Enumerable not implemented for %TestMsg.Foo.Bar/
end

assert_raise Protocol.UndefinedError, message, fn ->
Foo.new(h: Foo.Bar.new())
end
end

test "builds correct message for non matched struct" do
foo = Foo.new(Foo2.new(non_matched: 1))

assert_raise Protobuf.EncodeError, fn ->
Foo.encode(foo)
end
end

test "ignores structs with transform modules" do
assert ContainsTransformModule.new(field: 123) == %ContainsTransformModule{field: 123}
end
end

# TODO: remove these tests once we remove new!/2.
describe "new!/2" do
test "raises for fields that don't exist in the schema" do
assert_raise KeyError, fn ->
Foo.new!(nonexisting_field: "foo")
end
end

test "raises for non-matched struct" do
assert_raise ArgumentError, fn ->
Foo.new!(%Foo2{})
end
end
end
end
32 changes: 0 additions & 32 deletions test/protobuf/dsl_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -190,38 +190,6 @@ defmodule Protobuf.DSLTest do
refute msg_props.field_props[11].embedded?
end

# TODO: remove once we remove new/0 and new/1.
test "generates new/0 and new/1 functions" do
assert %Foo{
a: 0,
c: "",
d: +0.0,
e: nil,
f: 0,
g: [],
h: [],
i: [],
j: :UNKNOWN,
k: false,
l: %{}
} = Foo.new()

assert %Foo{
a: 1,
b: 42,
c: "abc",
d: +0.0,
e: %Foo.Bar{a: 2, b: "asd"},
f: 0,
g: [],
h: [],
i: [],
j: :UNKNOWN,
k: false,
l: %{}
} = Foo.new(%{a: 1, b: 42, c: "abc", e: Foo.Bar.new(a: 2, b: "asd")})
end

test "set oneof of message props" do
msg_props = TestMsg.Oneof.__message_props__()
assert %{oneof: [{:first, 0}, {:second, 1}]} = msg_props
Expand Down
10 changes: 0 additions & 10 deletions test/protobuf/encoder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -335,14 +335,4 @@ defmodule Protobuf.EncoderTest do
assert Encoder.encode(msg) == <<>>
assert TestMsg.ContainsTransformModule.decode(Encoder.encode(msg)) == msg
end

test "NewTransform calls new/2 before encoding" do
msg = %TestMsg.ContainsNewTransformModule{field: [field: 123]}
assert msg == %TestMsg.ContainsNewTransformModule{field: [field: 123]}

assert TestMsg.ContainsNewTransformModule.decode(Encoder.encode(msg)) ==
%TestMsg.ContainsNewTransformModule{
field: %TestMsg.WithNewTransformModule{field: 123}
}
end
end
4 changes: 2 additions & 2 deletions test/protobuf/protoc/cli_integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ defmodule Protobuf.Protoc.CLIIntegrationTest do
protoc!([
"--proto_path=#{tmp_dir}",
"--elixir_out=#{tmp_dir}",
"--elixir_opt=transform_module=Protobuf.TransformModule.InferFieldsFromEnum",
"--elixir_opt=transform_module=TestMsg.TransformModule",
"--plugin=./protoc-gen-elixir",
proto_path
])

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

assert mod.transform_module() == Protobuf.TransformModule.InferFieldsFromEnum
assert mod.transform_module() == TestMsg.TransformModule
end

test "gen_descriptors option", %{tmp_dir: tmp_dir, proto_path: proto_path} do
Expand Down
14 changes: 0 additions & 14 deletions test/support/test_msg.ex
Original file line number Diff line number Diff line change
Expand Up @@ -296,20 +296,6 @@ defmodule TestMsg do
field :field, 1, type: WithTransformModule
end

defmodule WithNewTransformModule do
use Protobuf, syntax: :proto3

field :field, 1, type: :int32

def transform_module(), do: Protobuf.TransformModule.InferFieldsFromEnum
end

defmodule ContainsNewTransformModule do
use Protobuf, syntax: :proto3

field :field, 1, type: WithNewTransformModule
end

defmodule TransformIntegerStrings do
@behaviour Protobuf.TransformModule

Expand Down
Loading