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

fix return value for document controller #171

Merged
merged 1 commit into from
Jul 4, 2018
Merged
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
18 changes: 6 additions & 12 deletions document/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ import (
// If the same document already exists:
// - resolves with an error if set to "error".
// - replaces the existing document if set to "replace"
func (d *Document) Create(index string, collection string, id string, body json.RawMessage, options types.QueryOptions) (string, error) {
func (d *Document) Create(index string, collection string, id string, body json.RawMessage, options types.QueryOptions) (json.RawMessage, error) {
if index == "" {
return "", types.NewError("Document.Create: index required", 400)
return nil, types.NewError("Document.Create: index required", 400)
}

if collection == "" {
return "", types.NewError("Document.Create: collection required", 400)
return nil, types.NewError("Document.Create: collection required", 400)
}

if body == nil {
return "", types.NewError("Document.Create: body required", 400)
return nil, types.NewError("Document.Create: body required", 400)
}

ch := make(chan *types.KuzzleResponse)
Expand All @@ -57,14 +57,8 @@ func (d *Document) Create(index string, collection string, id string, body json.
res := <-ch

if res.Error.Error() != "" {
return "", res.Error
return nil, res.Error
}

type response struct {
Created string `json:"_id"`
}
var created response
json.Unmarshal(res.Result, &created)

return created.Created, nil
return res.Result, nil
}
17 changes: 7 additions & 10 deletions document/createOrReplace.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ import (
// If the same document already exists:
// - resolves with an error if set to "error".
// - replaces the existing document if set to "replace"
func (d *Document) CreateOrReplace(index string, collection string, _id string, body json.RawMessage, options types.QueryOptions) (string, error) {
func (d *Document) CreateOrReplace(index string, collection string, _id string, body json.RawMessage, options types.QueryOptions) (json.RawMessage, error) {
if index == "" {
return "", types.NewError("Document.CreateOrReplace: index required", 400)
return nil, types.NewError("Document.CreateOrReplace: index required", 400)
}

if collection == "" {
return "", types.NewError("Document.CreateOrReplace: collection required", 400)
return nil, types.NewError("Document.CreateOrReplace: collection required", 400)
}

if _id == "" {
return "", types.NewError("Document.CreateOrReplace: id required", 400)
return nil, types.NewError("Document.CreateOrReplace: id required", 400)
}

if body == nil {
return "", types.NewError("Document.CreateOrReplace: body required", 400)
return nil, types.NewError("Document.CreateOrReplace: body required", 400)
}

ch := make(chan *types.KuzzleResponse)
Expand All @@ -61,11 +61,8 @@ func (d *Document) CreateOrReplace(index string, collection string, _id string,
res := <-ch

if res.Error.Error() != "" {
return "", res.Error
return nil, res.Error
}

var created string
json.Unmarshal(res.Result, &created)

return created, nil
return res.Result, nil
}
7 changes: 5 additions & 2 deletions document/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ func (d *Document) Delete(index string, collection string, _id string, options t
return "", res.Error
}

var deleted string
type r struct {
Id string `json:"_id"`
}
var deleted r
json.Unmarshal(res.Result, &deleted)

return deleted, nil
return deleted.Id, nil
}
19 changes: 11 additions & 8 deletions document/mCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ import (
)

// MCreate creates the provided documents.
func (d *Document) MCreate(index string, collection string, body json.RawMessage, options types.QueryOptions) (string, error) {
func (d *Document) MCreate(index string, collection string, body json.RawMessage, options types.QueryOptions) (json.RawMessage, error) {
if index == "" {
return "", types.NewError("Document.MCreate: index required", 400)
return nil, types.NewError("Document.MCreate: index required", 400)
}

if collection == "" {
return "", types.NewError("Document.MCreate: collection required", 400)
return nil, types.NewError("Document.MCreate: collection required", 400)
}

if body == nil {
return "", types.NewError("Document.MCreate: body required", 400)
return nil, types.NewError("Document.MCreate: body required", 400)
}

ch := make(chan *types.KuzzleResponse)
Expand All @@ -49,11 +49,14 @@ func (d *Document) MCreate(index string, collection string, body json.RawMessage
res := <-ch

if res.Error.Error() != "" {
return "", res.Error
return nil, res.Error
}

var mCreated string
json.Unmarshal(res.Result, &mCreated)
type r struct {
Hits json.RawMessage `json:"hits"`
}
var docs r
json.Unmarshal(res.Result, &docs)

return mCreated, nil
return docs.Hits, nil
}
19 changes: 11 additions & 8 deletions document/mCreateOrReplace.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ import (
)

// MCreateOrReplace creates or replaces the provided documents.
func (d *Document) MCreateOrReplace(index string, collection string, body json.RawMessage, options types.QueryOptions) (string, error) {
func (d *Document) MCreateOrReplace(index string, collection string, body json.RawMessage, options types.QueryOptions) (json.RawMessage, error) {
if index == "" {
return "", types.NewError("Document.MCreateOrReplace: index required", 400)
return nil, types.NewError("Document.MCreateOrReplace: index required", 400)
}

if collection == "" {
return "", types.NewError("Document.MCreateOrReplace: collection required", 400)
return nil, types.NewError("Document.MCreateOrReplace: collection required", 400)
}

if body == nil {
return "", types.NewError("Document.MCreateOrReplace: body required", 400)
return nil, types.NewError("Document.MCreateOrReplace: body required", 400)
}

ch := make(chan *types.KuzzleResponse)
Expand All @@ -49,11 +49,14 @@ func (d *Document) MCreateOrReplace(index string, collection string, body json.R
res := <-ch

if res.Error.Error() != "" {
return "", res.Error
return nil, res.Error
}

var mCreated string
json.Unmarshal(res.Result, &mCreated)
type r struct {
Hits json.RawMessage `json:"hits"`
}
var docs r
json.Unmarshal(res.Result, &docs)

return mCreated, nil
return docs.Hits, nil
}
17 changes: 10 additions & 7 deletions document/mGet.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ import (
)

// MGet fetches multiple documents at once
func (d *Document) MGet(index string, collection string, ids []string, includeTrash bool, options types.QueryOptions) (string, error) {
func (d *Document) MGet(index string, collection string, ids []string, includeTrash bool, options types.QueryOptions) (json.RawMessage, error) {
if index == "" {
return "", types.NewError("Document.MGet: index required", 400)
return nil, types.NewError("Document.MGet: index required", 400)
}

if collection == "" {
return "", types.NewError("Document.MGet: collection required", 400)
return nil, types.NewError("Document.MGet: collection required", 400)
}

if len(ids) == 0 {
return "", types.NewError("Document.MGet: ids filled array required", 400)
return nil, types.NewError("Document.MGet: ids filled array required", 400)
}

ch := make(chan *types.KuzzleResponse)
Expand All @@ -54,11 +54,14 @@ func (d *Document) MGet(index string, collection string, ids []string, includeTr
res := <-ch

if res.Error.Error() != "" {
return "", res.Error
return nil, res.Error
}

var docs string
type r struct {
Hits json.RawMessage `json:"hits"`
}
var docs r
json.Unmarshal(res.Result, &docs)

return docs, nil
return docs.Hits, nil
}
19 changes: 11 additions & 8 deletions document/mReplace.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ import (
)

// MReplace replaces multiple documents at once.
func (d *Document) MReplace(index string, collection string, body json.RawMessage, options types.QueryOptions) (string, error) {
func (d *Document) MReplace(index string, collection string, body json.RawMessage, options types.QueryOptions) (json.RawMessage, error) {
if index == "" {
return "", types.NewError("Document.MReplace: index required", 400)
return nil, types.NewError("Document.MReplace: index required", 400)
}

if collection == "" {
return "", types.NewError("Document.MReplace: collection required", 400)
return nil, types.NewError("Document.MReplace: collection required", 400)
}

if body == nil {
return "", types.NewError("Document.MReplace: body required", 400)
return nil, types.NewError("Document.MReplace: body required", 400)
}

ch := make(chan *types.KuzzleResponse)
Expand All @@ -49,11 +49,14 @@ func (d *Document) MReplace(index string, collection string, body json.RawMessag
res := <-ch

if res.Error.Error() != "" {
return "", res.Error
return nil, res.Error
}

var mReplaced string
json.Unmarshal(res.Result, &mReplaced)
type r struct {
Hits json.RawMessage `json:"hits"`
}
var docs r
json.Unmarshal(res.Result, &docs)

return mReplaced, nil
return docs.Hits, nil
}
19 changes: 11 additions & 8 deletions document/mUpdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ import (
)

// MUpdate updates multiple documents at once
func (d *Document) MUpdate(index string, collection string, body json.RawMessage, options types.QueryOptions) (string, error) {
func (d *Document) MUpdate(index string, collection string, body json.RawMessage, options types.QueryOptions) (json.RawMessage, error) {
if index == "" {
return "", types.NewError("Document.MUpdate: index required", 400)
return nil, types.NewError("Document.MUpdate: index required", 400)
}

if collection == "" {
return "", types.NewError("Document.MUpdate: collection required", 400)
return nil, types.NewError("Document.MUpdate: collection required", 400)
}

if body == nil {
return "", types.NewError("Document.MUpdate: body required", 400)
return nil, types.NewError("Document.MUpdate: body required", 400)
}

ch := make(chan *types.KuzzleResponse)
Expand All @@ -49,11 +49,14 @@ func (d *Document) MUpdate(index string, collection string, body json.RawMessage
res := <-ch

if res.Error.Error() != "" {
return "", res.Error
return nil, res.Error
}

var mUpdated string
json.Unmarshal(res.Result, &mUpdated)
type r struct {
Hits json.RawMessage `json:"hits"`
}
var docs r
json.Unmarshal(res.Result, &docs)

return mUpdated, nil
return docs.Hits, nil
}
17 changes: 7 additions & 10 deletions document/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ import (
)

// Replace replaces a document in Kuzzle.
func (d *Document) Replace(index string, collection string, _id string, body json.RawMessage, options types.QueryOptions) (string, error) {
func (d *Document) Replace(index string, collection string, _id string, body json.RawMessage, options types.QueryOptions) (json.RawMessage, error) {
if index == "" {
return "", types.NewError("Document.Replace: index required", 400)
return nil, types.NewError("Document.Replace: index required", 400)
}

if collection == "" {
return "", types.NewError("Document.Replace: collection required", 400)
return nil, types.NewError("Document.Replace: collection required", 400)
}

if _id == "" {
return "", types.NewError("Document.Replace: id required", 400)
return nil, types.NewError("Document.Replace: id required", 400)
}

if body == nil {
return "", types.NewError("Document.Replace: body required", 400)
return nil, types.NewError("Document.Replace: body required", 400)
}

ch := make(chan *types.KuzzleResponse)
Expand All @@ -54,11 +54,8 @@ func (d *Document) Replace(index string, collection string, _id string, body jso
res := <-ch

if res.Error.Error() != "" {
return "", res.Error
return nil, res.Error
}

var replaced string
json.Unmarshal(res.Result, &replaced)

return replaced, nil
return res.Result, nil
}
20 changes: 7 additions & 13 deletions document/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ import (
)

// Update updates a document in Kuzzle.
func (d *Document) Update(index string, collection string, id string, body json.RawMessage, options types.QueryOptions) (string, error) {
func (d *Document) Update(index string, collection string, id string, body json.RawMessage, options types.QueryOptions) (json.RawMessage, error) {
if id == "" {
return "", types.NewError("Document.update: id required", 400)
return nil, types.NewError("Document.update: id required", 400)
}

if index == "" {
return "", types.NewError("Document.Update: index required", 400)
return nil, types.NewError("Document.Update: index required", 400)
}

if collection == "" {
return "", types.NewError("Document.Update: collection required", 400)
return nil, types.NewError("Document.Update: collection required", 400)
}

if body == nil {
return "", types.NewError("Document.Update: body required", 400)
return nil, types.NewError("Document.Update: body required", 400)
}

ch := make(chan *types.KuzzleResponse)
Expand All @@ -54,14 +54,8 @@ func (d *Document) Update(index string, collection string, id string, body json.
res := <-ch

if res.Error.Error() != "" {
return "", res.Error
return nil, res.Error
}

type response struct {
Updated string `json:"_id"`
}
var updated response
json.Unmarshal(res.Result, &updated)

return updated.Updated, nil
return res.Result, nil
}
Loading