Fix duplicate key in json parse

This commit is contained in:
Simon Prévost 2024-03-01 08:53:26 -05:00
parent e3fcf4fed8
commit adc0c1a90e
5 changed files with 34 additions and 8 deletions

View File

@ -19,5 +19,6 @@ defmodule Langue.Formatter.Json.Parser do
|> :jsone.decode(object_format: :tuple)
|> elem(0)
|> NestedParserHelper.parse()
|> Enum.uniq_by(& &1.key)
end
end

View File

@ -3,7 +3,5 @@ defmodule Langue.Formatter.Parser do
alias Langue.Formatter.ParserResult, as: Output
alias Langue.Formatter.SerializerResult, as: Input
@type result :: {:ok, Output.t()} | {:error, any()}
@callback parse(Input.t()) :: result
@callback parse(Input.t()) :: Output.t()
end

View File

@ -3,7 +3,5 @@ defmodule Langue.Formatter.Serializer do
alias Langue.Formatter.ParserResult, as: Input
alias Langue.Formatter.SerializerResult, as: Output
@type result :: {:ok, Output.t()} | {:error, any()}
@callback serialize(Input.t()) :: result
@callback serialize(Input.t()) :: Output.t()
end

View File

@ -2,20 +2,28 @@ defmodule LangueTest.Formatter.Json.Exception do
@moduledoc false
use ExUnit.Case, async: true
alias Accent.FormatterTestHelper
alias Langue.Formatter.Json
alias LangueTest.Formatter.Json.Expectation.DuplicateKey
alias LangueTest.Formatter.Json.Expectation.InvalidFloatValue
alias LangueTest.Formatter.Json.Expectation.InvalidIntegerValue
Code.require_file("expectation_test.exs", __DIR__)
test "invalid integer value" do
{expected_parse, result_parse} = Accent.FormatterTestHelper.test_serialize(InvalidIntegerValue, Json)
{expected_parse, result_parse} = FormatterTestHelper.test_serialize(InvalidIntegerValue, Json)
assert expected_parse == result_parse
end
test "invalid float value" do
{expected_parse, result_parse} = Accent.FormatterTestHelper.test_serialize(InvalidFloatValue, Json)
{expected_parse, result_parse} = FormatterTestHelper.test_serialize(InvalidFloatValue, Json)
assert expected_parse == result_parse
end
test "duplicate key" do
{expected_parse, result_parse} = FormatterTestHelper.test_parse(DuplicateKey, Json)
assert expected_parse == result_parse
end

View File

@ -126,6 +126,27 @@ defmodule LangueTest.Formatter.Json.Expectation do
end
end
defmodule DuplicateKey do
@moduledoc false
use Langue.Expectation.Case
def render do
"""
{
"test": "foo",
"test": "bar",
"test": "baz"
}
"""
end
def entries do
[
%Entry{index: 1, key: "test", value: "foo", value_type: "string"}
]
end
end
defmodule InvalidFloatValue do
@moduledoc false
use Langue.Expectation.Case