Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return new documents from UpdateDocumentsWithOptions #633

Open
thomaskrut opened this issue Nov 6, 2024 · 2 comments
Open

Return new documents from UpdateDocumentsWithOptions #633

thomaskrut opened this issue Nov 6, 2024 · 2 comments

Comments

@thomaskrut
Copy link

thomaskrut commented Nov 6, 2024

I'm having trouble returning the created documents from UpdateDocumentsWithOptions(). I also noticed there are no tests for this method, only for the single document update.

This works fine (updating a single document), the updated document is returned from the function:

func (c *Client[T]) Update(ctx context.Context, item T, key string) (T, error) {
	var updated T
	opts := &arangodb.CollectionDocumentUpdateOptions{
		NewObject: &updated,
	}

	if _, err := dbc.collection.UpdateDocumentWithOptions(ctx, key, item, opts); err != nil {
		return nil, err
	}
	return updated, nil
}

This however returns an empty slice:

func (dbc *Client[T]) UpdateMany(ctx context.Context, items []T) ([]T, error) {
	var updated []T

	opts := &arangodb.CollectionDocumentUpdateOptions{
		NewObject: &updated,
	}

	if _, err := dbc.collection.UpdateDocumentsWithOptions(ctx, items, opts); err != nil {
		return updated, dbc.handleError(ctx, "update document", err)
	}

	return updated, nil
}

Manual checks confirm that the documents are properly updated in the database. I tried calling Read()on the response object but got an error message:
json: cannot unmarshal object into Go struct field .new of type []*<myType>

@jwierzbo
Copy link
Collaborator

@thomaskrut This method is tested here with 512 docs (Read method is verified as well):

r, err := col.UpdateDocumentsWithOptions(ctx, newDocs, &arangodb.CollectionDocumentUpdateOptions{
OldObject: &old,
NewObject: &new,
})
require.NoError(t, err)
for {
meta, err := r.Read()

The problem is probably with the generics and your Client structure config. Could you share more code?

@thomaskrut
Copy link
Author

thomaskrut commented Nov 15, 2024

Thanks for pointing out the test for me. I realized that I should not be passing a slice as NewObject on the options struct. If I instead pass an instance of T, I do get the updated document by reading that variable for each iteration on meta.Read().

However this approach does not easily let me append these documents to a slice and return them from the function, as the slice will end up with pointers to the last document read. Also, doing type assertion on meta.New and appending those objects yields the same result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants