Skip to content

Commit

Permalink
make a copy when retrieving json path expression in cache
Browse files Browse the repository at this point in the history
Signed-off-by: xiongjiwei <xiongjiwei1996@outlook.com>
  • Loading branch information
xiongjiwei committed Oct 1, 2022
1 parent 9657c9e commit 4ce426c
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion types/json_path_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ type JSONPathExpression struct {
flags jsonPathExpressionFlag
}

func (pe JSONPathExpression) clone() JSONPathExpression {
legs := make([]jsonPathLeg, len(pe.legs))
copy(legs, pe.legs)
return JSONPathExpression{legs: legs, flags: pe.flags}
}

var peCache JSONPathExpressionCache

type jsonPathExpressionKey string
Expand Down Expand Up @@ -362,7 +368,7 @@ func ParseJSONPathExpr(pathExpr string) (JSONPathExpression, error) {
val, ok := peCache.cache.Get(jsonPathExpressionKey(pathExpr))
if ok {
peCache.mu.Unlock()
return val.(JSONPathExpression), nil
return val.(JSONPathExpression).clone(), nil
}
peCache.mu.Unlock()

Expand Down

0 comments on commit 4ce426c

Please sign in to comment.