2017-11-23 11:43:29 +01:00
|
|
|
package mvc2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-11-24 11:32:35 +01:00
|
|
|
|
|
|
|
"github.com/kataras/iris/context"
|
2017-11-23 11:43:29 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errNil = errors.New("nil")
|
|
|
|
errBad = errors.New("bad")
|
|
|
|
errAlreadyExists = errors.New("already exists")
|
|
|
|
)
|
2017-11-24 11:32:35 +01:00
|
|
|
|
|
|
|
type Mvc struct {
|
|
|
|
binders []*InputBinder
|
|
|
|
}
|
|
|
|
|
|
|
|
func New() *Mvc {
|
|
|
|
return new(Mvc)
|
|
|
|
}
|
|
|
|
|
2017-11-24 16:34:35 +01:00
|
|
|
func (m *Mvc) Child() *Mvc {
|
|
|
|
child := New()
|
|
|
|
|
|
|
|
// copy the current parent's ctx func binders and services to this new child.
|
|
|
|
if len(m.binders) > 0 {
|
|
|
|
binders := make([]*InputBinder, len(m.binders), len(m.binders))
|
|
|
|
for i, v := range m.binders {
|
|
|
|
binders[i] = v
|
2017-11-24 11:32:35 +01:00
|
|
|
}
|
2017-11-24 16:34:35 +01:00
|
|
|
child.binders = binders
|
2017-11-24 11:32:35 +01:00
|
|
|
}
|
|
|
|
|
2017-11-24 16:34:35 +01:00
|
|
|
return child
|
2017-11-24 11:32:35 +01:00
|
|
|
}
|
|
|
|
|
2017-11-24 16:34:35 +01:00
|
|
|
func (m *Mvc) In(binders ...interface{}) {
|
|
|
|
for _, binder := range binders {
|
|
|
|
typ := resolveBinderType(binder)
|
|
|
|
|
|
|
|
var (
|
|
|
|
b *InputBinder
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if typ == functionType {
|
|
|
|
b, err = MakeFuncInputBinder(binder)
|
|
|
|
} else if typ == serviceType {
|
|
|
|
b, err = MakeServiceInputBinder(binder)
|
|
|
|
} else {
|
|
|
|
err = errBad
|
|
|
|
}
|
|
|
|
|
2017-11-24 11:32:35 +01:00
|
|
|
if err != nil {
|
2017-11-24 16:34:35 +01:00
|
|
|
continue
|
2017-11-24 11:32:35 +01:00
|
|
|
}
|
2017-11-24 16:34:35 +01:00
|
|
|
|
2017-11-24 11:32:35 +01:00
|
|
|
m.binders = append(m.binders, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Mvc) Handler(handler interface{}) context.Handler {
|
2017-11-24 14:10:30 +01:00
|
|
|
h, _ := MakeHandler(handler, m.binders) // it logs errors already, so on any error the "h" will be nil.
|
|
|
|
return h
|
2017-11-24 11:32:35 +01:00
|
|
|
}
|