Skip to content

Commit

Permalink
[COAPL-660]: Support named types as keys of map types (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnabell authored and cottinisimone committed Sep 5, 2024
1 parent ba5a32c commit cf51473
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ by adding `avrogen` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:avrogen, "~> 0.2.1", organization: "primauk"}
{:avrogen, "~> 0.4.3", organization: "primauk"}
]
end
```
Expand Down
7 changes: 5 additions & 2 deletions lib/avrogen/code_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -825,9 +825,12 @@ defmodule Avrogen.CodeGenerator do
end
end

def random_instance_field(%{"type" => type} = field, global) do
def random_instance_field(%{"type" => type} = field, global),
do: random_instance_field(type, global, range_opts(field, type))

def random_instance_field(type, global, range_opts \\ "") when is_binary(type) do
if is_primitive(type) do
random_instance_primitive_constructor(type, range_opts(field, type))
random_instance_primitive_constructor(type, range_opts)
else
case Map.get(global[type], :type) do
:record ->
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Avrogen.MixProject do
use Mix.Project

@version "0.4.2"
@version "0.4.3"
@source_url "https://github.com/primait/avrogen"

def project do
Expand Down
37 changes: 37 additions & 0 deletions test/code_generator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,43 @@ defmodule Avrogen.CodeGenerator.Test do
assert ~s'Avrogen.Util.Random.Constructors.map(Avrogen.Util.Random.Constructors.decimal())' ==
CodeGenerator.random_instance_field(map_type, %{})
end

test "handling maps, with custom types as values" do
map_type = %{
"name" => "premium_breakdown",
"type" => %{
"type" => "map",
"values" => "renewals.events.v1.Price"
}
}

assert ~s'Avrogen.Util.Random.Constructors.map(fn rand_state -> Price.random_instance(rand_state) end)' ==
CodeGenerator.random_instance_field(map_type, %{
"renewals.events.v1.Price" => %{
name: "Price",
referenced_schemas: ["renewals.events.v1.MonthlyPrice"],
schema: %{
"fields" => [
%{
"doc" => "The total cost when paying annually",
"logicalType" => "decimal",
"name" => "annual",
"type" => "string"
},
%{
"doc" => "The monthly payment plan details",
"name" => "monthly",
"type" => ["null", "renewals.events.v1.MonthlyPrice"]
}
],
"name" => "Price",
"namespace" => "renewals.events.v1",
"type" => "record"
},
type: :record
}
})
end
end

describe "generate_schema" do
Expand Down

0 comments on commit cf51473

Please sign in to comment.