From 02b23ef7488e78fb1a48cee2725739cffb0e06b2 Mon Sep 17 00:00:00 2001 From: Rob Mulholand Date: Fri, 16 Mar 2018 14:34:18 -0500 Subject: [PATCH] Rename handlers to transformers --- ...{HandlerREADME.md => TransformerREADME.md} | 12 +++--- ..._interface.go => transformer_interface.go} | 8 ++-- libraries/shared/watcher.go | 14 +++---- libraries/shared/watcher_test.go | 40 +++++++++---------- 4 files changed, 37 insertions(+), 37 deletions(-) rename libraries/shared/{HandlerREADME.md => TransformerREADME.md} (50%) rename libraries/shared/{handler_interface.go => transformer_interface.go} (65%) diff --git a/libraries/shared/HandlerREADME.md b/libraries/shared/TransformerREADME.md similarity index 50% rename from libraries/shared/HandlerREADME.md rename to libraries/shared/TransformerREADME.md index 878a4091..1049f271 100644 --- a/libraries/shared/HandlerREADME.md +++ b/libraries/shared/TransformerREADME.md @@ -1,21 +1,21 @@ -# Handlers +# Transformers ## 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 ### Initializer -Accepts DB and Blockchain from Vulcanize and returns a new handler. E.g. for a new object "Cup": -`func NewCupHandler(db *postgres.DB, blockchain core.ContractDataFetcher) handlers.Handler` +Accepts DB and Blockchain from Vulcanize and returns a new transformer. E.g. for a new object "Cup": +`func NewCupTransformer(db *postgres.DB, blockchain core.ContractDataFetcher) transformers.Transformer` ### Execute 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.: -`func (cupHandler *CupHandler) Execute() error` +`func (cupTransformer *CupTransformer) Execute() error` ## 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 { Name: "CupsBite", diff --git a/libraries/shared/handler_interface.go b/libraries/shared/transformer_interface.go similarity index 65% rename from libraries/shared/handler_interface.go rename to libraries/shared/transformer_interface.go index 4382fba1..6a0c753a 100644 --- a/libraries/shared/handler_interface.go +++ b/libraries/shared/transformer_interface.go @@ -6,15 +6,15 @@ import ( "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" ) -type Handler interface { +type Transformer interface { 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 { - intHash := common.HexToHash(byteString) - return intHash.Big().Int64() + value := common.HexToHash(byteString) + return value.Big().Int64() } func HexToString(byteString string) string { diff --git a/libraries/shared/watcher.go b/libraries/shared/watcher.go index 2a18afec..369b20a0 100644 --- a/libraries/shared/watcher.go +++ b/libraries/shared/watcher.go @@ -6,22 +6,22 @@ import ( ) type Watcher struct { - Handlers []Handler + Transformers []Transformer DB postgres.DB Blockchain core.Blockchain } -func (watcher *Watcher) AddHandlers(us []HandlerInitializer) { - for _, handlerInitializer := range us { - handler := handlerInitializer(&watcher.DB, watcher.Blockchain) - watcher.Handlers = append(watcher.Handlers, handler) +func (watcher *Watcher) AddTransformers(us []TransformerInitializer) { + for _, transformerInitializer := range us { + transformer := transformerInitializer(&watcher.DB, watcher.Blockchain) + watcher.Transformers = append(watcher.Transformers, transformer) } } func (watcher *Watcher) Execute() error { var err error - for _, handler := range watcher.Handlers { - err = handler.Execute() + for _, transformer := range watcher.Transformers { + err = transformer.Execute() } return err } diff --git a/libraries/shared/watcher_test.go b/libraries/shared/watcher_test.go index 2b79085c..729c4908 100644 --- a/libraries/shared/watcher_test.go +++ b/libraries/shared/watcher_test.go @@ -10,12 +10,12 @@ import ( "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" ) -type MockHandler struct { +type MockTransformer struct { executeWasCalled bool executeError error } -func (mh *MockHandler) Execute() error { +func (mh *MockTransformer) Execute() error { if mh.executeError != nil { return mh.executeError } @@ -23,47 +23,47 @@ func (mh *MockHandler) Execute() error { return nil } -func fakeHandlerInitializer(db *postgres.DB, blockchain core.Blockchain) shared.Handler { - return &MockHandler{} +func fakeTransformerInitializer(db *postgres.DB, blockchain core.Blockchain) shared.Transformer { + return &MockTransformer{} } var _ = Describe("Watcher", func() { - It("Adds handlers", func() { + It("Adds transformers", func() { watcher := shared.Watcher{} - watcher.AddHandlers([]shared.HandlerInitializer{fakeHandlerInitializer}) + watcher.AddTransformers([]shared.TransformerInitializer{fakeTransformerInitializer}) - Expect(len(watcher.Handlers)).To(Equal(1)) - Expect(watcher.Handlers).To(ConsistOf(&MockHandler{})) + Expect(len(watcher.Transformers)).To(Equal(1)) + Expect(watcher.Transformers).To(ConsistOf(&MockTransformer{})) }) - It("Adds handlers from multiple sources", func() { + It("Adds transformers from multiple sources", func() { watcher := shared.Watcher{} - watcher.AddHandlers([]shared.HandlerInitializer{fakeHandlerInitializer}) - watcher.AddHandlers([]shared.HandlerInitializer{fakeHandlerInitializer}) + watcher.AddTransformers([]shared.TransformerInitializer{fakeTransformerInitializer}) + 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{} - fakeHandler := &MockHandler{} - watcher.Handlers = []shared.Handler{fakeHandler} + fakeTransformer := &MockTransformer{} + watcher.Transformers = []shared.Transformer{fakeTransformer} 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{} - fakeHandler := &MockHandler{executeError: errors.New("Something bad happened")} - watcher.Handlers = []shared.Handler{fakeHandler} + fakeTransformer := &MockTransformer{executeError: errors.New("Something bad happened")} + watcher.Transformers = []shared.Transformer{fakeTransformer} err := watcher.Execute() Expect(err).To(HaveOccurred()) - Expect(fakeHandler.executeWasCalled).To(BeFalse()) + Expect(fakeTransformer.executeWasCalled).To(BeFalse()) }) })