Skip to content

Commit 3dd8d2f

Browse files
committed
feat: upgrade to struct-based Operation API
- Replace map[string]interface{} with clean struct literals - Enhance type safety with compile-time field validation - Improve user experience with intuitive API syntax - Update all documentation and examples - Fix linting issues and remove unused functions - Maintain 100% json-joy compatibility
1 parent a52d946 commit 3dd8d2f

File tree

123 files changed

+3372
-4402
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+3372
-4402
lines changed

CLAUDE.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@ This file provides guidance to Claude Code when working with code in this reposi
66

77
This is a comprehensive Go implementation of JSON Patch (RFC 6902), JSON Predicate, and extended operations for JSON document manipulation with full type safety and generic support. It's a Go port of json-joy/json-patch.
88

9+
## API Usage
10+
11+
The library provides a clean struct-based API for JSON Patch operations:
12+
13+
```go
14+
// Create operations using struct literals
15+
patch := []jsonpatch.Operation{
16+
{Op: "add", Path: "/name", Value: "John"},
17+
{Op: "inc", Path: "/age", Inc: 1},
18+
{Op: "str_ins", Path: "/bio", Pos: 0, Str: "Hello "},
19+
}
20+
21+
// Apply with type-safe generic API
22+
result, err := jsonpatch.ApplyPatch(doc, patch)
23+
```
24+
925
## Development Commands
1026

1127
### Essential Commands

README.md

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ func main() {
3636
"age": 30,
3737
}
3838

39-
// Create patch operations
39+
// Create patch operations using struct syntax
4040
patch := []jsonpatch.Operation{
4141
{
42-
"op": "replace",
43-
"path": "/name",
44-
"value": "Jane",
42+
Op: "replace",
43+
Path: "/name",
44+
Value: "Jane",
4545
},
4646
{
47-
"op": "add",
48-
"path": "/email",
49-
"value": "jane@example.com",
47+
Op: "add",
48+
Path: "/email",
49+
Value: "jane@example.com",
5050
},
5151
}
5252

@@ -87,8 +87,8 @@ func main() {
8787
user := User{Name: "John", Age: 30}
8888

8989
patch := []jsonpatch.Operation{
90-
{"op": "replace", "path": "/name", "value": "Jane"},
91-
{"op": "add", "path": "/email", "value": "jane@example.com"},
90+
{Op: "replace", Path: "/name", Value: "Jane"},
91+
{Op: "add", Path: "/email", Value: "jane@example.com"},
9292
}
9393

9494
// Type-safe: result.Doc is automatically typed as User
@@ -111,9 +111,9 @@ import "github.com/kaptinlin/jsonpatch/codec/compact"
111111
func main() {
112112
// Standard JSON Patch operations
113113
standardOps := []jsonpatch.Operation{
114-
{"op": "add", "path": "/name", "value": "John"},
115-
{"op": "replace", "path": "/age", "value": 30},
116-
{"op": "remove", "path": "/temp"},
114+
{Op: "add", Path: "/name", Value: "John"},
115+
{Op: "replace", Path: "/age", Value: 30},
116+
{Op: "remove", Path: "/temp"},
117117
}
118118

119119
// Encode to compact format with numeric opcodes (35.9% space savings)
@@ -299,9 +299,9 @@ type Config struct {
299299
config := Config{Version: 1, Status: "active", Enabled: true}
300300

301301
patch := []jsonpatch.Operation{
302-
{"op": "inc", "path": "/version", "inc": 1},
303-
{"op": "replace", "path": "/status", "value": "updated"},
304-
{"op": "flip", "path": "/enabled"},
302+
{Op: "inc", Path: "/version", Inc: 1},
303+
{Op: "replace", Path: "/status", Value: "updated"},
304+
{Op: "flip", Path: "/enabled"},
305305
}
306306

307307
// result.Doc is automatically typed as Config
@@ -323,19 +323,19 @@ patch := []jsonpatch.Operation{
323323
{
324324
"op": "test",
325325
"path": "/version",
326-
"value": 1,
326+
Value: 1,
327327
},
328328
// Make the change
329329
{
330330
"op": "replace",
331331
"path": "/status",
332-
"value": "updated",
332+
Value: "updated",
333333
},
334334
// Increment version
335335
{
336336
"op": "inc",
337-
"path": "/version",
338-
"inc": 1,
337+
Path: "/version",
338+
Inc: 1,
339339
},
340340
}
341341

@@ -362,7 +362,7 @@ for i := 0; i < itemCount; i++ {
362362
patch = append(patch, jsonpatch.Operation{
363363
"op": "replace",
364364
"path": fmt.Sprintf("/items/%d/status", i),
365-
"value": "processed",
365+
Value: "processed",
366366
})
367367
}
368368

@@ -376,19 +376,19 @@ patch := []jsonpatch.Operation{
376376
// Add to end of array
377377
{
378378
"op": "add",
379-
"path": "/users/-",
380-
"value": map[string]interface{}{"name": "New User"},
379+
Path: "/users/-",
380+
Value: map[string]interface{}{"name": "New User"},
381381
},
382382
// Insert at specific position
383383
{
384384
"op": "add",
385-
"path": "/tags/0",
386-
"value": "important",
385+
Path: "/tags/0",
386+
Value: "important",
387387
},
388388
// Remove array element
389389
{
390390
"op": "remove",
391-
"path": "/items/2",
391+
Path: "/items/2",
392392
},
393393
}
394394

@@ -402,15 +402,15 @@ patch := []jsonpatch.Operation{
402402
// Insert text at position
403403
{
404404
"op": "str_ins",
405-
"path": "/content",
406-
"pos": 0,
405+
Path: "/content",
406+
Pos: 0,
407407
"str": "Prefix: ",
408408
},
409409
// Insert at end
410410
{
411411
"op": "str_ins",
412-
"path": "/content",
413-
"pos": 20,
412+
Path: "/content",
413+
Pos: 20,
414414
"str": " (Updated)",
415415
},
416416
}
@@ -445,9 +445,9 @@ import "github.com/kaptinlin/jsonpatch/codec/compact"
445445

446446
// Standard operations
447447
ops := []jsonpatch.Operation{
448-
{"op": "add", "path": "/users/-", "value": map[string]string{"name": "Alice"}},
449-
{"op": "inc", "path": "/counter", "inc": 1},
450-
{"op": "flip", "path": "/enabled"},
448+
{Op: "add", Path: "/users/-", Value: map[string]string{"name": "Alice"}},
449+
{Op: "inc", Path: "/counter", Inc: 1},
450+
{Op: "flip", Path: "/enabled"},
451451
}
452452

453453
// Choose encoding format
@@ -509,8 +509,8 @@ result, err := jsonpatch.ApplyPatch(doc, patch,
509509
```go
510510
// Good: Test before critical changes
511511
patch := []jsonpatch.Operation{
512-
{"op": "test", "path": "/balance", "value": 1000.0},
513-
{"op": "replace", "path": "/balance", "value": 500.0},
512+
{Op: "test", Path: "/balance", Value: 1000.0},
513+
{Op: "replace", Path: "/balance", Value: 500.0},
514514
}
515515
```
516516

@@ -553,9 +553,9 @@ if err == nil {
553553
```go
554554
// Efficient: Single patch with multiple operations
555555
patch := []jsonpatch.Operation{
556-
{"op": "replace", "path": "/status", "value": "active"},
557-
{"op": "inc", "path": "/version", "inc": 1},
558-
{"op": "add", "path": "/lastModified", "value": time.Now()},
556+
{Op: "replace", Path: "/status", Value: "active"},
557+
{Op: "inc", Path: "/version", Inc: 1},
558+
{Op: "add", Path: "/lastModified", Value: time.Now()},
559559
}
560560
```
561561

0 commit comments

Comments
 (0)