-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #383 from sleipnir/ref/remove-warnings
Remove warnings
- Loading branch information
Showing
3 changed files
with
52 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters