forked from owenthereal/ccat
/
query_etc.go
82 lines (68 loc) · 2.52 KB
/
query_etc.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
package sourcegraph
import (
"encoding/json"
"fmt"
)
// A Plan is a query plan that fetches the data necessary to satisfy
// (and provide autocomplete suggestions for) a query.
type Plan struct {
Repos *RepoListOptions
Defs *DefListOptions
Users *UsersListOptions
Tree *RepoTreeSearchOptions
// TreeRepoRevs constrains the Tree search results to a set of
// repository revisions (given by their URIs plus an optional "@"
// and a revision specifier). For example, "repo.com/foo@revspec".
//
// TODO(sqs): gorilla/schema does not respect ",comma" and it has
// no similar option, so specifying multiple repo revs here does
// NOT work.
TreeRepoRevs []string `url:",omitempty,comma" json:",omitempty"`
}
// A Suggestion is a possible completion of a query (returned by
// Suggest method). It does not attempt to "complete" a query but
// rather indicate to the user what types of queries are possible.
type Suggestion struct {
// Query is a suggested query related to the original query.
Query Tokens
// QueryString is what the user needs to enter into the search
// field to search using this suggested query.
QueryString string
// Description is the human-readable description of Query (usually
// generated by calling the Describe func).
Description string `json:",omitempty"`
}
func (p *Plan) String() string {
b, _ := json.MarshalIndent(p, "", " ")
return string(b)
}
// A TokenError is an error about a specific token.
type TokenError struct {
// Index is the 1-indexed index of the token that caused the error
// (0 means not associated with any particular token).
//
// NOTE: Index is 1-indexed (not 0-indexed) because some
// TokenErrors don't pertain to a token, and it's misleading if
// the Index in the JSON is 0 (which could mean that it pertains
// to the 1st token if index was 0-indexed).
Index int `json:",omitempty"`
Token Token `json:",omitempty"` // the token that caused the error
Message string // the public, user-readable error message to display
}
func (e TokenError) Error() string { return fmt.Sprintf("%s (%v)", e.Message, e.Token) }
type jsonTokenError struct {
Index int `json:",omitempty"`
Token jsonToken `json:",omitempty"`
Message string
}
func (e TokenError) MarshalJSON() ([]byte, error) {
return json.Marshal(jsonTokenError{e.Index, jsonToken{e.Token}, e.Message})
}
func (e *TokenError) UnmarshalJSON(b []byte) error {
var jv jsonTokenError
if err := json.Unmarshal(b, &jv); err != nil {
return err
}
*e = TokenError{jv.Index, jv.Token.Token, jv.Message}
return nil
}