Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove warnings #383

Merged
merged 2 commits into from
Jul 12, 2024
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
92 changes: 49 additions & 43 deletions lib/grpc/codec/json.ex
Original file line number Diff line number Diff line change
@@ -1,67 +1,73 @@
defmodule GRPC.Codec.JSON do
@moduledoc """
JSON Codec for gRPC communication.
if Code.ensure_loaded?(Jason) do
defmodule GRPC.Codec.JSON do
@moduledoc """
JSON Codec for gRPC communication.

This module implements the `GRPC.Codec` behaviour, providing encoding and decoding functions
for JSON serialization in the context of gRPC communication.
This module implements the `GRPC.Codec` behaviour, providing encoding and decoding functions
for JSON serialization in the context of gRPC communication.

## Behavior Functions
## Behavior Functions

- `name/0`: Returns the name of the codec, which is "json".
- `encode/1`: Encodes a struct using the Protobuf.JSON.encode!/1 function.
- `decode/2`: Decodes binary data into a map using the Jason library.
- `name/0`: Returns the name of the codec, which is "json".
- `encode/1`: Encodes a struct using the Protobuf.JSON.encode!/1 function.
- `decode/2`: Decodes binary data into a map using the Jason library.

This module requires the Jason dependency.
"""
@behaviour GRPC.Codec
This module requires the Jason dependency.
"""
@behaviour GRPC.Codec

def name(), do: "json"
def name(), do: "json"

@doc """
Encodes a struct using the Protobuf.JSON.encode!/1 function.
@doc """
Encodes a struct using the Protobuf.JSON.encode!/1 function.

### Parameters:
### Parameters:

- `struct` - The struct to be encoded.
- `struct` - The struct to be encoded.

### Returns:
### Returns:

The encoded binary data.
The encoded binary data.

### Example:
### Example:

```elixir
%MyStruct{id: 1, name: "John"} |> GRPC.Codec.JSON.encode()
```
```elixir
%MyStruct{id: 1, name: "John"} |> GRPC.Codec.JSON.encode()
```

"""
"""

def encode(struct) do
Protobuf.JSON.encode!(struct)
end
def encode(struct) do
Protobuf.JSON.encode!(struct)
end

@doc """
Decodes binary data into a map using the Jason library.
Parameters:
@doc """
Decodes binary data into a map using the Jason library.
Parameters:

binary - The binary data to be decoded.
module - Module to be created.
binary - The binary data to be decoded.
module - Module to be created.

Returns:
Returns:

A map representing the decoded data.
A map representing the decoded data.

Raises:
Raises:

Raises an error if the Jason library is not loaded.
Raises an error if the Jason library is not loaded.

Example:
Example:

```elixir
binary_data |> GRPC.Codec.JSON.decode(__MODULE__)
```
"""
def decode(<<>>, _module), do: %{}
```elixir
binary_data |> GRPC.Codec.JSON.decode(__MODULE__)
```
"""
def decode(<<>>, _module), do: %{}

def decode(binary, _module), do: Jason.decode!(binary)
def decode(binary, _module), do: Jason.decode!(binary)
end
else
defmodule GRPC.Codec.JSON do
def decode(_, _), do: raise(ArgumentError, "Module Jason not found")
end
end
2 changes: 1 addition & 1 deletion lib/grpc/protoc/generator/service.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ defmodule GRPC.Protoc.Generator.Service do
methods: methods,
descriptor_fun_body: descriptor_fun_body,
version: Util.version(),
module_doc?: ctx.include_docs?
module_doc?: false
)
)}
end
Expand Down
4 changes: 2 additions & 2 deletions test/grpc/integration/server_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ defmodule GRPC.Integration.ServerTest do

test "should map grpc error codes to http status" do
run_server([TranscodeErrorServer], fn port ->
for {code_name, status} <- [
for {code_name, _status} <- [
{"cancelled", 400},
{"unknown", 500},
{"invalid_argument", 400},
Expand All @@ -397,7 +397,7 @@ defmodule GRPC.Integration.ServerTest do
] do
{:ok, conn_pid} = :gun.open(~c"localhost", port)

stream_ref =
_stream_ref =
:gun.get(
conn_pid,
"/v1/messages/#{code_name}",
Expand Down
Loading