Skip to content

Commit

Permalink
Use copystructure for deep copying data structures.
Browse files Browse the repository at this point in the history
  • Loading branch information
apognu committed Feb 7, 2025
1 parent a597028 commit 7e4e82d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,11 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mfridman/interpolate v0.0.2 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,16 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY=
github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg=
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
github.com/mitchellh/hashstructure/v2 v2.0.2 h1:vGKWl0YJqUNxE8d+h8f6NJLcCJrgbhC4NcD46KavDd4=
github.com/mitchellh/hashstructure/v2 v2.0.2/go.mod h1:MG3aRVU/N29oo/V/IhBX8GR/zz4kQkprJgF2EVszyDE=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0=
Expand Down
29 changes: 19 additions & 10 deletions usecases/ast_eval/evaluate_ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/checkmarble/marble-backend/models/ast"
"github.com/checkmarble/marble-backend/pure_utils"
"github.com/mohae/deepcopy"
"github.com/mitchellh/copystructure"
"golang.org/x/sync/singleflight"
)

Expand Down Expand Up @@ -48,15 +48,17 @@ func EvaluateAst(ctx context.Context, cache *EvaluationCache,

if cache != nil {
if cached, ok := cache.Cache.Load(hash); ok {
response := deepcopy.Copy(cached).(nodeEvaluationResponse)
response.Eval.Index = node.Index
if response, err := copystructure.Copy(cached); err == nil {
response := response.(nodeEvaluationResponse)
response.Eval.Index = node.Index

response.Eval.EvaluationPlan = ast.NodeEvaluationPlan{
Took: 0,
Cached: true,
}
response.Eval.EvaluationPlan = ast.NodeEvaluationPlan{
Took: 0,
Cached: true,
}

return response.Eval, response.Ok
return response.Eval, response.Ok
}
}
}

Expand Down Expand Up @@ -140,7 +142,7 @@ func EvaluateAst(ctx context.Context, cache *EvaluationCache,
evaluationResponse := nodeEvaluationResponse{evaluation, ok}

if cache != nil {
cache.Cache.Store(hash, deepcopy.Copy(evaluationResponse))
cache.Cache.Store(hash, evaluationResponse)
}

return evaluationResponse, nil
Expand All @@ -154,7 +156,14 @@ func EvaluateAst(ctx context.Context, cache *EvaluationCache,
}

if !evaluated {
evaluation.Eval = deepcopy.Copy(evaluation.Eval).(ast.NodeEvaluation)
result, err := copystructure.Copy(evaluation.Eval)
if err != nil {
evaluation.Eval.Errors = append(evaluation.Eval.Errors, err)

return evaluation.Eval, false
}

evaluation.Eval = result.(ast.NodeEvaluation)
evaluation.Eval.SetCached()
}

Expand Down

0 comments on commit 7e4e82d

Please sign in to comment.