diff --git a/lib/grpc/codec/json.ex b/lib/grpc/codec/json.ex index d84a75c1..b82b455d 100644 --- a/lib/grpc/codec/json.ex +++ b/lib/grpc/codec/json.ex @@ -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 diff --git a/lib/grpc/protoc/generator/service.ex b/lib/grpc/protoc/generator/service.ex index f7aeea10..107e302d 100644 --- a/lib/grpc/protoc/generator/service.ex +++ b/lib/grpc/protoc/generator/service.ex @@ -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 diff --git a/test/grpc/integration/server_test.exs b/test/grpc/integration/server_test.exs index 6dd76900..d8041179 100644 --- a/test/grpc/integration/server_test.exs +++ b/test/grpc/integration/server_test.exs @@ -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}, @@ -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}",