@@ -471,23 +471,11 @@ func NewParse(q url.Values, v Validations) (*Query, error) {
471
471
return query , query .Parse ()
472
472
}
473
473
474
- // Parse parses the query of URL
475
- // as query you can use standart http.Request query by r.URL.Query()
476
- func (q * Query ) Parse () error {
477
-
478
- // clean the filters slice
479
- if len (q .Filters ) > 0 {
480
- for i := range q .Filters {
481
- q .Filters [i ] = nil
482
- }
483
- q .Filters = nil
484
- }
485
-
486
- // construct a slice with required names of filters
487
-
474
+ // requiredNames returns list of required filters
475
+ func requiredNames (validations Validations ) []string {
488
476
var requiredNames []string
489
477
490
- for name , f := range q . validations {
478
+ for name , f := range validations {
491
479
if strings .Contains (name , ":required" ) {
492
480
oldname := name
493
481
// oldname = arg1:required
@@ -517,10 +505,93 @@ func (q *Query) Parse() error {
517
505
requiredNames = append (requiredNames , name )
518
506
}
519
507
520
- q .validations [newname ] = f
521
- delete (q .validations , oldname )
508
+ validations [newname ] = f
509
+ delete (validations , oldname )
510
+ }
511
+ }
512
+ return requiredNames
513
+ }
514
+
515
+ func (q * Query ) parseFilter (key , value string ) error {
516
+ value = strings .TrimSpace (value )
517
+
518
+ if len (value ) == 0 {
519
+ return errors .Wrap (ErrEmptyValue , key )
520
+ }
521
+
522
+ if strings .Contains (value , q .delimiterOR ) { // OR multiple filter
523
+ parts := strings .Split (value , q .delimiterOR )
524
+ for i , v := range parts {
525
+ if i > 0 {
526
+ u := strings .Split (v , "=" )
527
+ if len (u ) < 2 {
528
+ return errors .Wrap (ErrBadFormat , key )
529
+ }
530
+ key = u [0 ]
531
+ v = u [1 ]
532
+ }
533
+
534
+ v := strings .TrimSpace (v )
535
+ if len (v ) == 0 {
536
+ return errors .Wrap (ErrEmptyValue , key )
537
+ }
538
+
539
+ filter , err := newFilter (key , v , q .delimiterIN , q .validations )
540
+
541
+ if err != nil {
542
+ if err == ErrValidationNotFound {
543
+ if q .ignoreUnknown {
544
+ continue
545
+ } else {
546
+ return errors .Wrap (ErrFilterNotFound , key )
547
+ }
548
+ }
549
+ return errors .Wrap (err , key )
550
+ }
551
+
552
+ // set OR
553
+ filter .Or = true
554
+
555
+ q .Filters = append (q .Filters , filter )
556
+ }
557
+ } else { // Single filter
558
+ filter , err := newFilter (key , value , q .delimiterIN , q .validations )
559
+ if err != nil {
560
+ if err == ErrValidationNotFound {
561
+ if q .ignoreUnknown {
562
+ return nil
563
+ } else {
564
+ return errors .Wrap (ErrFilterNotFound , key )
565
+ }
566
+ }
567
+ return errors .Wrap (err , key )
568
+ }
569
+
570
+ q .Filters = append (q .Filters , filter )
571
+ }
572
+
573
+ return nil
574
+ }
575
+
576
+ // clean the filters slice
577
+ func (q * Query ) cleanFilters () {
578
+ if len (q .Filters ) > 0 {
579
+ for i := range q .Filters {
580
+ q .Filters [i ] = nil
522
581
}
582
+ q .Filters = nil
523
583
}
584
+ }
585
+
586
+ // Parse parses the query of URL
587
+ // as query you can use standart http.Request query by r.URL.Query()
588
+ func (q * Query ) Parse () error {
589
+
590
+ // clean previously parserd filters
591
+ q .cleanFilters ()
592
+
593
+ // construct a slice with required names of filters
594
+ requiredNames := requiredNames (q .validations )
524
595
525
596
//fmt.Println("NEW QUERY:")
526
597
@@ -554,74 +625,13 @@ func (q *Query) Parse() error {
554
625
}
555
626
requiredNames = removeFromSlice (requiredNames , low )
556
627
default :
557
- if len (values ) == 1 {
558
-
559
- //fmt.Println("new filter:")
560
-
561
- value := strings .TrimSpace (values [0 ])
562
- if len (value ) == 0 {
563
- return errors .Wrap (ErrEmptyValue , key )
564
- }
565
-
566
- if strings .Contains (value , q .delimiterOR ) { // OR multiple filter
567
- parts := strings .Split (value , q .delimiterOR )
568
- for i , v := range parts {
569
- if i > 0 {
570
- u := strings .Split (v , "=" )
571
- if len (u ) < 2 {
572
- return errors .Wrap (ErrBadFormat , key )
573
- }
574
- key = u [0 ]
575
- v = u [1 ]
576
- }
577
-
578
- //fmt.Println("key:", key, "value:", v)
579
-
580
- v := strings .TrimSpace (v )
581
- if len (v ) == 0 {
582
- return errors .Wrap (ErrEmptyValue , key )
583
- }
584
-
585
- filter , err := newFilter (key , v , q .delimiterIN , q .validations )
586
-
587
- if err != nil {
588
- if err == ErrValidationNotFound {
589
- if q .ignoreUnknown {
590
- continue
591
- } else {
592
- return errors .Wrap (ErrFilterNotFound , key )
593
- }
594
- }
595
- return errors .Wrap (err , key )
596
- }
597
-
598
- // set OR
599
- filter .Or = true
600
-
601
- q .Filters = append (q .Filters , filter )
602
- }
603
- } else { // Single filter
604
- //fmt.Println("key:", key, "value:", value)
605
- filter , err := newFilter (key , value , q .delimiterIN , q .validations )
606
- if err != nil {
607
- if err == ErrValidationNotFound {
608
- if q .ignoreUnknown {
609
- continue
610
- } else {
611
- return errors .Wrap (ErrFilterNotFound , key )
612
- }
613
- }
614
- return errors .Wrap (err , key )
615
- }
616
-
617
- q .Filters = append (q .Filters , filter )
618
- }
619
-
620
- } else {
628
+ if len (values ) != 1 {
621
629
return errors .Wrap (ErrBadFormat , key )
622
630
}
631
+ if err := q .parseFilter (key , values [0 ]); err != nil {
632
+ return err
633
+ }
623
634
}
624
-
625
635
}
626
636
627
637
// check required filters
0 commit comments