mirror of
https://github.com/gocsaf/csaf.git
synced 2025-12-22 11:55:40 +01:00
Make extraction of fields optional
This commit is contained in:
parent
41e4029b0d
commit
d6c0fa3518
2 changed files with 22 additions and 7 deletions
|
|
@ -51,8 +51,8 @@ func NewAdvisorySummary(
|
|||
{Expr: titleExpr, Action: util.StringMatcher(&e.Title)},
|
||||
{Expr: currentReleaseDateExpr, Action: util.TimeMatcher(&e.CurrentReleaseDate, time.RFC3339)},
|
||||
{Expr: initialReleaseDateExpr, Action: util.TimeMatcher(&e.InitialReleaseDate, time.RFC3339)},
|
||||
{Expr: summaryExpr, Action: util.StringMatcher(&e.Summary)},
|
||||
{Expr: tlpLabelExpr, Action: util.StringMatcher(&e.TLPLabel)},
|
||||
{Expr: summaryExpr, Action: util.StringMatcher(&e.Summary), Optional: true},
|
||||
{Expr: tlpLabelExpr, Action: util.StringMatcher(&e.TLPLabel), Optional: true},
|
||||
{Expr: publisherExpr, Action: util.ReMarshalMatcher(e.Publisher)},
|
||||
}, doc); err != nil {
|
||||
return nil, err
|
||||
|
|
|
|||
25
util/json.go
25
util/json.go
|
|
@ -65,6 +65,8 @@ type PathEvalMatcher struct {
|
|||
Expr string
|
||||
// Action is executed with the result of the match.
|
||||
Action func(interface{}) error
|
||||
// Optional expresses if the expression is optional.
|
||||
Optional bool
|
||||
}
|
||||
|
||||
// ReMarshalMatcher is an action to re-marshal the result to another type.
|
||||
|
|
@ -102,14 +104,27 @@ func TimeMatcher(dst *time.Time, format string) func(interface{}) error {
|
|||
}
|
||||
}
|
||||
|
||||
// Extract extracts a value from a given document with a given expression/action.
|
||||
func (pe *PathEval) Extract(
|
||||
expr string,
|
||||
action func(interface{}) error,
|
||||
optional bool,
|
||||
doc interface{},
|
||||
) error {
|
||||
x, err := pe.Eval(expr, doc)
|
||||
if err != nil {
|
||||
if optional {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
return action(x)
|
||||
}
|
||||
|
||||
// Match matches a list of PathEvalMatcher pairs against a document.
|
||||
func (pe *PathEval) Match(matcher []PathEvalMatcher, doc interface{}) error {
|
||||
for _, m := range matcher {
|
||||
x, err := pe.Eval(m.Expr, doc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := m.Action(x); err != nil {
|
||||
if err := pe.Extract(m.Expr, m.Action, m.Optional, doc); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue