Skip to content

Commit 7e48910

Browse files
added len method
1 parent d48ce0a commit 7e48910

14 files changed

+415
-13
lines changed

v2/arangodb/collection_documents_create.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ type CollectionDocumentCreate interface {
7171
}
7272

7373
type CollectionDocumentCreateResponseReader interface {
74+
shared.ReadAllReadable[CollectionDocumentCreateResponse]
7475
Read() (CollectionDocumentCreateResponse, error)
76+
Len() int
7577
}
7678

7779
type CollectionDocumentCreateResponse struct {

v2/arangodb/collection_documents_create_impl.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ func newCollectionDocumentCreateResponseReader(array *connection.Array, options
133133
c.response.New = newUnmarshalInto(c.options.NewObject)
134134
}
135135

136+
c.ReadAllReader = shared.ReadAllReader[CollectionDocumentCreateResponse, *collectionDocumentCreateResponseReader]{Reader: c}
136137
return c
137138
}
138139

@@ -147,6 +148,12 @@ type collectionDocumentCreateResponseReader struct {
147148
Old *UnmarshalInto `json:"old,omitempty"`
148149
New *UnmarshalInto `json:"new,omitempty"`
149150
}
151+
shared.ReadAllReader[CollectionDocumentCreateResponse, *collectionDocumentCreateResponseReader]
152+
153+
// Cache for len() method
154+
cachedResults []CollectionDocumentCreateResponse
155+
cachedErrors []error
156+
cached bool
150157
}
151158

152159
func (c *collectionDocumentCreateResponseReader) Read() (CollectionDocumentCreateResponse, error) {
@@ -171,9 +178,24 @@ func (c *collectionDocumentCreateResponseReader) Read() (CollectionDocumentCreat
171178
return CollectionDocumentCreateResponse{}, err
172179
}
173180

181+
// Update meta with the unmarshaled data
182+
meta.DocumentMeta = *c.response.DocumentMeta
183+
meta.ResponseStruct = *c.response.ResponseStruct
184+
meta.Old = c.response.Old
185+
meta.New = c.response.New
186+
174187
if meta.Error != nil && *meta.Error {
175188
return meta, meta.AsArangoError()
176189
}
177190

178191
return meta, nil
179192
}
193+
194+
// Len returns the number of items in the response
195+
func (c *collectionDocumentCreateResponseReader) Len() int {
196+
if !c.cached {
197+
c.cachedResults, c.cachedErrors = c.ReadAll()
198+
c.cached = true
199+
}
200+
return len(c.cachedResults)
201+
}

v2/arangodb/collection_documents_delete.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ type CollectionDocumentDeleteResponse struct {
6363
}
6464

6565
type CollectionDocumentDeleteResponseReader interface {
66+
shared.ReadAllIntoReadable[CollectionDocumentDeleteResponse]
6667
Read(i interface{}) (CollectionDocumentDeleteResponse, error)
68+
Len() int
6769
}
6870

6971
type CollectionDocumentDeleteOptions struct {
@@ -82,6 +84,7 @@ type CollectionDocumentDeleteOptions struct {
8284
WithWaitForSync *bool
8385

8486
// Return additionally the complete previous revision of the changed document
87+
// Should be a pointer to an object
8588
OldObject interface{}
8689

8790
// If set to true, an empty object is returned as response if the document operation succeeds.

v2/arangodb/collection_documents_delete_impl.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"context"
2525
"io"
2626
"net/http"
27+
"reflect"
2728

2829
"github.com/pkg/errors"
2930

@@ -42,6 +43,7 @@ var _ CollectionDocumentDelete = &collectionDocumentDelete{}
4243

4344
type collectionDocumentDelete struct {
4445
collection *collection
46+
shared.ReadAllIntoReader[CollectionDocumentDeleteResponse, *collectionDocumentDeleteResponseReader]
4547
}
4648

4749
func (c collectionDocumentDelete) DeleteDocument(ctx context.Context, key string) (CollectionDocumentDeleteResponse, error) {
@@ -103,6 +105,7 @@ func (c collectionDocumentDelete) DeleteDocumentsWithOptions(ctx context.Context
103105
func newCollectionDocumentDeleteResponseReader(array *connection.Array, options *CollectionDocumentDeleteOptions) *collectionDocumentDeleteResponseReader {
104106
c := &collectionDocumentDeleteResponseReader{array: array, options: options}
105107

108+
c.ReadAllIntoReader = shared.ReadAllIntoReader[CollectionDocumentDeleteResponse, *collectionDocumentDeleteResponseReader]{Reader: c}
106109
return c
107110
}
108111

@@ -111,6 +114,11 @@ var _ CollectionDocumentDeleteResponseReader = &collectionDocumentDeleteResponse
111114
type collectionDocumentDeleteResponseReader struct {
112115
array *connection.Array
113116
options *CollectionDocumentDeleteOptions
117+
shared.ReadAllIntoReader[CollectionDocumentDeleteResponse, *collectionDocumentDeleteResponseReader]
118+
// Cache for len() method
119+
cachedResults []CollectionDocumentDeleteResponse
120+
cachedErrors []error
121+
cached bool
114122
}
115123

116124
func (c *collectionDocumentDeleteResponseReader) Read(i interface{}) (CollectionDocumentDeleteResponse, error) {
@@ -146,11 +154,30 @@ func (c *collectionDocumentDeleteResponseReader) Read(i interface{}) (Collection
146154
}
147155

148156
if c.options != nil && c.options.OldObject != nil {
149-
meta.Old = c.options.OldObject
157+
// Create a new instance for each document to avoid reusing the same pointer
158+
oldObjectType := reflect.TypeOf(c.options.OldObject).Elem()
159+
meta.Old = reflect.New(oldObjectType).Interface()
160+
161+
// Extract old data into the new instance
150162
if err := response.Object.Object.Extract("old").Inject(meta.Old); err != nil {
151163
return CollectionDocumentDeleteResponse{}, err
152164
}
165+
166+
// Copy data from the new instance to the original OldObject for backward compatibility
167+
oldValue := reflect.ValueOf(meta.Old).Elem()
168+
originalValue := reflect.ValueOf(c.options.OldObject).Elem()
169+
originalValue.Set(oldValue)
153170
}
154171

155172
return meta, nil
156173
}
174+
175+
// Len returns the number of items in the response
176+
func (c *collectionDocumentDeleteResponseReader) Len() int {
177+
if !c.cached {
178+
var dummySlice []interface{}
179+
c.cachedResults, c.cachedErrors = c.ReadAll(&dummySlice)
180+
c.cached = true
181+
}
182+
return len(c.cachedResults)
183+
}

v2/arangodb/collection_documents_read.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ type CollectionDocumentRead interface {
5959

6060
type CollectionDocumentReadResponseReader interface {
6161
Read(i interface{}) (CollectionDocumentReadResponse, error)
62+
shared.ReadAllIntoReadable[CollectionDocumentReadResponse]
63+
Len() int
6264
}
6365

6466
type CollectionDocumentReadResponse struct {

v2/arangodb/collection_documents_read_impl.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (c collectionDocumentRead) ReadDocumentWithOptions(ctx context.Context, key
9797

9898
func newCollectionDocumentReadResponseReader(array *connection.Array, options *CollectionDocumentReadOptions) *collectionDocumentReadResponseReader {
9999
c := &collectionDocumentReadResponseReader{array: array, options: options}
100-
100+
c.ReadAllIntoReader = shared.ReadAllIntoReader[CollectionDocumentReadResponse, *collectionDocumentReadResponseReader]{Reader: c}
101101
return c
102102
}
103103

@@ -106,6 +106,11 @@ var _ CollectionDocumentReadResponseReader = &collectionDocumentReadResponseRead
106106
type collectionDocumentReadResponseReader struct {
107107
array *connection.Array
108108
options *CollectionDocumentReadOptions
109+
shared.ReadAllIntoReader[CollectionDocumentReadResponse, *collectionDocumentReadResponseReader]
110+
// Cache for len() method
111+
cachedResults []CollectionDocumentReadResponse
112+
cachedErrors []error
113+
cached bool
109114
}
110115

111116
func (c *collectionDocumentReadResponseReader) Read(i interface{}) (CollectionDocumentReadResponse, error) {
@@ -142,3 +147,13 @@ func (c *collectionDocumentReadResponseReader) Read(i interface{}) (CollectionDo
142147

143148
return meta, nil
144149
}
150+
151+
// Len returns the number of items in the response
152+
func (c *collectionDocumentReadResponseReader) Len() int {
153+
if !c.cached {
154+
var dummySlice []interface{}
155+
c.cachedResults, c.cachedErrors = c.ReadAll(&dummySlice)
156+
c.cached = true
157+
}
158+
return len(c.cachedResults)
159+
}

v2/arangodb/collection_documents_replace.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ type CollectionDocumentReplace interface {
6161
}
6262

6363
type CollectionDocumentReplaceResponseReader interface {
64+
shared.ReadAllReadable[CollectionDocumentReplaceResponse]
6465
Read() (CollectionDocumentReplaceResponse, error)
66+
Len() int
6567
}
6668

6769
type CollectionDocumentReplaceResponse struct {

v2/arangodb/collection_documents_replace_impl.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"context"
2525
"io"
2626
"net/http"
27+
"reflect"
2728

2829
"github.com/pkg/errors"
2930

@@ -132,7 +133,7 @@ func newCollectionDocumentReplaceResponseReader(array *connection.Array, options
132133
c.response.Old = newUnmarshalInto(c.options.OldObject)
133134
c.response.New = newUnmarshalInto(c.options.NewObject)
134135
}
135-
136+
c.ReadAllReader = shared.ReadAllReader[CollectionDocumentReplaceResponse, *collectionDocumentReplaceResponseReader]{Reader: c}
136137
return c
137138
}
138139

@@ -147,6 +148,12 @@ type collectionDocumentReplaceResponseReader struct {
147148
Old *UnmarshalInto `json:"old,omitempty"`
148149
New *UnmarshalInto `json:"new,omitempty"`
149150
}
151+
shared.ReadAllReader[CollectionDocumentReplaceResponse, *collectionDocumentReplaceResponseReader]
152+
153+
// Cache for len() method
154+
cachedResults []CollectionDocumentReplaceResponse
155+
cachedErrors []error
156+
cached bool
150157
}
151158

152159
func (c *collectionDocumentReplaceResponseReader) Read() (CollectionDocumentReplaceResponse, error) {
@@ -157,8 +164,15 @@ func (c *collectionDocumentReplaceResponseReader) Read() (CollectionDocumentRepl
157164
var meta CollectionDocumentReplaceResponse
158165

159166
if c.options != nil {
160-
meta.Old = c.options.OldObject
161-
meta.New = c.options.NewObject
167+
// Create new instances for each document to avoid reusing the same pointers
168+
if c.options.OldObject != nil {
169+
oldObjectType := reflect.TypeOf(c.options.OldObject).Elem()
170+
meta.Old = reflect.New(oldObjectType).Interface()
171+
}
172+
if c.options.NewObject != nil {
173+
newObjectType := reflect.TypeOf(c.options.NewObject).Elem()
174+
meta.New = reflect.New(newObjectType).Interface()
175+
}
162176
}
163177

164178
c.response.DocumentMetaWithOldRev = &meta.DocumentMetaWithOldRev
@@ -171,9 +185,24 @@ func (c *collectionDocumentReplaceResponseReader) Read() (CollectionDocumentRepl
171185
return CollectionDocumentReplaceResponse{}, err
172186
}
173187

188+
// Update meta with the unmarshaled data
189+
meta.DocumentMetaWithOldRev = *c.response.DocumentMetaWithOldRev
190+
meta.ResponseStruct = *c.response.ResponseStruct
191+
meta.Old = c.response.Old
192+
meta.New = c.response.New
193+
174194
if meta.Error != nil && *meta.Error {
175195
return meta, meta.AsArangoError()
176196
}
177197

178198
return meta, nil
179199
}
200+
201+
// Len returns the number of items in the response
202+
func (c *collectionDocumentReplaceResponseReader) Len() int {
203+
if !c.cached {
204+
c.cachedResults, c.cachedErrors = c.ReadAll()
205+
c.cached = true
206+
}
207+
return len(c.cachedResults)
208+
}

v2/arangodb/collection_documents_update.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ type CollectionDocumentUpdate interface {
6262
}
6363

6464
type CollectionDocumentUpdateResponseReader interface {
65+
shared.ReadAllReadable[CollectionDocumentUpdateResponse]
6566
Read() (CollectionDocumentUpdateResponse, error)
67+
Len() int
6668
}
6769

6870
type CollectionDocumentUpdateResponse struct {

v2/arangodb/collection_documents_update_impl.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"context"
2525
"io"
2626
"net/http"
27+
"reflect"
2728

2829
"github.com/pkg/errors"
2930

@@ -132,7 +133,7 @@ func newCollectionDocumentUpdateResponseReader(array *connection.Array, options
132133
c.response.Old = newUnmarshalInto(c.options.OldObject)
133134
c.response.New = newUnmarshalInto(c.options.NewObject)
134135
}
135-
136+
c.ReadAllReader = shared.ReadAllReader[CollectionDocumentUpdateResponse, *collectionDocumentUpdateResponseReader]{Reader: c}
136137
return c
137138
}
138139

@@ -147,6 +148,12 @@ type collectionDocumentUpdateResponseReader struct {
147148
Old *UnmarshalInto `json:"old,omitempty"`
148149
New *UnmarshalInto `json:"new,omitempty"`
149150
}
151+
shared.ReadAllReader[CollectionDocumentUpdateResponse, *collectionDocumentUpdateResponseReader]
152+
153+
// Cache for len() method
154+
cachedResults []CollectionDocumentUpdateResponse
155+
cachedErrors []error
156+
cached bool
150157
}
151158

152159
func (c *collectionDocumentUpdateResponseReader) Read() (CollectionDocumentUpdateResponse, error) {
@@ -157,8 +164,15 @@ func (c *collectionDocumentUpdateResponseReader) Read() (CollectionDocumentUpdat
157164
var meta CollectionDocumentUpdateResponse
158165

159166
if c.options != nil {
160-
meta.Old = c.options.OldObject
161-
meta.New = c.options.NewObject
167+
// Create new instances for each document to avoid reusing the same pointers
168+
if c.options.OldObject != nil {
169+
oldObjectType := reflect.TypeOf(c.options.OldObject).Elem()
170+
meta.Old = reflect.New(oldObjectType).Interface()
171+
}
172+
if c.options.NewObject != nil {
173+
newObjectType := reflect.TypeOf(c.options.NewObject).Elem()
174+
meta.New = reflect.New(newObjectType).Interface()
175+
}
162176
}
163177

164178
c.response.DocumentMetaWithOldRev = &meta.DocumentMetaWithOldRev
@@ -171,9 +185,24 @@ func (c *collectionDocumentUpdateResponseReader) Read() (CollectionDocumentUpdat
171185
return CollectionDocumentUpdateResponse{}, err
172186
}
173187

188+
// Update meta with the unmarshaled data
189+
meta.DocumentMetaWithOldRev = *c.response.DocumentMetaWithOldRev
190+
meta.ResponseStruct = *c.response.ResponseStruct
191+
meta.Old = c.response.Old
192+
meta.New = c.response.New
193+
174194
if meta.Error != nil && *meta.Error {
175195
return meta, meta.AsArangoError()
176196
}
177197

178198
return meta, nil
179199
}
200+
201+
// Len returns the number of items in the response
202+
func (c *collectionDocumentUpdateResponseReader) Len() int {
203+
if !c.cached {
204+
c.cachedResults, c.cachedErrors = c.ReadAll()
205+
c.cached = true
206+
}
207+
return len(c.cachedResults)
208+
}

0 commit comments

Comments
 (0)