aboutsummaryrefslogtreecommitdiffstats
path: root/query/parser.go
blob: 722045f5e3c9a0e3f7daf6247d22d1f4b4ff855c (plain) (blame)
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
package query

import (
	"fmt"

	"github.com/git-bug/git-bug/entities/common"
)

// Parse parse a query DSL
//
// Ex: "status:open author:descartes sort:edit-asc"
//
// Supported filter qualifiers and syntax are described in docs/queries.md
func Parse(query string) (*Query, error) {
	tokens, err := tokenize(query)
	if err != nil {
		return nil, err
	}

	q := &Query{
		OrderBy:        OrderByCreation,
		OrderDirection: OrderDescending,
	}
	sortingDone := false

	for _, t := range tokens {
		switch t.kind {
		case tokenKindSearch:
			q.Search = append(q.Search, t.term)
			break

		case tokenKindKV:
			switch t.qualifier {
			case "status", "state":
				status, err := common.StatusFromString(t.value)
				if err != nil {
					return nil, err
				}
				q.Status = append(q.Status, status)
			case "author":
				q.Author = append(q.Author, t.value)
			case "actor":
				q.Actor = append(q.Actor, t.value)
			case "participant":
				q.Participant = append(q.Participant, t.value)
			case "label":
				q.Label = append(q.Label, t.value)
			case "title":
				q.Title = append(q.Title, t.value)
			case "no":
				switch t.value {
				case "label":
					q.NoLabel = true
				default:
					return nil, fmt.Errorf("unknown \"no\" filter \"%s\"", t.value)
				}
			case "sort":
				if sortingDone {
					return nil, fmt.Errorf("multiple sorting")
				}
				err = parseSorting(q, t.value)
				if err != nil {
					return nil, err
				}
				sortingDone = true

			default:
				return nil, fmt.Errorf("unknown qualifier \"%s\"", t.qualifier)
			}

		case tokenKindKVV:
			switch t.qualifier {
			case "metadata":
				q.Metadata = append(q.Metadata, StringPair{Key: t.subQualifier, Value: t.value})

			default:
				return nil, fmt.Errorf("unknown qualifier \"%s:%s\"", t.qualifier, t.subQualifier)
			}
		}
	}
	return q, nil
}

func parseSorting(q *Query, value string) error {
	switch value {
	// default ASC
	case "id-desc":
		q.OrderBy = OrderById
		q.OrderDirection = OrderDescending
	case "id", "id-asc":
		q.OrderBy = OrderById
		q.OrderDirection = OrderAscending

	// default DESC
	case "creation", "creation-desc":
		q.OrderBy = OrderByCreation
		q.OrderDirection = OrderDescending
	case "creation-asc":
		q.OrderBy = OrderByCreation
		q.OrderDirection = OrderAscending

	// default DESC
	case "edit", "edit-desc":
		q.OrderBy = OrderByEdit
		q.OrderDirection = OrderDescending
	case "edit-asc":
		q.OrderBy = OrderByEdit
		q.OrderDirection = OrderAscending

	default:
		return fmt.Errorf("unknown sorting %s", value)
	}

	return nil
}