-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMain.purs
1551 lines (1288 loc) · 83.3 KB
/
Main.purs
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
-- | Entry point for the code-generating executable plugin `protoc-gen-purescript`.
-- | See the package README for instructions on how to run the code generator.
module Main (main) where
import Prelude
import Control.Monad.Rec.Class (tailRecM, Step(..))
import Data.Array (catMaybes, concat, fold, mapWithIndex)
import Data.Array as Array
import Data.ArrayBuffer.ArrayBuffer as AB
import Data.ArrayBuffer.Builder (execPutM)
import Data.ArrayBuffer.DataView as DV
import Data.CodePoint.Unicode as Unicode
import Data.Either (Either(..), either)
import Data.Maybe (Maybe(..), fromMaybe, maybe)
import Data.Newtype (unwrap)
import Data.String (Pattern(..))
import Data.String as String
import Data.String.Pattern as String.Pattern
import Data.String.Regex as String.Regex
import Data.String.Regex.Flags as String.Regex.Flags
import Data.Traversable (sequence, traverse)
import Data.Tuple (Tuple(..))
import Data.UInt64 (UInt64)
import Data.UInt64 as UInt64
import Effect (Effect)
import Effect.Aff (runAff_, throwError, error)
import Effect.Class (liftEffect)
import Effect.Console as Console
import Google.Protobuf.Compiler.Plugin (CodeGeneratorRequest(..), CodeGeneratorResponse, CodeGeneratorResponse_File(..), mkCodeGeneratorResponse, parseCodeGeneratorRequest, putCodeGeneratorResponse)
import Google.Protobuf.Descriptor (DescriptorProto(..), EnumDescriptorProto(..), EnumValueDescriptorProto(..), FieldDescriptorProto(..), FieldDescriptorProto_Label(..), FieldDescriptorProto_Type(..), FieldOptions(..), FileDescriptorProto(..), OneofDescriptorProto(..), SourceCodeInfo(..), SourceCodeInfo_Location(..))
import Node.Buffer (Buffer, toArrayBuffer, fromArrayBuffer)
import Node.Buffer as Buffer
import Node.Path (basenameWithoutExt)
import Node.Process (stdin, stdout)
import Node.Stream.Aff (readSome, write)
import Parsing (runParserT)
import Unsafe.Coerce (unsafeCoerce)
main :: Effect Unit
main = runAff_ (either (unsafeCoerce >>> Console.error) (\_ -> pure unit)) do
-- There is a race condition here.
-- Protoc just assumes that when it writes the request, we get the whole
-- request on our read of stdin. Protoc doesn't tell us how to determine
-- that we have read the whole request.
-- So we read and parse in a loop until we have enough bytes that the
-- parse succeeds.
flip tailRecM [] \bs -> do
{buffers:b,readagain} <- readSome stdin
let bs' = bs <> b
ab <- liftEffect $ toArrayBuffer =<< Buffer.concat bs'
runParserT (DV.whole ab) (parseCodeGeneratorRequest (AB.byteLength ab)) >>= case _ of
Left err -> do
if not readagain then do
void $ throwError $ error "stdin is not readable."
pure (Done unit)
else if (Array.length bs') < 20 then do
pure (Loop bs')
else do
void $ throwError $ error $ unsafeCoerce err
pure (Done unit)
Right request -> do
responseBuf <- execPutM $ putCodeGeneratorResponse (generate request)
buf :: Buffer <- liftEffect $ fromArrayBuffer responseBuf
write stdout [buf]
pure (Done unit)
generate :: CodeGeneratorRequest -> CodeGeneratorResponse
generate (CodeGeneratorRequest { proto_file }) = do
case traverse (genFile proto_file) proto_file of
Right file ->
mkCodeGeneratorResponse
{ error: Nothing
, file: file
, supported_features: Just feature_proto3_optional
}
Left err ->
mkCodeGeneratorResponse
{ error: Just err
, supported_features: Just feature_proto3_optional
}
where
-- https://github.com/protocolbuffers/protobuf/blob/main/docs/implementing_proto3_presence.md#signaling-that-your-code-generator-supports-proto3-optional
-- https://github.com/protocolbuffers/protobuf/blob/3f5fc4df1de8e12b2235c3006593e22d6993c3f5/src/google/protobuf/compiler/plugin.proto#L115
feature_proto3_optional = UInt64.unsafeFromInt 1 :: UInt64
-- | Names of parent messages for a message or enum.
type NameSpace
= Array String
-- | Protobuf package name.
type PackageName = Array String
-- | * the names of all parent messages
-- | * The “path attribute”
-- | * A message descriptor
data ScopedMsg
= ScopedMsg NameSpace Path DescriptorProto
-- | An enum descriptor, plus the names of all parent messages.
data ScopedEnum
= ScopedEnum NameSpace Path EnumDescriptorProto
-- | Scoped field name which has the qualified package namespace and the field name.
data ScopedField
= ScopedField NameSpace String
-- | This is how we'll return errors while trying to generate the response from the request.
type Resp a
= Either String a
-- | Best way to parse SourceCodeInfo Data From Protobuf Files
-- | https://groups.google.com/g/protobuf/c/AyOQvhtwvYc
type Path = Array Int
genFile :: Array FileDescriptorProto -> FileDescriptorProto -> Resp CodeGeneratorResponse_File
genFile proto_file ( FileDescriptorProto
{ name: fileName
, package
, dependency
, message_type
, enum_type
, source_code_info
}
) = do
let
packageName = case package of -- Optional package, https://protobuf.dev/programming-guides/proto3#packages
Nothing -> []
Just ps -> String.split (String.Pattern.Pattern ".") ps
baseName = case fileName of
Nothing -> "Generated"
Just "" -> "Generated"
Just n -> basenameWithoutExt n ".proto"
messages :: Array ScopedMsg <- sequence $ flattenMessages [] [4] message_type
enums :: Array ScopedEnum <- Right (mapWithIndex (\i e -> ScopedEnum [] [5,i] e) enum_type) <> sequence (flattenEnums [] [4] message_type)
let
fileNameOut = baseName <> "." <> (String.joinWith "." ((map capitalize packageName))) <> ".purs"
-- We have to import the modules qualified in the way because
-- 1. When protoc "fully qualifies" a field type from an imported
-- desriptor, the qualification consists of only the package name
-- 2. protoc allows multiple files to have the same package name,
-- such as descriptor.proto and any.proto (package "google.protobuf")
-- but Purescript requires each file to have a different module name.
let
genImport :: String -> Resp String
genImport fpath = do
pkg <- lookupPackageByFilepath
let
moduleName = mkImportName fpath pkg
let
qualifiedName = Array.dropEnd 1 moduleName
Right $ "import " <> make moduleName <> " as " <> make qualifiedName
where
make = String.joinWith "." <<< map capitalize
lookupPackageByFilepath :: Resp (Array String)
lookupPackageByFilepath = case Array.find (\(FileDescriptorProto f) -> maybe false (_ == fpath) f.name) proto_file of
Just (FileDescriptorProto { package: Just p }) -> Right $ String.split (String.Pattern.Pattern ".") p
_ -> Left $ "Failed genImport lookupPackageByFilepath " <> fpath
mkImportName ::
-- file path
String ->
-- package name
Array String ->
Array String
mkImportName fileString packages = map mkModuleName $ packages <> file
where
file = [ basenameWithoutExt fileString ".proto" ]
let
mkFieldType ::
-- prefix for the name, i.e. "put" "parse"
String ->
-- package-qualified period-separated field name
String ->
String
mkFieldType prefix s =
let
(ScopedField names name) = parseFieldName s
in
if names `beginsWith` packageName && (isLocalMessageName name || isLocalEnumName name) then
-- it's a name in this package
prefix <> (mkTypeName $ Array.drop (Array.length packageName) names <> [ name ])
else
-- it's a name in the top-level of an imported package
String.joinWith "." $ (map mkModuleName $ names) <> [ prefix <> capitalize name ]
where
isLocalMessageName :: String -> Boolean
isLocalMessageName fname =
maybe false (const true)
$ flip Array.find messages
$ \(ScopedMsg _ _ (DescriptorProto { name })) ->
maybe false (fname == _) name
isLocalEnumName :: String -> Boolean
isLocalEnumName ename =
maybe false (const true)
$ flip Array.find enums
$ \(ScopedEnum _ _ (EnumDescriptorProto { name })) ->
maybe false (ename == _) name
parseFieldName :: String -> ScopedField
parseFieldName fname =
if String.take 1 fname == "." then
-- fully qualified
let
names = Array.dropWhile (_ == "") $ String.split (String.Pattern.Pattern ".") fname
in
ScopedField (Array.dropEnd 1 names) (fromMaybe "" $ Array.last names)
else
ScopedField [] fname -- this case should never occur, protoc always qualifies the names for us
beginsWith :: Array String -> Array String -> Boolean
beginsWith xs x = x == Array.take (Array.length x) xs
let
-- Find source code comments
--
-- Best way to parse SourceCodeInfo Data From Protobuf Files
-- https://groups.google.com/g/protobuf/c/AyOQvhtwvYc
commentsLeading :: Path -> Maybe String
commentsLeading path' = do
(SourceCodeInfo {location}) <- source_code_info
(SourceCodeInfo_Location info) <- Array.find (\(SourceCodeInfo_Location {path}) -> path == path') location
commentlines <- arraytrim <$> map trimws <$> String.split (Pattern "\n") <$> info.leading_comments
Just $ String.joinWith "\n" $ append ["\n-- | "] $ append "-- | " <$> commentlines
where
arraytrim :: Array String -> Array String
arraytrim = trimhead <<< trimtail
where
trimhead xs' = case Array.uncons xs' of
Just {head,tail} | String.null head -> tail
_ -> xs'
trimtail xs' = case Array.unsnoc xs' of
Just {init,last} | String.null last -> init
_ -> xs'
trimws :: String -> String
trimws s = case String.uncons s of
Just {head,tail} | Unicode.isSpace head -> tail
_ -> s
-- We have an r and we're merging an l.
-- About merging: https://github.com/protocolbuffers/protobuf/blob/master/docs/field_presence.md
let
genFieldMerge :: FieldDescriptorProto -> Resp String
genFieldMerge ( FieldDescriptorProto
{ name: Just name'
, label: Just FieldDescriptorProto_Label_LABEL_REPEATED
}
) = Right $ fname <> ": r." <> fname <> " <> l." <> fname
where
fname = decapitalize name'
genFieldMerge ( FieldDescriptorProto
{ name: Just name'
, label: Just _
, type: Just FieldDescriptorProto_Type_TYPE_MESSAGE
, type_name: Just tname
}
) = Right $ fname <> ": Prelude.mergeWith " <> mkFieldType "merge" tname <> " l." <> fname <> " r." <> fname
where
fname = decapitalize name'
genFieldMerge ( FieldDescriptorProto
{ name: Just name'
, label: Just _
, type: Just _
}
) = Right $ fname <> ": Prelude.alt l." <> fname <> " r." <> fname
where
fname = decapitalize name'
genFieldMerge _ = Left "Failed genFieldDefault missing FieldDescriptorProto name or label"
let
genFieldMergeOneof ::
NameSpace ->
(Tuple OneofDescriptorProto (Array FieldDescriptorProto)) ->
Resp String
genFieldMergeOneof nameSpace (Tuple (OneofDescriptorProto { name: Just oname }) _) = Right $ fname <> ": merge" <> cname <> " l." <> fname <> " r." <> fname
where
fname = decapitalize oname
cname = String.joinWith "_" $ map capitalize $ nameSpace <> [ oname ]
genFieldMergeOneof _ _ = Left "Failed genFieldMergeOneof missing name"
let
genOneofMerge ::
NameSpace ->
(Tuple OneofDescriptorProto (Array FieldDescriptorProto)) ->
Resp String
genOneofMerge nameSpace (Tuple (OneofDescriptorProto { name: Just oname }) fields) = do
Right $ "merge" <> cname <> " :: Prelude.Maybe " <> cname <> " -> Prelude.Maybe " <> cname <> " -> Prelude.Maybe " <> cname <> "\n"
<> "merge"
<> cname
<> " l r = case Prelude.Tuple l r of\n"
<> (fold $ catMaybes $ map genField fields)
<> " _ -> Prelude.alt l r\n"
where
cname = String.joinWith "_" $ map capitalize $ nameSpace <> [ oname ]
genField :: FieldDescriptorProto -> Maybe String
genField ( FieldDescriptorProto
{ type: Just FieldDescriptorProto_Type_TYPE_MESSAGE
, name: Just name_inner
, type_name: Just tname
}
) = Just $ " Prelude.Tuple (Prelude.Just (" <> fname_inner <> " l')) (Prelude.Just (" <> fname_inner <> " r')) -> Prelude.map " <> fname_inner <> " $ Prelude.mergeWith " <> mkFieldType "merge" tname <> " (Prelude.Just l') (Prelude.Just r')\n"
where
fname_inner = String.joinWith "_" $ map capitalize [ cname, name_inner ]
genField _ = Nothing
genOneofMerge _ _ = Left "Failed genOneofMerge missing name"
let
genTypeOneof ::
NameSpace ->
(Tuple OneofDescriptorProto (Array FieldDescriptorProto)) ->
Resp String
genTypeOneof nameSpace (Tuple (OneofDescriptorProto { name: Just oname }) pfields) = do
fields <- catMaybes <$> traverse go pfields
Right
$ String.joinWith "\n"
[ "data " <> cname
, " = " <> String.joinWith "\n | " fields
, ""
, "derive instance generic" <> cname <> " :: Prelude.Generic " <> cname <> " _"
, "derive instance eq" <> cname <> " :: Prelude.Eq " <> cname
, "instance show" <> cname <> " :: Prelude.Show " <> cname <> " where show = Prelude.genericShow"
, ""
]
where
cname = String.joinWith "_" $ map capitalize $ nameSpace <> [ oname ]
go :: FieldDescriptorProto -> Resp (Maybe String)
go (FieldDescriptorProto { name: Just fname, oneof_index: Just _, type: Just ftype, type_name }) = do
fieldType <- genFieldType ftype type_name
Right $ Just $ (String.joinWith "_" $ map capitalize [ cname, fname ]) <> " " <> fieldType
where
genFieldType :: FieldDescriptorProto_Type -> Maybe String -> Resp String
genFieldType FieldDescriptorProto_Type_TYPE_DOUBLE _ = Right "Number"
genFieldType FieldDescriptorProto_Type_TYPE_FLOAT _ = Right "Prelude.Float32"
genFieldType FieldDescriptorProto_Type_TYPE_INT64 _ = Right "Prelude.Int64"
genFieldType FieldDescriptorProto_Type_TYPE_UINT64 _ = Right "Prelude.UInt64"
genFieldType FieldDescriptorProto_Type_TYPE_INT32 _ = Right "Int"
genFieldType FieldDescriptorProto_Type_TYPE_FIXED64 _ = Right "Prelude.UInt64"
genFieldType FieldDescriptorProto_Type_TYPE_FIXED32 _ = Right "Prelude.UInt"
genFieldType FieldDescriptorProto_Type_TYPE_BOOL _ = Right "Boolean"
genFieldType FieldDescriptorProto_Type_TYPE_STRING _ = Right "String"
genFieldType FieldDescriptorProto_Type_TYPE_MESSAGE (Just tname) = Right $ mkFieldType "" tname
genFieldType FieldDescriptorProto_Type_TYPE_MESSAGE _ = Left "Failed genTypeOneof missing FieldDescriptorProto type_name"
genFieldType FieldDescriptorProto_Type_TYPE_BYTES _ = Right "Prelude.Bytes"
genFieldType FieldDescriptorProto_Type_TYPE_UINT32 _ = Right "Prelude.UInt"
genFieldType FieldDescriptorProto_Type_TYPE_ENUM (Just tname) = Right $ mkFieldType "" tname
genFieldType FieldDescriptorProto_Type_TYPE_ENUM _ = Left "Failed genTypeOneof missing FieldDescriptorProto type_name"
genFieldType FieldDescriptorProto_Type_TYPE_SFIXED32 _ = Right "Int"
genFieldType FieldDescriptorProto_Type_TYPE_SFIXED64 _ = Right "Prelude.Int64"
genFieldType FieldDescriptorProto_Type_TYPE_SINT32 _ = Right "Int"
genFieldType FieldDescriptorProto_Type_TYPE_SINT64 _ = Right "Prelude.Int64"
genFieldType FieldDescriptorProto_Type_TYPE_GROUP _ = Left "Failed genTypeOneof GROUP not supported."
go _ = Right Nothing
genTypeOneof _ _ = Left $ "Failed genTypeOneof missing OneofDescriptorProto name\n"
let
genOneofPut :: NameSpace -> (Tuple OneofDescriptorProto (Array FieldDescriptorProto)) -> Resp String
genOneofPut nameSpace (Tuple (OneofDescriptorProto { name: Just oname }) myfields) =
map (String.joinWith "\n") $ sequence
$ [ Right $ " case r." <> decapitalize oname <> " of"
, Right " Prelude.Nothing -> pure Prelude.unit"
]
<> (map genOneofFieldPut myfields)
where
genOneofFieldPut :: FieldDescriptorProto -> Resp String
genOneofFieldPut ( FieldDescriptorProto
{ name: Just name'
, number: Just fnumber
, type: Just ftype
, type_name
}
) = go ftype type_name
where
-- If you set a oneof field to the default value (such as setting an int32 oneof field to 0), the "case" of that oneof field will be set, and the value will be serialized on the wire.
-- https://protobuf.dev/programming-guides/proto3/#oneof-features
go FieldDescriptorProto_Type_TYPE_DOUBLE _ = Right $ " Prelude.Just (" <> mkTypeName (nameSpace <> [ oname, name' ]) <> " x) -> Prelude.putOptional " <> show fnumber <> " (Prelude.Just x) (\\_ -> false) Prelude.encodeDoubleField"
go FieldDescriptorProto_Type_TYPE_FLOAT _ = Right $ " Prelude.Just (" <> mkTypeName (nameSpace <> [ oname, name' ]) <> " x) -> Prelude.putOptional " <> show fnumber <> " (Prelude.Just x) (\\_ -> false) Prelude.encodeFloatField"
go FieldDescriptorProto_Type_TYPE_INT64 _ = Right $ " Prelude.Just (" <> mkTypeName (nameSpace <> [ oname, name' ]) <> " x) -> Prelude.putOptional " <> show fnumber <> " (Prelude.Just x) (\\_ -> false) Prelude.encodeInt64Field"
go FieldDescriptorProto_Type_TYPE_UINT64 _ = Right $ " Prelude.Just (" <> mkTypeName (nameSpace <> [ oname, name' ]) <> " x) -> Prelude.putOptional " <> show fnumber <> " (Prelude.Just x) (\\_ -> false) Prelude.encodeUint64Field"
go FieldDescriptorProto_Type_TYPE_INT32 _ = Right $ " Prelude.Just (" <> mkTypeName (nameSpace <> [ oname, name' ]) <> " x) -> Prelude.putOptional " <> show fnumber <> " (Prelude.Just x) (\\_ -> false) Prelude.encodeInt32Field"
go FieldDescriptorProto_Type_TYPE_FIXED64 _ = Right $ " Prelude.Just (" <> mkTypeName (nameSpace <> [ oname, name' ]) <> " x) -> Prelude.putOptional " <> show fnumber <> " (Prelude.Just x) (\\_ -> false) Prelude.encodeFixed64Field"
go FieldDescriptorProto_Type_TYPE_FIXED32 _ = Right $ " Prelude.Just (" <> mkTypeName (nameSpace <> [ oname, name' ]) <> " x) -> Prelude.putOptional " <> show fnumber <> " (Prelude.Just x) (\\_ -> false) Prelude.encodeFixed32Field"
go FieldDescriptorProto_Type_TYPE_BOOL _ = Right $ " Prelude.Just (" <> mkTypeName (nameSpace <> [ oname, name' ]) <> " x) -> Prelude.putOptional " <> show fnumber <> " (Prelude.Just x) (\\_ -> false) Prelude.encodeBoolField"
go FieldDescriptorProto_Type_TYPE_STRING _ = Right $ " Prelude.Just (" <> mkTypeName (nameSpace <> [ oname, name' ]) <> " x) -> Prelude.putOptional " <> show fnumber <> " (Prelude.Just x) (\\_ -> false) Prelude.encodeStringField"
go FieldDescriptorProto_Type_TYPE_MESSAGE (Just tname) = Right $ " Prelude.Just (" <> mkTypeName (nameSpace <> [ oname, name' ]) <> " x) -> Prelude.putOptional " <> show fnumber <> " (Prelude.Just x) (\\_ -> false) $ Prelude.putLenDel " <> mkFieldType "put" tname
go FieldDescriptorProto_Type_TYPE_MESSAGE _ = Left "Failed genOneofPut missing FieldDescriptorProto type_name"
go FieldDescriptorProto_Type_TYPE_BYTES _ = Right $ " Prelude.Just (" <> mkTypeName (nameSpace <> [ oname, name' ]) <> " x) -> Prelude.putOptional " <> show fnumber <> " (Prelude.Just x) (\\_ -> false) Prelude.encodeBytesField"
go FieldDescriptorProto_Type_TYPE_UINT32 _ = Right $ " Prelude.Just (" <> mkTypeName (nameSpace <> [ oname, name' ]) <> " x) -> Prelude.putOptional " <> show fnumber <> " (Prelude.Just x) (\\_ -> false) Prelude.encodeUint32Field"
go FieldDescriptorProto_Type_TYPE_ENUM _ = Right $ " Prelude.Just (" <> mkTypeName (nameSpace <> [ oname, name' ]) <> " x) -> Prelude.putOptional " <> show fnumber <> " (Prelude.Just x) (\\_ -> false) Prelude.putEnumField"
go FieldDescriptorProto_Type_TYPE_SFIXED32 _ = Right $ " Prelude.Just (" <> mkTypeName (nameSpace <> [ oname, name' ]) <> " x) -> Prelude.putOptional " <> show fnumber <> " (Prelude.Just x) (\\_ -> false) Prelude.encodeSfixed32Field"
go FieldDescriptorProto_Type_TYPE_SFIXED64 _ = Right $ " Prelude.Just (" <> mkTypeName (nameSpace <> [ oname, name' ]) <> " x) -> Prelude.putOptional " <> show fnumber <> " (Prelude.Just x) (\\_ -> false) Prelude.encodeSfixed64Field"
go FieldDescriptorProto_Type_TYPE_SINT32 _ = Right $ " Prelude.Just (" <> mkTypeName (nameSpace <> [ oname, name' ]) <> " x) -> Prelude.putOptional " <> show fnumber <> " (Prelude.Just x) (\\_ -> false) Prelude.encodeSint32Field"
go FieldDescriptorProto_Type_TYPE_SINT64 _ = Right $ " Prelude.Just (" <> mkTypeName (nameSpace <> [ oname, name' ]) <> " x) -> Prelude.putOptional " <> show fnumber <> " (Prelude.Just x) (\\_ -> false) Prelude.encodeSint64Field"
go FieldDescriptorProto_Type_TYPE_GROUP _ = Left "Failed genOneofPut GROUP not supported."
genOneofFieldPut _ = Left "Failed genOneofPut missing FieldDescriptorProto name or number or type"
genOneofPut _ _ = Left "Failed genOneofPut missing OneofDescriptoroProto name"
let
genFieldPut :: NameSpace -> FieldDescriptorProto -> Resp String
genFieldPut _ ( FieldDescriptorProto
{ name: Just name'
, number: Just fnumber
, label: Just flabel
, type: Just ftype
, type_name
, options
, proto3_optional
}
) = go flabel ftype type_name options
where
isSyntheticOneof = fromMaybe false proto3_optional
fname = decapitalize name'
-- For repeated fields of primitive numeric types, always put the packed
-- encoding.
-- https://protobuf.dev/programming-guides/encoding?hl=en#packed
-- For optional synthetic Oneofs, write even if it's the default value.
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_DOUBLE _ (Just (FieldOptions { packed: Just false })) = Right $ " Prelude.putRepeated " <> show fnumber <> " r." <> fname <> " Prelude.encodeDoubleField"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_DOUBLE _ _ = Right $ " Prelude.putPacked " <> show fnumber <> " r." <> fname <> " Prelude.encodeDouble"
go _ FieldDescriptorProto_Type_TYPE_DOUBLE _ _ =
if isSyntheticOneof then
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " (\\_ -> false) Prelude.encodeDoubleField"
else
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " Prelude.isDefault Prelude.encodeDoubleField"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_FLOAT _ (Just (FieldOptions { packed: Just false })) = Right $ " Prelude.putRepeated " <> show fnumber <> " r." <> fname <> " Prelude.encodeFloatField"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_FLOAT _ _ = Right $ " Prelude.putPacked " <> show fnumber <> " r." <> fname <> " Prelude.encodeFloat"
go _ FieldDescriptorProto_Type_TYPE_FLOAT _ _ =
if isSyntheticOneof then
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " (\\_ -> false) Prelude.encodeFloatField"
else
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " Prelude.isDefault Prelude.encodeFloatField"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_INT64 _ (Just (FieldOptions { packed: Just false })) = Right $ " Prelude.putRepeated " <> show fnumber <> " r." <> fname <> " Prelude.encodeInt64Field"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_INT64 _ _ = Right $ " Prelude.putPacked " <> show fnumber <> " r." <> fname <> " Prelude.encodeInt64"
go _ FieldDescriptorProto_Type_TYPE_INT64 _ _ =
if isSyntheticOneof then
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " (\\_ -> false) Prelude.encodeInt64Field"
else
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " Prelude.isDefault Prelude.encodeInt64Field"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_UINT64 _ (Just (FieldOptions { packed: Just false })) = Right $ " Prelude.putRepeated " <> show fnumber <> " r." <> fname <> " Prelude.encodeUint64Field"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_UINT64 _ _ = Right $ " Prelude.putPacked " <> show fnumber <> " r." <> fname <> " Prelude.encodeUint64"
go _ FieldDescriptorProto_Type_TYPE_UINT64 _ _ =
if isSyntheticOneof then
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " (\\_ -> false) Prelude.encodeUint64Field"
else
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " Prelude.isDefault Prelude.encodeUint64Field"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_INT32 _ (Just (FieldOptions { packed: Just false })) = Right $ " Prelude.putRepeated " <> show fnumber <> " r." <> fname <> " Prelude.encodeInt32Field"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_INT32 _ _ = Right $ " Prelude.putPacked " <> show fnumber <> " r." <> fname <> " Prelude.encodeInt32"
go _ FieldDescriptorProto_Type_TYPE_INT32 _ _ =
if isSyntheticOneof then
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " (\\_ -> false) Prelude.encodeInt32Field"
else
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " Prelude.isDefault Prelude.encodeInt32Field"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_FIXED64 _ (Just (FieldOptions { packed: Just false })) = Right $ " Prelude.putRepeated " <> show fnumber <> " r." <> fname <> " Prelude.encodeFixed64Field"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_FIXED64 _ _ = Right $ " Prelude.putPacked " <> show fnumber <> " r." <> fname <> " Prelude.encodeFixed64"
go _ FieldDescriptorProto_Type_TYPE_FIXED64 _ _ =
if isSyntheticOneof then
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " (\\_ -> false) Prelude.encodeFixed64Field"
else
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " Prelude.isDefault Prelude.encodeFixed64Field"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_FIXED32 _ (Just (FieldOptions { packed: Just false })) = Right $ " Prelude.putRepeated " <> show fnumber <> " r." <> fname <> " Prelude.encodeFixed32Field"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_FIXED32 _ _ = Right $ " Prelude.putPacked " <> show fnumber <> " r." <> fname <> " Prelude.encodeFixed32"
go _ FieldDescriptorProto_Type_TYPE_FIXED32 _ _ =
if isSyntheticOneof then
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " (\\_ -> false) Prelude.encodeFixed32Field"
else
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " Prelude.isDefault Prelude.encodeFixed32Field"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_BOOL _ (Just (FieldOptions { packed: Just false })) = Right $ " Prelude.putRepeated " <> show fnumber <> " r." <> fname <> " Prelude.encodeBoolField"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_BOOL _ _ = Right $ " Prelude.putPacked " <> show fnumber <> " r." <> fname <> " Prelude.encodeBool"
go _ FieldDescriptorProto_Type_TYPE_BOOL _ _ =
if isSyntheticOneof then
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " (\\_ -> false) Prelude.encodeBoolField"
else
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " Prelude.isDefault Prelude.encodeBoolField"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_STRING _ _ = Right $ " Prelude.putRepeated " <> show fnumber <> " r." <> fname <> " Prelude.encodeStringField"
go _ FieldDescriptorProto_Type_TYPE_STRING _ _ =
if isSyntheticOneof then
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " (\\_ -> false) Prelude.encodeStringField"
else
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " Prelude.isDefault Prelude.encodeStringField"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_MESSAGE (Just tname) _ = Right $ " Prelude.putRepeated " <> show fnumber <> " r." <> fname <> " $ Prelude.putLenDel " <> mkFieldType "put" tname
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_MESSAGE _ _ = Left "Failed genFieldPut missing FieldDescriptorProto type_name"
go _ FieldDescriptorProto_Type_TYPE_MESSAGE (Just tname) _ = Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " (\\_ -> false) $ Prelude.putLenDel " <> mkFieldType "put" tname
go _ FieldDescriptorProto_Type_TYPE_MESSAGE _ _ = Left "Failed genFieldPut missing FieldDescriptorProto type_name"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_BYTES _ _ = Right $ " Prelude.putRepeated " <> show fnumber <> " r." <> fname <> " $ Prelude.encodeBytesField"
go _ FieldDescriptorProto_Type_TYPE_BYTES _ _ =
if isSyntheticOneof then
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " (\\_ -> false) Prelude.encodeBytesField"
else
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " Prelude.isDefault Prelude.encodeBytesField"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_UINT32 _ (Just (FieldOptions { packed: Just false })) = Right $ " Prelude.putRepeated " <> show fnumber <> " r." <> fname <> " Prelude.encodeUint32Field"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_UINT32 _ _ = Right $ " Prelude.putPacked " <> show fnumber <> " r." <> fname <> " Prelude.encodeUint32"
go _ FieldDescriptorProto_Type_TYPE_UINT32 _ _ =
if isSyntheticOneof then
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " (\\_ -> false) Prelude.encodeUint32Field"
else
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " Prelude.isDefault Prelude.encodeUint32Field"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_ENUM _ (Just (FieldOptions { packed: Just false })) = Right $ " Prelude.putRepeated " <> show fnumber <> " r." <> fname <> " Prelude.putEnumField"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_ENUM _ _ = Right $ " Prelude.putPacked " <> show fnumber <> " r." <> fname <> " Prelude.putEnum"
go _ FieldDescriptorProto_Type_TYPE_ENUM _ _ =
if isSyntheticOneof then
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " (\\_ -> false) Prelude.putEnumField"
else
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " Prelude.isDefault Prelude.putEnumField"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_SFIXED32 _ (Just (FieldOptions { packed: Just false })) = Right $ " Prelude.putRepeated " <> show fnumber <> " r." <> fname <> " Prelude.encodeSfixed32Field"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_SFIXED32 _ _ = Right $ " Prelude.putPacked " <> show fnumber <> " r." <> fname <> " Prelude.encodeSfixed32"
go _ FieldDescriptorProto_Type_TYPE_SFIXED32 _ _ =
if isSyntheticOneof then
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " (\\_ -> false) Prelude.encodeSfixed32Field"
else
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " Prelude.isDefault Prelude.encodeSfixed32Field"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_SFIXED64 _ (Just (FieldOptions { packed: Just false })) = Right $ " Prelude.putRepeated " <> show fnumber <> " r." <> fname <> " Prelude.encodeSfixed64Field"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_SFIXED64 _ _ = Right $ " Prelude.putPacked " <> show fnumber <> " r." <> fname <> " Prelude.encodeSfixed64"
go _ FieldDescriptorProto_Type_TYPE_SFIXED64 _ _ =
if isSyntheticOneof then
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " (\\_ -> false) Prelude.encodeSfixed64Field"
else
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " Prelude.isDefault Prelude.encodeSfixed64Field"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_SINT32 _ (Just (FieldOptions { packed: Just false })) = Right $ " Prelude.putRepeated " <> show fnumber <> " r." <> fname <> " Prelude.encodeSint32Field"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_SINT32 _ _ = Right $ " Prelude.putPacked " <> show fnumber <> " r." <> fname <> " Prelude.encodeSint32"
go _ FieldDescriptorProto_Type_TYPE_SINT32 _ _ =
if isSyntheticOneof then
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " (\\_ -> false) Prelude.encodeSint32Field"
else
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " Prelude.isDefault Prelude.encodeSint32Field"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_SINT64 _ (Just (FieldOptions { packed: Just false })) = Right $ " Prelude.putRepeated " <> show fnumber <> " r." <> fname <> " Prelude.encodeSint64Field"
go FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_SINT64 _ _ = Right $ " Prelude.putPacked " <> show fnumber <> " r." <> fname <> " Prelude.encodeSint64"
go _ FieldDescriptorProto_Type_TYPE_SINT64 _ _ =
if isSyntheticOneof then
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " (\\_ -> false) Prelude.encodeSint64Field"
else
Right $ " Prelude.putOptional " <> show fnumber <> " r." <> fname <> " Prelude.isDefault Prelude.encodeSint64Field"
go _ FieldDescriptorProto_Type_TYPE_GROUP _ _ = Left "Failed genFieldPut GROUP not supported"
genFieldPut _ arg = Left $ "Failed genFieldPut missing FieldDescriptorProto name or number or label or type\n" <> show arg
let
genFieldParser :: NameSpace -> Array OneofDescriptorProto -> FieldDescriptorProto -> Resp String
genFieldParser nameSpace oneof_decl ( FieldDescriptorProto
{ name: Just name'
, number: Just fnumber
, label: Just flabel
, type: Just ftype
, type_name
, oneof_index
, proto3_optional
}
) = go (lookupOneof oneof_index) flabel ftype type_name
where
lookupOneof :: Maybe Int -> Maybe String
lookupOneof Nothing = Nothing
lookupOneof (Just i) = case Array.index oneof_decl i of
Just (OneofDescriptorProto { name }) -> case proto3_optional of
Just true -> Nothing -- If it's an optional synthetic Oneof, then we pretend it's not a Oneof at all.
_ -> name
_ -> Nothing
fname = decapitalize name'
mkConstructor oname = mkTypeName (nameSpace <> [ oname, name' ])
-- For repeated fields of primitive numeric types, also parse the packed
-- encoding.
-- https://protobuf.dev/programming-guides/encoding?hl=en#packed
go _ FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_DOUBLE _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.Bits64 = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeDouble"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.snoc x"
, " parseField " <> show fnumber <> " Prelude.LenDel = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.parseLenDel $ Prelude.decodeDoubleArray"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.append x"
]
go _ FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_FLOAT _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.Bits32 = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeFloat"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.snoc x"
, " parseField " <> show fnumber <> " Prelude.LenDel = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.parseLenDel $ Prelude.decodeFloatArray"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.append x"
]
go _ FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_INT64 _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.VarInt = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeInt64"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.snoc x"
, " parseField " <> show fnumber <> " Prelude.LenDel = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.parseLenDel $ Prelude.manyLength Prelude.decodeInt64"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.append x"
]
go _ FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_UINT64 _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.VarInt = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeUint64"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.snoc x"
, " parseField " <> show fnumber <> " Prelude.LenDel = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.parseLenDel $ Prelude.manyLength Prelude.decodeUint64"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.append x"
]
go _ FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_INT32 _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.VarInt = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeInt32"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.snoc x"
, " parseField " <> show fnumber <> " Prelude.LenDel = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.parseLenDel $ Prelude.manyLength Prelude.decodeInt32"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.append x"
]
go _ FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_FIXED64 _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.Bits64 = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeFixed64"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.snoc x"
, " parseField " <> show fnumber <> " Prelude.LenDel = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.parseLenDel $ Prelude.decodeFixed64Array"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.append x"
]
go _ FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_FIXED32 _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.Bits32 = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeFixed32"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.snoc x"
, " parseField " <> show fnumber <> " Prelude.LenDel = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.parseLenDel $ Prelude.decodeFixed32Array"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.append x"
]
go _ FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_BOOL _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.VarInt = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeBool"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.snoc x"
, " parseField " <> show fnumber <> " Prelude.LenDel = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.parseLenDel $ Prelude.manyLength Prelude.decodeBool"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.append x"
]
go _ FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_STRING _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.LenDel = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeString"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.snoc x"
]
go _ FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_MESSAGE (Just tname) =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.LenDel = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.parseLenDel " <> mkFieldType "parse" tname
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.snoc x"
]
go _ FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_BYTES _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.LenDel = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeBytes"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.snoc x"
]
go _ FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_UINT32 _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.VarInt = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeUint32"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.snoc x"
, " parseField " <> show fnumber <> " Prelude.LenDel = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.parseLenDel $ Prelude.manyLength Prelude.decodeUint32"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.append x"
]
go _ FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_ENUM _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.VarInt = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.parseEnum"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.snoc x"
, " parseField " <> show fnumber <> " Prelude.LenDel = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.parseLenDel $ Prelude.manyLength Prelude.parseEnum"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.append x"
]
go _ FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_SFIXED32 _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.Bits32 = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeSfixed32"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.snoc x"
, " parseField " <> show fnumber <> " Prelude.LenDel = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.parseLenDel $ Prelude.decodeSfixed32Array"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.append x"
]
go _ FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_SFIXED64 _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.Bits64 = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeSfixed64"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.snoc x"
, " parseField " <> show fnumber <> " Prelude.LenDel = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.parseLenDel $ Prelude.decodeSfixed64Array"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.append x"
]
go _ FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_SINT32 _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.VarInt = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeSint32"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.snoc x"
, " parseField " <> show fnumber <> " Prelude.LenDel = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.parseLenDel $ Prelude.manyLength Prelude.decodeSint32"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.append x"
]
go _ FieldDescriptorProto_Label_LABEL_REPEATED FieldDescriptorProto_Type_TYPE_SINT64 _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.VarInt = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeSint64"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.snoc x"
, " parseField " <> show fnumber <> " Prelude.LenDel = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.parseLenDel $ Prelude.manyLength Prelude.decodeSint64"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ Prelude.flip Prelude.append x"
]
go (Just oname) _ FieldDescriptorProto_Type_TYPE_DOUBLE _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.Bits64 = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeDouble"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> decapitalize oname <> "\") $ \\_ -> Prelude.Just (" <> mkConstructor oname <> " x)"
]
go (Just oname) _ FieldDescriptorProto_Type_TYPE_FLOAT _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.Bits32 = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeFloat"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> decapitalize oname <> "\") $ \\_ -> Prelude.Just (" <> mkConstructor oname <> " x)"
]
go (Just oname) _ FieldDescriptorProto_Type_TYPE_INT64 _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.VarInt = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeInt64"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> decapitalize oname <> "\") $ \\_ -> Prelude.Just (" <> mkConstructor oname <> " x)"
]
go (Just oname) _ FieldDescriptorProto_Type_TYPE_UINT64 _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.VarInt = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeUint64"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> decapitalize oname <> "\") $ \\_ -> Prelude.Just (" <> mkConstructor oname <> " x)"
]
go (Just oname) _ FieldDescriptorProto_Type_TYPE_INT32 _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.VarInt = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeInt32"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> decapitalize oname <> "\") $ \\_ -> Prelude.Just (" <> mkConstructor oname <> " x)"
]
go (Just oname) _ FieldDescriptorProto_Type_TYPE_FIXED64 _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.Bits64 = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeFixed64"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> decapitalize oname <> "\") $ \\_ -> Prelude.Just (" <> mkConstructor oname <> " x)"
]
go (Just oname) _ FieldDescriptorProto_Type_TYPE_FIXED32 _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.Bits32 = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeFixed32"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> decapitalize oname <> "\") $ \\_ -> Prelude.Just (" <> mkConstructor oname <> " x)"
]
go (Just oname) _ FieldDescriptorProto_Type_TYPE_BOOL _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.VarInt = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeBool"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> decapitalize oname <> "\") $ \\_ -> Prelude.Just (" <> mkConstructor oname <> " x)"
]
go (Just oname) _ FieldDescriptorProto_Type_TYPE_STRING _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.LenDel = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeString"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> decapitalize oname <> "\") $ \\_ -> Prelude.Just (" <> mkConstructor oname <> " x)"
]
go (Just oname) _ FieldDescriptorProto_Type_TYPE_MESSAGE (Just tname) =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.LenDel = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.parseLenDel " <> mkFieldType "parse" tname
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> decapitalize oname <> "\") $ " <> mkFieldType "merge" cname <> " (Prelude.Just (" <> mkConstructor oname <> " x))"
]
where
cname = String.joinWith "_" $ map capitalize $ nameSpace <> [ oname ]
go (Just oname) _ FieldDescriptorProto_Type_TYPE_BYTES _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.LenDel = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeBytes"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> decapitalize oname <> "\") $ \\_ -> Prelude.Just (" <> mkConstructor oname <> " x)"
]
go (Just oname) _ FieldDescriptorProto_Type_TYPE_UINT32 _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.VarInt = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeUint32"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> decapitalize oname <> "\") $ \\_ -> Prelude.Just (" <> mkConstructor oname <> " x)"
]
go (Just oname) _ FieldDescriptorProto_Type_TYPE_ENUM _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.VarInt = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.parseEnum"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> decapitalize oname <> "\") $ \\_ -> Prelude.Just (" <> mkConstructor oname <> " x)"
]
go (Just oname) _ FieldDescriptorProto_Type_TYPE_SFIXED32 _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.Bits32 = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeSfixed32"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> decapitalize oname <> "\") $ \\_ -> Prelude.Just (" <> mkConstructor oname <> " x)"
]
go (Just oname) _ FieldDescriptorProto_Type_TYPE_SFIXED64 _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.Bits64 = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeSfixed64"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> decapitalize oname <> "\") $ \\_ -> Prelude.Just (" <> mkConstructor oname <> " x)"
]
go (Just oname) _ FieldDescriptorProto_Type_TYPE_SINT32 _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.VarInt = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeSint32"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> decapitalize oname <> "\") $ \\_ -> Prelude.Just (" <> mkConstructor oname <> " x)"
]
go (Just oname) _ FieldDescriptorProto_Type_TYPE_SINT64 _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.VarInt = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeSint64"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> decapitalize oname <> "\") $ \\_ -> Prelude.Just (" <> mkConstructor oname <> " x)"
]
go _ _ FieldDescriptorProto_Type_TYPE_DOUBLE _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.Bits64 = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeDouble"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ \\_ -> Prelude.Just x"
]
go _ _ FieldDescriptorProto_Type_TYPE_FLOAT _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.Bits32 = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeFloat"
, " pure $ Prelude.modify (Prelude.Proxy :: Prelude.Proxy \"" <> fname <> "\") $ \\_ -> Prelude.Just x"
]
go _ _ FieldDescriptorProto_Type_TYPE_INT64 _ =
Right
$ String.joinWith "\n"
[ " parseField " <> show fnumber <> " Prelude.VarInt = Prelude.label \"" <> name' <> " / \" $ do"
, " x <- Prelude.decodeInt64"