Skip to content

Commit a8e385e

Browse files
committed
fix: fixes for map types nested in expressions
requires latest ash_sql for build to pass
1 parent a913124 commit a8e385e

File tree

3 files changed

+52
-31
lines changed

3 files changed

+52
-31
lines changed

lib/sql_implementation.ex

+21-31
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ defmodule AshPostgres.SqlImplementation do
2323
nil
2424

2525
{:array, type} ->
26-
parameterized_type({:array, Ash.Type.get_type(type)}, [], false)
26+
parameterized_type({:array, Ash.Type.get_type(type)}, [])
2727

2828
{:array, type, constraints} ->
29-
parameterized_type({:array, Ash.Type.get_type(type)}, constraints, false)
29+
parameterized_type({:array, Ash.Type.get_type(type)}, constraints)
3030

3131
{type, constraints} ->
32-
parameterized_type(type, constraints, false)
32+
parameterized_type(type, constraints)
3333

3434
type ->
35-
parameterized_type(type, [], false)
35+
parameterized_type(type, [])
3636
end
3737
end
3838

@@ -241,22 +241,20 @@ defmodule AshPostgres.SqlImplementation do
241241
end
242242

243243
@impl true
244-
def parameterized_type(type, constraints, no_maps? \\ true)
245-
246-
def parameterized_type({:parameterized, _} = type, _, _) do
244+
def parameterized_type({:parameterized, _} = type, _) do
247245
type
248246
end
249247

250-
def parameterized_type({:parameterized, _, _} = type, _, _) do
248+
def parameterized_type({:parameterized, _, _} = type, _) do
251249
type
252250
end
253251

254-
def parameterized_type({:in, type}, constraints, no_maps?) do
255-
parameterized_type({:array, type}, constraints, no_maps?)
252+
def parameterized_type({:in, type}, constraints) do
253+
parameterized_type({:array, type}, constraints)
256254
end
257255

258-
def parameterized_type({:array, type}, constraints, _) do
259-
case parameterized_type(type, constraints[:items] || [], false) do
256+
def parameterized_type({:array, type}, constraints) do
257+
case parameterized_type(type, constraints[:items] || []) do
260258
nil ->
261259
nil
262260

@@ -265,31 +263,23 @@ defmodule AshPostgres.SqlImplementation do
265263
end
266264
end
267265

268-
def parameterized_type({type, constraints}, [], no_maps?) do
269-
parameterized_type(type, constraints, no_maps?)
266+
def parameterized_type({type, constraints}, []) do
267+
parameterized_type(type, constraints)
270268
end
271269

272-
def parameterized_type(Ash.Type.CiString, constraints, no_maps?) do
273-
parameterized_type(AshPostgres.Type.CiStringWrapper, constraints, no_maps?)
270+
def parameterized_type(Ash.Type.CiString, constraints) do
271+
parameterized_type(AshPostgres.Type.CiStringWrapper, constraints)
274272
end
275273

276-
def parameterized_type(Ash.Type.String, constraints, no_maps?) do
277-
parameterized_type(AshPostgres.Type.StringWrapper, constraints, no_maps?)
274+
def parameterized_type(Ash.Type.String, constraints) do
275+
parameterized_type(AshPostgres.Type.StringWrapper, constraints)
278276
end
279277

280-
def parameterized_type(:tsquery, constraints, no_maps?) do
281-
parameterized_type(AshPostgres.Tsquery, constraints, no_maps?)
278+
def parameterized_type(:tsquery, constraints) do
279+
parameterized_type(AshPostgres.Tsquery, constraints)
282280
end
283281

284-
def parameterized_type(type, _constraints, false)
285-
when type in [Ash.Type.Map, Ash.Type.Map.EctoType],
286-
do: :map
287-
288-
def parameterized_type(type, _constraints, true)
289-
when type in [Ash.Type.Map, Ash.Type.Map.EctoType],
290-
do: nil
291-
292-
def parameterized_type(type, constraints, no_maps?) do
282+
def parameterized_type(type, constraints) do
293283
if Ash.Type.ash_type?(type) do
294284
cast_in_query? =
295285
if function_exported?(Ash.Type, :cast_in_query?, 2) do
@@ -301,7 +291,7 @@ defmodule AshPostgres.SqlImplementation do
301291
if cast_in_query? do
302292
type = Ash.Type.ecto_type(type)
303293

304-
parameterized_type(type, constraints, no_maps?)
294+
parameterized_type(type, constraints)
305295
else
306296
nil
307297
end
@@ -312,7 +302,7 @@ defmodule AshPostgres.SqlImplementation do
312302
else
313303
case type.type(constraints || []) do
314304
:ci_string ->
315-
parameterized_type(AshPostgres.Type.CiStringWrapper, constraints, no_maps?)
305+
parameterized_type(AshPostgres.Type.CiStringWrapper, constraints)
316306

317307
_ ->
318308
Ecto.ParameterizedType.init(type, constraints || [])

test/calculation_test.exs

+12
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,18 @@ defmodule AshPostgres.CalculationTest do
776776
end
777777

778778
describe "maps" do
779+
test "literal maps inside of conds can be loaded" do
780+
post =
781+
Post
782+
|> Ash.Changeset.for_create(:create, %{title: "match", score: 42})
783+
|> Ash.create!()
784+
785+
assert Ash.load!(post, :literal_map_in_expr).literal_map_in_expr == %{
786+
match: true,
787+
of: "match"
788+
}
789+
end
790+
779791
test "maps can reference filtered aggregates" do
780792
post =
781793
Post

test/support/resources/post.ex

+19
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,25 @@ defmodule AshPostgres.Test.Post do
789789
load: [:count_of_comments, :count_of_linked_posts]
790790
)
791791

792+
calculate(
793+
:literal_map_in_expr,
794+
:map,
795+
expr(
796+
cond do
797+
title == "match" ->
798+
%{match: true, of: "match"}
799+
800+
title == "not-match" ->
801+
%{match: true, of: "not-match"}
802+
803+
true ->
804+
%{match: false}
805+
end
806+
)
807+
) do
808+
constraints(fields: [match: [type: :boolean], of: [type: :string]])
809+
end
810+
792811
calculate :similarity,
793812
:boolean,
794813
expr(fragment("(to_tsvector(?) @@ ?)", title, ^arg(:search))) do

0 commit comments

Comments
 (0)