-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Rewrite handling * Minor refactoring * Rename variables * Update README.md * Update README.md
- Loading branch information
Showing
4 changed files
with
109 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package router | ||
|
||
import ( | ||
"encoding/json" | ||
"reflect" | ||
) | ||
|
||
// Handler is an abstract function | ||
type Handler struct { | ||
function interface{} | ||
} | ||
|
||
// Prepare parses event payload | ||
func (h *Handler) Prepare(payload json.RawMessage) ([]reflect.Value, error) { | ||
argsType := reflect.TypeOf(h.function).In(0) | ||
args := reflect.New(argsType) | ||
|
||
if err := json.Unmarshal(payload, args.Interface()); err != nil { | ||
return nil, err | ||
} | ||
|
||
return append([]reflect.Value{}, args.Elem()), nil | ||
} | ||
|
||
// Call the handler and pass event | ||
func (h *Handler) Call(args []reflect.Value) (interface{}, error) { | ||
response := reflect.ValueOf(h.function).Call(args) | ||
|
||
return response[0].Interface(), response[1].Interface().(error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters