-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatatable.lua
1269 lines (1051 loc) · 39 KB
/
datatable.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
Lua Version Check
--]]
local supported_lua_versions <const> = {['Lua 5.4']=true}
if not supported_lua_versions[_VERSION] then
warn("lua-datatable: detected unsupported lua version: " .. tostring(_VERSION))
end
--[[
Slot
--]]
local slot_metatable <const> = {}
local slot_private <const> = setmetatable({}, {__mode='k'})
-- implementation
local slot_internal_metatable <const> = {
__name = 'Slot',
__metatable = slot_metatable,
__index = function (self, key)
local private <const> = assert(slot_private[self], "Slot instance not recognized: " .. tostring(self))
if key == 'validator' or key == 'formatter' then
return private[key]
end
end,
__newindex = function (_, _, _)
error("Slot definition cannot be modified")
end,
__call = function (self, operation, value)
local private <const> = assert(slot_private[self], "Slot instance not recognized: " .. tostring(self))
if operation == 'validate' then
local value, message = private.validator(value)
if type(message) == 'string' then
if value ~= nil then
error("Slot validator returned a value and an error message")
end
elseif message ~= nil then
error("Slot validator message must be a string or nil")
end
return value, message
elseif operation == 'format' then
local string_representation <const> = private.formatter(value)
assert(type(string_representation) == 'string', "Slot formatter did not return a string")
return string_representation
else
error("Slot unknown operation: " .. tostring(operation))
end
end,
__gc = function (self)
slot_private[self] = nil
end
}
-- public interface
local Slot <const> = setmetatable({
create = function (validator, formatter)
if type(validator) ~= 'function' then
error("Slot validator must be a function")
end
local formatter <const> = formatter or tostring
if type(formatter) ~= 'function' then
error("Slot formatter must be a function")
end
local instance <const> = {}
slot_private[instance] = {
validator=validator,
formatter=formatter
}
return setmetatable(instance, slot_internal_metatable)
end,
is = function (value)
return (getmetatable(value) == slot_metatable)
end
}, {
__call = function (self, ...)
return self.create(...)
end
})
--[[
Default Slots
--]]
local AnySlot <const> = Slot.create(function (value)
if value == nil then
return nil, "slot value must not be nil"
end
if type(value) == 'table' then
return nil, "slot value must not be a table"
end
return value
end)
local BooleanSlot <const> = Slot.create(function (value)
if type(value) == 'boolean' then
return value
else
return nil, "slot value must be a boolean"
end
end)
local StringSlot <const> = Slot.create(function (value)
if type(value) == 'string' then
return value
else
return nil, "slot value must be a string"
end
end, function (value)
return '"' .. tostring(value) .. '"'
end)
local NumberSlot <const> = Slot.create(function (value)
if type(value) == 'number' then
return value
else
return nil, "slot value must be a number"
end
end)
local IntegerSlot <const> = Slot.create(function (value)
if math.type(value) == 'integer' then
return value
else
return nil, "slot value must be an integer"
end
end)
local FloatSlot <const> = Slot.create(function (value)
if math.type(value) == 'float' then
return value
else
return nil, "slot value must be a float"
end
end)
--[[
Slot Optional Wrapper
--]]
local function Optional(internal_slot)
assert(Slot.is(internal_slot), "internal_slot value must be a Slot instance")
return Slot.create(function (value)
if value == nil then
return nil
end
return internal_slot('validate', value)
end, function (value)
return internal_slot('format', value)
end)
end
--[[
DataTable
A table with defined string keys pointing to corresponding slot values.
--]]
local datatable_type_metatable <const> = {}
local datatable_type_private <const> = setmetatable({}, {__mode='k'})
local datatable_instance_metatable <const> = {}
local datatable_instance_private <const> = setmetatable({}, {__mode='k'})
-- instance implementation
local datatable_instance_check <const> = function (value)
local instance_private <const> = assert(
datatable_instance_private[value],
"DataTable instance not recognized: " .. tostring(value)
)
local datatable_private <const> = assert(
datatable_type_private[instance_private.datatable],
"DataTable type not recognized: " .. tostring(value)
)
return instance_private, datatable_private
end
local datatable_instance_internal_metatable <const> = {
__name = 'DataTable',
__metatable = datatable_instance_metatable,
__index = function (self, key)
local private <const>, datatable_private <const> = datatable_instance_check(self)
local slot <const> = assert(datatable_private.slots[key], "DataTable slot not found: " .. tostring(key))
return private.data[key]
end,
__newindex = function (self, key, value)
local private <const>, datatable_private <const> = datatable_instance_check(self)
local slot <const> = assert(datatable_private.slots[key], "DataTable slot not found: " .. tostring(key))
assert(not private.frozen, "DataTable instance is frozen")
local value, message = slot('validate', value)
if message then
error("DataTable slot '" .. key .. "': " .. message)
end
-- TODO: use a flag to toggle whether we should always run the datatable
-- validator or not, we may want to delay that until later
local previous_value <const> = private.data[key]
private.data[key] = value
local message = datatable_private.validator(private.data)
if message ~= nil then
private.data[key] = previous_value -- XXX: is there a better way without a shallow copy of private.data?
assert(type(message) == 'string', "DataTable validator function must return a string message")
error("DataTable instance data is not valid: " .. message)
end
end,
__pairs = function (self)
local private <const>, datatable_private <const> = datatable_instance_check(self)
-- note: lua table iteration is in arbitrary order whereas this always
-- iterates in the same order which is technically backwards compatible
-- for loops: https://www.lua.org/manual/5.4/manual.html#3.3.5
local function iterate(keys, key) -- state variable, initial or previous control value
-- note: not strictly necessary as table.remove({}, 1) and inner[nil]
-- both return nil so the loop ends on it's own but this is safer
if #keys == 0 then
return
end
local key <const> = table.remove(keys, 1)
local value <const> = private.data[key]
return key, value -- control value, remaining loop values
end
local keys <const> = {}
for name, _ in pairs(datatable_private.slots) do
table.insert(keys, name)
end
-- iterator function, state variable, initial control value, closing variable
return iterate, keys, keys[1], nil
end,
__gc = function (self)
datatable_instance_private[self] = nil
end
}
-- type implementation
local datatable_type_is <const> = function (self, value)
if getmetatable(value) ~= datatable_instance_metatable then
return false
end
local instance_private <const>, _ = datatable_instance_check(value)
return (instance_private.datatable == self)
end
local datatable_type_is_frozen <const> = function (self, instance)
local instance_private <const>, _ = datatable_instance_check(instance)
assert(instance_private.datatable == self, "DataTable type method used with incompatible type")
return instance_private.frozen
end
local datatable_type_class_methods <const> = {
['is']=datatable_type_is,
['is_frozen']=datatable_type_is_frozen,
}
local datatable_type_internal_metatable <const> = {
__name = 'DataTableType',
__metatable = datatable_type_metatable,
__index = function (self, key)
local private <const> = assert(
datatable_type_private[self],
"DataTable type not recognized: " .. tostring(self)
)
-- datatable type slots
if key == 'slots' then
-- shallow copy to prevent mutation
local slots <const> = {}
for name, slot in pairs(private.slots) do
slots[name] = slot
end
return slots
-- datatable frozen flag
elseif key == 'freeze_instances' then
return private.freeze_instances
-- class methods
elseif datatable_type_class_methods[key] ~= nil then
return datatable_type_class_methods[key]
-- unknown key
else
return nil
end
end,
__newindex = function (_, _, _)
error("DataTable definition cannot be modified")
end,
__call = function (self, value_data, flag_data)
local private <const> = assert(
datatable_type_private[self],
"DataTable type not recognized: " .. tostring(self)
)
local initial_data <const> = {}
for name, slot in pairs(private.slots) do
local value, message = slot('validate', value_data[name])
if message then
error("DataTable slot '" .. name .. "': " .. message)
end
initial_data[name] = value
end
local message <const> = private.validator(initial_data)
if message ~= nil then
assert(type(message) == 'string', "DataTable validator function must return a string message")
error("DataTable instance data is not valid: " .. message)
end
local flags <const> = flag_data or {}
if type(flags) ~= 'table' then
error("DataTable instance flags must be a table")
end
local frozen <const> = flags['frozen']
if frozen ~= nil then
if type(frozen) ~= 'boolean' then
error("DataTable instance frozen flag must be a boolean")
end
if private.freeze_instances and not frozen then
error("DataTable type freeze_instances requires instances to also be frozen")
end
end
local instance_frozen <const> = (frozen or private.freeze_instances)
local instance <const> = {}
datatable_instance_private[instance] = {
datatable=self,
data=initial_data,
frozen=instance_frozen
}
return setmetatable(instance, datatable_instance_internal_metatable)
end,
__gc = function (self)
datatable_type_private[self] = nil
end
}
-- public interface
local DataTable <const> = setmetatable({
create = function (slot_data, flag_data)
if type(slot_data) ~= 'table' or not next(slot_data) then
error("DataTable slots must be a non-empty table")
end
local flag_data <const> = flag_data or {}
if type(flag_data) ~= 'table' then
error("DataTable flags must be a table")
end
local freeze_instances <const> = (flag_data['freeze_instances'] or false)
if type(freeze_instances) ~= 'boolean' then
error("DataTable freeze_instances flag must be a boolean")
end
local validator <const> = (flag_data['validator'] or (function (_) return end))
if type(validator) ~= 'function' then
error("DataTable validator flag must be a function")
end
local slots <const> = {}
for name, value in pairs(slot_data) do
-- XXX: can this ever happen?
if slots[name] then
error("DataTable duplicate slot name: " .. tostring(name))
end
if Slot.is(value) then
slots[name] = value
elseif type(value) == 'table' then
slots[name] = Slot.create(table.unpack(value))
else
error("DataTable invalid slot value: " .. tostring(value))
end
end
local instance <const> = {}
datatable_type_private[instance] = {
slots=slots,
freeze_instances=freeze_instances,
validator=validator
}
return setmetatable(instance, datatable_type_internal_metatable)
end,
is = function (value)
return (getmetatable(value) == datatable_type_metatable)
end
}, {
__call = function (self, ...)
return self.create(...)
end
})
--[[
ArrayTable
A table with contiguous positive integer keys pointing to slot enforced values.
--]]
local arraytable_type_metatable <const> = {}
local arraytable_type_private <const> = setmetatable({}, {__mode='k'})
local arraytable_instance_metatable <const> = {}
local arraytable_instance_private <const> = setmetatable({}, {__mode='k'})
-- instance implementation
local arraytable_instance_check <const> = function (value)
local instance_private <const> = assert(
arraytable_instance_private[value],
"ArrayTable instance not recognized: " .. tostring(value)
)
local arraytable_private <const> = assert(
arraytable_type_private[instance_private.arraytable],
"ArrayTable type not recognized: " .. tostring(value)
)
return instance_private, arraytable_private
end
local arraytable_instance_internal_metatable <const> = {
__name = 'ArrayTable',
__metatable = arraytable_instance_metatable,
__len = function (self)
local private <const>, _ = arraytable_instance_check(self)
return #private.data
end,
__index = function (self, key)
local private <const>, _ = arraytable_instance_check(self)
if math.type(key) ~= 'integer' then
error("ArrayTable index must be an integer: " .. tostring(key))
end
return private.data[key]
end,
__newindex = function (self, key, value)
local private <const>, arraytable_private <const> = arraytable_instance_check(self)
assert(not private.frozen, "ArrayTable instance is frozen")
if math.type(key) ~= 'integer' then
error("ArrayTable index must be an integer: " .. tostring(key))
end
if key <= 0 then
error("ArrayTable index must be positive: " .. tostring(key))
end
if value ~= nil then
local value, message = arraytable_private.value_slot('validate', value)
if message then
error("ArrayTable index " .. tostring(key) .. ": " .. message)
end
end
-- TODO: use a flag to toggle whether we should always run the arraytable
-- validator or not, we may want to delay that until later
local previous_value <const> = private.data[key]
private.data[key] = value
-- validate array indices are contiguous
local key_count = 0
for _, _ in pairs(private.data) do
key_count = key_count + 1
end
if key_count ~= #private.data then
private.data[key] = previous_value
error("ArrayTable indices must be contiguous")
end
-- validate new array data
local message = arraytable_private.validator(private.data)
if message ~= nil then
private.data[key] = previous_value
assert(type(message) == 'string', "ArrayTable validator function must return a string message")
error("ArrayTable instance data is not valid: " .. message)
end
end,
__pairs = function (self)
local private <const>, _ = arraytable_instance_check(self)
-- for loops: https://www.lua.org/manual/5.4/manual.html#3.3.5
local function iterate(data, index) -- state variable, initial or previous control value
index = index + 1
local value <const> = data[index]
if value == nil then
return
end
return index, value -- control value, remaining loop values
end
-- iterator function, state variable, initial control value, closing variable
return iterate, private.data, 0, nil
end,
__gc = function (self)
arraytable_instance_private[self] = nil
end
}
-- type implementation
local arraytable_type_is <const> = function (self, value)
if getmetatable(value) ~= arraytable_instance_metatable then
return false
end
local instance_private <const>, _ = arraytable_instance_check(value)
return (instance_private.arraytable == self)
end
local arraytable_type_is_frozen <const> = function (self, instance)
local instance_private <const>, _ = arraytable_instance_check(instance)
assert(instance_private.arraytable == self, "ArrayTable type method used with incompatible type")
return instance_private.frozen
end
local arraytable_type_class_methods <const> = {
['is']=arraytable_type_is,
['is_frozen']=arraytable_type_is_frozen,
}
local arraytable_type_internal_metatable <const> = {
__name = 'ArrayTableType',
__metatable = arraytable_type_metatable,
__index = function (self, key)
local private <const> = assert(
arraytable_type_private[self],
"ArrayTable type not recognized: " .. tostring(self)
)
-- value slot
if key == 'value_slot' then
return private.value_slot
-- arraytable frozen flag
elseif key == 'freeze_instances' then
return private.freeze_instances
-- class methods
elseif arraytable_type_class_methods[key] ~= nil then
return arraytable_type_class_methods[key]
-- unknown key
else
return nil
end
end,
__newindex = function (_, _, _)
error("ArrayTable definition cannot be modified")
end,
__call = function (self, value_data, flag_data)
local private <const> = assert(
arraytable_type_private[self],
"ArrayTable type not recognized: " .. tostring(self)
)
local key_count = 0
local initial_data <const> = {}
for key, value in pairs(value_data) do
if math.type(key) ~= 'integer' then
error("ArrayTable indices must be integers")
end
if key <= 0 then
error("ArrayTable indices must be positive")
end
local value, message = private.value_slot('validate', value)
if message then
error("ArrayTable index " .. tostring(index) .. ": " .. message)
end
key_count = key_count + 1
initial_data[key] = value
end
if key_count ~= #value_data then
error("ArrayTable indices must be contiguous")
end
local message <const> = private.validator(initial_data)
if message ~= nil then
assert(type(message) == 'string', "ArrayTable validator function must return a string message")
error("ArrayTable instance data is not valid: " .. message)
end
local flags <const> = flag_data or {}
if type(flags) ~= 'table' then
error("ArrayTable instance flags must be a table")
end
local frozen <const> = flags['frozen']
if frozen ~= nil then
if type(frozen) ~= 'boolean' then
error("ArrayTable instance frozen flag must be a boolean")
end
if private.freeze_instances and not frozen then
error("ArrayTable type freeze_instances requires instances to also be frozen")
end
end
local instance_frozen <const> = (frozen or private.freeze_instances)
local instance <const> = {}
arraytable_instance_private[instance] = {
arraytable=self,
data=initial_data,
frozen=instance_frozen
}
return setmetatable(instance, arraytable_instance_internal_metatable)
end,
__gc = function (self)
arraytable_type_private[self] = nil
end
}
-- public interface
local ArrayTable <const> = setmetatable({
create = function (config_data)
if type(config_data) ~= 'table' then
error("ArrayTable config must be a table")
end
local value_slot <const> = (config_data['value_slot'] or AnySlot)
if not Slot.is(value_slot) then
error("ArrayTable value_slot must be a Slot instance")
end
local freeze_instances <const> = (config_data['freeze_instances'] or false)
if type(freeze_instances) ~= 'boolean' then
error("ArrayTable freeze_instances flag must be a boolean")
end
local validator <const> = (config_data['validator'] or (function (_) return end))
if type(validator) ~= 'function' then
error("ArrayTable validator flag must be a function")
end
local instance <const> = {}
arraytable_type_private[instance] = {
value_slot=value_slot,
freeze_instances=freeze_instances,
validator=validator
}
return setmetatable(instance, arraytable_type_internal_metatable)
end,
is = function (value)
return (getmetatable(value) == arraytable_type_metatable)
end
}, {
__call = function (self, ...)
return self.create(...)
end
})
--[[
MapTable
A table with slot enforced keys pointing to slot enforced values.
--]]
local maptable_type_metatable <const> = {}
local maptable_type_private <const> = setmetatable({}, {__mode='k'})
local maptable_instance_metatable <const> = {}
local maptable_instance_private <const> = setmetatable({}, {__mode='k'})
-- instance implementation
local maptable_instance_check <const> = function (value)
local instance_private <const> = assert(
maptable_instance_private[value],
"MapTable instance not recognized: " .. tostring(value)
)
local maptable_private <const> = assert(
maptable_type_private[instance_private.maptable],
"MapTable type not recognized: " .. tostring(value)
)
return instance_private, maptable_private
end
local maptable_instance_internal_metatable <const> = {
__name = 'MapTable',
__metatable = maptable_instance_metatable,
__index = function (self, key)
local private <const>, _ = maptable_instance_check(self)
return private.data[key]
end,
__newindex = function (self, key, value)
local private <const>, maptable_private <const> = maptable_instance_check(self)
assert(not private.frozen, "MapTable instance is frozen")
if key == nil then
error("MapTable keys cannot be nil")
end
local key, message = maptable_private.key_slot('validate', key)
if message then
error("MapTable key is invalid: " .. message)
end
if value ~= nil then
local value, message = maptable_private.value_slot('validate', value)
if message then
error("MapTable value is invalid: " .. message)
end
end
-- TODO: use a flag to toggle whether we should always run the arraytable
-- validator or not, we may want to delay that until later
local previous_value <const> = private.data[key]
private.data[key] = value
-- validate new map data
local message = maptable_private.validator(private.data)
if message ~= nil then
private.data[key] = previous_value
assert(type(message) == 'string', "MapTable validator function must return a string message")
error("MapTable instance data is not valid: " .. message)
end
end,
__pairs = function (self)
local private <const>, _ = maptable_instance_check(self)
-- for loops: https://www.lua.org/manual/5.4/manual.html#3.3.5
local function iterate(keys, key) -- state variable, initial or previous control value
-- note: not strictly necessary as table.remove({}, 1) and inner[nil]
-- both return nil so the loop ends on it's own but this is safer
if #keys == 0 then
return
end
local key <const> = table.remove(keys, 1)
local value <const> = private.data[key]
return key, value -- control value, remaining loop values
end
local keys <const> = {}
for key, _ in pairs(private.data) do
table.insert(keys, key)
end
-- iterator function, state variable, initial control value, closing variable
return iterate, keys, keys[1], nil
end,
__gc = function (self)
maptable_instance_private[self] = nil
end
}
-- type implementation
local maptable_type_is <const> = function (self, value)
if getmetatable(value) ~= maptable_instance_metatable then
return false
end
local instance_private <const>, _ = maptable_instance_check(value)
return (instance_private.maptable == self)
end
local maptable_type_is_frozen <const> = function (self, instance)
local instance_private <const>, _ = maptable_instance_check(instance)
assert(instance_private.maptable == self, "MapTable type method used with incompatible type")
return instance_private.frozen
end
local maptable_type_class_methods <const> = {
['is']=maptable_type_is,
['is_frozen']=maptable_type_is_frozen,
}
local maptable_type_internal_metatable <const> = {
__name = 'MapTableType',
__metatable = maptable_type_metatable,
__index = function (self, key)
local private <const> = assert(
maptable_type_private[self],
"MapTable type not recognized: " .. tostring(self)
)
-- key slot
if key == 'key_slot' then
return private.key_slot
-- value slot
elseif key == 'value_slot' then
return private.value_slot
-- frozen flag
elseif key == 'freeze_instances' then
return private.freeze_instances
-- class methods
elseif maptable_type_class_methods[key] ~= nil then
return maptable_type_class_methods[key]
-- unknown key
else
return nil
end
end,
__newindex = function (_, _, _)
error("MapTable definition cannot be modified")
end,
__call = function (self, value_data, flag_data)
local private <const> = assert(
maptable_type_private[self],
"MapTable type not recognized: " .. tostring(self)
)
local initial_data <const> = {}
for key, value in pairs(value_data) do
if key == nil then
error("MapTable keys cannot be nil")
end
local valid_key <const>, message = private.key_slot('validate', key)
if message then
error("MapTable key '" .. tostring(key) .. "': " .. message)
end
local valid_value <const>, message = private.value_slot('validate', value)
if message then
error("MapTable value '" .. tostring(value) .. "': " .. message)
end
initial_data[valid_key] = valid_value
end
local message <const> = private.validator(initial_data)
if message ~= nil then
assert(type(message) == 'string', "MapTable validator function must return a string message")
error("MapTable instance data is not valid: " .. message)
end
local flags <const> = flag_data or {}
if type(flags) ~= 'table' then
error("MapTable instance flags must be a table")
end
local frozen <const> = flags['frozen']
if frozen ~= nil then
if type(frozen) ~= 'boolean' then
error("MapTable instance frozen flag must be a boolean")
end
if private.freeze_instances and not frozen then
error("MapTable type freeze_instances requires instances to also be frozen")
end
end
local instance_frozen <const> = (frozen or private.freeze_instances)
local instance <const> = {}
maptable_instance_private[instance] = {
maptable=self,
data=initial_data,
frozen=instance_frozen
}
return setmetatable(instance, maptable_instance_internal_metatable)
end,
__gc = function (self)
maptable_type_private[self] = nil
end
}
-- public interface
local MapTable <const> = setmetatable({
create = function (config_data)
if type(config_data) ~= 'table' then
error("MapTable config must be a table")
end
local key_slot <const> = (config_data['key_slot'] or AnySlot)
if not Slot.is(key_slot) then
error("MapTable key_slot must be a Slot instance")
end
local value_slot <const> = (config_data['value_slot'] or AnySlot)
if not Slot.is(value_slot) then
error("MapTable value_slot must be a Slot instance")
end
local freeze_instances <const> = (config_data['freeze_instances'] or false)
if type(freeze_instances) ~= 'boolean' then
error("MapTable freeze_instances flag must be a boolean")
end
local validator <const> = (config_data['validator'] or (function (_) return end))
if type(validator) ~= 'function' then
error("MapTable validator flag must be a function")
end
local instance <const> = {}
maptable_type_private[instance] = {
key_slot=key_slot,
value_slot=value_slot,
freeze_instances=freeze_instances,
validator=validator,
}
return setmetatable(instance, maptable_type_internal_metatable)
end,
is = function (value)
return (getmetatable(value) == maptable_type_metatable)
end
}, {
__call = function (self, ...)
return self.create(...)
end
})
--[[
Generic Internal Helpers
--]]
local generic_table_is_instance <const> = function (value)
local mt <const> = getmetatable(value)
return (
mt == datatable_instance_metatable or
mt == arraytable_instance_metatable or
mt == maptable_instance_metatable
)
end
local generic_table_is_type <const> = function (value)
local mt <const> = getmetatable(value)
return (
mt == datatable_type_metatable or
mt == arraytable_type_metatable or
mt == maptable_type_metatable
)
end
-- return a shallow copy of table instances contained by the provided instance
-- note: this only checks one level deep in order to avoid searching
local generic_table_nested_instances = function (instance)
local values <const> = {}
local mt <const> = getmetatable(instance)
if mt == datatable_instance_metatable then
local private <const> = assert(datatable_instance_private[instance])
for _, value in pairs(private.data) do
if generic_table_is_instance(value) then
table.insert(values, value)
end
end
elseif mt == arraytable_instance_metatable then
local private <const> = assert(arraytable_instance_private[instance])
for _, value in ipairs(private.data) do
if generic_table_is_instance(value) then
table.insert(values, value)
end
end
elseif mt == maptable_instance_metatable then
local private <const> = assert(maptable_instance_private[instance])
for key, value in pairs(private.data) do
if generic_table_is_instance(key) then
table.insert(values, key)
end
if generic_table_is_instance(value) then
table.insert(values, value)
end
end
else
error("invalid table instance type")
end
return values
end