@@ -132,6 +132,7 @@ type Col struct {
132
132
db * DB
133
133
Collection
134
134
}
135
+
135
136
func (c * Col ) ctx (ctx context.Context ) context.Context {
136
137
return context .WithValue (ctx , collKey {}, string (c .Collection .Id ))
137
138
}
@@ -147,6 +148,10 @@ func (c *Col) CreateDocument(ctx context.Context, doc interface{}) error {
147
148
return c .db .c .CreateDocument (c .ctx (ctx ), c .Self , doc )
148
149
}
149
150
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
+
150
155
func (c * Col ) UpsertDocument (ctx context.Context , doc interface {}, etag string ) error {
151
156
return c .db .c .UpsertDocument (c .ctx (ctx ), c .Self , doc , etag )
152
157
}
@@ -370,6 +375,36 @@ func (c *DocumentDB) CreateDocument(ctx context.Context, coll string, doc interf
370
375
return c .createDocument (ctx , coll , doc , nil )
371
376
}
372
377
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
+
373
408
// Create document
374
409
func (c * DocumentDB ) UpsertDocument (ctx context.Context , coll string , doc interface {}, etag string ) error {
375
410
headers := map [string ]string {
@@ -413,21 +448,21 @@ func (c *DocumentDB) DeleteUserDefinedFunction(ctx context.Context, link string)
413
448
414
449
// Replace database
415
450
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 )
417
452
if err != nil {
418
453
return nil , err
419
454
}
420
455
return
421
456
}
422
457
423
458
// 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 )
426
461
}
427
462
428
463
// Replace stored procedure
429
464
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 )
431
466
if err != nil {
432
467
return nil , err
433
468
}
@@ -436,7 +471,7 @@ func (c *DocumentDB) ReplaceStoredProcedure(ctx context.Context, link string, bo
436
471
437
472
// Replace stored procedure
438
473
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 )
440
475
if err != nil {
441
476
return nil , err
442
477
}
0 commit comments