2020-04-04 08:36:52 +00:00
|
|
|
package dynfunc
|
2020-03-29 13:00:22 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
|
2020-03-29 15:01:02 +00:00
|
|
|
"git.xdrm.io/go/aicra/api"
|
2020-03-29 13:00:22 +00:00
|
|
|
"git.xdrm.io/go/aicra/internal/config"
|
|
|
|
)
|
|
|
|
|
2020-04-04 10:45:36 +00:00
|
|
|
// Handler represents a dynamic api handler
|
|
|
|
type Handler struct {
|
|
|
|
spec spec
|
|
|
|
fn interface{}
|
|
|
|
}
|
|
|
|
|
2020-04-04 08:05:27 +00:00
|
|
|
// Build a handler from a service configuration and a dynamic function
|
2020-03-29 13:00:22 +00:00
|
|
|
//
|
2021-03-28 16:49:23 +00:00
|
|
|
// @fn must have as a signature : `func(inputStruct) (*outputStruct, api.Err)`
|
2020-03-29 13:00:22 +00:00
|
|
|
// - `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:
|
2020-04-04 08:05:27 +00:00
|
|
|
// - 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) {
|
2020-03-29 13:00:22 +00:00
|
|
|
h := &Handler{
|
|
|
|
spec: makeSpec(service),
|
|
|
|
fn: fn,
|
|
|
|
}
|
|
|
|
|
2020-03-29 14:58:53 +00:00
|
|
|
fnv := reflect.ValueOf(fn)
|
2020-03-29 13:00:22 +00:00
|
|
|
|
2020-03-29 14:58:53 +00:00
|
|
|
if fnv.Type().Kind() != reflect.Func {
|
2020-04-04 10:46:43 +00:00
|
|
|
return nil, errHandlerNotFunc
|
2020-03-29 13:00:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 14:58:53 +00:00
|
|
|
if err := h.spec.checkInput(fnv); err != nil {
|
2020-03-29 13:00:22 +00:00
|
|
|
return nil, fmt.Errorf("input: %w", err)
|
|
|
|
}
|
2020-03-29 14:58:53 +00:00
|
|
|
if err := h.spec.checkOutput(fnv); err != nil {
|
2020-03-29 13:00:22 +00:00
|
|
|
return nil, fmt.Errorf("output: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return h, nil
|
|
|
|
}
|
|
|
|
|
2020-04-04 08:05:27 +00:00
|
|
|
// Handle binds input @data into the dynamic function and returns map output
|
2021-04-17 12:03:59 +00:00
|
|
|
func (h *Handler) Handle(ctx *api.Context, data map[string]interface{}) (map[string]interface{}, api.Err) {
|
2021-03-28 16:49:23 +00:00
|
|
|
var ert = reflect.TypeOf(api.Err{})
|
|
|
|
var fnv = reflect.ValueOf(h.fn)
|
2020-03-29 13:00:22 +00:00
|
|
|
|
2020-03-29 15:01:02 +00:00
|
|
|
callArgs := []reflect.Value{}
|
|
|
|
|
|
|
|
// bind input data
|
|
|
|
if fnv.Type().NumIn() > 0 {
|
|
|
|
// create zero value struct
|
|
|
|
callStructPtr := reflect.New(fnv.Type().In(0))
|
|
|
|
callStruct := callStructPtr.Elem()
|
|
|
|
|
|
|
|
// set each field
|
|
|
|
for name := range h.spec.Input {
|
|
|
|
field := callStruct.FieldByName(name)
|
|
|
|
if !field.CanSet() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// get value from @data
|
|
|
|
value, inData := data[name]
|
|
|
|
if !inData {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
field.Set(reflect.ValueOf(value).Convert(field.Type()))
|
|
|
|
}
|
|
|
|
callArgs = append(callArgs, callStruct)
|
|
|
|
}
|
|
|
|
|
|
|
|
// call the HandlerFn
|
|
|
|
output := fnv.Call(callArgs)
|
|
|
|
|
|
|
|
// no output OR pointer to output struct is nil
|
|
|
|
outdata := make(map[string]interface{})
|
|
|
|
if len(h.spec.Output) < 1 || output[0].IsNil() {
|
2021-03-28 16:49:23 +00:00
|
|
|
var structerr = output[len(output)-1].Convert(ert)
|
|
|
|
return outdata, api.Err{
|
|
|
|
Code: int(structerr.FieldByName("Code").Int()),
|
|
|
|
Reason: structerr.FieldByName("Reason").String(),
|
|
|
|
Status: int(structerr.FieldByName("Status").Int()),
|
|
|
|
}
|
2020-03-29 15:01:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// extract struct from pointer
|
|
|
|
returnStruct := output[0].Elem()
|
|
|
|
|
|
|
|
for name := range h.spec.Output {
|
|
|
|
field := returnStruct.FieldByName(name)
|
|
|
|
outdata[name] = field.Interface()
|
|
|
|
}
|
|
|
|
|
2021-03-28 16:49:23 +00:00
|
|
|
// extract api.Err
|
|
|
|
var structerr = output[len(output)-1].Convert(ert)
|
|
|
|
return outdata, api.Err{
|
|
|
|
Code: int(structerr.FieldByName("Code").Int()),
|
|
|
|
Reason: structerr.FieldByName("Reason").String(),
|
|
|
|
Status: int(structerr.FieldByName("Status").Int()),
|
|
|
|
}
|
2020-03-29 13:00:22 +00:00
|
|
|
}
|