mirror of
https://github.com/gocsaf/csaf.git
synced 2025-12-22 05:40:11 +01:00
Factor out summary extraction from advisories.
This commit is contained in:
parent
d4270e2d39
commit
b12ad718c5
4 changed files with 84 additions and 73 deletions
36
util/json.go
36
util/json.go
|
|
@ -9,7 +9,12 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/PaesslerAG/gval"
|
||||
"github.com/PaesslerAG/jsonpath"
|
||||
)
|
||||
|
||||
// ReMarshalJSON transforms data from src to dst via JSON marshalling.
|
||||
|
|
@ -20,3 +25,34 @@ func ReMarshalJSON(dst, src interface{}) error {
|
|||
}
|
||||
return json.Unmarshal(intermediate, dst)
|
||||
}
|
||||
|
||||
// PathEval is a helper to evaluate JSON paths on documents.
|
||||
type PathEval struct {
|
||||
builder gval.Language
|
||||
exprs map[string]gval.Evaluable
|
||||
}
|
||||
|
||||
// NewPathEval creates a new PathEval.
|
||||
func NewPathEval() *PathEval {
|
||||
return &PathEval{
|
||||
builder: gval.Full(jsonpath.Language()),
|
||||
exprs: map[string]gval.Evaluable{},
|
||||
}
|
||||
}
|
||||
|
||||
// Eval evalutes expression expr on document doc.
|
||||
// Returns the result of the expression.
|
||||
func (pe *PathEval) Eval(expr string, doc interface{}) (interface{}, error) {
|
||||
if doc == nil {
|
||||
return nil, errors.New("no document to extract data from")
|
||||
}
|
||||
eval := pe.exprs[expr]
|
||||
if eval == nil {
|
||||
var err error
|
||||
if eval, err = pe.builder.NewEvaluable(expr); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pe.exprs[expr] = eval
|
||||
}
|
||||
return eval(context.Background(), doc)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue