This repository has been archived by the owner on Aug 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
query.go
79 lines (64 loc) · 1.54 KB
/
query.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
package gqlshield
import (
"sort"
"time"
)
// Query represents a query object
type Query interface {
// Query returns a copy of the query string
Query() []byte
// ID returns the unique identifier of the query
ID() ID
// Creation returns the time of creation
Creation() time.Time
// Name returns the query name
Name() string
// Parameters returns a copy of the list of query parameters
Parameters() map[string]Parameter
// WhitelistedFor returns the copy of role IDs the query is whitelisted for
WhitelistedFor() []int
}
// Parameter represents a query parameter
type Parameter struct {
MaxValueLength uint32 `json:"max-value-length"`
}
// query represents a whitelisted query
type query struct {
id ID
query []byte
creation time.Time
name string
parameters map[string]Parameter
whitelistedFor map[int]struct{}
}
func (q *query) ID() ID {
return q.id
}
func (q *query) Creation() time.Time {
return q.creation
}
func (q *query) Name() string {
return q.name
}
func (q *query) Query() []byte {
query := make([]byte, len(q.query))
copy(query, q.query)
return query
}
func (q *query) Parameters() map[string]Parameter {
params := make(map[string]Parameter, len(q.parameters))
for name, param := range q.parameters {
params[name] = param
}
return params
}
func (q *query) WhitelistedFor() []int {
clientRoleIDs := make([]int, len(q.whitelistedFor))
index := 0
for roleID := range q.whitelistedFor {
clientRoleIDs[index] = roleID
index++
}
sort.Ints(clientRoleIDs)
return clientRoleIDs
}