/
cosmos_patch_operations.go
110 lines (97 loc) · 3.06 KB
/
cosmos_patch_operations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package azcosmos
import (
"bytes"
"encoding/json"
"fmt"
)
// PatchOperationType defines supported values for operation types in Patch Document.
type patchOperationType string
const (
// Represents a patch operation Add.
patchOperationTypeAdd patchOperationType = "add"
// Represents a patch operation Replace.
patchOperationTypeReplace patchOperationType = "replace"
// Represents a patch operation Remove.
patchOperationTypeRemove patchOperationType = "remove"
// Represents a patch operation Set.
patchOperationTypeSet patchOperationType = "set"
// Represents a patch operation Increment.
patchOperationTypeIncrement patchOperationType = "incr"
)
// PatchOperation represents individual patch operation.
type patchOperation struct {
Op patchOperationType `json:"op"`
Path string `json:"path"`
Value any `json:"value,omitempty"`
}
// PatchOperations represents the patch request.
// See https://learn.microsoft.com/azure/cosmos-db/partial-document-update
type PatchOperations struct {
condition *string
operations []patchOperation
}
// MarshalJSON implements the json.Marshaler interface
func (o PatchOperations) MarshalJSON() ([]byte, error) {
buffer := bytes.NewBufferString("{")
if o.condition != nil {
buffer.WriteString(fmt.Sprintf("\"condition\":\"%s\",", *o.condition))
}
buffer.WriteString("\"operations\":[")
for i, operation := range o.operations {
if i > 0 {
buffer.WriteString(",")
}
operationBytes, err := json.Marshal(operation)
if err != nil {
return nil, err
}
buffer.Write(operationBytes)
}
buffer.WriteString("]}")
return buffer.Bytes(), nil
}
// SetCondition sets condition for the patch request.
func (p *PatchOperations) SetCondition(condition string) {
p.condition = &condition
}
// AppendReplace appends a replace operation to the patch request.
func (p *PatchOperations) AppendReplace(path string, value any) {
p.operations = append(p.operations, patchOperation{
Op: patchOperationTypeReplace,
Path: path,
Value: value,
})
}
// AppendAdd appends an add operation to the patch request.
func (p *PatchOperations) AppendAdd(path string, value any) {
p.operations = append(p.operations, patchOperation{
Op: patchOperationTypeAdd,
Path: path,
Value: value,
})
}
// AppendSet appends a set operation to the patch request.
func (p *PatchOperations) AppendSet(path string, value any) {
p.operations = append(p.operations, patchOperation{
Op: patchOperationTypeSet,
Path: path,
Value: value,
})
}
// AppendRemove appends a remove operation to the patch request.
func (p *PatchOperations) AppendRemove(path string) {
p.operations = append(p.operations, patchOperation{
Op: patchOperationTypeRemove,
Path: path,
})
}
// AppendIncrement appends an increment operation to the patch request.
func (p *PatchOperations) AppendIncrement(path string, value int64) {
p.operations = append(p.operations, patchOperation{
Op: patchOperationTypeIncrement,
Path: path,
Value: value,
})
}