Skip to content

批量操作方法

Processor 提供批量操作能力,一次处理多个 JSON 操作。

ProcessBatch

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

批量处理多个 JSON 操作。

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, 结果: %v\n", result.ID, result.Result)
}

BatchOperation 结构

go
type BatchOperation struct {
    Type    string `json:"type"`     // 操作类型:"get", "set", "delete", "validate"
    JSONStr string `json:"json_str"` // JSON 字符串
    Path    string `json:"path"`     // 目标路径
    Value   any    `json:"value"`    // Set 操作的值
    ID      string `json:"id"`       // 操作标识符
}
字段类型说明
Typestring操作类型:getsetdeletevalidate
JSONStrstring要操作的 JSON 字符串
Pathstring目标路径
ValueanySet 操作时设置的值
IDstring操作标识符,用于匹配结果

BatchResult 结构

go
type BatchResult struct {
    ID     string `json:"id"`     // 对应的操作 ID
    Result any    `json:"result"` // 操作结果
    Error  error  `json:"error"`  // 错误(如有)
}
字段类型说明
IDstring对应 BatchOperation 的 ID
Resultany操作结果(Get 返回值,Set/Delete 返回新 JSON)
Errorerror单个操作的错误(不影响其他操作)

使用示例

批量读取

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)
}

批量修改

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)

混合操作

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)

// 检查验证结果
for _, r := range results {
    if r.ID == "check" {
        if m, ok := r.Result.(map[string]any); ok {
            fmt.Printf("验证结果: %v\n", m["valid"])
        }
    }
}

注意事项

  1. 每个操作独立执行,一个失败不影响其他操作
  2. 结果顺序与操作顺序一致
  3. 通过 ID 匹配操作和结果

相关