@ -8,11 +8,12 @@ import (
"github.com/aziis98/cabret/operation"
)
func switchMap ( m map [ string ] any , v * any ) func ( k string ) bool {
// switchMapHasKey returns a function that returns true if the map "m" contains the given key and binds the corresponding value to the provided "target" pointer, useful for writing map key checks using a switch instead of an if chain
func switchMapHasKey ( m map [ string ] any , target * any ) func ( k string ) bool {
return func ( k string ) bool {
val , ok := m [ k ]
if ok {
* v = val
* target = val
}
return ok
}
@ -21,48 +22,48 @@ func switchMap(m map[string]any, v *any) func(k string) bool {
func ParsePipeline ( p config . Pipeline ) ( [ ] cabret . Operation , error ) {
ops := [ ] cabret . Operation { }
for _ , op Config := range p . Pipeline {
var v any
has := switchMap ( opConfig , & v )
for _ , op eration Config := range p . Pipeline {
var rawValue any
has Key := switchMapHasKey ( operationConfig , & rawValue )
switch {
case has ( "source" ) :
value , ok := v . ( string )
case has Key ( "source" ) :
value , ok := rawValue . ( string )
if ! ok {
return nil , fmt . Errorf ( ` expected string but got "%v" of type %T ` , v, v )
return nil , fmt . Errorf ( ` expected string but got "%v" of type %T ` , rawValue, rawValue )
}
op Config[ operation . ShortFormValueKey ] = value
op eration Config[ operation . ShortFormValueKey ] = value
op , err := ParseOperation ( "source" , op Config)
op , err := ParseOperation ( "source" , op eration Config)
if err != nil {
return nil , err
}
ops = append ( ops , op )
case has ( "target" ) :
value , ok := v . ( string )
case has Key ( "target" ) :
value , ok := rawValue . ( string )
if ! ok {
return nil , fmt . Errorf ( ` expected string but got "%v" of type %T ` , v, v )
return nil , fmt . Errorf ( ` expected string but got "%v" of type %T ` , rawValue, rawValue )
}
op Config[ operation . ShortFormValueKey ] = value
op eration Config[ operation . ShortFormValueKey ] = value
op , err := ParseOperation ( "target" , op Config)
op , err := ParseOperation ( "target" , op eration Config)
if err != nil {
return nil , err
}
ops = append ( ops , op )
case has ( "use" ) :
name , ok := v . ( string )
case has Key ( "use" ) :
name , ok := rawValue . ( string )
if ! ok {
return nil , fmt . Errorf ( ` expected string but got "%v" of type %T ` , v, v )
return nil , fmt . Errorf ( ` expected string but got "%v" of type %T ` , rawValue, rawValue )
}
op , err := ParseOperation ( name , op Config)
op , err := ParseOperation ( name , op eration Config)
if err != nil {
return nil , err
}
@ -70,7 +71,7 @@ func ParsePipeline(p config.Pipeline) ([]cabret.Operation, error) {
ops = append ( ops , op )
default :
return nil , fmt . Errorf ( ` pipeline entry is missing one of "use", "source" or "target", got %#v ` , op Config)
return nil , fmt . Errorf ( ` pipeline entry is missing one of "use", "source" or "target", got %#v ` , op eration Config)
}
}