From 71381ad1a85d947fdc5ffb22d78b24eda27936fd Mon Sep 17 00:00:00 2001 From: Sodesnei <1452401269@qq.com> Date: Sun, 19 Sep 2021 18:29:45 +0800 Subject: [PATCH] middle --- pkg/interface.go | 119 ------------------ pkg/middle/{Data.go => data.go} | 0 pkg/middle/{Middle.go => middle.go} | 0 pkg/middle/{Middle_test.go => middle_test.go} | 0 pkg/middle/{Send.go => send.go} | 0 pkg/middle/{Storage.go => storage.go} | 0 pkg/middle/{Upload.go => upload.go} | 0 pkg/test/Hello.go | 5 - pkg/test/Hello_test.go | 11 -- 9 files changed, 135 deletions(-) delete mode 100644 pkg/interface.go rename pkg/middle/{Data.go => data.go} (100%) rename pkg/middle/{Middle.go => middle.go} (100%) rename pkg/middle/{Middle_test.go => middle_test.go} (100%) rename pkg/middle/{Send.go => send.go} (100%) rename pkg/middle/{Storage.go => storage.go} (100%) rename pkg/middle/{Upload.go => upload.go} (100%) delete mode 100644 pkg/test/Hello.go delete mode 100644 pkg/test/Hello_test.go diff --git a/pkg/interface.go b/pkg/interface.go deleted file mode 100644 index c165027..0000000 --- a/pkg/interface.go +++ /dev/null @@ -1,119 +0,0 @@ -package pkg - -import "time" - -type Value interface { - Get(key string) interface{} - Put(key string, val interface{}) error -} - -type Data struct { - m map[string]interface{} -} - -func (d *Data) Get(key string) interface{} { - return d.m[key] -} - -func (d *Data) Put(key string, val interface{}) error { - d.m[key] = val - return nil -} - -func NewData() *Data { - return &Data{ - m: make(map[string]interface{}), - } -} - -type MiddlewareConf struct { - Weight int - Middle Middleware - IsOut bool -} - -type Driver struct { - conf []*MiddlewareConf -} - -func (d *Driver) Stat(value Value) error { - for _, m := range d.conf { - m.Middle.Put(value) // 协程 - value = m.Middle.Out() - } - - return nil -} - -func NewDriver() *Driver { - return new(Driver) -} - -func main() { - conf := []*MiddlewareConf{ - { - Weight: 1, - Middle: NewM1(), - IsOut: false, - }, - { - Weight: 2, - Middle: NewM2(), - IsOut: true, - }, - } - - driver := NewDriver() - driver.conf = conf - - // 调用中间件驱动 - val := NewData() - val.Put("2", 2) - - driver.Stat(val) - -} - -type Middleware interface { - Put(val Value) - Out() Value -} - -type M1 struct { - ch chan *Data -} - -func (m *M1) Put(val Value) { - time.Sleep(200 * time.Second) - da := NewData() - da.Put("1", val) - m.ch <- da -} - -func (m *M1) Out() Value { - return <-m.ch -} - -func NewM1() *M1 { - return &M1{ - make(chan *Data), - } -} - -type M2 struct { - da Value -} - -func (m *M2) Put(val Value) { - da := NewData() - m.da = da -} - -func (m *M2) Out() Value { - m.da.Put("2", "plp") - return m.da -} - -func NewM2() *M2 { - return &M2{} -} diff --git a/pkg/middle/Data.go b/pkg/middle/data.go similarity index 100% rename from pkg/middle/Data.go rename to pkg/middle/data.go diff --git a/pkg/middle/Middle.go b/pkg/middle/middle.go similarity index 100% rename from pkg/middle/Middle.go rename to pkg/middle/middle.go diff --git a/pkg/middle/Middle_test.go b/pkg/middle/middle_test.go similarity index 100% rename from pkg/middle/Middle_test.go rename to pkg/middle/middle_test.go diff --git a/pkg/middle/Send.go b/pkg/middle/send.go similarity index 100% rename from pkg/middle/Send.go rename to pkg/middle/send.go diff --git a/pkg/middle/Storage.go b/pkg/middle/storage.go similarity index 100% rename from pkg/middle/Storage.go rename to pkg/middle/storage.go diff --git a/pkg/middle/Upload.go b/pkg/middle/upload.go similarity index 100% rename from pkg/middle/Upload.go rename to pkg/middle/upload.go diff --git a/pkg/test/Hello.go b/pkg/test/Hello.go deleted file mode 100644 index 5a6b557..0000000 --- a/pkg/test/Hello.go +++ /dev/null @@ -1,5 +0,0 @@ -package test - -func Hello(str string) (bool, error) { - return str == "Nice", nil -} diff --git a/pkg/test/Hello_test.go b/pkg/test/Hello_test.go deleted file mode 100644 index 1b0c4fd..0000000 --- a/pkg/test/Hello_test.go +++ /dev/null @@ -1,11 +0,0 @@ -package test - -import ( - "github.com/stretchr/testify/require" - "testing" -) - -func TestHello(t *testing.T) { - _, err := Hello("Nice") - require.NoError(t, err) -}