Skip to content

Batch Operation Methods

Processor provides batch operation capabilities to process multiple JSON operations at once.

ProcessBatch

Signature: func (p *Processor) ProcessBatch(operations []BatchOperation, cfg ...Config) ([]BatchResult, error)

Batch processes multiple JSON operations.

go
operations := []json.BatchOperation{
    {Type: "get", JSONStr: data, Path: "user.name", ID: "1"},
    {Type: "set", JSONStr: data, Path: "user.age", Value: 30, ID: "2"},
    {Type: "delete", JSONStr: data, Path: "user.temporary", ID: "3"},
}

results, err := processor.ProcessBatch(operations)
if err != nil {
    panic(err)
}

for _, result := range results {
    fmt.Printf("ID: %s, Result: %v\n", result.ID, result.Result)
}

BatchOperation Struct

go
type BatchOperation struct {
    Type    string `json:"type"`     // Operation type: "get", "set", "delete", "validate"
    JSONStr string `json:"json_str"` // JSON string
    Path    string `json:"path"`     // Target path
    Value   any    `json:"value"`    // Value for set operations
    ID      string `json:"id"`       // Operation identifier
}
FieldTypeDescription
TypestringOperation type: get, set, delete, validate
JSONStrstringJSON string to operate on
PathstringTarget path
ValueanyValue to set (for set operations)
IDstringOperation identifier for matching results

BatchResult Struct

go
type BatchResult struct {
    ID     string `json:"id"`     // Corresponding operation ID
    Result any    `json:"result"` // Operation result
    Error  error  `json:"error"`  // Error (if any)
}
FieldTypeDescription
IDstringCorresponds to BatchOperation's ID
ResultanyOperation result (Get returns value, Set/Delete returns new JSON)
ErrorerrorError for individual operation (does not affect other operations)

Usage Examples

Batch Read

go
operations := []json.BatchOperation{
    {Type: "get", JSONStr: data, Path: "user.name", ID: "name"},
    {Type: "get", JSONStr: data, Path: "user.email", ID: "email"},
    {Type: "get", JSONStr: data, Path: "user.age", ID: "age"},
}

results, _ := processor.ProcessBatch(operations)
for _, r := range results {
    fmt.Printf("%s: %v\n", r.ID, r.Result)
}

Batch Modification

go
operations := []json.BatchOperation{
    {Type: "set", JSONStr: data, Path: "status", Value: "active", ID: "1"},
    {Type: "set", JSONStr: data, Path: "updated_at", Value: time.Now().Unix(), ID: "2"},
    {Type: "delete", JSONStr: data, Path: "temp_field", ID: "3"},
}

results, _ := processor.ProcessBatch(operations)

Mixed Operations

go
operations := []json.BatchOperation{
    {Type: "validate", JSONStr: data, ID: "check"},
    {Type: "get", JSONStr: data, Path: "user.name", ID: "name"},
    {Type: "set", JSONStr: data, Path: "processed", Value: true, ID: "mark"},
}

results, _ := processor.ProcessBatch(operations)

// Check validation result
for _, r := range results {
    if r.ID == "check" {
        if m, ok := r.Result.(map[string]any); ok {
            fmt.Printf("Validation result: %v\n", m["valid"])
        }
    }
}

Notes

  1. Each operation is executed independently; one failure does not affect other operations
  2. Result order matches the operation order
  3. Use ID to match operations with results

See Also