/
Query.go
84 lines (66 loc) · 2.25 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
80
81
82
83
84
/* Query.go: defines the Query object used by the QueryEngine for querying state
*
* Author: J. Lowell Wofford <lowell@lanl.gov>
*
* This software is open source software available under the BSD-3 license.
* Copyright (c) 2018-2021, Triad National Security, LLC
* See LICENSE file for details.
*/
package core
import (
"reflect"
"github.com/kraken-hpc/kraken/lib/types"
)
//////////////////
// Query Object /
////////////////
var _ types.Query = (*Query)(nil)
// Query objects describe a state query
type Query struct {
t types.QueryType
s types.QueryState
u string
v []reflect.Value
c chan types.QueryResponse
}
// NewQuery creates an initialized query; this is how all Queries should be created
func NewQuery(t types.QueryType, s types.QueryState, url string, v []reflect.Value) (*Query, chan types.QueryResponse) {
q := &Query{}
q.t = t
q.s = s
q.u = url
q.v = v
q.c = make(chan types.QueryResponse)
return q, q.c
}
// Type returns the type of the query (e.g., Create, Update...)
func (q *Query) Type() types.QueryType { return q.t }
// State returns the state (Dsc, Cfg, or Both) we are querying
func (q *Query) State() types.QueryState { return q.s }
// URL returns a string representing the object being queried
func (q *Query) URL() string { return q.u }
// Value returns an array of associated refelct.Value's with this query
func (q *Query) Value() []reflect.Value { return q.v }
// ResponseChan returns the channel that a QueryResponse should be sent on
func (q *Query) ResponseChan() chan<- types.QueryResponse { return q.c }
//////////////////////////
// QueryResponse Object /
////////////////////////
var _ types.QueryResponse = (*QueryResponse)(nil)
// A QueryResponse is sent by the Engine to the requester with results and possible errors
type QueryResponse struct {
e error
v []reflect.Value
}
// NewQueryResponse creates an initialized and fully specified QueryResponse
func NewQueryResponse(v []reflect.Value, e error) *QueryResponse {
qr := &QueryResponse{
e: e,
v: v,
}
return qr
}
// Error returns the error value of the QueryResponse
func (q *QueryResponse) Error() error { return q.e }
// Value returns an array of []reflect.Value's that may have resulted from the query
func (q *QueryResponse) Value() []reflect.Value { return q.v }