-
Notifications
You must be signed in to change notification settings - Fork 42
/
query_manager.go
94 lines (81 loc) · 2.94 KB
/
query_manager.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
package cardinal
import (
"github.com/rotisserie/eris"
"pkg.world.dev/world-engine/cardinal/server/utils"
"pkg.world.dev/world-engine/cardinal/types"
)
var _ QueryManager = &queryManager{}
type QueryManager interface {
RegisterQuery(queryInput query) error
GetRegisteredQueries() []query
HandleQuery(group string, name string, bz []byte) ([]byte, error)
HandleQueryEVM(group string, name string, abiRequest []byte) ([]byte, error)
getQuery(group string, name string) (query, error)
BuildQueryFields() []types.FieldDetail
}
type queryManager struct {
world *World
registeredQueriesByGroup map[string]map[string]query // group:name:query
}
func newQueryManager(world *World) QueryManager {
return &queryManager{
world: world,
registeredQueriesByGroup: make(map[string]map[string]query),
}
}
// RegisterQuery registers a query with the query manager.
// There can only be one query with a given name.
func (m *queryManager) RegisterQuery(queryInput query) error {
_, ok := m.registeredQueriesByGroup[queryInput.Group()]
if !ok {
m.registeredQueriesByGroup[queryInput.Group()] = make(map[string]query)
}
m.registeredQueriesByGroup[queryInput.Group()][queryInput.Name()] = queryInput
return nil
}
// GetRegisteredQueries returns all the registered queries.
func (m *queryManager) GetRegisteredQueries() []query {
registeredQueries := make([]query, 0, len(m.registeredQueriesByGroup))
for _, queryGroup := range m.registeredQueriesByGroup {
for _, query := range queryGroup {
registeredQueries = append(registeredQueries, query)
}
}
return registeredQueries
}
func (m *queryManager) HandleQuery(group string, name string, bz []byte) ([]byte, error) {
q, err := m.getQuery(group, name)
if err != nil {
return nil, eris.Wrapf(err, "unable to find query %s/%s", group, name)
}
return q.handleQueryJSON(NewReadOnlyWorldContext(m.world), bz)
}
func (m *queryManager) HandleQueryEVM(group string, name string, abiRequest []byte) ([]byte, error) {
q, err := m.getQuery(group, name)
if err != nil {
return nil, eris.Wrapf(err, "unable to find EVM-compatible query %s/%s", group, name)
}
return q.handleQueryEVM(NewReadOnlyWorldContext(m.world), abiRequest)
}
// getQuery returns a query corresponding to the identifier with the format <group>/<name>.
func (m *queryManager) getQuery(group string, name string) (query, error) {
query, ok := m.registeredQueriesByGroup[group][name]
if !ok {
return nil, types.ErrQueryNotFound
}
return query, nil
}
func (m *queryManager) BuildQueryFields() []types.FieldDetail {
// Collecting the structure of all queries
queries := m.GetRegisteredQueries()
queriesFields := make([]types.FieldDetail, 0, len(queries))
for _, q := range queries {
// Extracting the fields of the q
queriesFields = append(queriesFields, types.FieldDetail{
Name: q.Name(),
Fields: q.GetRequestFieldInformation(),
URL: utils.GetQueryURL(q.Group(), q.Name()),
})
}
return queriesFields
}