Skip to content
This repository was archived by the owner on Jul 25, 2024. It is now read-only.

Commit ac121b8

Browse files
author
Josh Price
committed
Replace macros with functions in TypeInfoVisitor
1 parent fddf0a7 commit ac121b8

File tree

1 file changed

+30
-34
lines changed

1 file changed

+30
-34
lines changed

lib/graphql/lang/ast/type_info_visitor.ex

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,18 @@ defmodule GraphQL.Lang.AST.TypeInfoVisitor do
1919

2020
defimpl Visitor do
2121

22-
defmacrop stack_push(stack_name, value) do
23-
quote do
24-
old_type_info = var!(accumulator)[:type_info]
25-
new_type_info = %TypeInfo{old_type_info |
26-
unquote(stack_name) => Stack.push(old_type_info.unquote(stack_name), unquote(value))}
27-
put_in(var!(accumulator)[:type_info], new_type_info)
28-
end
22+
def stack_push(accumulator, stack_name, value) do
23+
old_type_info = accumulator[:type_info]
24+
stack = Stack.push(Map.get(old_type_info, stack_name), value)
25+
new_type_info = Map.merge(old_type_info, %{stack_name => stack})
26+
put_in(accumulator[:type_info], new_type_info)
2927
end
3028

31-
defmacrop stack_pop(stack_name) do
32-
quote do
33-
old_type_info = var!(accumulator)[:type_info]
34-
new_type_info = %TypeInfo{old_type_info |
35-
unquote(stack_name) => Stack.pop(old_type_info.unquote(stack_name))}
36-
put_in(var!(accumulator)[:type_info], new_type_info)
37-
end
29+
def stack_pop(accumulator, stack_name) do
30+
old_type_info = accumulator[:type_info]
31+
stack = Stack.pop(Map.get(old_type_info, stack_name))
32+
new_type_info = Map.merge(old_type_info, %{stack_name => stack})
33+
put_in(accumulator[:type_info], new_type_info)
3834
end
3935

4036
def set_directive(accumulator, directive) do
@@ -57,9 +53,9 @@ defmodule GraphQL.Lang.AST.TypeInfoVisitor do
5753
type = TypeInfo.type(accumulator[:type_info])
5854
named_type = TypeInfo.named_type(type)
5955
if Type.is_composite_type?(named_type) do
60-
stack_push(:parent_type_stack, named_type)
56+
stack_push(accumulator, :parent_type_stack, named_type)
6157
else
62-
stack_push(:parent_type_stack, nil)
58+
stack_push(accumulator, :parent_type_stack, nil)
6359
end
6460
:Field ->
6561
parent_type = TypeInfo.parent_type(accumulator[:type_info])
@@ -70,11 +66,11 @@ defmodule GraphQL.Lang.AST.TypeInfoVisitor do
7066
node
7167
)
7268
field_def_type = if field_def, do: field_def.type, else: nil
73-
accumulator = stack_push(:field_def_stack, field_def)
74-
stack_push(:type_stack, field_def_type)
69+
accumulator = stack_push(accumulator, :field_def_stack, field_def)
70+
stack_push(accumulator, :type_stack, field_def_type)
7571
else
76-
accumulator = stack_push(:field_def_stack, nil)
77-
stack_push(:type_stack, nil)
72+
accumulator = stack_push(accumulator, :field_def_stack, nil)
73+
stack_push(accumulator, :type_stack, nil)
7874
end
7975
:Directive ->
8076
# add this once we add directive validations
@@ -92,17 +88,17 @@ defmodule GraphQL.Lang.AST.TypeInfoVisitor do
9288
:mutation -> accumulator[:type_info].schema.mutation
9389
_ -> raise "node operation #{node.operation} not handled"
9490
end
95-
stack_push(:type_stack, type)
91+
stack_push(accumulator, :type_stack, type)
9692
kind when kind in [:InlineFragment, :FragmentDefinition] ->
9793
output_type = if Map.has_key?(node, :typeCondition) do
9894
Schema.type_from_ast(node.typeCondition, accumulator[:type_info].schema)
9995
else
10096
TypeInfo.type(accumulator[:type_info])
10197
end
102-
stack_push(:type_stack, output_type)
98+
stack_push(accumulator, :type_stack, output_type)
10399
:VariableDefinition ->
104100
input_type = Schema.type_from_ast(node.type, accumulator[:type_info].schema)
105-
stack_push(:input_type_stack, input_type)
101+
stack_push(accumulator, :input_type_stack, input_type)
106102
:Argument ->
107103
field_or_directive = TypeInfo.directive(accumulator[:type_info]) ||
108104
TypeInfo.field_def(accumulator[:type_info])
@@ -112,18 +108,18 @@ defmodule GraphQL.Lang.AST.TypeInfoVisitor do
112108
fn(arg) -> arg == node.name.value end
113109
)
114110
accumulator = set_argument(accumulator, arg_def)
115-
stack_push(:input_type_stack, (if arg_def && Map.has_key?(arg_def, :type), do: arg_def.type, else: nil))
111+
stack_push(accumulator, :input_type_stack, (if arg_def && Map.has_key?(arg_def, :type), do: arg_def.type, else: nil))
116112
else
117113
accumulator = set_argument(accumulator, nil)
118-
stack_push(:input_type_stack, nil)
114+
stack_push(accumulator, :input_type_stack, nil)
119115
end
120116
:List ->
121117
input_type = TypeInfo.input_type(accumulator[:type_info])
122118
list_type = TypeInfo.named_type(input_type)
123119
if %Type.List{} === list_type do
124-
stack_push(:input_type_stack, list_type.ofType)
120+
stack_push(accumulator, :input_type_stack, list_type.ofType)
125121
else
126-
stack_push(:input_type_stack, nil)
122+
stack_push(accumulator, :input_type_stack, nil)
127123
end
128124
:ObjectField ->
129125
input_type = TypeInfo.input_type(accumulator[:type_info])
@@ -135,9 +131,9 @@ defmodule GraphQL.Lang.AST.TypeInfoVisitor do
135131
node
136132
)
137133
field_type = if input_field, do: input_field.type, else: nil
138-
stack_push(:input_type_stack, field_type)
134+
stack_push(accumulator, :input_type_stack, field_type)
139135
else
140-
stack_push(:input_type_stack, nil)
136+
stack_push(accumulator, :input_type_stack, nil)
141137
end
142138
_ ->
143139
accumulator
@@ -148,18 +144,18 @@ defmodule GraphQL.Lang.AST.TypeInfoVisitor do
148144
def leave(_visitor, node, accumulator) do
149145
case node.kind do
150146
:SelectionSet ->
151-
stack_pop(:parent_type_stack)
147+
stack_pop(accumulator, :parent_type_stack)
152148
:Field ->
153-
accumulator = stack_pop(:field_def_stack)
154-
stack_pop(:type_stack)
149+
accumulator = stack_pop(accumulator, :field_def_stack)
150+
stack_pop(accumulator, :type_stack)
155151
:Directive ->
156152
set_directive(accumulator, nil)
157153
kind when kind in [:OperationDefinition, :InlineFragment, :FragmentDefinition] ->
158-
stack_pop(:type_stack)
154+
stack_pop(accumulator, :type_stack)
159155
:Argument ->
160156
set_argument(accumulator, nil)
161157
kind when kind in [:List, :ObjectField, :VariableDefinition] ->
162-
stack_pop(:input_type_stack)
158+
stack_pop(accumulator, :input_type_stack)
163159
_ ->
164160
accumulator
165161
end

0 commit comments

Comments
 (0)