mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-19 14:53:44 +00:00
cgo invoke params
This commit is contained in:
1 parent
4b4c03fd90
commit
63296f6950
5 files changed
+28
-35
No files matched your search
+8
-3
@@ -2,11 +2,11 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/GopeedLab/gopeed/pkg/api/model"
|
"github.com/GopeedLab/gopeed/pkg/api/model"
|
||||||
"github.com/GopeedLab/gopeed/pkg/base"
|
"github.com/GopeedLab/gopeed/pkg/base"
|
||||||
"github.com/GopeedLab/gopeed/pkg/download"
|
"github.com/GopeedLab/gopeed/pkg/download"
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/util"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
@@ -346,7 +346,7 @@ func (i *Instance) Close() *model.Result[any] {
|
|||||||
|
|
||||||
type Request struct {
|
type Request struct {
|
||||||
Method string `json:"method"`
|
Method string `json:"method"`
|
||||||
Params []string `json:"params"`
|
Params []any `json:"params"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Invoke support dynamic call method
|
// Invoke support dynamic call method
|
||||||
@@ -364,6 +364,11 @@ func Invoke(instance *Instance, request *Request) (ret any) {
|
|||||||
in := make([]reflect.Value, numIn)
|
in := make([]reflect.Value, numIn)
|
||||||
for i := 0; i < numIn; i++ {
|
for i := 0; i < numIn; i++ {
|
||||||
paramType := fn.Type().In(i)
|
paramType := fn.Type().In(i)
|
||||||
|
arg := args[i]
|
||||||
|
if arg == nil {
|
||||||
|
in[i] = reflect.Zero(fn.Type().In(i))
|
||||||
|
continue
|
||||||
|
}
|
||||||
var param reflect.Value
|
var param reflect.Value
|
||||||
var paramPtr any
|
var paramPtr any
|
||||||
if paramType.Kind() == reflect.Ptr {
|
if paramType.Kind() == reflect.Ptr {
|
||||||
@@ -373,7 +378,7 @@ func Invoke(instance *Instance, request *Request) (ret any) {
|
|||||||
param = reflect.New(paramType).Elem()
|
param = reflect.New(paramType).Elem()
|
||||||
paramPtr = param.Addr().Interface()
|
paramPtr = param.Addr().Interface()
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal([]byte(args[i]), paramPtr); err != nil {
|
if err := util.MapToStruct(arg, paramPtr); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
in[i] = param
|
in[i] = param
|
||||||
|
|||||||
+14
-17
@@ -10,26 +10,20 @@ import (
|
|||||||
var instance, _ = Create(nil)
|
var instance, _ = Create(nil)
|
||||||
|
|
||||||
func TestInvoke(t *testing.T) {
|
func TestInvoke(t *testing.T) {
|
||||||
doTestInvoke(t, "Info", []any{})
|
doTestInvoke(t, "Info", []any{}, model.CodeOk)
|
||||||
doTestInvoke(t, "GetTasks", nil)
|
doTestInvoke(t, "Info1", []any{}, model.CodeError)
|
||||||
doTestInvoke(t, "GetTasks", []any{})
|
doTestInvoke(t, "CreateTask", []any{nil}, model.CodeError)
|
||||||
doTestInvoke(t, "GetTasks", []any{nil})
|
doTestInvoke(t, "GetTasks", nil, model.CodeError)
|
||||||
doTestInvoke(t, "GetTasks", []any{&download.TaskFilter{}})
|
doTestInvoke(t, "GetTasks", []any{}, model.CodeError)
|
||||||
}
|
doTestInvoke(t, "GetTasks", []any{"{abc:123"}, model.CodeError)
|
||||||
|
doTestInvoke(t, "GetTasks", []any{&download.TaskFilter{}}, model.CodeOk)
|
||||||
func doTestInvoke(t *testing.T, method string, params []any) *model.Result[any] {
|
doTestInvoke(t, "SwitchExtension", []any{"test", &model.SwitchExtension{Status: true}}, model.CodeError)
|
||||||
reqParams := make([]string, len(params))
|
|
||||||
for i, p := range params {
|
|
||||||
bytes, err := json.Marshal(p)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
reqParams[i] = string(bytes)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func doTestInvoke(t *testing.T, method string, params []any, expectCode model.RespCode) {
|
||||||
result := Invoke(instance, &Request{
|
result := Invoke(instance, &Request{
|
||||||
Method: method,
|
Method: method,
|
||||||
Params: reqParams,
|
Params: params,
|
||||||
})
|
})
|
||||||
buf, err := json.Marshal(result)
|
buf, err := json.Marshal(result)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -40,5 +34,8 @@ func doTestInvoke(t *testing.T, method string, params []any) *model.Result[any]
|
|||||||
if err := json.Unmarshal(buf, &res); err != nil {
|
if err := json.Unmarshal(buf, &res); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
return &res
|
|
||||||
|
if res.Code != expectCode {
|
||||||
|
t.Fatalf("Invoke method [%s] failed, expect code [%d], got [%d], msg: %s", method, expectCode, res.Code, res.Msg)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -62,14 +62,7 @@ class LibgopeedBootNative
|
|||||||
List<dynamic> params,
|
List<dynamic> params,
|
||||||
T Function(dynamic json)? fromJsonT,
|
T Function(dynamic json)? fromJsonT,
|
||||||
) async {
|
) async {
|
||||||
final invokeRequest = InvokeRequest(
|
final invokeRequest = InvokeRequest(method: method, params: params);
|
||||||
method: method,
|
|
||||||
params: params.map((e) {
|
|
||||||
if (e.toJson != null) {
|
|
||||||
return jsonEncode(e.toJson());
|
|
||||||
}
|
|
||||||
return jsonEncode(e);
|
|
||||||
}).toList());
|
|
||||||
final resp = await _libgopeedAbi.invoke(jsonEncode(invokeRequest));
|
final resp = await _libgopeedAbi.invoke(jsonEncode(invokeRequest));
|
||||||
final result =
|
final result =
|
||||||
Result<T>.fromJson(jsonDecode(resp), fromJsonT ??= (json) => null as T);
|
Result<T>.fromJson(jsonDecode(resp), fromJsonT ??= (json) => null as T);
|
||||||
@@ -172,7 +165,7 @@ class LibgopeedBootNative
|
|||||||
@override
|
@override
|
||||||
Future<List<Task>> getTasks(TaskFilter? filter) async {
|
Future<List<Task>> getTasks(TaskFilter? filter) async {
|
||||||
return _invoke<List<Task>>("GetTasks", _parseTaskFilter(filter),
|
return _invoke<List<Task>>("GetTasks", _parseTaskFilter(filter),
|
||||||
(json) => json.map((e) => Task.fromJson(e)).toList());
|
(json) => (json as List).map((e) => Task.fromJson(e)).toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -200,7 +193,7 @@ class LibgopeedBootNative
|
|||||||
@override
|
@override
|
||||||
Future<List<Extension>> getExtensions() async {
|
Future<List<Extension>> getExtensions() async {
|
||||||
return _invoke<List<Extension>>("GetExtensions", [],
|
return _invoke<List<Extension>>("GetExtensions", [],
|
||||||
(json) => json.map((e) => Extension.fromJson(e)).toList());
|
(json) => (json as List).map((e) => Extension.fromJson(e)).toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
@@ -125,8 +125,7 @@ class LibgopeedBootWeb
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> pauseTask(String id) async {
|
Future<void> pauseTask(String id) async {
|
||||||
await _fetch<void>(
|
await _fetch<void>(() => _dio.put("$_apiPrefix/tasks/$id/pause"), null);
|
||||||
() => _dio.put("$_apiPrefix/tasks/$id/pause", data: {}), null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -139,8 +138,7 @@ class LibgopeedBootWeb
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> continueTask(String id) async {
|
Future<void> continueTask(String id) async {
|
||||||
await _fetch<void>(
|
await _fetch<void>(() => _dio.put("$_apiPrefix/tasks/$id/continue"), null);
|
||||||
() => _dio.put("$_apiPrefix/tasks/$id/continue", data: {}), null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ part 'invoke_request.g.dart';
|
|||||||
@JsonSerializable()
|
@JsonSerializable()
|
||||||
class InvokeRequest {
|
class InvokeRequest {
|
||||||
String method;
|
String method;
|
||||||
List<String> params;
|
List<dynamic> params;
|
||||||
|
|
||||||
InvokeRequest({
|
InvokeRequest({
|
||||||
this.method = '',
|
this.method = '',
|
||||||
|
|||||||
Reference in new issue
Block a user