-
Notifications
You must be signed in to change notification settings - Fork 172
/
query.go
127 lines (109 loc) · 3.48 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
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
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package service
import (
"encoding/json"
"errors"
"github.com/golang/protobuf/ptypes/any"
"github.com/micro/protobuf/jsonpb"
"github.com/micro/protobuf/proto"
"github.com/micro/protobuf/ptypes"
"github.com/mitchellh/mapstructure"
)
type ConcreteQuery struct {
Name string `json:"Name,omitempty"`
Data interface{} `json:"Data,omitempty"`
}
type outputFormat struct {
SubQueries []interface{} `json:"SubQueries,omitempty"`
Operation OperationType `json:"Operation,omitempty"`
Offset int64 `json:"Offset,omitempty"`
Limit int64 `json:"Limit,omitempty"`
GroupBy int32 `json:"groupBy,omitempty"`
}
func (q *Query) MarshalJSONPB(marshaler *jsonpb.Marshaler) ([]byte, error) {
data := outputFormat{
SubQueries: []interface{}{},
Operation: q.Operation,
Offset: q.Offset,
Limit: q.Limit,
GroupBy: q.GroupBy,
}
for _, obj := range q.SubQueries {
if concrete, err := q.marshalAnyTypes(obj); err == nil {
data.SubQueries = append(data.SubQueries, concrete)
} else {
data.SubQueries = append(data.SubQueries, obj)
}
}
return json.Marshal(data)
}
func (q *Query) marshalAnyTypes(obj *any.Any) (*ConcreteQuery, error) {
/*
TODO - find a solution to register a marshaller
nodeQ := &tree.Query{}
if e := ptypes.UnmarshalAny(obj, nodeQ); e == nil {
return &ConcreteQuery{Name: "tree.Query", Data: nodeQ}, nil
}
*/
aoQ := &ActionOutputQuery{}
if e := ptypes.UnmarshalAny(obj, aoQ); e == nil {
return &ConcreteQuery{Name: "ActionOutputQuery", Data: aoQ}, nil
}
sourceQ := &SourceSingleQuery{}
if e := ptypes.UnmarshalAny(obj, sourceQ); e == nil {
return &ConcreteQuery{Name: "SourceSingleQuery", Data: sourceQ}, nil
}
return nil, errors.New("Type Not Found")
}
func (q *Query) UnmarshalJSONPB(unmarshaller *jsonpb.Unmarshaler, data []byte) error {
input := &outputFormat{SubQueries: []interface{}{}}
json.Unmarshal(data, input)
q.GroupBy = input.GroupBy
q.Limit = input.Limit
q.Offset = input.Offset
q.Operation = input.Operation
q.SubQueries = []*any.Any{}
for _, obj := range input.SubQueries {
var casted ConcreteQuery
if err := mapstructure.Decode(obj, &casted); err == nil {
var o proto.Message
switch casted.Name {
/*case "tree.Query":
o = &tree.Query{}
*/
case "ActionOutputQuery":
o = &ActionOutputQuery{}
case "SourceSingleQuery":
o = &SourceSingleQuery{}
}
if e := mapstructure.Decode(casted.Data, o); e == nil {
anyfied, _ := ptypes.MarshalAny(o)
q.SubQueries = append(q.SubQueries, anyfied)
}
} else {
var a any.Any
if e := mapstructure.Decode(obj, &a); e == nil {
q.SubQueries = append(q.SubQueries, &a)
}
}
}
return nil
}