Skip to content

Commit 7f9db47

Browse files
chessmandennwc
authored andcommitted
add update document (#4)
1 parent 027783a commit 7f9db47

File tree

4 files changed

+53
-16
lines changed

4 files changed

+53
-16
lines changed

client.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ type Clienter interface {
6969
Delete(ctx context.Context, link string, headers map[string] string) error
7070
Query(ctx context.Context, link string, qu *Query, ret interface{}) (token string, err error)
7171
Create(ctx context.Context, link string, body, ret interface{}, headers map[string]string) error
72-
Replace(ctx context.Context, link string, body, ret interface{}) error
72+
Replace(ctx context.Context, link string, body, ret interface{}, headers map[string]string) error
7373
Execute(ctx context.Context, link string, body, ret interface{}) error
7474
}
7575

@@ -134,13 +134,13 @@ func (c *Client) Create(ctx context.Context, link string, body, ret interface{},
134134
}
135135

136136
// Replace resource
137-
func (c *Client) Replace(ctx context.Context, link string, body, ret interface{}) error {
137+
func (c *Client) Replace(ctx context.Context, link string, body, ret interface{}, headers map[string]string) error {
138138
data, err := stringify(body)
139139
if err != nil {
140140
return err
141141
}
142142
buf := bytes.NewBuffer(data)
143-
_, err = c.method(ctx, "PUT", link, ret, buf, nil)
143+
_, err = c.method(ctx, "PUT", link, ret, buf, headers)
144144
return err
145145
}
146146

client_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package documentdb
33
import (
44
"context"
55
"fmt"
6-
"github.com/stretchr/testify/assert"
76
"io/ioutil"
87
"net/http"
98
"net/http/httptest"
109
"testing"
10+
11+
"github.com/stretchr/testify/assert"
1112
)
1213

1314
// I more interested in the request, instead of the response
@@ -161,21 +162,21 @@ func TestReplace(t *testing.T) {
161162

162163
// First call
163164
var db Database
164-
err := client.Replace(ctx, "dbs", `{"id": 3}`, &db)
165+
err := client.Replace(ctx, "dbs", `{"id": 3}`, &db, nil)
165166
s.AssertHeaders(t, HEADER_XDATE, HEADER_AUTH, HEADER_VER)
166167
assert.Equal(db.Colls, "colls", "Should fill the fields from response body")
167168
assert.Nil(err, "err should be nil")
168169

169170
// Second call
170171
var doc, tDoc Document
171172
tDoc.Id = "9"
172-
err = client.Replace(ctx, "dbs", tDoc, &doc)
173+
err = client.Replace(ctx, "dbs", tDoc, &doc, nil)
173174
s.AssertHeaders(t, HEADER_XDATE, HEADER_AUTH, HEADER_VER)
174175
assert.Equal(doc.Id, "9", "Should fill the fields from response body")
175176
assert.Nil(err, "err should be nil")
176177

177178
// Last Call, when StatusCode != StatusOK && StatusCreated
178-
err = client.Replace(ctx, "dbs", tDoc, &doc)
179+
err = client.Replace(ctx, "dbs", tDoc, &doc, nil)
179180
assert.Equal(err.Error(), "500, DocumentDB error")
180181
}
181182

documentdb.go

+40-5
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ type Col struct {
132132
db *DB
133133
Collection
134134
}
135+
135136
func (c *Col) ctx(ctx context.Context) context.Context {
136137
return context.WithValue(ctx, collKey{}, string(c.Collection.Id))
137138
}
@@ -147,6 +148,10 @@ func (c *Col) CreateDocument(ctx context.Context, doc interface{}) error {
147148
return c.db.c.CreateDocument(c.ctx(ctx), c.Self, doc)
148149
}
149150

151+
func (c *Col) UpdateDocument(ctx context.Context, doc interface{}, etag string) error {
152+
return c.db.c.UpdateDocument(c.ctx(ctx), c.Self, doc, etag)
153+
}
154+
150155
func (c *Col) UpsertDocument(ctx context.Context, doc interface{}, etag string) error {
151156
return c.db.c.UpsertDocument(c.ctx(ctx), c.Self, doc, etag)
152157
}
@@ -370,6 +375,36 @@ func (c *DocumentDB) CreateDocument(ctx context.Context, coll string, doc interf
370375
return c.createDocument(ctx, coll, doc, nil)
371376
}
372377

378+
func (c *DocumentDB) UpdateDocument(ctx context.Context, coll string, doc interface{}, etag string) error {
379+
rv := reflect.ValueOf(doc)
380+
if rv.Kind() == reflect.Ptr {
381+
rv = rv.Elem()
382+
}
383+
384+
id := rv.FieldByName("Id")
385+
if !id.IsValid() || id.String() == "" {
386+
id = rv.FieldByName("ID")
387+
if !id.IsValid() || id.String() == "" {
388+
return errors.New("document doesn't have id")
389+
}
390+
}
391+
392+
var docs []Document
393+
_, err := c.QueryDocuments(ctx, coll, selectByID(id.String()), &docs)
394+
if err != nil {
395+
return err
396+
}
397+
if len(docs) == 0 {
398+
return ErrNotFound
399+
}
400+
401+
headers := make(map[string]string)
402+
if etag != "" {
403+
headers[HEADER_IF_MATCH] = etag
404+
}
405+
return c.ReplaceDocument(ctx, coll+"docs/"+docs[0].Id, doc, headers)
406+
}
407+
373408
// Create document
374409
func (c *DocumentDB) UpsertDocument(ctx context.Context, coll string, doc interface{}, etag string) error {
375410
headers := map[string]string{
@@ -413,21 +448,21 @@ func (c *DocumentDB) DeleteUserDefinedFunction(ctx context.Context, link string)
413448

414449
// Replace database
415450
func (c *DocumentDB) ReplaceDatabase(ctx context.Context, link string, body interface{}) (db *Database, err error) {
416-
err = c.client.Replace(ctx, link, body, &db)
451+
err = c.client.Replace(ctx, link, body, &db, nil)
417452
if err != nil {
418453
return nil, err
419454
}
420455
return
421456
}
422457

423458
// Replace document
424-
func (c *DocumentDB) ReplaceDocument(ctx context.Context, link string, doc interface{}) error {
425-
return c.client.Replace(ctx, link, doc, &doc)
459+
func (c *DocumentDB) ReplaceDocument(ctx context.Context, link string, doc interface{}, headers map[string]string) error {
460+
return c.client.Replace(ctx, link, doc, &doc, headers)
426461
}
427462

428463
// Replace stored procedure
429464
func (c *DocumentDB) ReplaceStoredProcedure(ctx context.Context, link string, body interface{}) (sproc *Sproc, err error) {
430-
err = c.client.Replace(ctx, link, body, &sproc)
465+
err = c.client.Replace(ctx, link, body, &sproc, nil)
431466
if err != nil {
432467
return nil, err
433468
}
@@ -436,7 +471,7 @@ func (c *DocumentDB) ReplaceStoredProcedure(ctx context.Context, link string, bo
436471

437472
// Replace stored procedure
438473
func (c *DocumentDB) ReplaceUserDefinedFunction(ctx context.Context, link string, body interface{}) (udf *UDF, err error) {
439-
err = c.client.Replace(ctx, link, body, &udf)
474+
err = c.client.Replace(ctx, link, body, &udf, nil)
440475
if err != nil {
441476
return nil, err
442477
}

documentdb_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package documentdb
22

33
import (
44
"context"
5+
"testing"
6+
57
"github.com/stretchr/testify/assert"
68
"github.com/stretchr/testify/mock"
7-
"testing"
89
)
910

1011
type ClientStub struct {
@@ -21,12 +22,12 @@ func (c *ClientStub) Create(ctx context.Context, link string, body, ret interfac
2122
return nil
2223
}
2324

24-
func (c *ClientStub) Delete(ctx context.Context, link string, headers map[string] string) error {
25+
func (c *ClientStub) Delete(ctx context.Context, link string, headers map[string]string) error {
2526
c.Called(link)
2627
return nil
2728
}
2829

29-
func (c *ClientStub) Replace(ctx context.Context, link string, body, ret interface{}) error {
30+
func (c *ClientStub) Replace(ctx context.Context, link string, body, ret interface{}, headers map[string]string) error {
3031
c.Called(link, body)
3132
return nil
3233
}
@@ -279,7 +280,7 @@ func TestReplaceDocument(t *testing.T) {
279280
c := &DocumentDB{client}
280281
client.On("Replace", "doc_link", "{}").Return(nil)
281282
ctx := context.Background()
282-
c.ReplaceDocument(ctx, "doc_link", "{}")
283+
c.ReplaceDocument(ctx, "doc_link", "{}", nil)
283284
client.AssertCalled(t, "Replace", "doc_link", "{}")
284285
}
285286

0 commit comments

Comments
 (0)