/
search_param_types.go
1116 lines (993 loc) · 34.8 KB
/
search_param_types.go
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 search
import (
"fmt"
"math/big"
"net/url"
"regexp"
"strconv"
"strings"
"time"
)
// Constant values for search paramaters and search result parameters
const (
IDParam = "_id"
LastUpdatedParam = "_lastUpdated"
TagParam = "_tag"
ProfileParam = "_profile"
SecurityParam = "_security"
TextParam = "_text"
ContentParam = "_content"
ListParam = "_list"
QueryParam = "_query"
SortParam = "_sort"
CountParam = "_count"
IncludeParam = "_include"
RevIncludeParam = "_revinclude"
SummaryParam = "_summary"
ElementsParam = "_elements"
ContainedParam = "_contained"
ContainedTypeParam = "_containedType"
OffsetParam = "_offset" // Custom param, not in FHIR spec
FormatParam = "_format"
)
var globalSearchParams = map[string]bool{IDParam: true, LastUpdatedParam: true, TagParam: true,
ProfileParam: true, SecurityParam: true, TextParam: true, ContentParam: true, ListParam: true,
QueryParam: true}
func isGlobalSearchParam(param string) bool {
_, found := globalSearchParams[param]
return found
}
var searchResultParams = map[string]bool{SortParam: true, CountParam: true, IncludeParam: true,
RevIncludeParam: true, SummaryParam: true, ElementsParam: true, ContainedParam: true,
ContainedTypeParam: true, OffsetParam: true, FormatParam: true}
func isSearchResultParam(param string) bool {
_, found := searchResultParams[param]
return found
}
// Query describes a string-based FHIR query and the resource it is associated
// with. For example, the URL http://acme.com/Condition?patient=123&onset=2012
// should be represented as:
// Query { Resource: "Condition", Query: "patient=123&onset=2012" }
type Query struct {
Resource string
Query string
}
// Params parses the query string and returns a slice containing the
// appropriate SearchParam instances. For example, a Query on the "Condition"
// resource with the query string "patient=123&onset=2012" should return a
// slice containing a ReferenceParam (for patient) and a DateParam (for onset).
func (q *Query) Params() []SearchParam {
var results []SearchParam
queryParams, _ := ParseQuery(q.Query)
for _, queryParam := range queryParams.All() {
param, modifier, postfix := ParseParamNameModifierAndPostFix(queryParam.Key)
if isSearchResultParam(param) {
continue
}
info, ok := SearchParameterDictionary[q.Resource][param]
if ok {
info.Postfix = postfix
info.Modifier = modifier
results = append(results, info.CreateSearchParam(queryParam.Value))
} else {
if isGlobalSearchParam(param) {
panic(createUnsupportedSearchError("MSG_PARAM_UNKNOWN", fmt.Sprintf("Parameter \"%s\" not understood", param)))
} else {
panic(createInvalidSearchError("SEARCH_NONE", fmt.Sprintf("Error: no processable search found for %s search parameters \"%s\"", q.Resource, param)))
}
}
}
return results
}
// Options parses the query string and returns the QueryOptions.
func (q *Query) Options() *QueryOptions {
options := NewQueryOptions()
queryParams, _ := ParseQuery(q.Query)
for _, queryParam := range queryParams.All() {
param, modifier, _ := ParseParamNameModifierAndPostFix(queryParam.Key)
if !strings.HasPrefix(param, "_") || isGlobalSearchParam(param) {
continue
}
switch param {
case CountParam:
count, err := strconv.Atoi(queryParam.Value)
if err != nil {
panic(createInvalidSearchError("MSG_PARAM_INVALID", "Parameter \"_count\" content is invalid"))
}
if count >= 0 {
options.Count = count
}
case OffsetParam:
offset, err := strconv.Atoi(queryParam.Value)
if err != nil {
panic(createInvalidSearchError("MSG_PARAM_INVALID", "Parameter \"_offset\" content is invalid"))
}
if offset >= 0 {
options.Offset = offset
}
case SortParam:
// The following supports both DSTU2-style sorts and STU3-style sorts
keys := strings.Split(queryParam.Value, ",")
for _, key := range keys {
desc := strings.HasPrefix(key, "-") || modifier == "desc"
sortParam, ok := SearchParameterDictionary[q.Resource][strings.TrimPrefix(key, "-")]
if !ok {
panic(createInvalidSearchError("MSG_PARAM_INVALID", "Parameter \"_sort\" content is invalid"))
}
options.Sort = append(options.Sort, SortOption{Descending: desc, Parameter: sortParam})
}
// If this was an STU3-style sort, remember that so we reconstruct the query URL correctly
if len(keys) > 1 || strings.HasPrefix(queryParam.Value, "-") {
options.IsSTU3Sort = true
}
case IncludeParam:
incls := strings.Split(queryParam.Value, ":")
if len(incls) < 2 || len(incls) > 3 {
panic(createInvalidSearchError("MSG_PARAM_INVALID", "Parameter \"_include\" content is invalid"))
}
inclParam, ok := SearchParameterDictionary[incls[0]][incls[1]]
if !ok {
panic(createInvalidSearchError("MSG_PARAM_INVALID", "Parameter \"_include\" content is invalid"))
}
// Only reference paramaters count, so verify it is a reference parameter
if inclParam.Type != "reference" {
panic(createInvalidSearchError("MSG_PARAM_INVALID", "Parameter \"_include\" content is invalid"))
}
if len(incls) == 3 {
if isValidTarget(incls[2], inclParam) {
// Modify the targets to include only the one noted
inclParam.Targets = []string{incls[2]}
} else {
panic(createInvalidSearchError("MSG_PARAM_INVALID", "Parameter \"_include\" content is invalid"))
}
}
options.Include = append(options.Include, IncludeOption{Resource: incls[0], Parameter: inclParam})
case RevIncludeParam:
incls := strings.Split(queryParam.Value, ":")
if len(incls) < 2 || len(incls) > 3 {
panic(createInvalidSearchError("MSG_PARAM_INVALID", "Parameter \"_revinclude\" content is invalid"))
}
revInclParam, ok := SearchParameterDictionary[incls[0]][incls[1]]
if !ok {
panic(createInvalidSearchError("MSG_PARAM_INVALID", "Parameter \"_revinclude\" content is invalid"))
}
// Only reference paramaters count, so verify it is a reference parameter
if revInclParam.Type != "reference" {
panic(createInvalidSearchError("MSG_PARAM_INVALID", "Parameter \"_revinclude\" content is invalid"))
}
// Only the currently searched on resource is a valid target (or "Any")
target := q.Resource
if len(incls) == 3 && incls[2] != target && incls[2] != "Any" {
panic(createInvalidSearchError("MSG_PARAM_INVALID", "Parameter \"_revinclude\" content is invalid"))
}
// Make sure the selected param actually supports the intended target
if isValidTarget(target, revInclParam) {
// Modify the targets to include only the resource we're searching on
revInclParam.Targets = []string{target}
} else {
panic(createInvalidSearchError("MSG_PARAM_INVALID", "Parameter \"_revinclude\" content is invalid"))
}
options.RevInclude = append(options.RevInclude, RevIncludeOption{Resource: incls[0], Parameter: revInclParam})
case FormatParam:
if queryParam.Value != "json" && queryParam.Value != "application/json" && queryParam.Value != "application/json+fhir" {
// Currently we only support JSON
panic(createUnsupportedSearchError("MSG_PARAM_INVALID", "Parameter \"_format\" content is invalid"))
}
default:
panic(createUnsupportedSearchError("MSG_PARAM_UNKNOWN", fmt.Sprintf("Parameter \"%s\" not understood", param)))
}
}
return options
}
func getSingletonParamValue(param string, values []string) string {
if len(values) != 1 {
panic(createInvalidSearchError("MSG_PARAM_NO_REPEAT", fmt.Sprintf("Parameter \"%s\" is not allowed to repeat", param)))
}
return values[0]
}
func isValidTarget(target string, param SearchParamInfo) bool {
for _, pTarget := range param.Targets {
if pTarget == target || pTarget == "Any" {
return true
}
}
return false
}
// URLQueryParameters reconstructs the URL-encoded query based on parsed
// parameters. This ensures better uniformity/consistency and also removes any
// garbage parameters or bad formatting in the passed in parameters. If
// withOptions is specified, the query options will also be included in the
// URLQueryParameters.
func (q *Query) URLQueryParameters(withOptions bool) URLQueryParameters {
var queryParams URLQueryParameters
for _, param := range q.Params() {
k, v := param.getQueryParamAndValue()
queryParams.Add(k, v)
}
if withOptions {
oQueryParams := q.Options().URLQueryParameters()
for _, oQueryParam := range oQueryParams.All() {
queryParams.Add(oQueryParam.Key, oQueryParam.Value)
}
}
return queryParams
}
// QueryOptions contains option values such as count and offset.
type QueryOptions struct {
Count int
Offset int
Sort []SortOption
Include []IncludeOption
RevInclude []RevIncludeOption
IsSTU3Sort bool
}
// NewQueryOptions constructs a new QueryOptions with default values (offset = 0, Count = 100)
func NewQueryOptions() *QueryOptions {
return &QueryOptions{Offset: 0, Count: 100}
}
// URLQueryParameters returns URLQueryParameters representing the query options.
func (o *QueryOptions) URLQueryParameters() URLQueryParameters {
var queryParams URLQueryParameters
if o.IsSTU3Sort {
keys := make([]string, len(o.Sort))
for i := range o.Sort {
if o.Sort[i].Descending {
keys[i] = fmt.Sprintf("-%s", o.Sort[i].Parameter.Name)
} else {
keys[i] = o.Sort[i].Parameter.Name
}
}
queryParams.Add(SortParam, strings.Join(keys, ","))
} else {
for _, sort := range o.Sort {
sortParamKey := SortParam
if sort.Descending {
sortParamKey += ":desc"
}
queryParams.Add(sortParamKey, sort.Parameter.Name)
}
}
queryParams.Set(OffsetParam, strconv.Itoa(o.Offset))
queryParams.Set(CountParam, strconv.Itoa(o.Count))
for _, incl := range o.Include {
queryParams.Add(IncludeParam, fmt.Sprintf("%s:%s", incl.Resource, incl.Parameter.Name))
}
for _, incl := range o.RevInclude {
queryParams.Add(RevIncludeParam, fmt.Sprintf("%s:%s", incl.Resource, incl.Parameter.Name))
}
return queryParams
}
// IncludeOption describes the data that should be included in query results
type IncludeOption struct {
Resource string
Parameter SearchParamInfo
}
// RevIncludeOption describes the data that should be included in query results
type RevIncludeOption struct {
Resource string
Parameter SearchParamInfo
}
// SortOption indicates what parameter to sort on and the sort order
type SortOption struct {
Descending bool
Parameter SearchParamInfo
}
// SearchParam is an interface for all search parameter classes that exposes
// the SearchParamInfo.
type SearchParam interface {
getInfo() SearchParamInfo
getQueryParamAndValue() (string, string)
}
// SearchParamData represents the data associated to an instance of a search param
type SearchParamData struct {
Modifier string
Chain string
Prefix Prefix
Value string
}
// SearchParamInfo contains information about a FHIR search parameter,
// including its name, type, and paths or composites.
type SearchParamInfo struct {
Resource string
Name string
Type string
Paths []SearchParamPath
Composites []string
Targets []string
Prefix Prefix
Postfix string
Modifier string
}
// CreateSearchParam converts a singular string query value (e.g. "2012") into
// a SearchParam object corresponding to the SearchParamInfo.
func (s SearchParamInfo) CreateSearchParam(paramStr string) SearchParam {
if ors := escapeFriendlySplit(paramStr, ','); len(ors) > 1 {
return ParseOrParam(ors, s)
}
switch s.Type {
case "composite":
return ParseCompositeParam(paramStr, s)
case "date":
return ParseDateParam(paramStr, s)
case "number":
return ParseNumberParam(paramStr, s)
case "quantity":
return ParseQuantityParam(paramStr, s)
case "reference":
return ParseReferenceParam(paramStr, s)
case "string":
return ParseStringParam(paramStr, s)
case "token":
return ParseTokenParam(paramStr, s)
case "uri":
return ParseURIParam(paramStr, s)
default:
// Check for a custom search parameter
if parser, err := GlobalRegistry().LookupParameterParser(s.Type); err == nil {
data := SearchParamData{
Modifier: s.Modifier,
Chain: s.Postfix,
Prefix: s.Prefix,
Value: paramStr,
}
param, err := parser(s, data)
if err != nil {
panic(createInternalServerError("MSG_PARAM_INVALID", fmt.Sprintf("Parameter \"%s\" content is invalid", s.Name)))
}
return param
}
}
return nil
}
// SearchParamPath indicates a dot-separated path to the property that should
// be searched, as well as the FHIR type of that property (e.g., "dateTime").
// The path indicates elements that are arrays by prefixing the element name
// with "[]" (e.g., "order.[]item.name"). In the rare case that the search
// path has an indexer, it will be in the brackets (e.g.,"[0]item.entry")
type SearchParamPath struct {
Path string
Type string
}
// CompositeParam represents a composite-flavored search parameter. The
// following description is from the FHIR DSTU2 specification:
//
// A resource may also specify composite parameters that take
// sequences of single values that match other defined parameters as an
// argument. The matching parameter of each component in such a sequence is
// documented in the definition of the parameter. These sequences are formed by
// joining the single values with a "$". Note that this sequence is a single
// value and itself can be composed into a set of values, so that, for example,
// multiple matching state-on-date parameters can be specified as
// state-on-date=new$2013-05-04,active$2013-05-05.
type CompositeParam struct {
SearchParamInfo
CompositeValues []string
}
func (c *CompositeParam) getInfo() SearchParamInfo {
return c.SearchParamInfo
}
func (c *CompositeParam) getQueryParamAndValue() (string, string) {
value := strings.Join(c.CompositeValues, "$")
return queryParamAndValue(c.SearchParamInfo, value)
}
// ParseCompositeParam parses a composite query string and returns a pointer to
// a CompositeParam based on the query and the parameter definition.
func ParseCompositeParam(paramString string, info SearchParamInfo) *CompositeParam {
return &CompositeParam{info, escapeFriendlySplit(paramString, '$')}
}
// DateParam represents a date-flavored search parameter. The following
// description is from the FHIR DSTU2 specification:
//
// A date parameter searches on a date/time or period. As is usual for
// date/time related functionality, while the concepts are relatively
// straight-forward, there are a number of subtleties involved in ensuring
// consistent behavior.
type DateParam struct {
SearchParamInfo
Date *Date
}
func (d *DateParam) getInfo() SearchParamInfo {
return d.SearchParamInfo
}
func (d *DateParam) getQueryParamAndValue() (string, string) {
return queryParamAndValue(d.SearchParamInfo, d.Date.String())
}
// ParseDateParam parses a date-based query string and returns a pointer to a
// DateParam based on the query and the parameter definition.
func ParseDateParam(paramStr string, info SearchParamInfo) *DateParam {
date := &DateParam{SearchParamInfo: info}
var value string
date.Prefix, value = ExtractPrefixAndValue(paramStr)
date.Date = ParseDate(value)
return date
}
// Date represents a date in a search query. FHIR search params may define
// dates to varying levels of precision, and the amount of precision affects
// the behavior of the query. Date's value should only be interpreted in the
// context of the Precision supplied.
type Date struct {
Value time.Time
Precision DatePrecision
}
// String returns a string representation of the date, honoring the supplied
// precision.
func (d *Date) String() string {
s := d.Value.Format(d.Precision.layout())
if strings.HasSuffix(s, "+00:00") {
s = strings.Replace(s, "+00:00", "Z", 1)
}
return s
}
// RangeLowIncl represents the low end of a date range to match against. As
// the name suggests, the low end of the range is inclusive.
func (d *Date) RangeLowIncl() time.Time {
return d.Value
}
// RangeHighExcl represents the high end of a date range to match against. As
// the name suggests, the high end of the range is exclusive.
func (d *Date) RangeHighExcl() time.Time {
switch d.Precision {
case Year:
return d.Value.AddDate(1, 0, 0)
case Month:
return d.Value.AddDate(0, 1, 0)
case Day:
return d.Value.AddDate(0, 0, 1)
case Minute:
return d.Value.Add(time.Minute)
case Second:
return d.Value.Add(time.Second)
case Millisecond:
return d.Value.Add(time.Millisecond)
default:
return d.Value.Add(time.Millisecond)
}
}
// ParseDate parses a FHIR date string (roughly ISO 8601) into a Date object,
// maintaining the value and the precision supplied.
func ParseDate(dateStr string) *Date {
dt := &Date{}
dateStr = strings.TrimSpace(dateStr)
dtRegex := regexp.MustCompile("([0-9]{4})(-(0[1-9]|1[0-2])(-(0[0-9]|[1-2][0-9]|3[0-1])(T([01][0-9]|2[0-3]):([0-5][0-9])(:([0-5][0-9])(\\.([0-9]+))?)?((Z)|(\\+|-)((0[0-9]|1[0-3]):([0-5][0-9])|(14):(00)))?)?)?)?")
if m := dtRegex.FindStringSubmatch(dateStr); m != nil {
y, mo, d, h, mi, s, ms, tzZu, tzOp, tzh, tzm := m[1], m[3], m[5], m[7], m[8], m[10], m[12], m[14], m[15], m[17], m[18]
switch {
case ms != "":
dt.Precision = Millisecond
// Fix milliseconds (.9 -> .900, .99 -> .990, .999999 -> .999 )
switch len(ms) {
case 1:
ms += "00"
case 2:
ms += "0"
case 3:
// do nothing
default:
ms = ms[:3]
}
case s != "":
dt.Precision = Second
case mi != "":
dt.Precision = Minute
// NOTE: Skip hour precision since FHIR specification disallows it
case d != "":
dt.Precision = Day
case mo != "":
dt.Precision = Month
case y != "":
dt.Precision = Year
default:
dt.Precision = Millisecond
}
// Get the location (if no time components or no location, use local)
loc := time.Local
if h != "" {
if tzZu == "Z" {
loc, _ = time.LoadLocation("UTC")
} else if tzOp != "" && tzh != "" && tzm != "" {
tzhi, _ := strconv.Atoi(tzh)
tzmi, _ := strconv.Atoi(tzm)
offset := tzhi*60*60 + tzmi*60
if tzOp == "-" {
offset *= -1
}
loc = time.FixedZone(tzOp+tzh+tzm, offset)
}
}
// Convert to a time.Time
yInt, _ := strconv.Atoi(y)
moInt, err := strconv.Atoi(mo)
if err != nil {
moInt = 1
}
dInt, err := strconv.Atoi(d)
if err != nil {
dInt = 1
}
hInt, _ := strconv.Atoi(h)
miInt, _ := strconv.Atoi(mi)
sInt, _ := strconv.Atoi(s)
msInt, _ := strconv.Atoi(ms)
dt.Value = time.Date(yInt, time.Month(moInt), dInt, hInt, miInt, sInt, msInt*1000*1000, loc)
} else {
// TODO: What should we do if the time format is wrong? Right now, we default to NOW
dt.Precision = Millisecond
dt.Value = time.Now()
}
return dt
}
// DatePrecision is an enum representing the precision of a date.
type DatePrecision int
// Constant values for the DatePrecision enum.
const (
Year DatePrecision = iota
Month
Day
Minute
Second
Millisecond
)
func (p DatePrecision) layout() string {
switch p {
case Year:
return "2006"
case Month:
return "2006-01"
case Day:
return "2006-01-02"
case Minute:
return "2006-01-02T15:04-07:00"
case Second:
return "2006-01-02T15:04:05-07:00"
case Millisecond:
return "2006-01-02T15:04:05.000-07:00"
default:
return "2006-01-02T15:04:05.000-07:00"
}
}
// NumberParam represents a number-flavored search parameter. The following
// description is from the FHIR DSTU2 specification:
//
// Searching on a simple numerical value in a resource.
type NumberParam struct {
SearchParamInfo
Number *Number
}
func (n *NumberParam) getInfo() SearchParamInfo {
return n.SearchParamInfo
}
func (n *NumberParam) getQueryParamAndValue() (string, string) {
return queryParamAndValue(n.SearchParamInfo, n.Number.String())
}
// ParseNumberParam parses a number-based query string and returns a pointer to
// a NumberParam based on the query and the parameter definition.
func ParseNumberParam(paramStr string, info SearchParamInfo) *NumberParam {
n := &NumberParam{SearchParamInfo: info}
var value string
n.Prefix, value = ExtractPrefixAndValue(paramStr)
n.Number = ParseNumber(value)
return n
}
// Number represents a number in a search query. FHIR search params may define
// numbers to varying levels of precision, and the amount of precision affects
// the behavior of the query. Number's value should only be interpreted in the
// context of the Precision supplied. The Precision indicates the number of
// decimal places in the precision.
type Number struct {
Value *big.Rat
Precision int
}
// String returns a string representation of the number, honoring the supplied
// precision.
func (n *Number) String() string {
return n.Value.FloatString(n.Precision)
}
// RangeLowIncl represents the low end of a range to match against. As
// the name suggests, the low end of the range is inclusive.
func (n *Number) RangeLowIncl() *big.Rat {
return new(big.Rat).Sub(n.Value, n.rangeDelta())
}
// RangeHighExcl represents the high end of a range to match against. As
// the name suggests, the high end of the range is exclusive.
func (n *Number) RangeHighExcl() *big.Rat {
return new(big.Rat).Add(n.Value, n.rangeDelta())
}
// The FHIR spec defines equality for 100 to be the range [99.5, 100.5) so we
// must support min/max using rounding semantics. The basic algorithm for
// determining low/high is:
// low (inclusive) = n - 5 / 10^p
// high (exclusive) = n + 5 / 10^p
// where n is the number and p is the count of the number's decimal places + 1.
//
// This function returns the delta ( 5 / 10^p )
func (n *Number) rangeDelta() *big.Rat {
p := n.Precision + 1
denomInt := new(big.Int).Exp(big.NewInt(int64(10)), big.NewInt(int64(p)), nil)
denomRat, _ := new(big.Rat).SetString(denomInt.String())
return new(big.Rat).Quo(new(big.Rat).SetInt64(5), denomRat)
}
// ParseNumber parses a numeric string into a Number object, maintaining the
// value and precision supplied.
func ParseNumber(numStr string) *Number {
n := &Number{}
numStr = strings.TrimSpace(numStr)
n.Value, _ = new(big.Rat).SetString(numStr)
i := strings.Index(numStr, ".")
if i != -1 {
n.Precision = len(numStr) - i - 1
} else {
n.Precision = 0
}
return n
}
// QuantityParam represents a quantity-flavored search parameter. The
// following description is from the FHIR DSTU2 specification:
//
// A quantity parameter searches on the Quantity data type.
type QuantityParam struct {
SearchParamInfo
Number *Number
System string
Code string
}
func (q *QuantityParam) getInfo() SearchParamInfo {
return q.SearchParamInfo
}
func (q *QuantityParam) getQueryParamAndValue() (string, string) {
value := q.Number.String()
if q.Code != "" {
value = fmt.Sprintf("%s|%s|%s", value, escape(q.System), escape(q.Code))
}
return queryParamAndValue(q.SearchParamInfo, value)
}
// ParseQuantityParam parses a quantity-based query string and returns a
// pointer to a QuantityParam based on the query and the parameter definition.
func ParseQuantityParam(paramStr string, info SearchParamInfo) *QuantityParam {
q := &QuantityParam{SearchParamInfo: info}
var value string
q.Prefix, value = ExtractPrefixAndValue(paramStr)
split := escapeFriendlySplit(value, '|')
q.Number = ParseNumber(split[0])
if len(split) == 3 {
q.System = unescape(split[1])
q.Code = unescape(split[2])
}
return q
}
// ReferenceParam represents a reference-flavored search parameter. The
// following description is from the FHIR DSTU2 specification:
//
// A reference parameter refers to references between resources,
// e.g. find all Conditions where the subject reference is a particular patient,
// where the patient is selected by name or identifier.
type ReferenceParam struct {
SearchParamInfo
Reference interface{}
}
func (r *ReferenceParam) getInfo() SearchParamInfo {
return r.SearchParamInfo
}
func (r *ReferenceParam) getQueryParamAndValue() (string, string) {
switch t := r.Reference.(type) {
case ChainedQueryReference:
// This is a weird one, so don't use the general encodedQueryParam function
// First get the chained query param (e.g., "gender=male")
chainedParams := t.ChainedQuery.Params()
if len(chainedParams) != 1 {
panic(createInternalServerError("MSG_PARAM_CHAINED", "Unknown chained parameter name \"\""))
}
cqParam, cqValue := chainedParams[0].getQueryParamAndValue()
// Then get the LHS representing the reference (e.g., "subject:Patient")
referenceParam := fmt.Sprintf("%s:%s", r.Name, t.Type)
// Then put them together to get the full param / value (e.g., "subject:Patient.gender", "male")
return fmt.Sprintf("%s.%s", referenceParam, cqParam), cqValue
case ExternalReference:
return r.Name, escape(t.URL)
case LocalReference:
return r.Name, fmt.Sprintf("%s/%s", t.Type, escape(t.ID))
}
panic(createInternalServerError("MSG_PARAM_INVALID", fmt.Sprintf("Parameter \"%s\" content is invalid", r.Name)))
}
// ParseReferenceParam parses a reference-based query string and returns a
// pointer to a ReferenceParam based on the query and the parameter definition.
func ParseReferenceParam(paramStr string, info SearchParamInfo) *ReferenceParam {
if info.Postfix != "" {
typ := findReferencedType("", info)
q := Query{Resource: typ, Query: info.Postfix + "=" + paramStr}
return &ReferenceParam{info, ChainedQueryReference{Type: typ, ChainedQuery: q}}
} else {
ref := unescape(paramStr)
re := regexp.MustCompile("\\/?(([^\\/]+)\\/)?([^\\/]+)$")
if m := re.FindStringSubmatch(ref); m != nil {
typ := findReferencedType(m[2], info)
if u, e := url.Parse(ref); e == nil && u.IsAbs() {
return &ReferenceParam{info, ExternalReference{Type: typ, URL: ref}}
} else {
return &ReferenceParam{info, LocalReference{Type: typ, ID: m[3]}}
}
}
}
return &ReferenceParam{info, nil}
}
func findReferencedType(typeFromVal string, info SearchParamInfo) string {
t := typeFromVal
if info.Modifier != "" {
if t != "" && t != info.Modifier {
panic(createInvalidSearchError("MSG_PARAM_MODIFIER_INVALID", fmt.Sprintf("Parameter \"%s\" modifier is invalid", info.Name)))
}
t = info.Modifier
}
valid := false
if len(info.Targets) == 1 {
target := info.Targets[0]
if target != "Any" {
if t == "" {
t = target
}
valid = (t == target)
}
} else if len(info.Targets) > 1 {
for _, target := range info.Targets {
if t == target {
valid = true
}
}
}
if !valid {
if info.Modifier != "" {
panic(createInvalidSearchError("MSG_PARAM_MODIFIER_INVALID", fmt.Sprintf("Parameter \"%s\" modifier is invalid", info.Name)))
} else {
panic(createInvalidSearchError("MSG_PARAM_INVALID", fmt.Sprintf("Parameter \"%s\" content is invalid", info.Name)))
}
}
return t
}
// LocalReference represents a local reference by ID (and potentially Type)
type LocalReference struct {
Type string
ID string
}
// ExternalReference represents an external reference by URL
type ExternalReference struct {
Type string
URL string
}
// ChainedQueryReference represents a chained query
type ChainedQueryReference struct {
Type string
ChainedQuery Query
}
// StringParam represents a string-flavored search parameter. The
// following description is from the FHIR DSTU2 specification:
//
// The string parameter refers to simple string searches against
// sequences of characters. Matches are case- and accent- insensitive. By
// default, a field matches a string query if the value of the field equals or
// starts with the supplied parameter value, after both have been normalized by
// case and accent.
type StringParam struct {
SearchParamInfo
String string
}
func (s *StringParam) getInfo() SearchParamInfo {
return s.SearchParamInfo
}
func (s *StringParam) getQueryParamAndValue() (string, string) {
return queryParamAndValue(s.SearchParamInfo, escape(s.String))
}
// ParseStringParam parses a string-based query string and returns a pointer to
// a StringParam based on the query and the parameter definition.
func ParseStringParam(paramString string, info SearchParamInfo) *StringParam {
return &StringParam{info, unescape(paramString)}
}
// TokenParam represents a token-flavored search parameter. The
// following description is from the FHIR DSTU2 specification:
//
// A token type is a parameter that searches on a pair, a URI and a
// value. It is used against code or identifier value where the value may have
// a URI that scopes its meaning. The search is performed against the pair from
// a Coding or an Identifier.
type TokenParam struct {
SearchParamInfo
System string
Code string
AnySystem bool
}
func (t *TokenParam) getInfo() SearchParamInfo {
return t.SearchParamInfo
}
func (t *TokenParam) getQueryParamAndValue() (string, string) {
value := escape(t.Code)
if !t.AnySystem || t.System != "" {
value = fmt.Sprintf("%s|%s", escape(t.System), escape(t.Code))
}
return queryParamAndValue(t.SearchParamInfo, value)
}
// ParseTokenParam parses a token-based query string and returns a pointer to
// a TokenParam based on the query and the parameter definition.
func ParseTokenParam(paramString string, info SearchParamInfo) *TokenParam {
t := &TokenParam{SearchParamInfo: info}
splitCode := escapeFriendlySplit(paramString, '|')
if len(splitCode) > 1 {
t.System = unescape(splitCode[0])
t.Code = unescape(splitCode[1])
} else {
t.AnySystem = true
t.Code = unescape(splitCode[0])
}
return t
}
// URIParam represents a uri-flavored search parameter. The
// following description is from the FHIR DSTU2 specification:
//
// The uri parameter refers to an element which is URI (RFC 3986). Matches
// are precise (e.g. case, accent, and escape) sensitive, and the entire URI
// must match.
type URIParam struct {
SearchParamInfo
URI string
}
func (u *URIParam) getInfo() SearchParamInfo {
return u.SearchParamInfo
}
func (u *URIParam) getQueryParamAndValue() (string, string) {
return queryParamAndValue(u.SearchParamInfo, escape(u.URI))
}
// ParseURIParam parses an uri-based query string and returns a pointer to
// an URIParam based on the query and the parameter definition.
func ParseURIParam(paramStr string, info SearchParamInfo) *URIParam {
return &URIParam{info, unescape(paramStr)}
}
// OrParam represents a search parameter that has multiple OR values. The
// following description is from the FHIR DSTU2 specification:
//
// If, instead, the search is to find patients that speak either language, then
// this is a single parameter with multiple values, separated by a ','. For
// example: "/Patient?language=FR,NL". This is known as an OR search parameter,
// since the server is expected to respond with results which match either
// value.
type OrParam struct {
SearchParamInfo
Items []SearchParam
}
func (o *OrParam) getInfo() SearchParamInfo {
return o.SearchParamInfo
}
func (o *OrParam) getQueryParamAndValue() (string, string) {
if len(o.Items) == 0 {
panic(createInternalServerError("MSG_PARAM_INVALID", fmt.Sprintf("Parameter \"%s\" content is invalid", o.Name)))
}
param, _ := o.Items[0].getQueryParamAndValue()
values := make([]string, len(o.Items))
for i := range o.Items {
_, values[i] = o.Items[i].getQueryParamAndValue()
}
value := strings.Join(values, ",")
return param, value
}
// ParseOrParam parses a slice of values to be ORed and returns a pointer to
// an OrParam based on the query and the parameter definition.
func ParseOrParam(paramStr []string, info SearchParamInfo) *OrParam {
ors := make([]SearchParam, len(paramStr))
for i := range paramStr {
ors[i] = info.CreateSearchParam(paramStr[i])
}
return &OrParam{SearchParamInfo{Name: info.Name, Type: "or"}, ors}
}
// ParseParamNameModifierAndPostFix parses a full parameter key and returns the parameter name,
// modifier, and postfix components. For example, "foo:bar.baz" would return ["foo","bar","baz"].
func ParseParamNameModifierAndPostFix(fullParam string) (param string, modifier string, postfix string) {
param = fullParam
if strings.Contains(fullParam, ".") {
split := strings.SplitN(fullParam, ".", 2)
param = split[0]
postfix = split[1]
}
if strings.Contains(param, ":") {
split := strings.SplitN(param, ":", 2)
param = split[0]
modifier = split[1]