Skip to content

Commit 8542be6

Browse files
add usage examples
1 parent 1c0b3ba commit 8542be6

File tree

3 files changed

+157
-90
lines changed

3 files changed

+157
-90
lines changed

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2016 Dmitriy Kalinin
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 2 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,5 @@
11
## go-patch
22

3-
More or less based on https://tools.ietf.org/html/rfc6902.
3+
Roughly based on JSON patch: https://tools.ietf.org/html/rfc6902.
44

5-
## Pointer (aka path)
6-
7-
More or less based on https://tools.ietf.org/html/rfc6901.
8-
9-
- Root
10-
- Index (ex: `/0`, `/-1`)
11-
- AfterLastIndex (ex: `/-`)
12-
- MatchingIndex (ex: `/key=val`)
13-
- Key: `/key`
14-
15-
## Operations
16-
17-
- Remove
18-
- Replace
19-
20-
## Example
21-
22-
### Input
23-
24-
```yaml
25-
releases:
26-
- name: capi
27-
version: 0.1
28-
29-
instance_groups:
30-
- name: cloud_controller
31-
instances: 0
32-
jobs:
33-
- name: cloud_controller
34-
release: capi
35-
36-
- name: uaa
37-
instances: 0
38-
```
39-
40-
### Operations
41-
42-
```yaml
43-
- type: replace
44-
path: /instance_groups/name=cloud_controller/instances
45-
value: 1
46-
47-
- type: replace
48-
path: /instance_groups/name=cloud_controller/jobs/name=cloud_controller/consumes?/db
49-
value:
50-
instances:
51-
- address: some-db.local
52-
properties:
53-
username: user
54-
password: pass
55-
56-
- type: replace
57-
path: /instance_groups/name=uaa/instances
58-
value: 1
59-
60-
- type: replace
61-
path: /instance_groups/-
62-
value:
63-
name: uaadb
64-
instances: 2
65-
```
66-
67-
### Output
68-
69-
```yaml
70-
releases:
71-
- name: capi
72-
version: latest
73-
74-
instance_groups:
75-
- name: cloud_controller
76-
instances: 1
77-
jobs:
78-
- name: cloud_controller
79-
release: capi
80-
consumes:
81-
db:
82-
instances:
83-
- address: some-db.local
84-
properties:
85-
username: user
86-
password: pass
87-
88-
- name: uaa
89-
instances: 1
90-
91-
- name: uaadb
92-
instances: 2
93-
```
5+
[Usage examples](docs/examples.md)

docs/examples.md

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
## Pointer syntax
2+
3+
- pointers always start at the root of the document
4+
- strings typically refer to hash keys (ex: `/key1`)
5+
- strings ending with `?` refer to hash keys that may or may not exist
6+
- integers refer to array indices (ex: `/0`, `/-1`)
7+
- `-` refers to an imaginary index after last array index (ex: `/-`)
8+
- `key=val` notation matches hashes (ex: `/key=val`)
9+
10+
See pointer test examples in [patch/pointer_test.go](../patch/pointer_test.go).
11+
12+
## Operations
13+
14+
Following example is used to demonstrate operations below:
15+
16+
```
17+
key: 1
18+
19+
key2:
20+
nested:
21+
super_nested: 2
22+
other: 3
23+
24+
array: [4,5,6]
25+
26+
items:
27+
- name: item7
28+
- name: item8
29+
- name: item8
30+
```
31+
32+
There are two available operations: `replace` and `remove`.
33+
34+
### Hash
35+
36+
```yaml
37+
- type: replace
38+
path: /key
39+
value: 10
40+
```
41+
42+
- sets `key` to `10`
43+
44+
```yaml
45+
- type: replace
46+
path: /key_not_there
47+
value: 10
48+
```
49+
50+
- errors because `key_not_there` is expected (does not have `?`)
51+
52+
```yaml
53+
- type: replace
54+
path: /new_key?
55+
value: 10
56+
```
57+
58+
- creates `new_key` because it ends with `?` and sets it to `10`
59+
60+
```yaml
61+
- type: replace
62+
path: /key2/nested/super_nested
63+
value: 10
64+
```
65+
66+
- requires that `key2` and `nested` hashes exist
67+
- sets `super_nested` to `10`
68+
69+
```yaml
70+
- type: replace
71+
path: /key2/nested?/another_nested/super_nested
72+
value: 10
73+
```
74+
75+
- requires that `key2` hash exists
76+
- allows `nested`, `another_nested` and `super_nested` not to exist because `?` carries over to nested keys
77+
- creates `another_nested` and `super_nested` before sets `super_nested` to `10`
78+
79+
### Array
80+
81+
```yaml
82+
- type: replace
83+
path: /array/0
84+
value: 10
85+
```
86+
87+
- requires `array` to exist and be an array
88+
- replaces 0th item in `array` array with `10`
89+
90+
```yaml
91+
- type: replace
92+
path: /array/-
93+
value: 10
94+
```
95+
96+
- requires `array` to exist and be an array
97+
- appends `10` to the end of `array`
98+
99+
```yaml
100+
- type: replace
101+
path: /array2?/-
102+
value: 10
103+
```
104+
105+
- creates `array2` array since it does not exist
106+
- appends `10` to the end of `array2`
107+
108+
### Arrays of hashes
109+
110+
```yaml
111+
- type: replace
112+
path: /items/name=item7/count
113+
value: 10
114+
```
115+
116+
- finds array item with matching key `name` with value `item7`
117+
- adds `count` key as a sibling of name, resulting in:
118+
119+
```
120+
...
121+
items:
122+
- name: item7
123+
count: 10
124+
- name: item8
125+
```
126+
127+
```yaml
128+
- type: replace
129+
path: /items/name=item8/count
130+
value: 10
131+
```
132+
133+
- errors because there are two values that have `item8` as their `name`
134+
135+
See full example in [patch/integration_test.go](../patch/integration_test.go).

0 commit comments

Comments
 (0)