2025-10-14 22:23:06 +03:00
|
|
|
package script
|
|
|
|
|
|
|
|
|
|
import "context"
|
|
|
|
|
|
|
|
|
|
// Chain chains provided Processors.
|
|
|
|
|
// When an error is returned by a Processor in chain, processing
|
|
|
|
|
// stops and the error is retuned without running further stages.
|
2025-10-17 23:24:16 +03:00
|
|
|
func ChainProcessor(processors ...Processor) Processor {
|
2025-10-14 22:23:06 +03:00
|
|
|
return func(ctx context.Context, in []string) ([]string, error) {
|
|
|
|
|
var err error
|
|
|
|
|
for _, p := range processors {
|
|
|
|
|
// not checking ctx expiry here,
|
|
|
|
|
// let the processor handle it
|
|
|
|
|
|
|
|
|
|
in, err = p(ctx, in)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return in, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-17 23:24:16 +03:00
|
|
|
|
|
|
|
|
type chainWriter struct {
|
|
|
|
|
w []Writer
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *chainWriter) Write(in []string) error {
|
|
|
|
|
for _, w := range c.w {
|
|
|
|
|
if err := w.Write(in); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ChainWriter(writers ...Writer) Writer {
|
|
|
|
|
return &chainWriter{
|
|
|
|
|
w: writers,
|
|
|
|
|
}
|
|
|
|
|
}
|