@ -8,11 +8,12 @@ import (
"github.com/aziis98/cabret/operation"
"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 {
return func ( k string ) bool {
val , ok := m [ k ]
val , ok := m [ k ]
if ok {
if ok {
* v = val
* target = val
}
}
return ok
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 ) {
func ParsePipeline ( p config . Pipeline ) ( [ ] cabret . Operation , error ) {
ops := [ ] cabret . Operation { }
ops := [ ] cabret . Operation { }
for _ , op Config := range p . Pipeline {
for _ , op eration Config := range p . Pipeline {
var v any
var rawValue any
has := switchMap ( opConfig , & v )
has Key := switchMapHasKey ( operationConfig , & rawValue )
switch {
switch {
case has ( "source" ) :
case has Key ( "source" ) :
value , ok := v . ( string )
value , ok := rawValue . ( string )
if ! ok {
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 {
if err != nil {
return nil , err
return nil , err
}
}
ops = append ( ops , op )
ops = append ( ops , op )
case has ( "target" ) :
case has Key ( "target" ) :
value , ok := v . ( string )
value , ok := rawValue . ( string )
if ! ok {
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 {
if err != nil {
return nil , err
return nil , err
}
}
ops = append ( ops , op )
ops = append ( ops , op )
case has ( "use" ) :
case has Key ( "use" ) :
name , ok := v . ( string )
name , ok := rawValue . ( string )
if ! ok {
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 {
if err != nil {
return nil , err
return nil , err
}
}
@ -70,7 +71,7 @@ func ParsePipeline(p config.Pipeline) ([]cabret.Operation, error) {
ops = append ( ops , op )
ops = append ( ops , op )
default :
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)
}
}
}
}