diff --git a/registry/registry.go b/registry/registry.go index 7d4c18f..bef09f6 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -3,4 +3,5 @@ package registry type RESTInvoker interface { HasMethod(method string) bool Invoke(method string, params []string, leadingParams ...interface{}) (result interface{}, err error) + InvokeWithBytes(method string, params [][]byte, leadingParams ...interface{}) (result interface{}, err error) } diff --git a/registry/rest-registry.go b/registry/rest-registry.go index cbafcbb..fba1a18 100644 --- a/registry/rest-registry.go +++ b/registry/rest-registry.go @@ -1,7 +1,9 @@ package registry import ( + "fmt" "reflect" + "strconv" cuej "git.loafle.net/commons/util-go/encoding/json" cus "git.loafle.net/commons/util-go/service" @@ -87,6 +89,48 @@ func (rr *restRegistry) HasMethod(method string) bool { return false } +// Invoke execute a method. +// +// Codecs are defined to process a given serialization scheme, e.g., JSON or +// XML. A codec is chosen based on the "Content-Type" header from the request, +// excluding the charset definition. +func (rr *restRegistry) InvokeWithBytes(method string, params [][]byte, leadingParams ...interface{}) (result interface{}, err error) { + _, methodSpec, errGet := rr.serviceRegistry.Get(method) + if nil != errGet { + return nil, errGet + } + // Decode the args. + + pValues, pInstances := methodSpec.ParamValues() + + lParamLen := 0 + if nil != leadingParams { + lParamLen = len(leadingParams) + } + + var _params []string + + if nil != pInstances && 0 < len(pInstances) { + if len(pInstances) != (len(params) + lParamLen) { + return nil, fmt.Errorf("count of parameter is not same method[%d] and %d", len(pInstances), (len(params) + lParamLen)) + } + + for indexC := 0; indexC < len(params); indexC++ { + if pValues[indexC+lParamLen].Type().Kind() != reflect.String { + _params = append(_params, string(params[indexC])) + continue + } + ts, err := strconv.Unquote(string(params[indexC])) + if nil != err { + return nil, err + } + _params = append(_params, ts) + } + } + + return rr.Invoke(method, _params, leadingParams...) +} + // Invoke execute a method. // // Codecs are defined to process a given serialization scheme, e.g., JSON or