* fix(expression): add validation for empty ExpressionOrList Signed-off-by: Jason Cameron <git@jasoncameron.dev> * fix(imports): block empty file imports with improved error checking logic Signed-off-by: Jason Cameron <git@jasoncameron.dev> * docs(expression): improve validation to error on empty CEL expressions Signed-off-by: Jason Cameron <git@jasoncameron.dev> --------- Signed-off-by: Jason Cameron <git@jasoncameron.dev>
65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
package config
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"slices"
|
|
)
|
|
|
|
var (
|
|
ErrExpressionOrListMustBeStringOrObject = errors.New("config: this must be a string or an object")
|
|
ErrExpressionEmpty = errors.New("config: this expression is empty")
|
|
ErrExpressionCantHaveBoth = errors.New("config: expression block can't contain multiple expression types")
|
|
)
|
|
|
|
type ExpressionOrList struct {
|
|
Expression string `json:"-"`
|
|
All []string `json:"all"`
|
|
Any []string `json:"any"`
|
|
}
|
|
|
|
func (eol ExpressionOrList) Equal(rhs *ExpressionOrList) bool {
|
|
if eol.Expression != rhs.Expression {
|
|
return false
|
|
}
|
|
|
|
if !slices.Equal(eol.All, rhs.All) {
|
|
return false
|
|
}
|
|
|
|
if !slices.Equal(eol.Any, rhs.Any) {
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func (eol *ExpressionOrList) UnmarshalJSON(data []byte) error {
|
|
switch string(data[0]) {
|
|
case `"`: // string
|
|
return json.Unmarshal(data, &eol.Expression)
|
|
case "{": // object
|
|
type RawExpressionOrList ExpressionOrList
|
|
var val RawExpressionOrList
|
|
if err := json.Unmarshal(data, &val); err != nil {
|
|
return err
|
|
}
|
|
eol.All = val.All
|
|
eol.Any = val.Any
|
|
|
|
return nil
|
|
}
|
|
|
|
return ErrExpressionOrListMustBeStringOrObject
|
|
}
|
|
|
|
func (eol *ExpressionOrList) Valid() error {
|
|
if eol.Expression == "" && len(eol.All) == 0 && len(eol.Any) == 0 {
|
|
return ErrExpressionEmpty
|
|
}
|
|
if len(eol.All) != 0 && len(eol.Any) != 0 {
|
|
return ErrExpressionCantHaveBoth
|
|
}
|
|
|
|
return nil
|
|
}
|