forked from cerc-io/ipld-eth-server
Rename handlers to transformers
This commit is contained in:
parent
a11fb709b2
commit
02b23ef748
@ -1,21 +1,21 @@
|
|||||||
# Handlers
|
# Transformers
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
Handlers must be defined in order to define what events should trigger data updates and how those are performed.
|
Transformers must be defined in order to define what events should trigger data updates and how those are performed.
|
||||||
|
|
||||||
## Interface
|
## Interface
|
||||||
|
|
||||||
### Initializer
|
### Initializer
|
||||||
Accepts DB and Blockchain from Vulcanize and returns a new handler. E.g. for a new object "Cup":
|
Accepts DB and Blockchain from Vulcanize and returns a new transformer. E.g. for a new object "Cup":
|
||||||
`func NewCupHandler(db *postgres.DB, blockchain core.ContractDataFetcher) handlers.Handler`
|
`func NewCupTransformer(db *postgres.DB, blockchain core.ContractDataFetcher) transformers.Transformer`
|
||||||
|
|
||||||
### Execute
|
### Execute
|
||||||
Triggers operations to take in response to a given log event.
|
Triggers operations to take in response to a given log event.
|
||||||
Can persist data from logs, fetch and persist arbitrary data from outside services (e.g. contract state), or take any number of other actions. E.g.:
|
Can persist data from logs, fetch and persist arbitrary data from outside services (e.g. contract state), or take any number of other actions. E.g.:
|
||||||
`func (cupHandler *CupHandler) Execute() error`
|
`func (cupTransformer *CupTransformer) Execute() error`
|
||||||
|
|
||||||
## Additional Requirements
|
## Additional Requirements
|
||||||
Handlers must define log filters and create them so that relevant watched events can be identified and retrieved. E.g.:
|
Transformers must define log filters and create them so that relevant watched events can be identified and retrieved. E.g.:
|
||||||
```$xslt
|
```$xslt
|
||||||
{
|
{
|
||||||
Name: "CupsBite",
|
Name: "CupsBite",
|
@ -6,15 +6,15 @@ import (
|
|||||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Handler interface {
|
type Transformer interface {
|
||||||
Execute() error
|
Execute() error
|
||||||
}
|
}
|
||||||
|
|
||||||
type HandlerInitializer func(db *postgres.DB, blockchain core.Blockchain) Handler
|
type TransformerInitializer func(db *postgres.DB, blockchain core.Blockchain) Transformer
|
||||||
|
|
||||||
func HexToInt64(byteString string) int64 {
|
func HexToInt64(byteString string) int64 {
|
||||||
intHash := common.HexToHash(byteString)
|
value := common.HexToHash(byteString)
|
||||||
return intHash.Big().Int64()
|
return value.Big().Int64()
|
||||||
}
|
}
|
||||||
|
|
||||||
func HexToString(byteString string) string {
|
func HexToString(byteString string) string {
|
@ -6,22 +6,22 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Watcher struct {
|
type Watcher struct {
|
||||||
Handlers []Handler
|
Transformers []Transformer
|
||||||
DB postgres.DB
|
DB postgres.DB
|
||||||
Blockchain core.Blockchain
|
Blockchain core.Blockchain
|
||||||
}
|
}
|
||||||
|
|
||||||
func (watcher *Watcher) AddHandlers(us []HandlerInitializer) {
|
func (watcher *Watcher) AddTransformers(us []TransformerInitializer) {
|
||||||
for _, handlerInitializer := range us {
|
for _, transformerInitializer := range us {
|
||||||
handler := handlerInitializer(&watcher.DB, watcher.Blockchain)
|
transformer := transformerInitializer(&watcher.DB, watcher.Blockchain)
|
||||||
watcher.Handlers = append(watcher.Handlers, handler)
|
watcher.Transformers = append(watcher.Transformers, transformer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (watcher *Watcher) Execute() error {
|
func (watcher *Watcher) Execute() error {
|
||||||
var err error
|
var err error
|
||||||
for _, handler := range watcher.Handlers {
|
for _, transformer := range watcher.Transformers {
|
||||||
err = handler.Execute()
|
err = transformer.Execute()
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -10,12 +10,12 @@ import (
|
|||||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MockHandler struct {
|
type MockTransformer struct {
|
||||||
executeWasCalled bool
|
executeWasCalled bool
|
||||||
executeError error
|
executeError error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mh *MockHandler) Execute() error {
|
func (mh *MockTransformer) Execute() error {
|
||||||
if mh.executeError != nil {
|
if mh.executeError != nil {
|
||||||
return mh.executeError
|
return mh.executeError
|
||||||
}
|
}
|
||||||
@ -23,47 +23,47 @@ func (mh *MockHandler) Execute() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func fakeHandlerInitializer(db *postgres.DB, blockchain core.Blockchain) shared.Handler {
|
func fakeTransformerInitializer(db *postgres.DB, blockchain core.Blockchain) shared.Transformer {
|
||||||
return &MockHandler{}
|
return &MockTransformer{}
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ = Describe("Watcher", func() {
|
var _ = Describe("Watcher", func() {
|
||||||
It("Adds handlers", func() {
|
It("Adds transformers", func() {
|
||||||
watcher := shared.Watcher{}
|
watcher := shared.Watcher{}
|
||||||
|
|
||||||
watcher.AddHandlers([]shared.HandlerInitializer{fakeHandlerInitializer})
|
watcher.AddTransformers([]shared.TransformerInitializer{fakeTransformerInitializer})
|
||||||
|
|
||||||
Expect(len(watcher.Handlers)).To(Equal(1))
|
Expect(len(watcher.Transformers)).To(Equal(1))
|
||||||
Expect(watcher.Handlers).To(ConsistOf(&MockHandler{}))
|
Expect(watcher.Transformers).To(ConsistOf(&MockTransformer{}))
|
||||||
})
|
})
|
||||||
|
|
||||||
It("Adds handlers from multiple sources", func() {
|
It("Adds transformers from multiple sources", func() {
|
||||||
watcher := shared.Watcher{}
|
watcher := shared.Watcher{}
|
||||||
|
|
||||||
watcher.AddHandlers([]shared.HandlerInitializer{fakeHandlerInitializer})
|
watcher.AddTransformers([]shared.TransformerInitializer{fakeTransformerInitializer})
|
||||||
watcher.AddHandlers([]shared.HandlerInitializer{fakeHandlerInitializer})
|
watcher.AddTransformers([]shared.TransformerInitializer{fakeTransformerInitializer})
|
||||||
|
|
||||||
Expect(len(watcher.Handlers)).To(Equal(2))
|
Expect(len(watcher.Transformers)).To(Equal(2))
|
||||||
})
|
})
|
||||||
|
|
||||||
It("Executes each handler", func() {
|
It("Executes each transformer", func() {
|
||||||
watcher := shared.Watcher{}
|
watcher := shared.Watcher{}
|
||||||
fakeHandler := &MockHandler{}
|
fakeTransformer := &MockTransformer{}
|
||||||
watcher.Handlers = []shared.Handler{fakeHandler}
|
watcher.Transformers = []shared.Transformer{fakeTransformer}
|
||||||
|
|
||||||
watcher.Execute()
|
watcher.Execute()
|
||||||
|
|
||||||
Expect(fakeHandler.executeWasCalled).To(BeTrue())
|
Expect(fakeTransformer.executeWasCalled).To(BeTrue())
|
||||||
})
|
})
|
||||||
|
|
||||||
It("Returns an error if handler returns an error", func() {
|
It("Returns an error if transformer returns an error", func() {
|
||||||
watcher := shared.Watcher{}
|
watcher := shared.Watcher{}
|
||||||
fakeHandler := &MockHandler{executeError: errors.New("Something bad happened")}
|
fakeTransformer := &MockTransformer{executeError: errors.New("Something bad happened")}
|
||||||
watcher.Handlers = []shared.Handler{fakeHandler}
|
watcher.Transformers = []shared.Transformer{fakeTransformer}
|
||||||
|
|
||||||
err := watcher.Execute()
|
err := watcher.Execute()
|
||||||
|
|
||||||
Expect(err).To(HaveOccurred())
|
Expect(err).To(HaveOccurred())
|
||||||
Expect(fakeHandler.executeWasCalled).To(BeFalse())
|
Expect(fakeTransformer.executeWasCalled).To(BeFalse())
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user