-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTHSpec.hs
1448 lines (1257 loc) · 51.2 KB
/
THSpec.hs
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
module FlatBuffers.Internal.Compiler.THSpec where
import Control.Arrow (second)
import Data.Bits ((.&.))
import Data.Int
import Data.Text (Text)
import Data.Text qualified as T
import Data.Word
import FlatBuffers.Internal.Build
import FlatBuffers.Internal.Compiler.Parser qualified as P
import FlatBuffers.Internal.Compiler.SemanticAnalysis (validateSchemas)
import FlatBuffers.Internal.Compiler.SyntaxTree (FileTree(..))
import FlatBuffers.Internal.Compiler.TH
import FlatBuffers.Internal.FileIdentifier (HasFileIdentifier(..), unsafeFileIdentifier)
import FlatBuffers.Internal.Read
import FlatBuffers.Internal.Types
import FlatBuffers.Internal.Write
import Language.Haskell.TH
import Language.Haskell.TH.Syntax
import TestImports
import Text.Megaparsec
(ParseErrorBundle, ShowErrorComponent, TraversableStream, VisualStream, errorBundlePretty, parse)
import Text.RawString.QQ (r)
spec :: Spec
spec =
describe "TH" $ do
describe "Tables" $ do
it "with file identifier" $
[r|
table t {}
root_type t;
file_identifier "ABCD";
|] `shouldCompileTo`
[d|
data T
t :: WriteTable T
t = writeTable []
instance HasFileIdentifier T where
getFileIdentifier = unsafeFileIdentifier (T.pack "ABCD")
|]
it "naming conventions" $ do
let expected =
[d|
data SomePerson
somePerson :: Maybe Int32 -> WriteTable SomePerson
somePerson personAge = writeTable [ optionalDef 0 writeInt32TableField personAge ]
somePersonPersonAge :: Table SomePerson -> Either ReadError Int32
somePersonPersonAge = readTableFieldWithDef readInt32 0 0
|]
[r| table some_person { person_age: int; }|] `shouldCompileTo` expected
[r| table Some_Person { Person_Age: int; }|] `shouldCompileTo` expected
[r| table SomePerson { PersonAge: int; }|] `shouldCompileTo` expected
[r| table somePerson { personAge: int; }|] `shouldCompileTo` expected
it "with keyword as name" $
[r| table Type {} |] `shouldCompileTo`
[d|
data Type
type_ :: WriteTable Type
type_ = writeTable []
|]
describe "numeric fields + boolean" $ do
it "normal fields" $
[r|
table Scalars {
// scalars
a: uint8;
b: uint16;
c: uint32;
d: uint64;
e: int8;
f: int16;
g: int32;
h: int64;
i: float32;
j: float64;
k: bool;
}
|] `shouldCompileTo`
[d|
data Scalars
scalars ::
Maybe Word8
-> Maybe Word16
-> Maybe Word32
-> Maybe Word64
-> Maybe Int8
-> Maybe Int16
-> Maybe Int32
-> Maybe Int64
-> Maybe Float
-> Maybe Double
-> Maybe Bool
-> WriteTable Scalars
scalars a b c d e f g h i j k =
writeTable
[ optionalDef 0 writeWord8TableField a
, optionalDef 0 writeWord16TableField b
, optionalDef 0 writeWord32TableField c
, optionalDef 0 writeWord64TableField d
, optionalDef 0 writeInt8TableField e
, optionalDef 0 writeInt16TableField f
, optionalDef 0 writeInt32TableField g
, optionalDef 0 writeInt64TableField h
, optionalDef 0.0 writeFloatTableField i
, optionalDef 0.0 writeDoubleTableField j
, optionalDef False writeBoolTableField k
]
scalarsA :: Table Scalars -> Either ReadError Word8
scalarsA = readTableFieldWithDef readWord8 0 0
scalarsB :: Table Scalars -> Either ReadError Word16
scalarsB = readTableFieldWithDef readWord16 1 0
scalarsC :: Table Scalars -> Either ReadError Word32
scalarsC = readTableFieldWithDef readWord32 2 0
scalarsD :: Table Scalars -> Either ReadError Word64
scalarsD = readTableFieldWithDef readWord64 3 0
scalarsE :: Table Scalars -> Either ReadError Int8
scalarsE = readTableFieldWithDef readInt8 4 0
scalarsF :: Table Scalars -> Either ReadError Int16
scalarsF = readTableFieldWithDef readInt16 5 0
scalarsG :: Table Scalars -> Either ReadError Int32
scalarsG = readTableFieldWithDef readInt32 6 0
scalarsH :: Table Scalars -> Either ReadError Int64
scalarsH = readTableFieldWithDef readInt64 7 0
scalarsI :: Table Scalars -> Either ReadError Float
scalarsI = readTableFieldWithDef readFloat 8 0.0
scalarsJ :: Table Scalars -> Either ReadError Double
scalarsJ = readTableFieldWithDef readDouble 9 0.0
scalarsK :: Table Scalars -> Either ReadError Bool
scalarsK = readTableFieldWithDef readBool 10 False
|]
it "deprecated fields" $
[r|
table Scalars {
a: uint8 (deprecated);
b: uint16 (deprecated);
c: uint32 (deprecated);
d: uint64 (deprecated);
e: int8 (deprecated);
f: int16 (deprecated);
g: int32 (deprecated);
h: int64 (deprecated);
i: float32 (deprecated);
j: float64 (deprecated);
k: bool (deprecated);
}
|] `shouldCompileTo`
[d|
data Scalars
scalars :: WriteTable Scalars
scalars =
writeTable
[ deprecated, deprecated, deprecated, deprecated, deprecated, deprecated, deprecated, deprecated, deprecated, deprecated, deprecated
]
|]
it "with default values" $
[r|
table Scalars {
// scalars
a: uint8 = 8;
b: uint16 = 16;
c: uint32 = 32;
d: uint64 = 64;
e: int8 = -1;
f: int16 = -2;
g: int32 = -4;
h: int64 = -8;
i: float32 = 3.9;
j: float64 = -2.3e10;
k: bool = true;
}
|] `shouldCompileTo`
[d|
data Scalars
scalars ::
Maybe Word8
-> Maybe Word16
-> Maybe Word32
-> Maybe Word64
-> Maybe Int8
-> Maybe Int16
-> Maybe Int32
-> Maybe Int64
-> Maybe Float
-> Maybe Double
-> Maybe Bool
-> WriteTable Scalars
scalars a b c d e f g h i j k =
writeTable
[ optionalDef 8 writeWord8TableField a
, optionalDef 16 writeWord16TableField b
, optionalDef 32 writeWord32TableField c
, optionalDef 64 writeWord64TableField d
, optionalDef (-1) writeInt8TableField e
, optionalDef (-2) writeInt16TableField f
, optionalDef (-4) writeInt32TableField g
, optionalDef (-8) writeInt64TableField h
, optionalDef 3.9 writeFloatTableField i
, optionalDef (-2.3e10) writeDoubleTableField j
, optionalDef True writeBoolTableField k
]
scalarsA :: Table Scalars -> Either ReadError Word8
scalarsA = readTableFieldWithDef readWord8 0 8
scalarsB :: Table Scalars -> Either ReadError Word16
scalarsB = readTableFieldWithDef readWord16 1 16
scalarsC :: Table Scalars -> Either ReadError Word32
scalarsC = readTableFieldWithDef readWord32 2 32
scalarsD :: Table Scalars -> Either ReadError Word64
scalarsD = readTableFieldWithDef readWord64 3 64
scalarsE :: Table Scalars -> Either ReadError Int8
scalarsE = readTableFieldWithDef readInt8 4 (-1)
scalarsF :: Table Scalars -> Either ReadError Int16
scalarsF = readTableFieldWithDef readInt16 5 -2
scalarsG :: Table Scalars -> Either ReadError Int32
scalarsG = readTableFieldWithDef readInt32 6 -4
scalarsH :: Table Scalars -> Either ReadError Int64
scalarsH = readTableFieldWithDef readInt64 7 -8
scalarsI :: Table Scalars -> Either ReadError Float
scalarsI = readTableFieldWithDef readFloat 8 3.9
scalarsJ :: Table Scalars -> Either ReadError Double
scalarsJ = readTableFieldWithDef readDouble 9 -2.3e10
scalarsK :: Table Scalars -> Either ReadError Bool
scalarsK = readTableFieldWithDef readBool 10 True
|]
describe "string fields" $ do
it "normal field" $
[r| table T {s: string;} |] `shouldCompileTo`
[d|
data T
t :: Maybe Text -> WriteTable T
t s = writeTable [optional writeTextTableField s]
tS :: Table T -> Either ReadError (Maybe Text)
tS = readTableFieldOpt readText 0
|]
it "deprecated" $
[r| table T {s: string (deprecated);} |] `shouldCompileTo`
[d|
data T
t :: WriteTable T
t = writeTable [deprecated]
|]
it "required" $
[r| table T {s: string (required);} |] `shouldCompileTo`
[d|
data T
t :: Text -> WriteTable T
t s = writeTable [writeTextTableField s]
tS :: Table T -> Either ReadError Text
tS = readTableFieldReq readText 0 "s"
|]
describe "enum fields" $
it "are encoded as fields of the underlying type" $
[r|
enum Color:int8 { Red = 1, Blue }
table T {x: Color = Blue; }
|] `shouldCompileTo`
[d|
data Color = ColorRed | ColorBlue
deriving (Eq, Show, Read, Ord, Bounded)
toColor :: Int8 -> Maybe Color
toColor n =
case n of
1 -> Just ColorRed
2 -> Just ColorBlue
_ -> Nothing
{-# INLINE toColor #-}
fromColor :: Color -> Int8
fromColor n =
case n of
ColorRed -> 1
ColorBlue -> 2
{-# INLINE fromColor #-}
colorName :: Color -> Text
colorName c =
case c of
ColorRed -> T.pack "Red"
ColorBlue -> T.pack "Blue"
{-# INLINE colorName #-}
data T
t :: Maybe Int8 -> WriteTable T
t x = writeTable [ optionalDef 2 writeInt8TableField x ]
tX :: Table T -> Either ReadError Int8
tX = readTableFieldWithDef readInt8 0 2
|]
describe "enum fields with bit_flags" $
it "are encoded as fields of the underlying type" $
[r|
enum Colors: ubyte (bit_flags) { Red = 2, Blue }
table T {x: Colors = Blue; }
|] `shouldCompileTo`
[d|
colorsRed :: Word8
colorsRed = 4
colorsBlue :: Word8
colorsBlue = 8
allColors :: [Word8]
allColors = [ colorsRed, colorsBlue ]
{-# INLINE allColors #-}
colorsNames :: Word8 -> [Text]
colorsNames c = res2
where
res0 = []
res1 = if colorsBlue .&. c /= 0 then T.pack "Blue" : res0 else res0
res2 = if colorsRed .&. c /= 0 then T.pack "Red" : res1 else res1
{-# INLINE colorsNames #-}
data T
t :: Maybe Word8 -> WriteTable T
t x = writeTable [ optionalDef 8 writeWord8TableField x ]
tX :: Table T -> Either ReadError Word8
tX = readTableFieldWithDef readWord8 0 8
|]
describe "struct fields" $ do
it "normal field" $
[r|
table T {x: S;}
struct S {x: int;}
|] `shouldCompileTo`
[d|
data S
instance IsStruct S where
structAlignmentOf = 4
structSizeOf = 4
s :: Int32 -> WriteStruct S
s x = WriteStruct (buildInt32 x)
sX :: Struct S -> Either ReadError Int32
sX = readStructField readInt32 0
data T
t :: Maybe (WriteStruct S) -> WriteTable T
t x = writeTable [optional writeStructTableField x]
tX :: Table T -> Either ReadError (Maybe (Struct S))
tX = readTableFieldOpt (Right . readStruct) 0
|]
it "deprecated" $
[r|
table T {x: S (deprecated);}
struct S {x: int;}
|] `shouldCompileTo`
[d|
data S
instance IsStruct S where
structAlignmentOf = 4
structSizeOf = 4
s :: Int32 -> WriteStruct S
s x = WriteStruct (buildInt32 x)
sX :: Struct S -> Either ReadError Int32
sX = readStructField readInt32 0
data T
t :: WriteTable T
t = writeTable [deprecated]
|]
it "required" $
[r|
table T {X: S (required) ;}
struct S {x: int;}
|] `shouldCompileTo`
[d|
data S
instance IsStruct S where
structAlignmentOf = 4
structSizeOf = 4
s :: Int32 -> WriteStruct S
s x = WriteStruct (buildInt32 x)
sX :: Struct S -> Either ReadError Int32
sX = readStructField readInt32 0
data T
t :: WriteStruct S -> WriteTable T
t x = writeTable [writeStructTableField x]
tX :: Table T -> Either ReadError (Struct S)
tX = readTableFieldReq (Right . readStruct) 0 "X"
|]
describe "table fields" $ do
it "normal field" $
[r|
table T1 {x: t2;}
table t2{}
|] `shouldCompileTo`
[d|
data T1
t1 :: Maybe (WriteTable T2) -> WriteTable T1
t1 x = writeTable [optional writeTableTableField x]
t1X :: Table T1 -> Either ReadError (Maybe (Table T2))
t1X = readTableFieldOpt readTable 0
data T2
t2 :: WriteTable T2
t2 = writeTable []
|]
it "deprecated" $
[r|
table T1 {x: t2 (deprecated) ;}
table t2{}
|] `shouldCompileTo`
[d|
data T1
t1 :: WriteTable T1
t1 = writeTable [deprecated]
data T2
t2 :: WriteTable T2
t2 = writeTable []
|]
it "required" $
[r|
table T1 {x: t2 (required) ;}
table t2{}
|] `shouldCompileTo`
[d|
data T1
t1 :: WriteTable T2 -> WriteTable T1
t1 x = writeTable [writeTableTableField x]
t1X :: Table T1 -> Either ReadError (Table T2)
t1X = readTableFieldReq readTable 0 "x"
data T2
t2 :: WriteTable T2
t2 = writeTable []
|]
describe "union fields" $ do
it "normal field" $
[r|
table t1 {x: u1;}
union u1{t1}
|] `shouldCompileTo`
[d|
data T1
t1 :: Maybe (WriteUnion U1) -> WriteTable T1
t1 x = writeTable
[ optional writeUnionTypeTableField x
, optional writeUnionValueTableField x
]
t1X :: Table T1 -> Either ReadError (Maybe (Union U1))
t1X = readTableFieldUnionOpt readU1 1
data U1
= U1T1 !(Table T1)
u1T1 :: WriteTable T1 -> WriteUnion U1
u1T1 = writeUnion 1
readU1 :: Positive Word8 -> PositionInfo -> Either ReadError (Union U1)
readU1 n pos =
case getPositive n of
1 -> Union . U1T1 <$> readTable' pos
n' -> pure $! UnionUnknown n'
|]
it "deprecated" $
[r|
table t1 {x: u1 (deprecated) ;}
union u1{t1}
|] `shouldCompileTo`
[d|
data T1
t1 :: WriteTable T1
t1 = writeTable
[ deprecated
, deprecated
]
data U1
= U1T1 !(Table T1)
u1T1 :: WriteTable T1 -> WriteUnion U1
u1T1 = writeUnion 1
readU1 :: Positive Word8 -> PositionInfo -> Either ReadError (Union U1)
readU1 n pos =
case getPositive n of
1 -> Union . U1T1 <$> readTable' pos
n' -> pure $! UnionUnknown n'
|]
it "required" $
[r|
table t1 {x: u1 (required) ;}
union u1{t1}
|] `shouldCompileTo`
[d|
data T1
t1 :: WriteUnion U1 -> WriteTable T1
t1 x = writeTable
[ writeUnionTypeTableField x
, writeUnionValueTableField x
]
t1X :: Table T1 -> Either ReadError (Union U1)
t1X = readTableFieldUnionReq readU1 1 "x"
data U1
= U1T1 !(Table T1)
u1T1 :: WriteTable T1 -> WriteUnion U1
u1T1 = writeUnion 1
readU1 :: Positive Word8 -> PositionInfo -> Either ReadError (Union U1)
readU1 n pos =
case getPositive n of
1 -> Union . U1T1 <$> readTable' pos
n' -> pure $! UnionUnknown n'
|]
describe "vector fields" $ do
it "deprecated" $
[r|
table t1 {
a: [int8] (deprecated);
b: [u1] (deprecated);
}
union u1{t1}
|] `shouldCompileTo`
[d|
data T1
t1 :: WriteTable T1
t1 = writeTable [ deprecated, deprecated, deprecated ]
data U1
= U1T1 !(Table T1)
u1T1 :: WriteTable T1 -> WriteUnion U1
u1T1 = writeUnion 1
readU1 :: Positive Word8 -> PositionInfo -> Either ReadError (Union U1)
readU1 n pos =
case getPositive n of
1 -> Union . U1T1 <$> readTable' pos
n' -> pure $! UnionUnknown n'
|]
describe "vector of numeric types / booolean" $ do
it "normal" $
[r|
table t1 {
a: [uint8];
b: [uint16];
c: [uint32];
d: [uint64];
e: [int8];
f: [int16];
g: [int32];
h: [int64];
i: [float32];
j: [float64];
k: [bool];
}
|] `shouldCompileTo`
[d|
data T1
t1 ::
Maybe (WriteVector Word8)
-> Maybe (WriteVector Word16)
-> Maybe (WriteVector Word32)
-> Maybe (WriteVector Word64)
-> Maybe (WriteVector Int8)
-> Maybe (WriteVector Int16)
-> Maybe (WriteVector Int32)
-> Maybe (WriteVector Int64)
-> Maybe (WriteVector Float)
-> Maybe (WriteVector Double)
-> Maybe (WriteVector Bool)
-> WriteTable T1
t1 a b c d e f g h i j k =
writeTable
[ optional writeVectorWord8TableField a
, optional writeVectorWord16TableField b
, optional writeVectorWord32TableField c
, optional writeVectorWord64TableField d
, optional writeVectorInt8TableField e
, optional writeVectorInt16TableField f
, optional writeVectorInt32TableField g
, optional writeVectorInt64TableField h
, optional writeVectorFloatTableField i
, optional writeVectorDoubleTableField j
, optional writeVectorBoolTableField k
]
t1A :: Table T1 -> Either ReadError (Maybe (Vector Word8))
t1A = readTableFieldOpt (readPrimVector VectorWord8) 0
t1B :: Table T1 -> Either ReadError (Maybe (Vector Word16))
t1B = readTableFieldOpt (readPrimVector VectorWord16) 1
t1C :: Table T1 -> Either ReadError (Maybe (Vector Word32))
t1C = readTableFieldOpt (readPrimVector VectorWord32) 2
t1D :: Table T1 -> Either ReadError (Maybe (Vector Word64))
t1D = readTableFieldOpt (readPrimVector VectorWord64) 3
t1E :: Table T1 -> Either ReadError (Maybe (Vector Int8))
t1E = readTableFieldOpt (readPrimVector VectorInt8) 4
t1F :: Table T1 -> Either ReadError (Maybe (Vector Int16))
t1F = readTableFieldOpt (readPrimVector VectorInt16) 5
t1G :: Table T1 -> Either ReadError (Maybe (Vector Int32))
t1G = readTableFieldOpt (readPrimVector VectorInt32) 6
t1H :: Table T1 -> Either ReadError (Maybe (Vector Int64))
t1H = readTableFieldOpt (readPrimVector VectorInt64) 7
t1I :: Table T1 -> Either ReadError (Maybe (Vector Float))
t1I = readTableFieldOpt (readPrimVector VectorFloat) 8
t1J :: Table T1 -> Either ReadError (Maybe (Vector Double))
t1J = readTableFieldOpt (readPrimVector VectorDouble) 9
t1K :: Table T1 -> Either ReadError (Maybe (Vector Bool))
t1K = readTableFieldOpt (readPrimVector VectorBool) 10
|]
it "required" $
[r|
table t1 {
a: [uint8] (required);
b: [uint16] (required);
c: [uint32] (required);
d: [uint64] (required);
e: [int8] (required);
f: [int16] (required);
g: [int32] (required);
h: [int64] (required);
i: [float32] (required);
j: [float64] (required);
k: [bool] (required);
}
|] `shouldCompileTo`
[d|
data T1
t1 ::
WriteVector Word8
-> WriteVector Word16
-> WriteVector Word32
-> WriteVector Word64
-> WriteVector Int8
-> WriteVector Int16
-> WriteVector Int32
-> WriteVector Int64
-> WriteVector Float
-> WriteVector Double
-> WriteVector Bool
-> WriteTable T1
t1 a b c d e f g h i j k =
writeTable
[ writeVectorWord8TableField a
, writeVectorWord16TableField b
, writeVectorWord32TableField c
, writeVectorWord64TableField d
, writeVectorInt8TableField e
, writeVectorInt16TableField f
, writeVectorInt32TableField g
, writeVectorInt64TableField h
, writeVectorFloatTableField i
, writeVectorDoubleTableField j
, writeVectorBoolTableField k
]
t1A :: Table T1 -> Either ReadError (Vector Word8)
t1A = readTableFieldReq (readPrimVector VectorWord8) 0 "a"
t1B :: Table T1 -> Either ReadError (Vector Word16)
t1B = readTableFieldReq (readPrimVector VectorWord16) 1 "b"
t1C :: Table T1 -> Either ReadError (Vector Word32)
t1C = readTableFieldReq (readPrimVector VectorWord32) 2 "c"
t1D :: Table T1 -> Either ReadError (Vector Word64)
t1D = readTableFieldReq (readPrimVector VectorWord64) 3 "d"
t1E :: Table T1 -> Either ReadError (Vector Int8)
t1E = readTableFieldReq (readPrimVector VectorInt8) 4 "e"
t1F :: Table T1 -> Either ReadError (Vector Int16)
t1F = readTableFieldReq (readPrimVector VectorInt16) 5 "f"
t1G :: Table T1 -> Either ReadError (Vector Int32)
t1G = readTableFieldReq (readPrimVector VectorInt32) 6 "g"
t1H :: Table T1 -> Either ReadError (Vector Int64)
t1H = readTableFieldReq (readPrimVector VectorInt64) 7 "h"
t1I :: Table T1 -> Either ReadError (Vector Float)
t1I = readTableFieldReq (readPrimVector VectorFloat) 8 "i"
t1J :: Table T1 -> Either ReadError (Vector Double)
t1J = readTableFieldReq (readPrimVector VectorDouble) 9 "j"
t1K :: Table T1 -> Either ReadError (Vector Bool)
t1K = readTableFieldReq (readPrimVector VectorBool) 10 "k"
|]
describe "vector of strings" $ do
it "normal" $
[r|
table t1 { a: [string]; }
|] `shouldCompileTo`
[d|
data T1
t1 :: Maybe (WriteVector Text) -> WriteTable T1
t1 a = writeTable [ optional writeVectorTextTableField a ]
t1A :: Table T1 -> Either ReadError (Maybe (Vector Text))
t1A = readTableFieldOpt (readPrimVector VectorText) 0
|]
it "required" $
[r|
table t1 { a: [string] (required); }
|] `shouldCompileTo`
[d|
data T1
t1 :: WriteVector Text -> WriteTable T1
t1 a = writeTable [ writeVectorTextTableField a ]
t1A :: Table T1 -> Either ReadError (Vector Text)
t1A = readTableFieldReq (readPrimVector VectorText) 0 "a"
|]
describe "vector of enums" $ do
it "normal" $
[r|
table t1 { a: [color]; }
enum color : short { red }
|] `shouldCompileTo`
[d|
data Color = ColorRed
deriving (Eq, Show, Read, Ord, Bounded)
toColor :: Int16 -> Maybe Color
toColor n =
case n of
0 -> Just ColorRed
_ -> Nothing
{-# INLINE toColor #-}
fromColor :: Color -> Int16
fromColor n = case n of ColorRed -> 0
{-# INLINE fromColor #-}
colorName :: Color -> Text
colorName c = case c of ColorRed -> T.pack "red"
{-# INLINE colorName #-}
data T1
t1 :: Maybe (WriteVector Int16) -> WriteTable T1
t1 a = writeTable
[ optional writeVectorInt16TableField a
]
t1A :: Table T1 -> Either ReadError (Maybe (Vector Int16))
t1A = readTableFieldOpt (readPrimVector VectorInt16) 0
|]
it "required" $
[r|
table t1 { a: [color] (required); }
enum color : short { red }
|] `shouldCompileTo`
[d|
data Color = ColorRed
deriving (Eq, Show, Read, Ord, Bounded)
toColor :: Int16 -> Maybe Color
toColor n =
case n of
0 -> Just ColorRed
_ -> Nothing
{-# INLINE toColor #-}
fromColor :: Color -> Int16
fromColor n = case n of ColorRed -> 0
{-# INLINE fromColor #-}
colorName :: Color -> Text
colorName c = case c of ColorRed -> T.pack "red"
{-# INLINE colorName #-}
data T1
t1 :: WriteVector Int16 -> WriteTable T1
t1 a = writeTable
[ writeVectorInt16TableField a
]
t1A :: Table T1 -> Either ReadError (Vector Int16)
t1A = readTableFieldReq (readPrimVector VectorInt16) 0 "a"
|]
describe "vector of enums with bit_flags" $ do
it "normal" $
[r|
table t1 { a: [colors]; }
enum colors : ulong (bit_flags) { red = 20 }
|] `shouldCompileTo`
[d|
colorsRed :: Word64
colorsRed = 1048576
allColors :: [Word64]
allColors = [ colorsRed ]
{-# INLINE allColors #-}
colorsNames :: Word64 -> [Text]
colorsNames c = res1
where
res0 = []
res1 = if colorsRed .&. c /= 0 then T.pack "red" : res0 else res0
{-# INLINE colorsNames #-}
data T1
t1 :: Maybe (WriteVector Word64) -> WriteTable T1
t1 a = writeTable
[ optional writeVectorWord64TableField a
]
t1A :: Table T1 -> Either ReadError (Maybe (Vector Word64))
t1A = readTableFieldOpt (readPrimVector VectorWord64) 0
|]
it "required" $
[r|
table t1 { a: [colors] (required); }
enum colors : uint64 (bit_flags) { red = 63 }
|] `shouldCompileTo`
[d|
colorsRed :: Word64
colorsRed = 9223372036854775808
allColors :: [Word64]
allColors = [ colorsRed ]
{-# INLINE allColors #-}
colorsNames :: Word64 -> [Text]
colorsNames c = res1
where
res0 = []
res1 = if colorsRed .&. c /= 0 then T.pack "red" : res0 else res0
{-# INLINE colorsNames #-}
data T1
t1 :: WriteVector Word64 -> WriteTable T1
t1 a = writeTable
[ writeVectorWord64TableField a
]
t1A :: Table T1 -> Either ReadError (Vector Word64)
t1A = readTableFieldReq (readPrimVector VectorWord64) 0 "a"
|]
describe "vector of structs" $ do
it "normal" $
[r|
table t1 { a: [s1]; }
struct s1 (force_align: 8) { a: ubyte; }
|] `shouldCompileTo`
[d|
data S1
instance IsStruct S1 where
structAlignmentOf = 8
structSizeOf = 8
s1 :: Word8 -> WriteStruct S1
s1 a = WriteStruct (buildWord8 a <> buildPadding 7)
s1A :: Struct S1 -> Either ReadError Word8
s1A = readStructField readWord8 0
data T1
t1 :: Maybe (WriteVector (WriteStruct S1)) -> WriteTable T1
t1 a = writeTable
[ optional writeVectorStructTableField a
]
t1A :: Table T1 -> Either ReadError (Maybe (Vector (Struct S1)))
t1A = readTableFieldOpt (readPrimVector VectorStruct) 0
|]
it "required" $
[r|
table t1 { a: [s1] (required); }
struct s1 (force_align: 8) { a: ubyte; }
|] `shouldCompileTo`
[d|
data S1
instance IsStruct S1 where
structAlignmentOf = 8
structSizeOf = 8
s1 :: Word8 -> WriteStruct S1
s1 a = WriteStruct (buildWord8 a <> buildPadding 7)
s1A :: Struct S1 -> Either ReadError Word8
s1A = readStructField readWord8 0
data T1
t1 :: WriteVector (WriteStruct S1) -> WriteTable T1
t1 a = writeTable
[ writeVectorStructTableField a
]
t1A :: Table T1 -> Either ReadError (Vector (Struct S1))
t1A = readTableFieldReq (readPrimVector VectorStruct) 0 "a"
|]
describe "vector of tables" $ do
it "normal" $
[r|
table t1 { a: [t1]; }
|] `shouldCompileTo`
[d|
data T1
t1 :: Maybe (WriteVector (WriteTable T1)) -> WriteTable T1
t1 a = writeTable
[ optional writeVectorTableTableField a
]
t1A :: Table T1 -> Either ReadError (Maybe (Vector (Table T1)))
t1A = readTableFieldOpt readTableVector 0
|]
it "required" $
[r|
table t1 { a: [t1] (required); }
|] `shouldCompileTo`
[d|
data T1
t1 :: WriteVector (WriteTable T1) -> WriteTable T1
t1 a = writeTable
[ writeVectorTableTableField a
]
t1A :: Table T1 -> Either ReadError (Vector (Table T1))
t1A = readTableFieldReq readTableVector 0 "a"
|]
describe "vector of unions" $ do
it "normal" $
[r|
table t1 {x: [u1];}
union u1{t1}
|] `shouldCompileTo`
[d|
data T1
t1 :: Maybe (WriteVector (WriteUnion U1)) -> WriteTable T1
t1 x = writeTable
[ optional writeUnionTypesVectorTableField x
, optional writeUnionValuesVectorTableField x
]
t1X :: Table T1 -> Either ReadError (Maybe (Vector (Union U1)))
t1X = readTableFieldUnionVectorOpt readU1 1
data U1
= U1T1 !(Table T1)
u1T1 :: WriteTable T1 -> WriteUnion U1
u1T1 = writeUnion 1
readU1 :: Positive Word8 -> PositionInfo -> Either ReadError (Union U1)
readU1 n pos =
case getPositive n of
1 -> Union . U1T1 <$> readTable' pos
n' -> pure $! UnionUnknown n'
|]
it "required" $
[r|
table t1 {x: [u1] (required);}
union u1{t1}
|] `shouldCompileTo`