-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
attribute.ex
196 lines (184 loc) · 6.91 KB
/
attribute.ex
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
defmodule Ash.Resource.Attribute do
@moduledoc "Represents an attribute on a resource"
defstruct [
:name,
:type,
:allow_nil?,
:generated?,
:primary_key?,
:private?,
:writable?,
:always_select?,
:default,
:update_default,
:description,
:source,
match_other_defaults?: false,
sensitive?: false,
filterable?: true,
constraints: []
]
defmodule Helpers do
@moduledoc "Helpers for building attributes"
defmacro timestamps(opts \\ []) do
quote do
create_timestamp :inserted_at, unquote(opts)
update_timestamp :updated_at, unquote(opts)
end
end
end
@type t :: %__MODULE__{
name: atom(),
constraints: Keyword.t(),
type: Ash.Type.t(),
primary_key?: boolean(),
private?: boolean(),
default: nil | term | (-> term),
update_default: nil | term | (-> term) | (Ash.Resource.record() -> term),
sensitive?: boolean(),
writable?: boolean()
}
alias Spark.OptionsHelpers
@schema [
name: [
type: :atom,
doc: "The name of the attribute.",
required: true
],
type: [
type: Ash.OptionsHelpers.ash_type(),
doc: "The type of the attribute. See `Ash.Type` for more.",
required: true
],
constraints: [
type: :keyword_list,
doc: """
Constraints to provide to the type when casting the value. For more, see the [constraints topic](/documentation/topics/constraints.md).
"""
],
description: [
type: :string,
doc: "An optional description for the attribute."
],
sensitive?: [
type: :boolean,
default: false,
doc: """
Whether or not the attribute value contains sensitive information, like PII. See the [Security guide](/documentation/topics/security.md) for more.
"""
],
source: [
type: :atom,
doc: """
If the field should be mapped to a different name in the data layer. Support varies by data layer.
"""
],
always_select?: [
type: :boolean,
default: false,
doc: """
Whether or not to ensure this attribute is always selected when reading from the database, regardless of applied select statements.
"""
],
primary_key?: [
type: :boolean,
default: false,
doc: """
Whether the attribute is the primary key. Composite primary key is also possible by using `primary_key? true` in more than one attribute. If primary_key? is true, allow_nil? must be false.
"""
],
allow_nil?: [
type: :boolean,
default: true,
doc: """
Whether or not the attribute can be set to nil. If nil value is given error is raised.
"""
],
generated?: [
type: :boolean,
default: false,
doc: """
Whether or not the value may be generated by the data layer.
"""
],
writable?: [
type: :boolean,
default: true,
doc: """
Whether or not the value can be written to. Non-writable attributes can still be written with `Ash.Changeset.force_change_attribute/3`.
"""
],
private?: [
type: :boolean,
default: false,
doc: """
The attribute is not publically writable, and should not be exposed over any public interfaces. See the [security guide](/documentation/topics/security.md) for more.
"""
],
default: [
type: {:or, [{:mfa_or_fun, 0}, :literal]},
doc: "A value to be set on all creates, unless a value is being provided already."
],
update_default: [
type: {:or, [{:mfa_or_fun, 0}, :literal]},
doc: "A value to be set on all updates, unless a value is being provided already."
],
filterable?: [
type: {:or, [:boolean, {:in, [:simple_equality]}]},
default: true,
doc: """
Whether or not the attribute can be referenced in filters.
"""
],
match_other_defaults?: [
type: :boolean,
default: false,
doc: """
Ensures that other attributes that use the same "lazy" default (a function or an mfa), use the same default value. Has no effect unless `default` is a zero argument function.
"""
]
]
@create_timestamp_schema @schema
|> OptionsHelpers.set_default!(:writable?, false)
|> OptionsHelpers.set_default!(:private?, true)
|> OptionsHelpers.set_default!(:default, &DateTime.utc_now/0)
|> OptionsHelpers.set_default!(:match_other_defaults?, true)
|> OptionsHelpers.set_default!(:type, Ash.Type.UtcDatetimeUsec)
|> OptionsHelpers.set_default!(:allow_nil?, false)
|> Ash.OptionsHelpers.hide_all_except([:name])
@update_timestamp_schema @schema
|> OptionsHelpers.set_default!(:writable?, false)
|> OptionsHelpers.set_default!(:private?, true)
|> OptionsHelpers.set_default!(:match_other_defaults?, true)
|> OptionsHelpers.set_default!(:default, &DateTime.utc_now/0)
|> OptionsHelpers.set_default!(
:update_default,
&DateTime.utc_now/0
)
|> OptionsHelpers.set_default!(:type, Ash.Type.UtcDatetimeUsec)
|> OptionsHelpers.set_default!(:allow_nil?, false)
|> Ash.OptionsHelpers.hide_all_except([:name])
@uuid_primary_key_schema @schema
|> OptionsHelpers.set_default!(:writable?, false)
|> OptionsHelpers.set_default!(:default, &Ash.UUID.generate/0)
|> OptionsHelpers.set_default!(:primary_key?, true)
|> OptionsHelpers.set_default!(:type, :uuid)
|> Keyword.delete(:allow_nil?)
|> Ash.OptionsHelpers.hide_all_except([:name])
@integer_primary_key_schema @schema
|> OptionsHelpers.set_default!(:writable?, false)
|> OptionsHelpers.set_default!(:primary_key?, true)
|> OptionsHelpers.set_default!(:generated?, true)
|> OptionsHelpers.set_default!(:type, :integer)
|> Keyword.delete(:allow_nil?)
|> Ash.OptionsHelpers.hide_all_except([:name])
def transform(attribute) do
Ash.Type.set_type_transformation(%{attribute | source: attribute.source || attribute.name})
end
@doc false
def attribute_schema, do: @schema
def create_timestamp_schema, do: @create_timestamp_schema
def update_timestamp_schema, do: @update_timestamp_schema
def uuid_primary_key_schema, do: @uuid_primary_key_schema
def integer_primary_key_schema, do: @integer_primary_key_schema
end