Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions v2/arangodb/collection_documents_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ type CollectionDocumentCreate interface {
}

type CollectionDocumentCreateResponseReader interface {
shared.ReadAllReadable[CollectionDocumentCreateResponse]
Read() (CollectionDocumentCreateResponse, error)
Len() int
}

type CollectionDocumentCreateResponse struct {
Expand Down
22 changes: 22 additions & 0 deletions v2/arangodb/collection_documents_create_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func newCollectionDocumentCreateResponseReader(array *connection.Array, options
c.response.New = newUnmarshalInto(c.options.NewObject)
}

c.ReadAllReader = shared.ReadAllReader[CollectionDocumentCreateResponse, *collectionDocumentCreateResponseReader]{Reader: c}
return c
}

Expand All @@ -147,6 +148,12 @@ type collectionDocumentCreateResponseReader struct {
Old *UnmarshalInto `json:"old,omitempty"`
New *UnmarshalInto `json:"new,omitempty"`
}
shared.ReadAllReader[CollectionDocumentCreateResponse, *collectionDocumentCreateResponseReader]

// Cache for len() method
cachedResults []CollectionDocumentCreateResponse
cachedErrors []error
cached bool
}

func (c *collectionDocumentCreateResponseReader) Read() (CollectionDocumentCreateResponse, error) {
Expand All @@ -171,9 +178,24 @@ func (c *collectionDocumentCreateResponseReader) Read() (CollectionDocumentCreat
return CollectionDocumentCreateResponse{}, err
}

// Update meta with the unmarshaled data
meta.DocumentMeta = *c.response.DocumentMeta
meta.ResponseStruct = *c.response.ResponseStruct
meta.Old = c.response.Old
meta.New = c.response.New

if meta.Error != nil && *meta.Error {
return meta, meta.AsArangoError()
}

return meta, nil
}

// Len returns the number of items in the response
func (c *collectionDocumentCreateResponseReader) Len() int {
if !c.cached {
c.cachedResults, c.cachedErrors = c.ReadAll()
c.cached = true
}
return len(c.cachedResults)
}
3 changes: 3 additions & 0 deletions v2/arangodb/collection_documents_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ type CollectionDocumentDeleteResponse struct {
}

type CollectionDocumentDeleteResponseReader interface {
shared.ReadAllIntoReadable[CollectionDocumentDeleteResponse]
Read(i interface{}) (CollectionDocumentDeleteResponse, error)
Len() int
}

type CollectionDocumentDeleteOptions struct {
Expand All @@ -82,6 +84,7 @@ type CollectionDocumentDeleteOptions struct {
WithWaitForSync *bool

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

// If set to true, an empty object is returned as response if the document operation succeeds.
Expand Down
29 changes: 28 additions & 1 deletion v2/arangodb/collection_documents_delete_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"context"
"io"
"net/http"
"reflect"

"github.com/pkg/errors"

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

type collectionDocumentDelete struct {
collection *collection
shared.ReadAllIntoReader[CollectionDocumentDeleteResponse, *collectionDocumentDeleteResponseReader]
}

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

c.ReadAllIntoReader = shared.ReadAllIntoReader[CollectionDocumentDeleteResponse, *collectionDocumentDeleteResponseReader]{Reader: c}
return c
}

Expand All @@ -111,6 +114,11 @@ var _ CollectionDocumentDeleteResponseReader = &collectionDocumentDeleteResponse
type collectionDocumentDeleteResponseReader struct {
array *connection.Array
options *CollectionDocumentDeleteOptions
shared.ReadAllIntoReader[CollectionDocumentDeleteResponse, *collectionDocumentDeleteResponseReader]
// Cache for len() method
cachedResults []CollectionDocumentDeleteResponse
cachedErrors []error
cached bool
}

func (c *collectionDocumentDeleteResponseReader) Read(i interface{}) (CollectionDocumentDeleteResponse, error) {
Expand Down Expand Up @@ -146,11 +154,30 @@ func (c *collectionDocumentDeleteResponseReader) Read(i interface{}) (Collection
}

if c.options != nil && c.options.OldObject != nil {
meta.Old = c.options.OldObject
// Create a new instance for each document to avoid reusing the same pointer
oldObjectType := reflect.TypeOf(c.options.OldObject).Elem()
meta.Old = reflect.New(oldObjectType).Interface()

// Extract old data into the new instance
if err := response.Object.Object.Extract("old").Inject(meta.Old); err != nil {
return CollectionDocumentDeleteResponse{}, err
}

// Copy data from the new instance to the original OldObject for backward compatibility
oldValue := reflect.ValueOf(meta.Old).Elem()
originalValue := reflect.ValueOf(c.options.OldObject).Elem()
originalValue.Set(oldValue)
}

return meta, nil
}

// Len returns the number of items in the response
func (c *collectionDocumentDeleteResponseReader) Len() int {
if !c.cached {
var dummySlice []interface{}
c.cachedResults, c.cachedErrors = c.ReadAll(&dummySlice)
c.cached = true
}
return len(c.cachedResults)
}
2 changes: 2 additions & 0 deletions v2/arangodb/collection_documents_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ type CollectionDocumentRead interface {

type CollectionDocumentReadResponseReader interface {
Read(i interface{}) (CollectionDocumentReadResponse, error)
shared.ReadAllIntoReadable[CollectionDocumentReadResponse]
Len() int
}

type CollectionDocumentReadResponse struct {
Expand Down
17 changes: 16 additions & 1 deletion v2/arangodb/collection_documents_read_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (c collectionDocumentRead) ReadDocumentWithOptions(ctx context.Context, key

func newCollectionDocumentReadResponseReader(array *connection.Array, options *CollectionDocumentReadOptions) *collectionDocumentReadResponseReader {
c := &collectionDocumentReadResponseReader{array: array, options: options}

c.ReadAllIntoReader = shared.ReadAllIntoReader[CollectionDocumentReadResponse, *collectionDocumentReadResponseReader]{Reader: c}
return c
}

Expand All @@ -106,6 +106,11 @@ var _ CollectionDocumentReadResponseReader = &collectionDocumentReadResponseRead
type collectionDocumentReadResponseReader struct {
array *connection.Array
options *CollectionDocumentReadOptions
shared.ReadAllIntoReader[CollectionDocumentReadResponse, *collectionDocumentReadResponseReader]
// Cache for len() method
cachedResults []CollectionDocumentReadResponse
cachedErrors []error
cached bool
}

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

return meta, nil
}

// Len returns the number of items in the response
func (c *collectionDocumentReadResponseReader) Len() int {
if !c.cached {
var dummySlice []interface{}
c.cachedResults, c.cachedErrors = c.ReadAll(&dummySlice)
c.cached = true
}
return len(c.cachedResults)
}
2 changes: 2 additions & 0 deletions v2/arangodb/collection_documents_replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ type CollectionDocumentReplace interface {
}

type CollectionDocumentReplaceResponseReader interface {
shared.ReadAllReadable[CollectionDocumentReplaceResponse]
Read() (CollectionDocumentReplaceResponse, error)
Len() int
}

type CollectionDocumentReplaceResponse struct {
Expand Down
35 changes: 32 additions & 3 deletions v2/arangodb/collection_documents_replace_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"context"
"io"
"net/http"
"reflect"

"github.com/pkg/errors"

Expand Down Expand Up @@ -132,7 +133,7 @@ func newCollectionDocumentReplaceResponseReader(array *connection.Array, options
c.response.Old = newUnmarshalInto(c.options.OldObject)
c.response.New = newUnmarshalInto(c.options.NewObject)
}

c.ReadAllReader = shared.ReadAllReader[CollectionDocumentReplaceResponse, *collectionDocumentReplaceResponseReader]{Reader: c}
return c
}

Expand All @@ -147,6 +148,12 @@ type collectionDocumentReplaceResponseReader struct {
Old *UnmarshalInto `json:"old,omitempty"`
New *UnmarshalInto `json:"new,omitempty"`
}
shared.ReadAllReader[CollectionDocumentReplaceResponse, *collectionDocumentReplaceResponseReader]

// Cache for len() method
cachedResults []CollectionDocumentReplaceResponse
cachedErrors []error
cached bool
}

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

if c.options != nil {
meta.Old = c.options.OldObject
meta.New = c.options.NewObject
// Create new instances for each document to avoid reusing the same pointers
if c.options.OldObject != nil {
oldObjectType := reflect.TypeOf(c.options.OldObject).Elem()
meta.Old = reflect.New(oldObjectType).Interface()
}
if c.options.NewObject != nil {
newObjectType := reflect.TypeOf(c.options.NewObject).Elem()
meta.New = reflect.New(newObjectType).Interface()
}
}

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

// Update meta with the unmarshaled data
meta.DocumentMetaWithOldRev = *c.response.DocumentMetaWithOldRev
meta.ResponseStruct = *c.response.ResponseStruct
meta.Old = c.response.Old
meta.New = c.response.New

if meta.Error != nil && *meta.Error {
return meta, meta.AsArangoError()
}

return meta, nil
}

// Len returns the number of items in the response
func (c *collectionDocumentReplaceResponseReader) Len() int {
if !c.cached {
c.cachedResults, c.cachedErrors = c.ReadAll()
c.cached = true
}
return len(c.cachedResults)
}
2 changes: 2 additions & 0 deletions v2/arangodb/collection_documents_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ type CollectionDocumentUpdate interface {
}

type CollectionDocumentUpdateResponseReader interface {
shared.ReadAllReadable[CollectionDocumentUpdateResponse]
Read() (CollectionDocumentUpdateResponse, error)
Len() int
}

type CollectionDocumentUpdateResponse struct {
Expand Down
35 changes: 32 additions & 3 deletions v2/arangodb/collection_documents_update_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"context"
"io"
"net/http"
"reflect"

"github.com/pkg/errors"

Expand Down Expand Up @@ -132,7 +133,7 @@ func newCollectionDocumentUpdateResponseReader(array *connection.Array, options
c.response.Old = newUnmarshalInto(c.options.OldObject)
c.response.New = newUnmarshalInto(c.options.NewObject)
}

c.ReadAllReader = shared.ReadAllReader[CollectionDocumentUpdateResponse, *collectionDocumentUpdateResponseReader]{Reader: c}
return c
}

Expand All @@ -147,6 +148,12 @@ type collectionDocumentUpdateResponseReader struct {
Old *UnmarshalInto `json:"old,omitempty"`
New *UnmarshalInto `json:"new,omitempty"`
}
shared.ReadAllReader[CollectionDocumentUpdateResponse, *collectionDocumentUpdateResponseReader]

// Cache for len() method
cachedResults []CollectionDocumentUpdateResponse
cachedErrors []error
cached bool
}

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

if c.options != nil {
meta.Old = c.options.OldObject
meta.New = c.options.NewObject
// Create new instances for each document to avoid reusing the same pointers
if c.options.OldObject != nil {
oldObjectType := reflect.TypeOf(c.options.OldObject).Elem()
meta.Old = reflect.New(oldObjectType).Interface()
}
if c.options.NewObject != nil {
newObjectType := reflect.TypeOf(c.options.NewObject).Elem()
meta.New = reflect.New(newObjectType).Interface()
}
}

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

// Update meta with the unmarshaled data
meta.DocumentMetaWithOldRev = *c.response.DocumentMetaWithOldRev
meta.ResponseStruct = *c.response.ResponseStruct
meta.Old = c.response.Old
meta.New = c.response.New

if meta.Error != nil && *meta.Error {
return meta, meta.AsArangoError()
}

return meta, nil
}

// Len returns the number of items in the response
func (c *collectionDocumentUpdateResponseReader) Len() int {
if !c.cached {
c.cachedResults, c.cachedErrors = c.ReadAll()
c.cached = true
}
return len(c.cachedResults)
}
Loading