-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.wake
1361 lines (1242 loc) · 51.7 KB
/
json.wake
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
package json
from wake import _
from result import prefixError
from query import _
# Anyone who depends on json will also
# need query functions.
from query export def single query queryExists queryOne queryOptional queryLast queryLastOptional
from query export def qPass qExtern qExternPass qSingle qSinglePass qIf qEditIf qFlatten qEditFlatten qAt qEditAt qDefault
from query export type QueryError
from query export def QueryError makeQueryError
## Helpers that will soon be in the standard library. This will not be exported here.
def lookup (selectFn: a => Boolean): (list: List (Pair a b)) => List b =
def select (Pair key value) =
if selectFn key then
Some value
else
None
mapPartial select
## JSON combinators
# Helper function to uniformly construct error messages for mismatched types.
def renderActualJValue (got: JValue): String =
def renderTypeFailure (got: JValue): String = match got
JNull -> "null"
# TODO: This should only print out a reasonable head of the string, to avoid over-long lines.
JString s -> format s
JInteger i -> str i
JBoolean b -> format b
JDouble d -> dstr d
JObject ks -> "object with {str ks.len} keys"
JArray vs -> "array with {str vs.len} elements"
"(got {renderTypeFailure got})"
# A getter for JNull. Can be used to execute a function when a null is present.
#
# Parameters:
# - `getFn`: The function that consumes the null if present
# - `input`: The JValue to gate the function on
#
# Examples:
# ```
# def isValid Unit = Pass Unit
# query JNull $ jNullFn isValid = Pass Unit
# query (JInteger 10) $ jNullFn isValid = Fail (Error ".: not a null" Nil)
# ```
export def jNullFn (getFn: Unit => Result a QueryError) (input: JValue): Result a QueryError =
match input
JNull -> getFn Unit
_ -> Fail "not a null {renderActualJValue input}".makeQueryError
# An editor for JNull. Can be used to replace a null with some other value.
#
# Parameters:
# - `editFn`: The function that modifies the assumed null
# - `input`: The JValue to be modified
#
# Examples:
# ```
# def insertString Unit = Pass (JString "previously null")
# query JNull $ jSetNullFn insertString = Pass (JString "previouslyNull")
# query (JInteger 10) $ jSetNullFn insertString = Fail (Error ".: not a null" Nil)
# ```
export def jSetNullFn (editFn: Unit => Result JValue QueryError) (input: JValue): Result JValue QueryError =
match input
JNull -> editFn Unit
_ -> Fail "not a null {renderActualJValue input}".makeQueryError
# A getter for JString. Can be used to access strings in json.
# If possible for your case consider using jString instead.
#
# Parameters:
# - `getFn`: The function that consumes the string if present
# - `input`: The JValue to get the string of
#
# Examples:
# ```
# def toInt s = match (int s)
# Some x = Pass x
# None = failWithError "'{s}' is not an integer"
# query (JString "10") $ jStringFn toInt = Pass 10
# query (JInteger 10) $ jStringFn toInt = Fail (Error ".: not a string" Nil)
# query person $ jField "age" $ jStringFn toInt = Pass 10 # If age is stored as a string
# query person $ jField "name" $ jStringFn toInt = Fail (Error "./name: 'Alice' is not an integer" Nil)
# ```
export def jStringFn (getFn: String => Result a QueryError) (input: JValue): Result a QueryError =
match input
JString s -> getFn s
_ -> Fail "not a string {renderActualJValue input}".makeQueryError
# A terminal getter for JString. Can be used to access strings in json.
# Can be used at the end of a '$' chain.
#
# Parameters:
# - `input`: The JValue to get the string of
#
# Examples:
# ```
# query (JString "foo") $ jString = Pass ("foo", Nil)
# query (JInteger 10) $ jString = Fail (Error ".: not a string" Nil)
# queryOne person $ jField "name" $ jString = Pass "Alice"
# queryOne person $ jField "age" $ jString = Fail (Error ".age: not a string" Nil)
# ```
export def jString: (input: JValue) => Result (List String) QueryError =
jStringFn qSinglePass
# An editor for JString. Can be used to modify a JString in
# a JValue in place. Prefer using jEditString when possible.
#
# Parameters:
# - `editFn`: The function that modifies the assumed string
# - `input`: The JValue to be modified
#
# Examples:
# ```
# def updateInt s = match (int s)
# Some x = str (x + 1) | Pass
# None = failWithError "'{s}' is not an integer"
# query (JString "10") $ jEditStringFn updateInt = Pass (JString "11")
# query (JInteger 10) $ jEditStringFn updateInt = Fail (Error ".: not a string" Nil)
#
# # Update your age stored as a string
# query person
# $ jEditField "name"
# $ jEditStringFn
# $ updateInt
#
# # Reports "./name: 'Alice' is not an integer"
# query person
# $ jEditField "name"
# $ jEditStringFn
# $ updateInt
# ```
export def jEditStringFn (editFn: String => Result String QueryError) (input: JValue): Result JValue QueryError =
match input
JString s ->
require Pass es = editFn s
Pass (JString es)
_ -> Fail "not a string {renderActualJValue input}".makeQueryError
# An editor for JString. Can be used to modify a JString in
# a JValue in place.
#
# Parameters:
# - `editFn`: The function that modifies the assumed string
# - `input`: The JValue to be modified
#
# Examples:
# ```
# query (JString "foo") $ jEditString ("{_}bar") = Pass (JString "foobar")
# query (JInteger 10) $ jEditString ("{_}bar") = Fail (Error ".: not a string" Nil)
#
# # Lowercase the .name field
# query person
# $ jEditField "name"
# $ jEditString
# $ unicodeLowercase
#
# # Returns an error. Reports "not a string"
# query person
# $ jEditField "age"
# $ jEditString
# $ unicodeLowercase
# ```
export def jEditString (editFn: String => String): (input: JValue) => Result JValue QueryError =
jEditStringFn
$ qPass editFn
# A getter for integers. Can be used to access integers in json.
# Doubles that are exact integers as well as true integers are
# both allowed but anything else will return an error.
# If possible for your case consider using jInteger instead.
#
# Parameters:
# - `getFn`: The function that consumes the integer if present
# - `input`: The JValue to get the integer of
#
# Examples:
# ```
# def checkAge = match _
# x if x > 130 = failWithError "No one is {str x} years old!"
# x if x < 0 = failWithError "{str x} is not a valid age"
# x = Pass x
# query (JInteger 28) $ jIntegerFn checkAge = Pass 10
# query (JInteger (-3)) $ jIntegerFn checkAge = Fail (Error ".: -3 is not a valid age" Nil)
# query person $ jField "age" $ jIntegerFn $ checkAge = Pass 28
# query person $ jField "name" $ jIntegerFn $ checkAge = Fail (Error "./name: not an integer" Nil)
# ```
export def jIntegerFn (getFn: Integer => Result a QueryError) (input: JValue): Result a QueryError =
match input
JInteger i -> getFn i
JDouble d ->
def Pair whole frac = dmodf d
if frac ==. 0.0 then
getFn whole
else
Fail "not an integer {renderActualJValue input}".makeQueryError
_ -> Fail "not an integer {renderActualJValue input}".makeQueryError
# A terminal getter for integers. Can be used to access integers
# in json. Doubles that are exact integers as well as true integers
# are both allowed but anything else will return an error. You
# can use this to terminate a chain of $ operations.
#
# Parameters:
# - `input`: The JValue to get the integer of
#
# Examples:
# ```
# query (JInteger 10) $ jInteger = Pass (10, Nil)
# query (JString "foo") $ jInteger = Fail ... # Reports "not an integer"
# queryOne person $ jField "age" $ jInteger = Pass 28
# queryOne person $ jField "name" $ jInteger = Fail (Error ".name: not an integer" Nil)
# ```
export def jInteger: (input: JValue) => Result (List Integer) QueryError =
jIntegerFn qSinglePass
# An editor for integers in json. Can be used to modify an
# integer in a JValue in place. Prefer using jEditInteger
# when possible.
#
# Parameters:
# - `editFn`: The function that modifies the assumed integer
# - `input`: The JValue to have its integer modified
#
# Examples:
# ```
# def birthday = match _
# x if x > 130 = failWithError "No one is {str x} years old!"
# x if x < 0 = failWithError "{str x} is not a valid age"
# x = Pass (x + 1)
# query (JInteger 10) $ jEditIntegerFn $ birthday = Pass (JInteger 11)
# query (JString "foo") $ jEditIntegerFn $ birthday = Fail (Error ".: not an integer" Nil)
# query person $ jEditField "age" $ jEditIntegerFn $ birthday
# query person $ jEditField "name" $ jEditIntegerFn $ birthday = Fail (Error "./name: not an integer" Nil)
# ```
export def jEditIntegerFn (editFn: Integer => Result Integer QueryError) (input: JValue): Result JValue QueryError =
match input
JInteger i ->
require Pass ei = editFn i
Pass (JInteger ei)
JDouble d ->
def Pair whole frac = dmodf d
if frac ==. 0.0 then
require Pass ei = editFn whole
Pass (JInteger ei)
else
Fail "not an integer {renderActualJValue input}".makeQueryError
_ -> Fail "not an integer {renderActualJValue input}".makeQueryError
# An editor for integers in json. Can be used to modify an
# integer in a JValue in place.
#
# Parameters:
# - `editFn`: The function that modifies the assumed integer
# - `input`: The JValue to have its integer modified
#
# Examples:
# ```
# query (JInteger 10) $ jEditInteger (_+1) = Pass (JString 11)
# query (JString "foo") $ jEditInteger (_+1) = Fail (Error ".: not an integer" Nil)
# query person $ jEditField "age" $ jEditInteger (_+1) # Birthday
# query person $ jEditField "name" $ jEditInteger (_+1) = Fail (Error ".name: not an integer" Nil)
# ```
export def jEditInteger (editFn: Integer => Integer): (input: JValue) => Result JValue QueryError =
jEditIntegerFn
$ qPass editFn
# A getter for JBoolean. Can be used to access booleans in json.
# If possible for your case consider using jBoolean instead.
#
# Parameters:
# - `getFn`: The function that consumes the boolean if present
# - `input`: The JValue to get the boolean of
#
# Examples:
# ```
# def onlyTrue = match _
# True = Pass True
# False = failWithError "only true is valid"
# query (JBoolean True) $ jBooleanFn onlyTrue = Pass True
# query (JInteger 10) $ jBooleanFn onlyTrue = Fail (Error ".: not a boolean" Nil)
# query order $ jField "frysWithThat" $ jBooleanFn $ onlyTrue = Pass True
# query person $ jField "name" $ jBooleanFn $ onlyTrue = Fail (Error "./name: not a boolean" Nil)
# ```
export def jBooleanFn (getFn: Boolean => Result a QueryError) (input: JValue): Result a QueryError =
match input
JBoolean b -> getFn b
_ -> Fail "not a boolean {renderActualJValue input}".makeQueryError
# A terminal getter for JBoolean. Can be used to access booleans
# in json. You can use this to terminate a chain of $ operations.
#
# Parameters:
# - `input`: The JValue to get the boolean of
#
# Examples:
# ```
# query (JBoolean True) $ jBoolean = Pass (True, Nil)
# query (JInteger 10) $ jBoolean = Fail (Error ".: not a boolean" Nil)
# queryOne order $ jField "frysWithThat" $ jBoolean = Pass True
# queryOne person $ jField "name" $ jBoolean = Fail (Error ".name: not a boolean" Nil)
# ```
export def jBoolean: (input: JValue) => Result (List Boolean) QueryError =
jBooleanFn qSinglePass
# An editor for JBoolean. Can be used to modify a
# boolean of a JValue in place. Prefer using jEditBoolean
# when possible
#
# Parameters:
# - `editFn`: The function that modifies the boolean if present
# - `input`: The JValue to have its boolean modified
#
# Examples:
# ```
# def onlyTrue = match _
# True = Pass True
# False = failWithError "only true is valid"
# query (JBoolean True) $ jEditBooleanFn $ onlyTrue = Pass (JBoolean True)
# query (JString "foo") $ jEditBooleanFn $ onlyTrue = Fail (Error ".: not a boolean" Nil)
# query order $ jEditField "frysWithThat" $ jEditBooleanFn $ onlyTrue # Frys are a must
# query person $ jEditField "name" $ jEditBooleanFn $ onlyTrue # Reports "./name: not a boolean"
# ```
export def jEditBooleanFn (editFn: Boolean => Result Boolean QueryError) (input: JValue): Result JValue QueryError =
match input
JBoolean b ->
require Pass eb = editFn b
Pass (JBoolean eb)
_ -> Fail "not a boolean {renderActualJValue input}".makeQueryError
# An editor for JBoolean. Can be used to modify a
# boolean of a JValue in place.
#
# Parameters:
# - `editFn`: The function that modifies the boolean if present
# - `input`: The JValue to have its boolean modified
#
# Examples:
# ```
# query (JBoolean True) $ jEditBoolean (!_) = Pass (JBoolean False)
# query (JString "foo") $ jEditBoolean (!_) = Fail (Error ".: not a boolean" Nil)
# query order $ jEditField "frysWithThat" $ jEditBoolean (\_ True) # I for sure want frys
# query person $ jEditField "name" $ jEditBoolean (!_) # Reports ".name: not a boolean"
# ```
export def jEditBoolean (editFn: Boolean => Boolean): (input: JValue) => Result JValue QueryError =
jEditBooleanFn
$ qPass editFn
# A getter for numbers. Can be used to access numbers in json.
# If possible for your case consider using jDouble instead.
#
# Parameters:
# - `getFn`: The function that consumes the number if present
# - `input`: The JValue to get the number of
#
# Examples:
# ```
# def jProb = match _
# x if x >. 1.0 = failWithError "probabilities should be <= to 1"
# x if x <. 0.0 = failWithError "probabilities should be >= to 0"
# x = Pass x
# query (JDouble 0.9) $ jDoubleFn jProb = Pass (0.9, Nil)
# query (JInteger 1) $ jDoubleFn jProb = Pass 1e0
# query (JString "foo") $ jDoubleFn jProb = Fail (Error ".: not a number" Nil)
# query constants $ jField "piOver4" $ jDoubleFn jProb = Pass 0.78539816339744828
# query person $ jField "name" $ jDoubleFn jProb = Fail (Error "./name: not a number" Nil)
# ```
export def jDoubleFn (getFn: Double => Result a QueryError) (input: JValue): Result a QueryError =
match input
JDouble d -> getFn d
JInteger i -> getFn (dint i)
_ -> Fail "not a number {renderActualJValue input}".makeQueryError
# A terminal getter for numbers. Can be used to access numbers
# in json. You can use this to terminate a chain of $ operations.
#
# Parameters:
# - `input`: The JValue to get the number of
#
# Examples:
# ```
# query (JDouble 10.0) $ jDouble = Pass (10e0, Nil)
# query (JInteger 10) $ jDouble = Pass (10e0, Nil)
# query (JBoolean True) $ jDouble = Fail (Error ".: not a number" Nil)
# queryOne constants $ jField "pi" $ jDouble = Pass 3.1415926535897931
# queryOne person $ jField "name" $ jDouble = Fail (Error ".name: not a number" Nil)
# ```
export def jDouble: (input: JValue) => Result (List Double) QueryError =
jDoubleFn qSinglePass
# An editor for numbers in json. Can be used to modify a
# number in a JValue in place. Prefer using jEditDouble
#
# Parameters:
# - `editFn`: The function that modifies the number if present
# - `input`: The JValue to have its number modified
#
# Examples:
# ```
# # Error if a double is not a probability
# def prob = match _
# p if p >. 1.0 = failWithError "probabilities should be <= to 1"
# p if p <. 0.0 = failWithError "probabilities should be >= to 0"
# _ = Pass Unit
# # Update belief after taking a covid test
# def bayes specificity sensitivity prior =
# require Pass _ = prob specificity
# require Pass _ = prob sensitivity
# require Pass _ = prob prior
# def marginal = sensitivity *. prior +. (1.0 -. specificity) *. (1.0 -. prior)
# sensitivity *. prior /. marginal | Pass
# query (JDouble 0.2) $ jEditDoubleFn $ bayes 0.98 0.86 = Pass (JDouble 0.91489361702127658)
# query (JInteger 1) $ jEditDoubleFn $ bayes 0.98 0.86 = Pass (JDouble 1e0)
# query (JDouble 3.14) $ jEditDoubleFn $ bayes 0.98 0.86 = Fail (Error ".: probabilities should be <= to 1" Nil)
# query (JString "foo") $ jEditDoubleFn $ bayes 0.98 0.86 = Fail (Error ".: not a number" Nil)
# ```
export def jEditDoubleFn (editFn: Double => Result Double QueryError) (input: JValue): Result JValue QueryError =
match input
JDouble d ->
require Pass ed = editFn d
Pass (JDouble ed)
JInteger i ->
require Pass ed = editFn (dint i)
Pass (JDouble ed)
_ -> Fail "not a number {renderActualJValue input}".makeQueryError
# An editor for numbers in json. Can be used to modify a
# number in a JValue in place. Prefer using jEditDouble
#
# Parameters:
# - `editFn`: The function that modifies the number if present
# - `input`: The JValue to have its number modified
#
# Examples:
# ```
# query (JDouble 10.0) $ jEditDouble (_+.1.0) = Pass (JDouble 11e0)
# query (JInteger 10) $ jEditDouble (_+.1.0) = Pass (JDouble 11e0)
# query (JString "foo") $ jEditDouble (_+.1.0) = Fail (Error ".: not a number" Nil)
# query person $ jEditField "height" $ jEditDouble (_+.1.5) # Grow a little bit
# query person $ jEditField "name" $ jEditDouble (_+.1.5) # Reports "not a number"
# ```
export def jEditDouble (editFn: Double => Double): (input: JValue) => Result JValue QueryError =
jEditDoubleFn
$ qPass editFn
# Pass the data through unchanged, but add a breadcrumb to any error message corresponding to
# the `getFn` being run on the contents of the indicated `index` in some array.
#
# This is handled automatically by library functions, but can be useful to call manually if a query
# has been split into two parts, so that any context from the first part would be lost when running
# the second.
#
# Example:
# ```
# require Pass json =
# parseJSONBody '[{"foo": "bar"}, {"foo": 3}]'
# require Pass foo =
# query json
# $ jAt 0
# $ Pass
# require Pass _baseline = # Fails with "./foo: not an integer (got \"bar\")"
# query foo
# $ jField "foo"
# $ jInteger
# require Pass _annotated = # Fails with ".[0]/foo: not an integer (got \"bar\")"
# query foo
# $ jArrayAnnotation 0
# $ jField "foo"
# $ jInteger
# ```
export def jArrayAnnotation (index: Integer) (getFn: a => Result b QueryError) (input: a): Result b QueryError =
qPathAnnotation (PathIndex index) getFn input
# A getter for JArray. Can be used to access an array in json.
# Consider using jFlatten instead.
#
# Parameters:
# - `getFn`: The function that consumes the array if present
# - `input`: The JValue to get the array of
#
# Examples:
# ```
# def getSingleton = match _
# x, Nil = Pass x
# _, _ = failWithError "too many elements"
# _ = failWithError "too few elements"
#
# query (JArray (x, Nil)) $ jArrayFn $ getSingleton = Pass x
# query (JInteger 10) $ jArrayFn $ getSingleton = Fail (Error ".: not an array" Nil)
# ```
export def jArrayFn (getFn: List JValue => Result a QueryError) (input: JValue): Result a QueryError =
match input
JArray arr -> getFn arr
_ -> Fail "not an array {renderActualJValue input}".makeQueryError
# An editor for JArray. Can be used to modify an array
# inside a JValue in place. Consider using jEditFlatten
# or jEditPrepend instead.
#
# Parameters:
# - `editFn`: The function that modifies the array if present
# - `input`: The JValue to have its array modified
#
# Examples:
# ```
# def removeFirst = match _
# Nil = failWithError "cannot remove first element of empty list"
# x, xs = Pass xs
#
# query names $ jEditArray $ removeFirst # Remove the first name from a list of names
# query (JString "foo") $ jEditArray $ removeFirst = Fail (Error ".: not an array" Nil)
# ```
export def jEditArray (editFn: List JValue => Result (List JValue) QueryError) (input: JValue): Result JValue QueryError =
match input
JArray arr ->
require Pass earr = editFn arr
Pass (JArray earr)
_ -> Fail "not an array {renderActualJValue input}".makeQueryError
# A getter that pulls out all the values of a list. You
# can think of this like `[]` in jq.
#
# Parameters:
# - `getFn`: The function that consumes each element of an array
# - `input`: The JValue containing an array to apply `getFn` to
#
# Examples:
# ```
# query names $ jFlatten jString = Pass ("Alice", "Bob", "Carol", Nil)
# query (JInteger 10) $ jFlatten jString = Fail (Error ".: not an array" Nil)
# queryOne person $ jField "friends" $ jFlatten $ jField "name" $ jString = Pass ("Alice", "Bob", "Carol", Nil)
# queryOne person $ jField "name" $ jFlatten $ jString = Fail (Error "./name: not an array" Nil)
# ```
export def jFlatten (getFn: JValue => Result (List a) QueryError) (input: JValue): Result (List a) QueryError =
match input
JArray list -> qFlatten getFn list
_ -> Fail "not an array {renderActualJValue input}".makeQueryError
# An editor for JValue that edits each element of a
# JArray.
#
# Parameters:
# - `editFn`: The function that modifies each element of an array
# - `input`: The JValue to have its array modified
#
# Examples:
# ```
# jEditFlatten (JArray fields) $ jEditInteger (_+1) # Add 1 to every integer in an array
# jEditFlatten (JString "foo") $ jEditInteger (_+1) = Fail ... # Reports "not an array"
# ```
export def jEditFlatten (editFn: JValue => Result JValue QueryError) (input: JValue): Result JValue QueryError =
match input
JArray list ->
require Pass newList = qEditFlatten editFn list
Pass (JArray newList)
_ -> Fail "not an array {renderActualJValue input}".makeQueryError
# A terminal editor for JValue that adds an element to the
# front of a JArray. You can use this at the end of a '$'
# chain
#
# Parameters:
# - `input`: The JArray to add an element to
#
# Examples:
# ```
# query (JArray names) $ jEditPrepend (JString "Bob") = Pass (JArray (JString "Bob", names))
# query (JString "foo") $ jEditPrepend (JString "Alice") = Fail (Error ".: not an array" Nil)
# ```
export def jEditPrepend (toAdd: JValue) (input: JValue): Result JValue QueryError = match input
JArray arr ->
JArray (toAdd, arr)
| Pass
_ -> Fail "not an array {renderActualJValue input}".makeQueryError
# A getter that pulls out one specific value of
# a JArray from a specific index.
#
# Parameters:
# - `index`: The index of the specific element you want to access
# - `getFn`: The function that consumes the specific element
# - `input`: The JValue containing an array to fetch a value from
#
# Examples:
# ```
# queryOne employees $ jAt 0 $ jField "name" $ jString = Pass "Alice"
# queryOne employees $ jAt 1000000 $ jField "name" $ jString = Fail (Error ".[1000000]: out of bounds" Nil)
# queryOne person $ jAt 0 $ jField "name" $ jString = Fail (Error ".not an array" Nil)
# ```
export def jAt (index: Integer) (getFn: JValue => Result a QueryError) (input: JValue): Result a QueryError =
match input
JArray arr -> qAt index getFn arr
_ -> Fail "not an array {renderActualJValue input}".makeQueryError
# An editor that edits one specific value of
# a JArray at a specific index.
#
# Parameters:
# - `index`: The index of the specific element you want to access
# - `editFn`: The function that edits the specific element
# - `input`: The JValue containing an array to edit a single value in
#
# Examples:
# ```
# query names $ jEditAt 1 $ jEditString ("_ {lastName}") # Add lastName to name mat index 1
# query (JArray (x, y, z, Nil)) $ jEditAt 7 $ fn = Fail (Error ".[7]: out of bounds" Nil)
# query (JString "foo") $ jEditAt 3 $ fn = Fail (Error ".: not an array" Nil)
# ```
export def jEditAt (index: Integer) (editFn: JValue => Result JValue QueryError) (input: JValue): Result JValue QueryError =
match input
JArray arr ->
require Pass earr = qEditAt index editFn arr
Pass (JArray earr)
_ -> Fail "not an array {renderActualJValue input}".makeQueryError
# A getter that allows the query to succeed even if the current value is a JSON `null`.
#
# You very likely don't want to use this with `queryOne`.
#
# Parameters:
# - `getFn`: The function that consumes the value
# - `input`: The JValue containing the value to attempt to fetch from
#
# Examples:
# ```
# queryOptional person $ jField "name" $ jNullable jString = Some "Jane Marple"
# queryOptional person $ jField "spouse" $ jNullable jString = None # Won't fail even though not everyone is married
# ```
export def jNullable (getFn: JValue => Result (List a) QueryError): (input: JValue) => Result (List a) QueryError =
match _
JNull -> Pass Nil
input -> getFn input
# An editor that allows the query to succeed even if the current value is a JSON `null`.
#
# Parameters:
# - `editFn`: The function that edits the value
# - `input`: The JValue to edit a single value in
#
# Examples:
# ```
# def makeSortable name = match (extract `(.*) (.*)` name)
# first, last, Nil = Pass "{last}, {first}"
# singleName = Pass singleName
# query person $ jEditField "name" $ jEditNullable (jEditStringFn makeSortable) # Results in something like "Marple, Jane"
# query person $ jEditField "spouse" $ jEditNullable (jEditStringFn makeSortable) # Won't fail even though not everyone is married
# ```
export def jEditNullable (editFn: JValue => Result JValue QueryError): (input: JValue) => Result JValue QueryError =
match _
JNull -> Pass JNull
input -> editFn input
# Pass the data through unchanged, but add a breadcrumb to any error message corresponding to
# the `getFn` being run on the contents of the indicated `field` in some object.
#
# This is handled automatically by library functions, but can be useful to call manually if a query
# has been split into two parts, so that any context from the first part would be lost when running
# the second.
#
# Example:
# ```
# require Pass json =
# parseJSONBody '{"foo": ["bar", 3]}'
# require Pass foo =
# query json
# $ jField "foo"
# $ Pass
# require Pass _baseline = # Fails with ".[0]: not an integer (got \"bar\")"
# query foo
# $ jFlatten
# $ jInteger
# require Pass _annotated = # Fails with "./foo[0]: not an integer (got \"bar\")"
# query foo
# $ jFieldAnnotation "foo"
# $ jFlatten
# $ jInteger
# ```
export def jFieldAnnotation (field: String) (getFn: a => Result b QueryError) (input: a): Result b QueryError =
qPathAnnotation (PathSlash field) getFn input
# A getter that pulls out one specific field of
# a JObject. An error will be produced if the field
# does not exist or if there are multiple copies of
# that field.
#
# Parameters:
# - `field`: The name of the field to access
# - `getFn`: The function that consumes the value of the field
# - `input`: The JValue containing an object to access the field of
#
# Examples:
# ```
# query (JObject ("foo" :-> x, Nil)) $ jField "foo" Pass = Pass x
# query (JObject ("baz" :-> x, Nil)) $ jField "bar" Pass = Fail (Error ".bar: key not found" Nil)
# query (JObject ("foo" :-> x, "foo" :-> y, Nil)) $ jField "foo" Pass = Fail ... # Reports "too many "foo" instances
# queryOne person $ jField "name" $ jString = Pass "Alice" # Typical use
# ```
export def jField (field: String) (getFn: JValue => Result a QueryError) (input: JValue): Result a QueryError =
def helper = match _
JObject pairs -> match (lookup (_ ==* field) pairs)
Nil -> Fail "key {format field} not found".makeQueryError
x, Nil -> jFieldAnnotation field getFn x
_ -> Fail "key {format field} found multiple times, expected only once".makeQueryError
_ -> Fail "not an object {renderActualJValue input}".makeQueryError
helper input
# A getter that pulls out all fields of a JObject.
# An error will be produced if no field matches
# the given predicate.
#
# Parameters:
# - `predicate`: The function that is used to compare against the field
# - `getFn`: The function that consumes the value of the field
# - `input`: The JValue containing an object to access the field of
#
# Examples:
# ```
# def comparator a b = a ==* b
# queryOne (JObject ("foo" :-> x, Nil)) $ jFieldPred "foo".comparator Pass = Pass x
# ```
export def jFieldPred (predicate: String => Boolean) (getFn: JValue => Result (List a) QueryError) (input: JValue): Result (List a) QueryError =
doJFieldPred predicate "predicate" getFn input
def doJFieldPred (predicate: String => Boolean) (name: String) (getFn: JValue => Result (List a) QueryError) (input: JValue): Result (List a) QueryError =
match input
JObject pairs ->
def selected = filter (predicate _.getPairFirst) pairs
match selected
Nil -> Fail "no keys matching {name} found".makeQueryError
_ ->
def getPairSecondFindFail (Pair field y) =
require Pass ey = jFieldAnnotation field getFn y
Pass ey
map getPairSecondFindFail selected
| findFail
| rmap flatten
_ -> Fail "not an object {renderActualJValue input}".makeQueryError
# A getter that pulls out one specific field of
# a JObject. An error is returned if there are multiple
# copies of that field. If the field is not present
# the supplied default value is used instead, wrapped as a
# single-element list.
#
# Parameters:
# - `field`: The name of the field to access
# - `default`: The default value to use if the field is missing
# - `getFn`: The function that consumes the value of the field
# - `input`: The JValue containing an object to access the field of
#
# Examples:
# ```
# queryOne (JObject ("foo" :-> JInteger 10, Nil)) $ jFieldDefault "foo" 10 $ jInteger = Pass 10
# queryOne (JObject Nil) $ jFieldDefault "foo" 10 $ jInteger = Pass 10
# queryOne (JObject ("foo" :-> JNull, "foo" :-> JNull, Nil)) $ jFieldDefault "foo" 10 $ jInteger = Fail ... # Reports "too many "foo" instances
# queryOne order $ jFieldDefault "frysWithThat" False $ jBoolean = Pass False # Typical use
# ```
export def jFieldDefault (field: String) (default: a) (getFn: JValue => Result (List a) QueryError) (input: JValue): Result (List a) QueryError =
jFieldDefaultMany field (default, Nil) getFn input
# A getter that pulls out one specific field of
# a JObject. An error is returned if there are multiple
# copies of that field. If the field is not present
# the supplied default value is used instead, allowing
# for queries which might return multiple values.
#
# Parameters:
# - `field`: The name of the field to access
# - `default`: The default value to use if the field is missing
# - `getFn`: The function that consumes the value of the field
# - `input`: The JValue containing an object to access the field of
#
# Examples:
# ```
# queryOne (JObject ("foo" :-> JArray (JInteger 10, Nil), Nil)) $ jFieldDefaultMany "foo" (10, Nil) $ jInteger = Pass (10, Nil)
# queryOne (JObject Nil) $ jFieldDefault "foo" (10, Nil) $ jInteger = Pass (10, Nil)
# queryOne (JObject ("foo" :-> JNull, "foo" :-> JNull, Nil)) $ jFieldDefault "foo" (10, Nil) $ jInteger = Fail ... # Reports "too many "foo" instances
# ```
export def jFieldDefaultMany (field: String) (default: a) (getFn: JValue => Result a QueryError) (input: JValue): Result a QueryError =
match input
JObject pairs -> match (lookup (_ ==* field) pairs)
Nil -> Pass default
x, Nil -> jFieldAnnotation field getFn x
_ -> Fail "key {format field} found multiple times, expected only once".makeQueryError
_ -> Fail "not an object {renderActualJValue input}".makeQueryError
# A getter that pulls out one specific field of
# a JObject if present. An error will be produced
# if there are multiple copies of that field.
# Prefer using `jField` for non-optional fields.
#
# Parameters:
# - `field`: The name of the field to access
# - `getFn`: The function that consumes the value of the field
# - `input`: The JValue containing an object to access the field of
#
# Examples:
# ```
# query person $ jFieldOpt "name" $ jString = Pass ("Alice", Nil)
# query flow $ jFieldOpt "Name" $ jString = Pass Nil # 'Name' is optional
# ```
export def jFieldOpt (field: String) (getFn: JValue => Result (List a) QueryError) (input: JValue): Result (List a) QueryError =
jFieldDefaultMany field Nil getFn input
# An editor that edits all fields of a JObject that
# match a field name. If the field is missing an
# error will be returned.
#
# Parameters:
# - `field`: The name of the fields to edit
# - `editFn`: The function that edits the value of the field
# - `input`: The JValue to have its fields edited
#
# Examples:
# ```
# query person $ jEditField "name" $ jEditString ("{_} {lastName}") # Add a last name to a person's name
# query person $ jEditField "age" $ jEditInteger (_+1) # Have a birthday
# query order $ jEditField "frysWithThat" $ jEditBoolean (\_ True) # Make sure we order frys
# query person $ jEditField "frysWithThat" $ jEditBoolean (\_ True) # Reports ".frysWithThat: key not found"
# ```
export def jEditField (field: String) (editFn: JValue => Result JValue QueryError) (input: JValue): Result JValue QueryError =
require JObject obj = input
else Fail "not an object {renderActualJValue input}".makeQueryError
# Loop over each entry to see if a match has been found
def loop (matchFound: Boolean) = match _
# If we find a match, edit it, and set matchFound to True
(Pair key value, rest) ->
# Check if the key is set
require True = field ==* key
else
require Pass restResult = loop matchFound rest
Pass (Pair key value, restResult)
# Now make matchFound=True for rest
def restResult = loop True rest
# Edit the value
require Pass result = jFieldAnnotation field editFn value
require Pass editedRest = restResult
Pass (Pair key result, editedRest)
Nil ->
if matchFound then
Pass Nil
else
Fail "key {format field} not found".makeQueryError
require Pass eobj = loop False obj
JObject eobj
| Pass
# An editor that edits the field of a JObject that
# matches a field name. If the field is missing it is
# added to the JObject. If the field is duplicated in
# the JObject then a failure will be reported.
#
# Parameters:
# - `field`: The name of the fields to edit or set
# - `value`: The value that is assigned to the matching field
# - `input`: The JValue to have its fields edited
#
# Examples:
# ```
# query person $ jEditSetField "name" $ JString "John Doe" # Set a person's name
# query person $ jEditSetField "newField" $ JInteger 10 # Add newField to a person
# query duplicateName $ jEditSetField "name" $ JString "New Name" # Reports "./name: duplicate key in JObject"
# ```
export def jEditSetField (field: String) (value: JValue) (input: JValue): Result JValue QueryError =
require JObject obj = input
else Fail "not an object {renderActualJValue input}".makeQueryError
# Loop over each entry to see if a match has been found
def loop (matchFound: Boolean) = match _
# If we find a match, edit it, and set matchFound to True
(Pair key v, rest) ->
# Check if the key is set
require True = field ==* key
else
require Pass restResult = loop matchFound rest
Pass (Pair key v, restResult)
# We should only find one matching key. If this is True then
# there is a duplicate key in the JObject.
require False = matchFound
else Fail "key {format field} found multiple times, expected only once".makeQueryError
# Now make matchFound=True for rest
def restResult = loop True rest
require Pass editedRest = restResult
Pass (Pair key value, editedRest)
Nil ->
if matchFound then
Pass Nil
else
# If we have fully explored this list and we haven't
# found a match, then insert it.
Pass (Pair field value, Nil)
require Pass eobj = loop False obj
JObject eobj
| Pass
# Checks if two JValues are equal. Prefer using `==/`
# unless you're in a setting where a Result List is
# needed. The intended use case is in '$' as a
# terminal getter that returns true/false based on
# if a particular value is equal to the supplied
# value.
#
# Parameters:
# - `value`: The value to check the input against
# - `input`: The input to the getter that will be checked against `value`
#
# Examples:
# ```
# jEq (JString "foo") (JString "foo") = Pass (True, Nil)
# jEq (JInteger 10) (JString "foo") = Pass (False, Nil)
# query objects $ qIf (queryOne _ $ jField "type" $ jEq "foo".JString) # get all JValues with .type == "foo"
# ```
export def jEq (value: JValue): (input: JValue) => Result (List Boolean) QueryError =
_ ==/ value
| single
| Pass
# A terminal getter to check if a JValue is equal to a
# an integer. The intended use case is in a '$' chain
# rather than for strict equality checking. This is
# still good to use if you really want to make sure that
# the type you're checking against is an integer, but
# otherwise prefer `==/`. This will return an error
# if the JValue being checked against is not an integer.
#
# Parameters:
# - `value`: The integer to check the input against
# - `input`: The input to the getter that will be checked against `value`
#
# Examples:
# ```
# queryOne (JInteger 10) $ jEqInt 10 = Pass True
# queryOne (JDouble 10.0) $ jEqInt 10 = Pass True
# queryOne (JInteger 10) $ jEqInt 11 = Pass False
# queryOne (JDouble 10.1) $ jEqInt 10 = Fail (Error ".not an integer" Nil)
# queryOne (JString "foo") $ jEqInt 10 = Fail (Error ".not an integer" Nil)
# query objects $ qIf (queryOne _ $ jField "type" $ jEqInt 1) # get all JValues with .type == 1
# ```
export def jEqInt (value: Integer) (input: JValue): Result (List Boolean) QueryError = match input
JInteger i ->
i == value
| single
| Pass
JDouble d ->
def Pair whole frac = dmodf d
if frac ==. 0.0 then
Pass (single (whole == value))
else
Fail "not an integer {renderActualJValue input}".makeQueryError
_ -> Fail "not an integer {renderActualJValue input}".makeQueryError
# A terminal getter to check if a JValue is equal to a
# a string. The intended use case is in a '$' chain
# rather than for strict equality checking. This is
# still good to use if you really want to make sure that
# the type you're checking against is a string but
# otherwise prefer `==/`. This will return an error
# if the JValue being checked against is not a JString.
#
# Parameters:
# - `value`: The string to check the input against
# - `input`: The input to the getter that will be checked against `value`
#