Co-authored-by: andrey1s <andrey@4devs.pro> Reviewed-on: #1 Co-authored-by: andrey <andrey@4devs.io> Co-committed-by: andrey <andrey@4devs.io>
45 lines
760 B
Go
45 lines
760 B
Go
package input
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"gitoa.ru/go-4devs/console/input/value"
|
|
)
|
|
|
|
func Chain(c ...Input) Input {
|
|
return chain(c)
|
|
}
|
|
|
|
type chain []Input
|
|
|
|
func (c chain) Option(ctx context.Context, name string) value.Value {
|
|
for _, in := range c {
|
|
if val := in.Option(ctx, name); !value.IsEmpty(val) {
|
|
return val
|
|
}
|
|
}
|
|
|
|
return value.Empty()
|
|
}
|
|
|
|
func (c chain) Argument(ctx context.Context, name string) value.Value {
|
|
for _, in := range c {
|
|
if val := in.Argument(ctx, name); !value.IsEmpty(val) {
|
|
return val
|
|
}
|
|
}
|
|
|
|
return value.Empty()
|
|
}
|
|
|
|
func (c chain) Bind(ctx context.Context, def *Definition) error {
|
|
for _, input := range c {
|
|
if err := input.Bind(ctx, def); err != nil {
|
|
return fmt.Errorf("%T:%w", input, err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|