add chain input
This commit is contained in:
46
input/chain.go
Normal file
46
input/chain.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"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 {
|
||||
log.Println(name, len(c))
|
||||
for _, in := range c {
|
||||
log.Printf("%T\n", in)
|
||||
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 err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -10,7 +10,7 @@ var (
|
||||
)
|
||||
|
||||
func IsEmpty(v Value) bool {
|
||||
return v == Empty
|
||||
return v == nil || v == Empty
|
||||
}
|
||||
|
||||
type empty struct{}
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
package input
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitoa.ru/go-4devs/console/input/value"
|
||||
)
|
||||
|
||||
type Wrap struct {
|
||||
Input
|
||||
Array
|
||||
}
|
||||
|
||||
func (w *Wrap) Option(ctx context.Context, name string) value.Value {
|
||||
if v, ok := w.Array.GetOption(name); ok {
|
||||
return v
|
||||
}
|
||||
|
||||
return w.Input.Option(ctx, name)
|
||||
}
|
||||
|
||||
func (w *Wrap) Argument(ctx context.Context, name string) value.Value {
|
||||
if v, ok := w.Array.GetArgument(name); ok {
|
||||
return v
|
||||
}
|
||||
|
||||
return w.Input.Argument(ctx, name)
|
||||
}
|
||||
|
||||
func (w *Wrap) Bind(ctx context.Context, def *Definition) error {
|
||||
if err := w.Input.Bind(ctx, def); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return w.Array.Bind(ctx, def)
|
||||
}
|
||||
Reference in New Issue
Block a user