/
AdorneeData.lua
310 lines (243 loc) · 6.93 KB
/
AdorneeData.lua
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
--[=[
Bridges attributes and serializable data table. It's typical to need to define data in 3 ways.
1. Attributes on an instance for replication
2. Tables for Lua configuration
3. Within AttributeValues for writing regular code
Providing all 3
@class AdorneeData
]=]
local require = require(script.Parent.loader).load(script)
local AdorneeDataEntry = require("AdorneeDataEntry")
local AdorneeDataValue = require("AdorneeDataValue")
local AttributeUtils = require("AttributeUtils")
local t = require("t")
local AdorneeData = {}
AdorneeData.ClassName = "AdorneeData"
AdorneeData.__index = AdorneeData
--[=[
Attribute data specification
@param prototype any
@return AdorneeData<T>
]=]
function AdorneeData.new(prototype)
local self = setmetatable({}, AdorneeData)
self._fullPrototype = assert(prototype, "Bad prototype")
self._attributePrototype = {}
self._defaultValuesPrototype = {}
self._valueObjectPrototype = {}
for key, item in pairs(self._fullPrototype) do
if AdorneeDataEntry.isAdorneeDataEntry(item) then
local default = item:GetDefaultValue()
self._defaultValuesPrototype[key] = default
self._valueObjectPrototype[key] = item
else
self._defaultValuesPrototype[key] = item
self._attributePrototype[key] = item
end
end
return self
end
--[=[
Returns true if the data is valid data, otherwise returns false and an error.
@param data any
@return boolean
@return string -- Error message
]=]
function AdorneeData:IsStrictData(data)
return self:GetStrictTInterface()(data)
end
--[=[
Validates and creates a new data table for the data that is readonly and frozen
@param data TStrict
@return TStrict
]=]
function AdorneeData:CreateStrictData(data)
assert(self:IsStrictData(data))
return table.freeze(table.clone(data))
end
--[=[
Validates and creates a new data table that is readonly. This table will have all values or
the defaults
@param data T
@return T
]=]
function AdorneeData:CreateFullData(data)
assert(self:IsData(data))
local result = table.clone(self._defaultValuesPrototype)
for key, value in pairs(data) do
result[key] = value
end
return table.freeze(table.clone(result))
end
--[=[
Validates and creates a new data table that is readonly and frozen, but for partial
data.
The data can just be part of the attributes.
@param data TPartial
@return TPartial
]=]
function AdorneeData:CreateData(data)
assert(self:IsData(data))
return table.freeze(table.clone(data))
end
--[=[
Observes the attribute table for adornee
@param adornee Instance
@return Observable<TStrict>
]=]
function AdorneeData:Observe(adornee)
assert(typeof(adornee) == "Instance", "Bad adornee")
return self:CreateAdorneeDataValue(adornee):Observe(adornee)
end
--[=[
Gets attribute table for the data
@param adornee Instance
@return AdorneeDataValue
]=]
function AdorneeData:CreateValue(adornee)
assert(typeof(adornee) == "Instance", "Bad adornee")
local attributeTableValue = AdorneeDataValue.new(adornee, self._fullPrototype)
return attributeTableValue
end
--[=[
Gets attribute table for the data
@param adornee Instance
@return AdorneeDataValue
]=]
function AdorneeData:CreateAdorneeDataValue(adornee)
assert(typeof(adornee) == "Instance", "Bad adornee")
return self:CreateValue(adornee)
end
--[=[
Gets the attributes for the adornee
@param adornee Instance
@return TStrict
]=]
function AdorneeData:GetAttributes(adornee)
assert(typeof(adornee) == "Instance", "Bad adornee")
local data = {}
for key, defaultValue in pairs(self._attributePrototype) do
local result = adornee:GetAttribute(key)
if result == nil then
result = defaultValue
end
data[key] = result
end
-- TODO: Avoid additional allocation
for key, value in pairs(self._valueObjectPrototype) do
data[key] = value:CreateValueObject(adornee).Value
end
return self:CreateStrictData(data)
end
--[=[
Sets the attributes for the adornee
@param adornee Instance
@param data T
]=]
function AdorneeData:SetAttributes(adornee, data)
assert(typeof(adornee) == "Instance", "Bad adornee")
assert(self:IsData(data))
local attributeTable = self:CreateAdorneeDataValue(adornee)
for key, value in pairs(data) do
attributeTable[key].Value = value
end
end
--[=[
Sets the attributes for the adornee
@param adornee Instance
@param data TStrict
]=]
function AdorneeData:SetStrictAttributes(adornee, data)
assert(typeof(adornee) == "Instance", "Bad adornee")
assert(self:IsStrictData(data))
for key, _ in pairs(self._attributePrototype) do
adornee:SetAttribute(key, data[key])
end
-- TODO: Avoid additional allocation
for key, value in pairs(self._valueObjectPrototype) do
value:CreateValueObject(adornee).Value = data[key]
end
end
--[=[
Initializes the attributes for the adornee
@param adornee Instance
@param data T | nil
]=]
function AdorneeData:InitAttributes(adornee, data)
data = data or {}
assert(typeof(adornee) == "Instance", "Bad adornee")
assert(self:IsData(data))
for key, defaultValue in pairs(self._attributePrototype) do
if adornee:GetAttribute(key) == nil then
if data[key] ~= nil then
adornee:SetAttribute(key, data[key])
else
adornee:SetAttribute(key, defaultValue)
end
end
end
-- TODO: Avoid additional allocation
for key, value in pairs(self._valueObjectPrototype) do
local valueObject = value:CreateValueObject(adornee)
if valueObject == nil then
if data[key] ~= nil then
valueObject.Value = data[key]
end
end
end
end
--[=[
Gets a strict interface which will return true if the value is a partial interface and
false otherwise.
@return function
]=]
function AdorneeData:GetStrictTInterface()
if self._fullInterface then
return self._fullInterface
end
self._fullInterface = t.strictInterface(self:_getOrCreateTypeInterfaceList())
return self._fullInterface
end
--[=[
Gets a [t] interface which will return true if the value is a partial interface, and
false otherwise.
@return function
]=]
function AdorneeData:GetTInterface()
if self._interface then
return self._interface
end
local interfaceList = {}
for key, value in pairs(self:_getOrCreateTypeInterfaceList()) do
interfaceList[key] = t.optional(value)
end
self._interface = t.strictInterface(interfaceList)
return self._interface
end
--[=[
Returns true if the data is valid partial data, otherwise returns false and an error.
@param data any
@return boolean
@return string -- Error message
]=]
function AdorneeData:IsData(data)
return self:GetTInterface()(data)
end
function AdorneeData:_getOrCreateTypeInterfaceList()
if self._typeInterfaceList then
return self._typeInterfaceList
end
local interfaceList = {}
for key, value in pairs(self._fullPrototype) do
if AdorneeDataEntry.isAdorneeDataEntry(value) then
interfaceList[key] = value:GetStrictInterface()
else
local valueType = typeof(value)
assert(AttributeUtils.isValidAttributeType(valueType), "Not a valid value type")
interfaceList[key] = t.typeof(valueType)
end
end
self._typeInterfaceList = interfaceList
return interfaceList
end
return AdorneeData