test dynfunc package; standardize and refactor api #14

Merged
xdrm-brackets merged 19 commits from test/dynamic into 0.3.0 2020-04-04 10:09:20 +00:00
4 changed files with 9 additions and 13 deletions
Showing only changes of commit e1606273dd - Show all commits

View File

@ -8,16 +8,16 @@ import (
"git.xdrm.io/go/aicra/internal/config"
)
// Build a handler from a service configuration and a HandlerFn
// Build a handler from a service configuration and a dynamic function
//
// a HandlerFn must have as a signature : `func(api.Request, inputStruct) (outputStruct, api.Error)`
// @fn must have as a signature : `func(inputStruct) (*outputStruct, api.Error)`
// - `inputStruct` is a struct{} containing a field for each service input (with valid reflect.Type)
// - `outputStruct` is a struct{} containing a field for each service output (with valid reflect.Type)
//
// Special cases:
// - it there is no input, `inputStruct` can be omitted
// - it there is no output, `outputStruct` can be omitted
func Build(fn HandlerFn, service config.Service) (*Handler, error) {
// - it there is no input, `inputStruct` must be omitted
// - it there is no output, `outputStruct` must be omitted
func Build(fn interface{}, service config.Service) (*Handler, error) {
h := &Handler{
spec: makeSpec(service),
fn: fn,
@ -39,7 +39,7 @@ func Build(fn HandlerFn, service config.Service) (*Handler, error) {
return h, nil
}
// Handle binds input @data into HandleFn and returns map output
// Handle binds input @data into the dynamic function and returns map output
func (h *Handler) Handle(data map[string]interface{}) (map[string]interface{}, api.Error) {
fnv := reflect.ValueOf(h.fn)

View File

@ -2,13 +2,10 @@ package dynamic
import "reflect"
// HandlerFn defines a dynamic handler function
type HandlerFn interface{}
// Handler represents a dynamic api handler
type Handler struct {
spec spec
fn HandlerFn
fn interface{}
}
type spec struct {

View File

@ -16,7 +16,7 @@ type handler struct {
// createHandler builds a handler from its http method and path
// also it checks whether the function signature is valid
func createHandler(method, path string, service config.Service, fn dynamic.HandlerFn) (*handler, error) {
func createHandler(method, path string, service config.Service, fn interface{}) (*handler, error) {
method = strings.ToUpper(method)
dynHandler, err := dynamic.Build(fn, service)

View File

@ -6,7 +6,6 @@ import (
"os"
"git.xdrm.io/go/aicra/datatype"
"git.xdrm.io/go/aicra/dynamic"
"git.xdrm.io/go/aicra/internal/config"
)
@ -47,7 +46,7 @@ func New(configPath string, dtypes ...datatype.T) (*Server, error) {
}
// Handle sets a new handler for an HTTP method to a path
func (s *Server) Handle(method, path string, fn dynamic.HandlerFn) error {
func (s *Server) Handle(method, path string, fn interface{}) error {
// find associated service
var found *config.Service = nil
for _, service := range s.config.Services {