Chain is a package
This commit is contained in:
parent
ed17fbb9ef
commit
18ca775e02
|
@ -1,18 +1,16 @@
|
||||||
package core
|
package chain
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/localhots/yeast/unit"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
Chain struct {
|
Chain struct {
|
||||||
Flow Flow
|
Flow Flow
|
||||||
Links []Caller
|
Links []unit.Caller
|
||||||
}
|
|
||||||
Caller interface {
|
|
||||||
Call([]byte) ([]byte, error)
|
|
||||||
Units() []string
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -20,6 +18,11 @@ const (
|
||||||
LF = byte(10)
|
LF = byte(10)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func New(name string) *Chain {
|
||||||
|
c, _ := chains[name]
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Chain) Call(data []byte) (resp []byte, err error) {
|
func (c *Chain) Call(data []byte) (resp []byte, err error) {
|
||||||
switch c.Flow {
|
switch c.Flow {
|
||||||
case SequentialFlow:
|
case SequentialFlow:
|
|
@ -1,4 +1,4 @@
|
||||||
package core
|
package chain
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
@ -6,34 +6,37 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
|
"github.com/localhots/yeast/unit"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ParseChains() (map[string]*Chain, error) {
|
var (
|
||||||
f, err := os.Open(Conf().ChainsConfig)
|
chains = map[string]*Chain{}
|
||||||
|
)
|
||||||
|
|
||||||
|
func LoadChains(path string) {
|
||||||
|
f, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("Failed to open chains config: " + Conf().ChainsConfig)
|
panic("Failed to open chains config: " + path)
|
||||||
}
|
}
|
||||||
b, err := ioutil.ReadAll(f)
|
b, err := ioutil.ReadAll(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("Failed to parse chains config: " + Conf().ChainsConfig)
|
panic("Failed to read chains config: " + path)
|
||||||
}
|
}
|
||||||
|
|
||||||
var schema map[string]interface{}
|
var schema map[string]interface{}
|
||||||
if err := json.Unmarshal(b, &schema); err != nil {
|
if err := json.Unmarshal(b, &schema); err != nil {
|
||||||
return nil, err
|
panic("Failed to parse chains config: " + path)
|
||||||
}
|
}
|
||||||
|
|
||||||
chains := map[string]*Chain{}
|
for name, c := range schema {
|
||||||
for name, chain := range schema {
|
chains[name] = Parse(interface{}(c))
|
||||||
chains[name] = buildChain(interface{}(chain))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return chains, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildChain(conf interface{}) *Chain {
|
func Parse(conf interface{}) *Chain {
|
||||||
c := &Chain{
|
c := &Chain{
|
||||||
Links: []Caller{},
|
Links: []unit.Caller{},
|
||||||
}
|
}
|
||||||
|
|
||||||
for f, links := range conf.(map[string]interface{}) {
|
for f, links := range conf.(map[string]interface{}) {
|
||||||
|
@ -48,17 +51,16 @@ func buildChain(conf interface{}) *Chain {
|
||||||
|
|
||||||
switch val.Kind() {
|
switch val.Kind() {
|
||||||
case reflect.Map:
|
case reflect.Map:
|
||||||
subchain := buildChain(interface{}(link))
|
subchain := Parse(interface{}(link))
|
||||||
if len(subchain.Links) > 0 {
|
if len(subchain.Links) > 0 {
|
||||||
c.Links = append(c.Links, Caller(subchain))
|
c.Links = append(c.Links, unit.Caller(subchain))
|
||||||
}
|
}
|
||||||
case reflect.String:
|
case reflect.String:
|
||||||
name := link.(string)
|
name := link.(string)
|
||||||
caller, ok := Units[name]
|
if caller := unit.New(name); caller != nil {
|
||||||
if !ok {
|
|
||||||
fmt.Println("Unknown unit:", name)
|
|
||||||
} else {
|
|
||||||
c.Links = append(c.Links, caller)
|
c.Links = append(c.Links, caller)
|
||||||
|
} else {
|
||||||
|
fmt.Println("Unknown unit:", name)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
panic("Unexpected chain element: " + val.Kind().String())
|
panic("Unexpected chain element: " + val.Kind().String())
|
Loading…
Reference in New Issue