Skip to content

Commit

Permalink
fix: decoding/encoding of document iface usage
Browse files Browse the repository at this point in the history
This commit fixes decoding/encoding of functions that power the
document interfaces.

Signed-off-by: Victor Adossi <vadossi@cosmonic.com>
  • Loading branch information
vados-cosmonic committed Nov 25, 2024
1 parent 9287b43 commit 945cb40
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
9 changes: 5 additions & 4 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ func GetAllReplicaOptions(o *document.DocumentGetAllReplicaOptions) *gocb.GetAll
// GetOptions
func GetOptions(o *document.DocumentGetOptions) *gocb.GetOptions {
if o == nil {
return &gocb.GetOptions{Transcoder: gocb.NewRawJSONTranscoder()}
return &gocb.GetOptions{Transcoder: gocb.NewRawStringTranscoder()}
}
return &gocb.GetOptions{
Transcoder: gocb.NewRawJSONTranscoder(),
Transcoder: gocb.NewRawStringTranscoder(),
WithExpiry: o.WithExpiry,
Project: o.Project,
// ...
Expand Down Expand Up @@ -67,7 +67,7 @@ func GetAnyReplicaOptions(o *document.DocumentGetAnyReplicaOptions) *gocb.GetAny
// InsertOptions
func InsertOptions(o *document.DocumentInsertOptions) *gocb.InsertOptions {
if o == nil {
return &gocb.InsertOptions{Transcoder: gocb.NewRawJSONTranscoder()}
return &gocb.InsertOptions{Transcoder: gocb.NewRawStringTranscoder()}
}
return &gocb.InsertOptions{
Timeout: time.Duration(*o.TimeoutNs),
Expand All @@ -92,6 +92,7 @@ func ReplaceOptions(o *document.DocumentReplaceOptions) *gocb.ReplaceOptions {
}
return &gocb.ReplaceOptions{
Timeout: time.Duration(*o.TimeoutNs),
Transcoder: gocb.NewRawStringTranscoder(),
}
}

Expand All @@ -118,7 +119,7 @@ func UnlockOptions(o *document.DocumentUnlockOptions) *gocb.UnlockOptions {
// UpsertOptions
func UpsertOptions(o *document.DocumentUpsertOptions) *gocb.UpsertOptions {
if o == nil {
return &gocb.UpsertOptions{Transcoder: gocb.NewRawJSONTranscoder()}
return &gocb.UpsertOptions{Transcoder: gocb.NewRawStringTranscoder()}
}
return &gocb.UpsertOptions{}
}
15 changes: 13 additions & 2 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ func (h *Handler) Insert(ctx context.Context, id string, doc *types.Document, op
result, err := collection.Insert(id, docToInsert, InsertOptions(options))
if err != nil {
h.Logger.Error("Error inserting document", "error", err)
return Err[types.MutationMetadata](*types.NewDocumentErrorInvalidValue()), err
if errors.Is(err, gocb.ErrDocumentExists) {
return Err[types.MutationMetadata](*types.NewDocumentErrorAlreadyExists()), nil
} else {
return Err[types.MutationMetadata](*types.NewDocumentErrorInvalidValue()), nil
}
}
return Ok(MutationMetadata(result)), nil
}
Expand All @@ -168,7 +172,14 @@ func (h *Handler) Replace(ctx context.Context, id string, doc *types.Document, o
h.Logger.Error("Error fetching collection from context", "error", err)
return nil, err
}
result, err := collection.Replace(id, doc, ReplaceOptions(options))

replacement, ok := doc.GetRaw()
if !ok {
h.Logger.Error("Error getting raw document", "doc", doc)
return Err[types.MutationMetadata](*types.NewDocumentErrorNotJson()), nil
}

result, err := collection.Replace(id, replacement, ReplaceOptions(options))
if err != nil {
h.Logger.Error("Error replacing document", "error", err)
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion results.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func GetResult(result *gocb.GetResult) (document.DocumentGetResult, error) {
}
expiresInNs := uint64(result.ExpiryTime().Nanosecond())
returnDoc := document.Document{}
returnDoc.SetRaw(content)
returnDoc.SetRaw(string(content))
return document.DocumentGetResult{
Document: &returnDoc,
ExpiresInNs: &expiresInNs,
Expand Down

0 comments on commit 945cb40

Please sign in to comment.