forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_revision_operation.go
159 lines (136 loc) · 4.19 KB
/
get_revision_operation.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package ravendb
import (
"reflect"
)
// GetRevisionOperation represents "get revisions" operation
type GetRevisionOperation struct {
session *InMemoryDocumentSessionOperations
result *JSONArrayResult
command *GetRevisionsCommand
}
func NewGetRevisionOperationWithChangeVectors(session *InMemoryDocumentSessionOperations, changeVectors []string) *GetRevisionOperation {
return &GetRevisionOperation{
session: session,
command: NewGetRevisionsCommand(changeVectors, false),
}
}
func NewGetRevisionOperationRange(session *InMemoryDocumentSessionOperations, id string, start int, pageSize int, metadataOnly bool) (*GetRevisionOperation, error) {
if session == nil {
return nil, newIllegalArgumentError("session cannot be null")
}
if id == "" {
return nil, newIllegalArgumentError("Id cannot be null")
}
return &GetRevisionOperation{
session: session,
command: NewGetRevisionsCommandRange(id, start, pageSize, metadataOnly),
}, nil
}
func (o *GetRevisionOperation) createRequest() (*GetRevisionsCommand, error) {
return o.command, nil
}
func (o *GetRevisionOperation) setResult(result *JSONArrayResult) {
o.result = result
}
// Note: in Java it's getRevision
func (o *GetRevisionOperation) GetRevisionWithDocument(result interface{}, document map[string]interface{}) error {
if document == nil {
return nil
}
var metadata map[string]interface{}
id := ""
if v, ok := document[MetadataKey]; ok {
metadata = v.(map[string]interface{})
id, _ = jsonGetAsText(metadata, MetadataID)
}
var changeVector *string
if metadata != nil {
changeVector = jsonGetAsTextPointer(metadata, MetadataChangeVector)
}
err := o.session.getEntityToJSON().convertToEntity2(result, id, document)
if err != nil {
return err
}
documentInfo := &documentInfo{}
documentInfo.id = id
documentInfo.changeVector = changeVector
documentInfo.document = document
documentInfo.metadata = metadata
documentInfo.setEntity(result)
setDocumentInfo(&o.session.documentsByEntity, documentInfo)
return nil
}
// results should be *[]*<type>
func (o *GetRevisionOperation) GetRevisionsFor(results interface{}) error {
a := o.result.getResults()
if len(a) == 0 {
return nil
}
// TODO: optimize by creating pre-allocated slice of size len(a)
slice, err := makeSliceForResults(results)
if err != nil {
return err
}
sliceElemType := reflect.TypeOf(results).Elem().Elem()
tmpSlice := slice
for _, document := range a {
// creates a pointer to value e.g. **Foo
resultV := reflect.New(sliceElemType)
err = o.GetRevisionWithDocument(resultV.Interface(), document)
if err != nil {
return err
}
// append *Foo to the slice
tmpSlice = reflect.Append(tmpSlice, resultV.Elem())
}
if slice != tmpSlice {
slice.Set(tmpSlice)
}
return nil
}
func (o *GetRevisionOperation) GetRevisionsMetadataFor() []*MetadataAsDictionary {
resultsCount := len(o.result.getResults())
results := make([]*MetadataAsDictionary, resultsCount)
for i := 0; i < resultsCount; i++ {
document := o.result.getResults()[i]
var metadata map[string]interface{}
if v, ok := document[MetadataKey]; ok {
metadata = v.(map[string]interface{})
}
results[i] = NewMetadataAsDictionaryWithSource(metadata)
}
return results
}
func (o *GetRevisionOperation) GetRevision(result interface{}) error {
if o.result == nil {
return nil
}
document := o.result.getResults()[0]
return o.GetRevisionWithDocument(result, document)
}
// result should be map[string]<type>
func (o *GetRevisionOperation) GetRevisions(results interface{}) error {
// Maybe: Java uses case-insensitive keys, but keys are change vectors
// so that shouldn't matter
rv := reflect.ValueOf(results)
elemType, ok := isMapStringToPtrStruct(rv.Type())
if !ok {
return newIllegalArgumentError("results should be of type map[string]*<struct>, is %T", results)
}
for i := 0; i < len(o.command.GetChangeVectors()); i++ {
changeVector := o.command.GetChangeVectors()[i]
if changeVector == "" {
continue
}
v := o.result.getResults()[i]
// resultV is **Foo
resultV := reflect.New(elemType)
err := o.GetRevisionWithDocument(resultV.Interface(), v)
if err != nil {
return err
}
key := reflect.ValueOf(changeVector)
rv.SetMapIndex(key, resultV.Elem())
}
return nil
}