From e5bc49a0afd3d1120fb80ad1fb48ad54ce7837a2 Mon Sep 17 00:00:00 2001 From: Vinicius Fortuna Date: Tue, 3 Dec 2024 16:05:46 -0500 Subject: [PATCH] Pull out InvokeMethod --- client/electron/go_plugin.ts | 25 ++++++----- client/electron/index.ts | 4 +- client/go/outline/electron/go_plugin.go | 41 +++++------------- client/go/outline/method_channel.go | 57 +++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 44 deletions(-) create mode 100644 client/go/outline/method_channel.go diff --git a/client/electron/go_plugin.ts b/client/electron/go_plugin.ts index 7f4a7bb645..7a054acdef 100644 --- a/client/electron/go_plugin.ts +++ b/client/electron/go_plugin.ts @@ -18,10 +18,10 @@ import koffi from 'koffi'; import {pathToBackendLibrary} from './app_paths'; -let invokeGoAPIFunc: Function | undefined; +let invokeMethodFunc: Function | undefined; /** - * Calls a Go function by invoking the `InvokeGoAPI` function in the native backend library. + * Calls a Go function by invoking the `InvokeMethod` function in the native backend library. * * @param method The name of the Go method to invoke. * @param input The input string to pass to the API. @@ -32,8 +32,11 @@ let invokeGoAPIFunc: Function | undefined; * Ensure that the function signature and data structures are consistent with the C definitions * in `./client/go/outline/electron/go_plugin.go`. */ -export async function invokeGoApi(method: string, input: string): Promise { - if (!invokeGoAPIFunc) { +export async function invokeMethod( + method: string, + input: string +): Promise { + if (!invokeMethodFunc) { const backendLib = koffi.load(pathToBackendLibrary()); // Define C strings and setup auto release @@ -43,19 +46,19 @@ export async function invokeGoApi(method: string, input: string): Promise => { - return invokeGoApi(method, params); + return invokeMethod(method, params); } ); diff --git a/client/go/outline/electron/go_plugin.go b/client/go/outline/electron/go_plugin.go index 9fcff7d12e..ea66d07d87 100644 --- a/client/go/outline/electron/go_plugin.go +++ b/client/go/outline/electron/go_plugin.go @@ -17,8 +17,8 @@ package main /* #include // for C.free -// InvokeGoAPIResult is a struct used to pass result from Go to TypeScript boundary. -typedef struct InvokeGoAPIResult_t +// InvokeMethodResult is a struct used to pass result from Go to TypeScript boundary. +typedef struct InvokeMethodResult_t { // A string representing the result of the Go function call. // This may be a raw string or a JSON string depending on the API call. @@ -28,7 +28,7 @@ typedef struct InvokeGoAPIResult_t // Go function call, or NULL if no error occurred. // This error can be parsed by the PlatformError in TypeScript. const char *ErrorJson; -} InvokeGoAPIResult; +} InvokeMethodResult; */ import "C" import ( @@ -41,40 +41,19 @@ import ( "github.com/Jigsaw-Code/outline-apps/client/go/outline/platerrors" ) -// API name constants -const ( - // FetchResourceAPI fetches a resource located at a given URL. - // - // - Input: the URL string of the resource to fetch - // - Output: the content in raw string of the fetched resource - FetchResourceAPI = "FetchResource" -) - -// InvokeGoAPI is the unified entry point for TypeScript to invoke various Go functions. +// InvokeMethod is the unified entry point for TypeScript to invoke various Go functions. // // The input and output are all defined as string, but they may represent either a raw string, // or a JSON string depending on the API call. // // Check the API name constants comment for more details about the input and output format. // -//export InvokeGoAPI -func InvokeGoAPI(api *C.char, input *C.char) C.InvokeGoAPIResult { - apiName := C.GoString(api) - switch apiName { - - case FetchResourceAPI: - res := outline.FetchResource(C.GoString(input)) - return C.InvokeGoAPIResult{ - Output: newCGoString(res.Content), - ErrorJson: marshalCGoErrorJson(platerrors.ToPlatformError(res.Error)), - } - - default: - err := &platerrors.PlatformError{ - Code: platerrors.InternalError, - Message: fmt.Sprintf("unsupported Go API: %s", apiName), - } - return C.InvokeGoAPIResult{ErrorJson: marshalCGoErrorJson(err)} +//export InvokeMethod +func InvokeMethod(method *C.char, input *C.char) C.InvokeMethodResult { + result := outline.InvokeMethod(C.GoString(method), C.GoString(input)) + return C.InvokeMethodResult{ + Output: newCGoString(result.Value), + ErrorJson: marshalCGoErrorJson(result.Error), } } diff --git a/client/go/outline/method_channel.go b/client/go/outline/method_channel.go new file mode 100644 index 0000000000..bb30b717bd --- /dev/null +++ b/client/go/outline/method_channel.go @@ -0,0 +1,57 @@ +// Copyright 2024 The Outline Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package outline + +import ( + "fmt" + + "github.com/Jigsaw-Code/outline-apps/client/go/outline/platerrors" +) + +// API name constants +const ( + // FetchResourceMethod fetches a resource located at a given URL. + // + // - Input: the URL string of the resource to fetch + // - Output: the content in raw string of the fetched resource + MethodFetchResource = "FetchResource" +) + +// InvokeMethodResult represents the result of an InvokeMethod call. +// +// We use a struct instead of a tuple to preserve a strongly typed error that gobind recognizes. +type InvokeMethodResult struct { + Value string + Error *platerrors.PlatformError +} + +// InvokeMethod calls a method by name. +func InvokeMethod(method string, input string) *InvokeMethodResult { + switch method { + case MethodFetchResource: + url := input + result := FetchResource(url) + return &InvokeMethodResult{ + Value: result.Content, + Error: result.Error, + } + + default: + return &InvokeMethodResult{Error: &platerrors.PlatformError{ + Code: platerrors.InternalError, + Message: fmt.Sprintf("unsupported Go method: %s", method), + }} + } +}