mirror of
https://github.com/gocsaf/csaf.git
synced 2025-12-22 11:55:40 +01:00
Simplified extraction from CSAF further.
This commit is contained in:
parent
ef0a92b491
commit
21533556e2
1 changed files with 22 additions and 31 deletions
|
|
@ -33,13 +33,23 @@ type extraction struct {
|
||||||
tlpLabel string
|
tlpLabel string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type extractFunc func(string) (interface{}, error)
|
||||||
|
|
||||||
func newExtraction(content interface{}) (*extraction, error) {
|
func newExtraction(content interface{}) (*extraction, error) {
|
||||||
|
|
||||||
builder := gval.Full(jsonpath.Language())
|
builder := gval.Full(jsonpath.Language())
|
||||||
|
|
||||||
|
path := func(expr string) (interface{}, error) {
|
||||||
|
eval, err := builder.NewEvaluable(expr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return eval(context.Background(), content)
|
||||||
|
}
|
||||||
|
|
||||||
e := new(extraction)
|
e := new(extraction)
|
||||||
|
|
||||||
for _, fn := range []func(*gval.Language, interface{}) error{
|
for _, fn := range []func(extractFunc) error{
|
||||||
extractText(idExpr, &e.id),
|
extractText(idExpr, &e.id),
|
||||||
extractText(titleExpr, &e.title),
|
extractText(titleExpr, &e.title),
|
||||||
extractTime(currentReleaseDateExpr, &e.currentReleaseDate),
|
extractTime(currentReleaseDateExpr, &e.currentReleaseDate),
|
||||||
|
|
@ -48,7 +58,7 @@ func newExtraction(content interface{}) (*extraction, error) {
|
||||||
extractText(tlpLabelExpr, &e.tlpLabel),
|
extractText(tlpLabelExpr, &e.tlpLabel),
|
||||||
e.extractPublisher,
|
e.extractPublisher,
|
||||||
} {
|
} {
|
||||||
if err := fn(&builder, content); err != nil {
|
if err := fn(path); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -56,16 +66,10 @@ func newExtraction(content interface{}) (*extraction, error) {
|
||||||
return e, nil
|
return e, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractText(
|
func extractText(expr string, store *string) func(extractFunc) error {
|
||||||
expr string,
|
|
||||||
store *string,
|
return func(path extractFunc) error {
|
||||||
) func(*gval.Language, interface{}) error {
|
s, err := path(expr)
|
||||||
return func(builder *gval.Language, content interface{}) error {
|
|
||||||
eval, err := builder.NewEvaluable(expr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
s, err := eval(context.Background(), content)
|
|
||||||
if text, ok := s.(string); ok && err == nil {
|
if text, ok := s.(string); ok && err == nil {
|
||||||
*store = text
|
*store = text
|
||||||
}
|
}
|
||||||
|
|
@ -73,16 +77,10 @@ func extractText(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractTime(
|
func extractTime(expr string, store *time.Time) func(extractFunc) error {
|
||||||
expr string,
|
|
||||||
store *time.Time,
|
return func(path extractFunc) error {
|
||||||
) func(*gval.Language, interface{}) error {
|
s, err := path(expr)
|
||||||
return func(builder *gval.Language, content interface{}) error {
|
|
||||||
eval, err := builder.NewEvaluable(expr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
s, err := eval(context.Background(), content)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -98,15 +96,8 @@ func extractTime(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *extraction) extractPublisher(
|
func (e *extraction) extractPublisher(path extractFunc) error {
|
||||||
builder *gval.Language,
|
p, err := path(publisherExpr)
|
||||||
content interface{},
|
|
||||||
) error {
|
|
||||||
eval, err := builder.NewEvaluable(publisherExpr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
p, err := eval(context.Background(), content)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue