Repository navigation
Expand file tree
/
Copy pathash.ex
More file actions
234 lines (194 loc) · 4.72 KB
/
Copy pathash.ex
File metadata and controls
234 lines (194 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
defmodule Ash do
@moduledoc """
General purpose tools for working with Ash.
Currently only contains setters/getters for process context.
"""
@doc """
Gets all of the ash context so it can be set into a new process.
Use `transfer_context/1` in the new process to set the context.
"""
@spec get_context_for_transfer(opts :: Keyword.t()) :: term
def get_context_for_transfer(opts \\ []) do
context = Ash.get_context()
actor = Process.get(:ash_actor)
authorize? = Process.get(:ash_authorize?)
tenant = Process.get(:ash_tenant)
tracer = Process.get(:ash_tracer)
tracer_context =
if opts[:tracer] do
{:tracer_context, opts[:tracer].get_span_context()}
else
case tracer do
{:tracer, tracer} ->
{:tracer_context, tracer.get_span_context()}
_ ->
nil
end
end
%{
context: context,
actor: actor,
tenant: tenant,
authorize?: authorize?,
tracer: tracer,
tracer_context: tracer_context
}
end
@spec transfer_context(term, opts :: Keyword.t()) :: :ok
def transfer_context(
%{
context: context,
actor: actor,
tenant: tenant,
authorize?: authorize?,
tracer: tracer,
tracer_context: tracer_context
},
opts \\ []
) do
case actor do
{:actor, actor} ->
Ash.set_actor(actor)
_ ->
:ok
end
case tenant do
{:tenant, tenant} ->
Ash.set_tenant(tenant)
_ ->
:ok
end
case authorize? do
{:authorize?, authorize?} ->
Ash.set_authorize?(authorize?)
_ ->
:ok
end
if opts[:tracer] do
case tracer_context do
{:tracer_context, tracer_context} ->
opts[:tracer].set_span_context(tracer_context)
_ ->
:ok
end
else
case tracer do
{:tracer, tracer} ->
Ash.set_tracer(tracer)
case tracer_context do
{:tracer_context, tracer_context} ->
tracer.set_span_context(tracer_context)
_ ->
:ok
end
_ ->
tracer = Application.get_env(:ash, :tracer) || opts[:tracer]
if tracer do
case tracer_context do
{:tracer_context, tracer_context} ->
tracer.set_span_context(tracer_context)
_ ->
:ok
end
end
:ok
end
end
Ash.set_context(context)
end
@doc """
Sets context into the process dictionary that is used for all changesets and queries.
"""
@spec set_context(map) :: :ok
def set_context(map) do
Process.put(:ash_context, map)
:ok
end
@doc """
Sets actor into the process dictionary that is used for all changesets and queries.
"""
@spec set_actor(map) :: :ok
def set_actor(map) do
Process.put(:ash_actor, {:actor, map})
:ok
end
@doc """
Sets authorize? into the process dictionary that is used for all changesets and queries.
"""
@spec set_authorize?(map) :: :ok
def set_authorize?(map) do
Process.put(:ash_authorize?, {:authorize?, map})
:ok
end
@doc """
Sets the tracer into the process dictionary that will be used to trace requests
"""
@spec set_tracer(module) :: :ok
def set_tracer(module) do
Process.put(:ash_tracer, module)
:ok
end
@doc """
Gets the current actor from the process dictionary
"""
@spec get_actor() :: term()
def get_actor do
case Process.get(:ash_actor) do
{:actor, value} ->
value
_ ->
nil
end
end
@doc """
Gets the current tracer
"""
@spec get_tracer() :: term()
def get_tracer do
case Process.get(:ash_tracer) do
{:tracer, value} ->
value
_ ->
Application.get_env(:ash, :tracer)
end
end
@doc """
Gets the current authorize? from the process dictionary
"""
@spec get_authorize?() :: term()
def get_authorize? do
case Process.get(:ash_authorize?) do
{:authorize?, value} ->
value
_ ->
nil
end
end
@doc """
Sets tenant into the process dictionary that is used for all changesets and queries.
"""
@spec set_tenant(map) :: :ok
def set_tenant(map) do
Process.put(:ash_tenant, {:tenant, map})
:ok
end
@doc """
Gets the current tenant from the process dictionary
"""
@spec get_tenant() :: term()
def get_tenant do
case Process.get(:ash_tenant) do
{:tenant, value} ->
value
_ ->
nil
end
end
@doc """
Gets the current context from the process dictionary
"""
@spec get_context() :: term()
def get_context do
Process.get(:ash_context, %{}) || %{}
end
end