diff --git a/context.go b/context.go index 55c44fb..a8984a4 100644 --- a/context.go +++ b/context.go @@ -2,6 +2,7 @@ package hunter // TaskContext 上下文 type TaskContext struct { + share map[string]interface{} hunter *Hunter curNode ITaskNode } @@ -16,10 +17,27 @@ func (cxt *TaskContext) AddTask(itask ITask) { if children := cxt.curNode.Children(); children == nil { cxt.curNode.SetChildren(cxt.hunter.createQueue()) } - cxt.curNode.Children().Push(itask) + bt := &BaseTask{} + bt.SetTask(itask) + cxt.curNode.Children().Push(bt) } // AddParentTask 添加到当前任务队列 func (cxt *TaskContext) AddParentTask(itask ITask) { - cxt.curNode.Parent().Children().Push(itask) + bt := &BaseTask{} + bt.SetTask(itask) + cxt.curNode.Parent().Children().Push(bt) +} + +// GetShare 获取share的数据, 存储用的 +func (cxt *TaskContext) GetShare(key string) interface{} { + if v, ok := cxt.share[key]; ok { + return v + } + return nil +} + +// SetShare 设置share的数据, 存储用的 +func (cxt *TaskContext) SetShare(key string, value interface{}) { + cxt.share[key] = value } diff --git a/go.mod b/go.mod index 7643f8d..6d02b85 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,6 @@ module github.com/474420502/hunter go 1.14 -require github.com/474420502/focus v0.8.1 +require ( + github.com/474420502/focus v0.8.1 +) diff --git a/go.sum b/go.sum index 6902777..8cf793a 100644 --- a/go.sum +++ b/go.sum @@ -3,3 +3,23 @@ github.com/474420502/focus v0.8.1/go.mod h1:jrDXvK1CnUJ3PCR3ZJVYinbS2Yz5kM8OoAbC github.com/Pallinder/go-randomdata v1.1.0/go.mod h1:yHmJgulpD2Nfrm0cR9tI/+oAgRqCQQixsA8HyRZfV9Y= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/mdempsky/gocode v0.0.0-20200405233807-4acdcbdea79d h1:P8ngpzttYTqLj67Xt66V+2o5C53fGhXvkbqwFXMFvVI= +github.com/mdempsky/gocode v0.0.0-20200405233807-4acdcbdea79d/go.mod h1:hltEC42XzfMNgg0S1v6JTywwra2Mu6F6cLR03debVQ8= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200407143752-a3568bac92ae h1:nMRC2i7oRGagyN5g3chxBF7gb2do/tIJ3ihoEnvYvsU= +golang.org/x/tools v0.0.0-20200407143752-a3568bac92ae/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/hunter.go b/hunter.go index 65afcc4..7cfd3e3 100644 --- a/hunter.go +++ b/hunter.go @@ -36,6 +36,7 @@ func NewPriorityHunter(queueCreator func() *pqueue.PriorityQueue) *Hunter { hunter.cxt = NewContext() hunter.cxt.curNode = hunter.task + hunter.cxt.share = make(map[string]interface{}) return hunter } diff --git a/hunter_test.go b/hunter_test.go index b669681..ee88348 100644 --- a/hunter_test.go +++ b/hunter_test.go @@ -1,7 +1,19 @@ package hunter -import "testing" +import ( + "testing" +) + +type Web struct { +} + +func (web Web) Execute(cxt *TaskContext) { + cxt.SetShare("123", 123) +} func TestCase1(t *testing.T) { - + hunter := NewHunter() + hunter.AddTask(&Web{}) + hunter.Execute() + t.Error(hunter.cxt.GetShare("123")) } diff --git a/priority.go b/priority.go index 6966809..4136898 100644 --- a/priority.go +++ b/priority.go @@ -52,12 +52,13 @@ func CreatePriorityMinQueue() *pqueue.PriorityQueue { // priorityMax 最大值优先 func priorityMax(k1, k2 interface{}) int { p1, p2 := 0.0, 0.0 + n1, n2 := k1.(ITaskNode), k2.(ITaskNode) - if priority, ok := k1.(IPriority); ok { + if priority, ok := n1.Task().(IPriority); ok { p1 = priority.Priority() } - if priority, ok := k2.(IPriority); ok { + if priority, ok := n2.Task().(IPriority); ok { p2 = priority.Priority() } @@ -70,12 +71,13 @@ func priorityMax(k1, k2 interface{}) int { // priorityMin 最小值优先 func priorityMin(k1, k2 interface{}) int { p1, p2 := 0.0, 0.0 + n1, n2 := k1.(ITaskNode), k2.(ITaskNode) - if priority, ok := k1.(IPriority); ok { + if priority, ok := n1.Task().(IPriority); ok { p1 = priority.Priority() } - if priority, ok := k2.(IPriority); ok { + if priority, ok := n2.Task().(IPriority); ok { p2 = priority.Priority() }