From cf51473d6488bc37cabfff3130d1fb12b67f7682 Mon Sep 17 00:00:00 2001 From: John Bell Date: Mon, 12 Feb 2024 13:23:05 +0000 Subject: [PATCH] [COAPL-660]: Support named types as keys of map types (#33) --- README.md | 2 +- lib/avrogen/code_generator.ex | 7 +++++-- mix.exs | 2 +- test/code_generator_test.exs | 37 +++++++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 09faca7..a7b50ea 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/lib/avrogen/code_generator.ex b/lib/avrogen/code_generator.ex index b3bc80c..b58d2d5 100644 --- a/lib/avrogen/code_generator.ex +++ b/lib/avrogen/code_generator.ex @@ -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 -> diff --git a/mix.exs b/mix.exs index 5b9f166..af8f296 100644 --- a/mix.exs +++ b/mix.exs @@ -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 diff --git a/test/code_generator_test.exs b/test/code_generator_test.exs index 0bd3eae..78840d1 100644 --- a/test/code_generator_test.exs +++ b/test/code_generator_test.exs @@ -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