fix(topdata): close invariant JSON roots
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package topdata
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
@@ -63,36 +64,65 @@ func unsupportedObjectKeysError(label string, obj map[string]any, supported ...s
|
||||
return fmt.Errorf("%s contains unsupported key %q (supported keys: %s)", label, unsupported[0], strings.Join(supported, ", "))
|
||||
}
|
||||
|
||||
func parseGlobalConditionGroups(injection map[string]any) (globalConditionGroups, error) {
|
||||
type globalConditionParseOptions struct {
|
||||
Label string
|
||||
Report func(error)
|
||||
}
|
||||
|
||||
func parseGlobalConditionGroups(injection map[string]any, options ...globalConditionParseOptions) (globalConditionGroups, error) {
|
||||
if injection == nil {
|
||||
return globalConditionGroups{}, nil
|
||||
}
|
||||
if err := unsupportedObjectKeysError("global injection", injection, globalInjectionKeys...); err != nil {
|
||||
return globalConditionGroups{}, err
|
||||
|
||||
label := "global injection"
|
||||
var report func(error)
|
||||
if len(options) > 0 {
|
||||
if strings.TrimSpace(options[0].Label) != "" {
|
||||
label = options[0].Label
|
||||
}
|
||||
report = options[0].Report
|
||||
}
|
||||
|
||||
var errs []error
|
||||
addError := func(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
errs = append(errs, err)
|
||||
if report != nil {
|
||||
report(err)
|
||||
}
|
||||
}
|
||||
if err := unsupportedObjectKeysError(label, injection, globalInjectionKeys...); err != nil {
|
||||
addError(err)
|
||||
}
|
||||
|
||||
var groups globalConditionGroups
|
||||
var err error
|
||||
|
||||
if raw, ok := injection["require_present"]; ok {
|
||||
groups.RequirePresent, err = parseGlobalConditionList("require_present", raw, false)
|
||||
parsed, err := parseGlobalConditionList(label+" require_present", raw, false)
|
||||
if err != nil {
|
||||
return globalConditionGroups{}, err
|
||||
addError(err)
|
||||
} else {
|
||||
groups.RequirePresent = parsed
|
||||
}
|
||||
}
|
||||
if raw, ok := injection["any_present"]; ok {
|
||||
groups.AnyPresent, err = parseGlobalConditionList("any_present", raw, true)
|
||||
parsed, err := parseGlobalConditionList(label+" any_present", raw, true)
|
||||
if err != nil {
|
||||
return globalConditionGroups{}, err
|
||||
addError(err)
|
||||
} else {
|
||||
groups.AnyPresent = parsed
|
||||
}
|
||||
}
|
||||
if raw, ok := injection["unless_present"]; ok {
|
||||
groups.UnlessPresent, err = parseGlobalConditionList("unless_present", raw, false)
|
||||
parsed, err := parseGlobalConditionList(label+" unless_present", raw, false)
|
||||
if err != nil {
|
||||
return globalConditionGroups{}, err
|
||||
addError(err)
|
||||
} else {
|
||||
groups.UnlessPresent = parsed
|
||||
}
|
||||
}
|
||||
return groups, nil
|
||||
return groups, errors.Join(errs...)
|
||||
}
|
||||
|
||||
func parseGlobalConditionList(name string, raw any, requireNonEmpty bool) ([]globalCondition, error) {
|
||||
|
||||
Reference in New Issue
Block a user