From 11d99ef6735a30ec914134696850239c8787a39b Mon Sep 17 00:00:00 2001 From: Deluan Date: Tue, 30 Dec 2025 13:11:17 -0500 Subject: [PATCH] refactor(plugins): update error handling for methods to return errors directly Signed-off-by: Deluan --- .../ndpgen/internal/templates/client.go.tmpl | 23 +++- .../internal/templates/client_stub.go.tmpl | 8 ++ plugins/cmd/ndpgen/internal/types.go | 5 + .../testdata/meta_client_expected.go.txt | 24 ++-- .../testdata/ping_client_expected.go.txt | 22 ++-- plugins/examples/crypto-ticker/main.go | 2 +- .../examples/discord-rich-presence/main.go | 2 +- plugins/examples/discord-rich-presence/rpc.go | 16 +-- plugins/pdk/go/host/nd_host_cache.go | 120 +++++++----------- plugins/pdk/go/host/nd_host_cache_stub.go | 35 +---- plugins/pdk/go/host/nd_host_kvstore.go | 48 +++---- plugins/pdk/go/host/nd_host_kvstore_stub.go | 14 +- plugins/pdk/go/host/nd_host_scheduler.go | 24 ++-- plugins/pdk/go/host/nd_host_scheduler_stub.go | 7 +- plugins/pdk/go/host/nd_host_websocket.go | 72 ++++------- plugins/pdk/go/host/nd_host_websocket_stub.go | 21 +-- plugins/testdata/test-cache-plugin/main.go | 10 +- plugins/testdata/test-kvstore/main.go | 4 +- plugins/testdata/test-websocket/main.go | 4 +- 19 files changed, 181 insertions(+), 280 deletions(-) diff --git a/plugins/cmd/ndpgen/internal/templates/client.go.tmpl b/plugins/cmd/ndpgen/internal/templates/client.go.tmpl index 9d060dcf1..dc199b64a 100644 --- a/plugins/cmd/ndpgen/internal/templates/client.go.tmpl +++ b/plugins/cmd/ndpgen/internal/templates/client.go.tmpl @@ -48,6 +48,7 @@ type {{requestType .}} struct { {{- end}} } {{- end}} +{{- if not .IsErrorOnly}} // {{responseType .}} is the response type for {{$.Service.Name}}.{{.Name}}. type {{responseType .}} struct { @@ -57,6 +58,7 @@ type {{responseType .}} struct { Error string `json:"error,omitempty"` } {{- end}} +{{- end}} {{- /* Generate wrapper functions */ -}} {{range .Service.Methods}} @@ -65,7 +67,11 @@ type {{responseType .}} struct { {{- if .Doc}} {{formatDoc .Doc}} {{- end}} +{{- if .IsErrorOnly}} +func {{$.Service.Name}}{{.Name}}({{range $i, $p := .Params}}{{if $i}}, {{end}}{{$p.Name}} {{$p.Type}}{{end}}) error { +{{- else}} func {{$.Service.Name}}{{.Name}}({{range $i, $p := .Params}}{{if $i}}, {{end}}{{$p.Name}} {{$p.Type}}{{end}}) (*{{responseType .}}, error) { +{{- end}} {{- if .HasParams}} // Marshal request to JSON req := {{requestType .}}{ @@ -75,7 +81,7 @@ func {{$.Service.Name}}{{.Name}}({{range $i, $p := .Params}}{{if $i}}, {{end}}{{ } reqBytes, err := json.Marshal(req) if err != nil { - return nil, err + return {{if not .IsErrorOnly}}nil, {{end}}err } reqMem := pdk.AllocateBytes(reqBytes) defer reqMem.Free() @@ -91,6 +97,20 @@ func {{$.Service.Name}}{{.Name}}({{range $i, $p := .Params}}{{if $i}}, {{end}}{{ // Read the response from memory responseMem := pdk.FindMemory(responsePtr) responseBytes := responseMem.ReadBytes() +{{- if .IsErrorOnly}} + + // Parse error-only response + var response struct { + Error string `json:"error,omitempty"` + } + if err := json.Unmarshal(responseBytes, &response); err != nil { + return err + } + if response.Error != "" { + return errors.New(response.Error) + } + return nil +{{- else}} // Parse the response var response {{responseType .}} @@ -104,5 +124,6 @@ func {{$.Service.Name}}{{.Name}}({{range $i, $p := .Params}}{{if $i}}, {{end}}{{ } return &response, nil +{{- end}} } {{- end}} diff --git a/plugins/cmd/ndpgen/internal/templates/client_stub.go.tmpl b/plugins/cmd/ndpgen/internal/templates/client_stub.go.tmpl index ef723c634..81389dc11 100644 --- a/plugins/cmd/ndpgen/internal/templates/client_stub.go.tmpl +++ b/plugins/cmd/ndpgen/internal/templates/client_stub.go.tmpl @@ -33,6 +33,7 @@ type {{requestType .}} struct { {{- end}} } {{- end}} +{{- if not .IsErrorOnly}} // {{responseType .}} is the response type for {{$.Service.Name}}.{{.Name}}. type {{responseType .}} struct { @@ -42,6 +43,7 @@ type {{responseType .}} struct { Error string `json:"error,omitempty"` } {{- end}} +{{- end}} {{- /* Generate stub wrapper functions that panic */ -}} {{range .Service.Methods}} @@ -50,7 +52,13 @@ type {{responseType .}} struct { {{- if .Doc}} {{formatDoc .Doc}} {{- end}} +{{- if .IsErrorOnly}} +func {{$.Service.Name}}{{.Name}}({{range $i, $p := .Params}}{{if $i}}, {{end}}{{$p.Name}} {{$p.Type}}{{end}}) error { + panic("{{$.Package}}: {{$.Service.Name}}{{.Name}} is only available in WASM plugins") +} +{{- else}} func {{$.Service.Name}}{{.Name}}({{range $i, $p := .Params}}{{if $i}}, {{end}}{{$p.Name}} {{$p.Type}}{{end}}) (*{{responseType .}}, error) { panic("{{$.Package}}: {{$.Service.Name}}{{.Name}} is only available in WASM plugins") } {{- end}} +{{- end}} diff --git a/plugins/cmd/ndpgen/internal/types.go b/plugins/cmd/ndpgen/internal/types.go index 6cd6c8215..cc10d3080 100644 --- a/plugins/cmd/ndpgen/internal/types.go +++ b/plugins/cmd/ndpgen/internal/types.go @@ -201,6 +201,11 @@ func (m Method) HasReturns() bool { return len(m.Returns) > 0 } +// IsErrorOnly returns true if the method only returns an error (no data fields). +func (m Method) IsErrorOnly() bool { + return m.HasError && !m.HasReturns() +} + // Param represents a method parameter or return value. type Param struct { Name string // Parameter name diff --git a/plugins/cmd/ndpgen/testdata/meta_client_expected.go.txt b/plugins/cmd/ndpgen/testdata/meta_client_expected.go.txt index 40f4d7ecf..d855fed73 100644 --- a/plugins/cmd/ndpgen/testdata/meta_client_expected.go.txt +++ b/plugins/cmd/ndpgen/testdata/meta_client_expected.go.txt @@ -40,11 +40,6 @@ type MetaSetRequest struct { Data map[string]any `json:"data"` } -// MetaSetResponse is the response type for Meta.Set. -type MetaSetResponse struct { - Error string `json:"error,omitempty"` -} - // MetaGet calls the meta_get host function. func MetaGet(key string) (*MetaGetResponse, error) { // Marshal request to JSON @@ -80,14 +75,14 @@ func MetaGet(key string) (*MetaGetResponse, error) { } // MetaSet calls the meta_set host function. -func MetaSet(data map[string]any) (*MetaSetResponse, error) { +func MetaSet(data map[string]any) error { // Marshal request to JSON req := MetaSetRequest{ Data: data, } reqBytes, err := json.Marshal(req) if err != nil { - return nil, err + return err } reqMem := pdk.AllocateBytes(reqBytes) defer reqMem.Free() @@ -99,16 +94,15 @@ func MetaSet(data map[string]any) (*MetaSetResponse, error) { responseMem := pdk.FindMemory(responsePtr) responseBytes := responseMem.ReadBytes() - // Parse the response - var response MetaSetResponse + // Parse error-only response + var response struct { + Error string `json:"error,omitempty"` + } if err := json.Unmarshal(responseBytes, &response); err != nil { - return nil, err + return err } - - // Convert Error field to Go error if response.Error != "" { - return nil, errors.New(response.Error) + return errors.New(response.Error) } - - return &response, nil + return nil } diff --git a/plugins/cmd/ndpgen/testdata/ping_client_expected.go.txt b/plugins/cmd/ndpgen/testdata/ping_client_expected.go.txt index e94e7e735..d3c270416 100644 --- a/plugins/cmd/ndpgen/testdata/ping_client_expected.go.txt +++ b/plugins/cmd/ndpgen/testdata/ping_client_expected.go.txt @@ -19,13 +19,8 @@ import ( //go:wasmimport extism:host/user ping_ping func ping_ping(uint64) uint64 -// PingPingResponse is the response type for Ping.Ping. -type PingPingResponse struct { - Error string `json:"error,omitempty"` -} - // PingPing calls the ping_ping host function. -func PingPing() (*PingPingResponse, error) { +func PingPing() error { // No parameters - allocate empty JSON object reqMem := pdk.AllocateBytes([]byte("{}")) defer reqMem.Free() @@ -37,16 +32,15 @@ func PingPing() (*PingPingResponse, error) { responseMem := pdk.FindMemory(responsePtr) responseBytes := responseMem.ReadBytes() - // Parse the response - var response PingPingResponse + // Parse error-only response + var response struct { + Error string `json:"error,omitempty"` + } if err := json.Unmarshal(responseBytes, &response); err != nil { - return nil, err + return err } - - // Convert Error field to Go error if response.Error != "" { - return nil, errors.New(response.Error) + return errors.New(response.Error) } - - return &response, nil + return nil } diff --git a/plugins/examples/crypto-ticker/main.go b/plugins/examples/crypto-ticker/main.go index 711634d52..08eb1f713 100755 --- a/plugins/examples/crypto-ticker/main.go +++ b/plugins/examples/crypto-ticker/main.go @@ -128,7 +128,7 @@ func connectAndSubscribe(tickers []string) error { } // Send subscription message - _, err = host.WebSocketSendText(connectionID, string(subscriptionJSON)) + err = host.WebSocketSendText(connectionID, string(subscriptionJSON)) if err != nil { return fmt.Errorf("WebSocket send error: %w", err) } diff --git a/plugins/examples/discord-rich-presence/main.go b/plugins/examples/discord-rich-presence/main.go index d36213bff..3a69443ba 100644 --- a/plugins/examples/discord-rich-presence/main.go +++ b/plugins/examples/discord-rich-presence/main.go @@ -126,7 +126,7 @@ func (p *discordPlugin) NowPlaying(input scrobbler.NowPlayingRequest) error { } // Cancel any existing completion schedule - _, _ = host.SchedulerCancelSchedule(fmt.Sprintf("%s-clear", input.Username)) + _ = host.SchedulerCancelSchedule(fmt.Sprintf("%s-clear", input.Username)) // Calculate timestamps now := time.Now().Unix() diff --git a/plugins/examples/discord-rich-presence/rpc.go b/plugins/examples/discord-rich-presence/rpc.go index cbc0da6ff..9db371a03 100644 --- a/plugins/examples/discord-rich-presence/rpc.go +++ b/plugins/examples/discord-rich-presence/rpc.go @@ -141,7 +141,7 @@ func processImage(imageURL, clientID, token string, isDefaultImage bool) (string ttl = 48 * 60 * 60 // 48 hours for default image } - _, _ = host.CacheSetString(cacheKey, processedImage, ttl) + _ = host.CacheSetString(cacheKey, processedImage, ttl) pdk.Log(pdk.LogDebug, fmt.Sprintf("Cached processed image URL for %s (TTL: %ds)", imageURL, ttl)) return processedImage, nil @@ -184,7 +184,7 @@ func sendMessage(username string, opCode int, payload any) error { return fmt.Errorf("failed to marshal message: %w", err) } - _, err = host.WebSocketSendText(username, string(b)) + err = host.WebSocketSendText(username, string(b)) if err != nil { return fmt.Errorf("failed to send message: %w", err) } @@ -222,17 +222,17 @@ func cleanupFailedConnection(username string) { pdk.Log(pdk.LogInfo, fmt.Sprintf("Cleaning up failed connection for user %s", username)) // Cancel the heartbeat schedule - if _, err := host.SchedulerCancelSchedule(username); err != nil { + if err := host.SchedulerCancelSchedule(username); err != nil { pdk.Log(pdk.LogWarn, fmt.Sprintf("Failed to cancel heartbeat schedule for user %s: %v", username, err)) } // Close the WebSocket connection - if _, err := host.WebSocketCloseConnection(username, 1000, "Connection lost"); err != nil { + if err := host.WebSocketCloseConnection(username, 1000, "Connection lost"); err != nil { pdk.Log(pdk.LogWarn, fmt.Sprintf("Failed to close WebSocket connection for user %s: %v", username, err)) } // Clean up cache entries - _, _ = host.CacheRemove(fmt.Sprintf("discord.seq.%s", username)) + _ = host.CacheRemove(fmt.Sprintf("discord.seq.%s", username)) pdk.Log(pdk.LogInfo, fmt.Sprintf("Cleaned up connection for user %s", username)) } @@ -296,11 +296,11 @@ func connect(username, token string) error { // disconnect closes the Discord connection for a user. func disconnect(username string) error { - if _, err := host.SchedulerCancelSchedule(username); err != nil { + if err := host.SchedulerCancelSchedule(username); err != nil { return fmt.Errorf("failed to cancel schedule: %w", err) } - if _, err := host.WebSocketCloseConnection(username, 1000, "Navidrome disconnect"); err != nil { + if err := host.WebSocketCloseConnection(username, 1000, "Navidrome disconnect"); err != nil { return fmt.Errorf("failed to close WebSocket connection: %w", err) } return nil @@ -324,7 +324,7 @@ func handleWebSocketMessage(connectionID, message string) error { if v := msg["s"]; v != nil { seq := int64(v.(float64)) pdk.Log(pdk.LogTrace, fmt.Sprintf("Received sequence number for connection '%s': %d", connectionID, seq)) - if _, err := host.CacheSetInt(fmt.Sprintf("discord.seq.%s", connectionID), seq, int64(heartbeatInterval*2)); err != nil { + if err := host.CacheSetInt(fmt.Sprintf("discord.seq.%s", connectionID), seq, int64(heartbeatInterval*2)); err != nil { return fmt.Errorf("failed to store sequence number for user %s: %w", connectionID, err) } } diff --git a/plugins/pdk/go/host/nd_host_cache.go b/plugins/pdk/go/host/nd_host_cache.go index c58cc09d7..1b1d93fc6 100644 --- a/plugins/pdk/go/host/nd_host_cache.go +++ b/plugins/pdk/go/host/nd_host_cache.go @@ -71,11 +71,6 @@ type CacheSetStringRequest struct { TtlSeconds int64 `json:"ttlSeconds"` } -// CacheSetStringResponse is the response type for Cache.SetString. -type CacheSetStringResponse struct { - Error string `json:"error,omitempty"` -} - // CacheGetStringRequest is the request type for Cache.GetString. type CacheGetStringRequest struct { Key string `json:"key"` @@ -95,11 +90,6 @@ type CacheSetIntRequest struct { TtlSeconds int64 `json:"ttlSeconds"` } -// CacheSetIntResponse is the response type for Cache.SetInt. -type CacheSetIntResponse struct { - Error string `json:"error,omitempty"` -} - // CacheGetIntRequest is the request type for Cache.GetInt. type CacheGetIntRequest struct { Key string `json:"key"` @@ -119,11 +109,6 @@ type CacheSetFloatRequest struct { TtlSeconds int64 `json:"ttlSeconds"` } -// CacheSetFloatResponse is the response type for Cache.SetFloat. -type CacheSetFloatResponse struct { - Error string `json:"error,omitempty"` -} - // CacheGetFloatRequest is the request type for Cache.GetFloat. type CacheGetFloatRequest struct { Key string `json:"key"` @@ -143,11 +128,6 @@ type CacheSetBytesRequest struct { TtlSeconds int64 `json:"ttlSeconds"` } -// CacheSetBytesResponse is the response type for Cache.SetBytes. -type CacheSetBytesResponse struct { - Error string `json:"error,omitempty"` -} - // CacheGetBytesRequest is the request type for Cache.GetBytes. type CacheGetBytesRequest struct { Key string `json:"key"` @@ -176,11 +156,6 @@ type CacheRemoveRequest struct { Key string `json:"key"` } -// CacheRemoveResponse is the response type for Cache.Remove. -type CacheRemoveResponse struct { - Error string `json:"error,omitempty"` -} - // CacheSetString calls the cache_setstring host function. // SetString stores a string value in the cache. // @@ -190,7 +165,7 @@ type CacheRemoveResponse struct { // - ttlSeconds: Time-to-live in seconds (0 uses default of 24 hours) // // Returns an error if the operation fails. -func CacheSetString(key string, value string, ttlSeconds int64) (*CacheSetStringResponse, error) { +func CacheSetString(key string, value string, ttlSeconds int64) error { // Marshal request to JSON req := CacheSetStringRequest{ Key: key, @@ -199,7 +174,7 @@ func CacheSetString(key string, value string, ttlSeconds int64) (*CacheSetString } reqBytes, err := json.Marshal(req) if err != nil { - return nil, err + return err } reqMem := pdk.AllocateBytes(reqBytes) defer reqMem.Free() @@ -211,18 +186,17 @@ func CacheSetString(key string, value string, ttlSeconds int64) (*CacheSetString responseMem := pdk.FindMemory(responsePtr) responseBytes := responseMem.ReadBytes() - // Parse the response - var response CacheSetStringResponse + // Parse error-only response + var response struct { + Error string `json:"error,omitempty"` + } if err := json.Unmarshal(responseBytes, &response); err != nil { - return nil, err + return err } - - // Convert Error field to Go error if response.Error != "" { - return nil, errors.New(response.Error) + return errors.New(response.Error) } - - return &response, nil + return nil } // CacheGetString calls the cache_getstring host function. @@ -275,7 +249,7 @@ func CacheGetString(key string) (*CacheGetStringResponse, error) { // - ttlSeconds: Time-to-live in seconds (0 uses default of 24 hours) // // Returns an error if the operation fails. -func CacheSetInt(key string, value int64, ttlSeconds int64) (*CacheSetIntResponse, error) { +func CacheSetInt(key string, value int64, ttlSeconds int64) error { // Marshal request to JSON req := CacheSetIntRequest{ Key: key, @@ -284,7 +258,7 @@ func CacheSetInt(key string, value int64, ttlSeconds int64) (*CacheSetIntRespons } reqBytes, err := json.Marshal(req) if err != nil { - return nil, err + return err } reqMem := pdk.AllocateBytes(reqBytes) defer reqMem.Free() @@ -296,18 +270,17 @@ func CacheSetInt(key string, value int64, ttlSeconds int64) (*CacheSetIntRespons responseMem := pdk.FindMemory(responsePtr) responseBytes := responseMem.ReadBytes() - // Parse the response - var response CacheSetIntResponse + // Parse error-only response + var response struct { + Error string `json:"error,omitempty"` + } if err := json.Unmarshal(responseBytes, &response); err != nil { - return nil, err + return err } - - // Convert Error field to Go error if response.Error != "" { - return nil, errors.New(response.Error) + return errors.New(response.Error) } - - return &response, nil + return nil } // CacheGetInt calls the cache_getint host function. @@ -360,7 +333,7 @@ func CacheGetInt(key string) (*CacheGetIntResponse, error) { // - ttlSeconds: Time-to-live in seconds (0 uses default of 24 hours) // // Returns an error if the operation fails. -func CacheSetFloat(key string, value float64, ttlSeconds int64) (*CacheSetFloatResponse, error) { +func CacheSetFloat(key string, value float64, ttlSeconds int64) error { // Marshal request to JSON req := CacheSetFloatRequest{ Key: key, @@ -369,7 +342,7 @@ func CacheSetFloat(key string, value float64, ttlSeconds int64) (*CacheSetFloatR } reqBytes, err := json.Marshal(req) if err != nil { - return nil, err + return err } reqMem := pdk.AllocateBytes(reqBytes) defer reqMem.Free() @@ -381,18 +354,17 @@ func CacheSetFloat(key string, value float64, ttlSeconds int64) (*CacheSetFloatR responseMem := pdk.FindMemory(responsePtr) responseBytes := responseMem.ReadBytes() - // Parse the response - var response CacheSetFloatResponse + // Parse error-only response + var response struct { + Error string `json:"error,omitempty"` + } if err := json.Unmarshal(responseBytes, &response); err != nil { - return nil, err + return err } - - // Convert Error field to Go error if response.Error != "" { - return nil, errors.New(response.Error) + return errors.New(response.Error) } - - return &response, nil + return nil } // CacheGetFloat calls the cache_getfloat host function. @@ -445,7 +417,7 @@ func CacheGetFloat(key string) (*CacheGetFloatResponse, error) { // - ttlSeconds: Time-to-live in seconds (0 uses default of 24 hours) // // Returns an error if the operation fails. -func CacheSetBytes(key string, value []byte, ttlSeconds int64) (*CacheSetBytesResponse, error) { +func CacheSetBytes(key string, value []byte, ttlSeconds int64) error { // Marshal request to JSON req := CacheSetBytesRequest{ Key: key, @@ -454,7 +426,7 @@ func CacheSetBytes(key string, value []byte, ttlSeconds int64) (*CacheSetBytesRe } reqBytes, err := json.Marshal(req) if err != nil { - return nil, err + return err } reqMem := pdk.AllocateBytes(reqBytes) defer reqMem.Free() @@ -466,18 +438,17 @@ func CacheSetBytes(key string, value []byte, ttlSeconds int64) (*CacheSetBytesRe responseMem := pdk.FindMemory(responsePtr) responseBytes := responseMem.ReadBytes() - // Parse the response - var response CacheSetBytesResponse + // Parse error-only response + var response struct { + Error string `json:"error,omitempty"` + } if err := json.Unmarshal(responseBytes, &response); err != nil { - return nil, err + return err } - - // Convert Error field to Go error if response.Error != "" { - return nil, errors.New(response.Error) + return errors.New(response.Error) } - - return &response, nil + return nil } // CacheGetBytes calls the cache_getbytes host function. @@ -568,14 +539,14 @@ func CacheHas(key string) (*CacheHasResponse, error) { // - key: The cache key (will be namespaced with plugin ID) // // Returns an error if the operation fails. Does not return an error if the key doesn't exist. -func CacheRemove(key string) (*CacheRemoveResponse, error) { +func CacheRemove(key string) error { // Marshal request to JSON req := CacheRemoveRequest{ Key: key, } reqBytes, err := json.Marshal(req) if err != nil { - return nil, err + return err } reqMem := pdk.AllocateBytes(reqBytes) defer reqMem.Free() @@ -587,16 +558,15 @@ func CacheRemove(key string) (*CacheRemoveResponse, error) { responseMem := pdk.FindMemory(responsePtr) responseBytes := responseMem.ReadBytes() - // Parse the response - var response CacheRemoveResponse + // Parse error-only response + var response struct { + Error string `json:"error,omitempty"` + } if err := json.Unmarshal(responseBytes, &response); err != nil { - return nil, err + return err } - - // Convert Error field to Go error if response.Error != "" { - return nil, errors.New(response.Error) + return errors.New(response.Error) } - - return &response, nil + return nil } diff --git a/plugins/pdk/go/host/nd_host_cache_stub.go b/plugins/pdk/go/host/nd_host_cache_stub.go index c19fdefe2..4d6b1eed1 100644 --- a/plugins/pdk/go/host/nd_host_cache_stub.go +++ b/plugins/pdk/go/host/nd_host_cache_stub.go @@ -15,11 +15,6 @@ type CacheSetStringRequest struct { TtlSeconds int64 `json:"ttlSeconds"` } -// CacheSetStringResponse is the response type for Cache.SetString. -type CacheSetStringResponse struct { - Error string `json:"error,omitempty"` -} - // CacheGetStringRequest is the request type for Cache.GetString. type CacheGetStringRequest struct { Key string `json:"key"` @@ -39,11 +34,6 @@ type CacheSetIntRequest struct { TtlSeconds int64 `json:"ttlSeconds"` } -// CacheSetIntResponse is the response type for Cache.SetInt. -type CacheSetIntResponse struct { - Error string `json:"error,omitempty"` -} - // CacheGetIntRequest is the request type for Cache.GetInt. type CacheGetIntRequest struct { Key string `json:"key"` @@ -63,11 +53,6 @@ type CacheSetFloatRequest struct { TtlSeconds int64 `json:"ttlSeconds"` } -// CacheSetFloatResponse is the response type for Cache.SetFloat. -type CacheSetFloatResponse struct { - Error string `json:"error,omitempty"` -} - // CacheGetFloatRequest is the request type for Cache.GetFloat. type CacheGetFloatRequest struct { Key string `json:"key"` @@ -87,11 +72,6 @@ type CacheSetBytesRequest struct { TtlSeconds int64 `json:"ttlSeconds"` } -// CacheSetBytesResponse is the response type for Cache.SetBytes. -type CacheSetBytesResponse struct { - Error string `json:"error,omitempty"` -} - // CacheGetBytesRequest is the request type for Cache.GetBytes. type CacheGetBytesRequest struct { Key string `json:"key"` @@ -120,11 +100,6 @@ type CacheRemoveRequest struct { Key string `json:"key"` } -// CacheRemoveResponse is the response type for Cache.Remove. -type CacheRemoveResponse struct { - Error string `json:"error,omitempty"` -} - // CacheSetString is a stub that panics on non-WASM platforms. // SetString stores a string value in the cache. // @@ -134,7 +109,7 @@ type CacheRemoveResponse struct { // - ttlSeconds: Time-to-live in seconds (0 uses default of 24 hours) // // Returns an error if the operation fails. -func CacheSetString(key string, value string, ttlSeconds int64) (*CacheSetStringResponse, error) { +func CacheSetString(key string, value string, ttlSeconds int64) error { panic("host: CacheSetString is only available in WASM plugins") } @@ -159,7 +134,7 @@ func CacheGetString(key string) (*CacheGetStringResponse, error) { // - ttlSeconds: Time-to-live in seconds (0 uses default of 24 hours) // // Returns an error if the operation fails. -func CacheSetInt(key string, value int64, ttlSeconds int64) (*CacheSetIntResponse, error) { +func CacheSetInt(key string, value int64, ttlSeconds int64) error { panic("host: CacheSetInt is only available in WASM plugins") } @@ -184,7 +159,7 @@ func CacheGetInt(key string) (*CacheGetIntResponse, error) { // - ttlSeconds: Time-to-live in seconds (0 uses default of 24 hours) // // Returns an error if the operation fails. -func CacheSetFloat(key string, value float64, ttlSeconds int64) (*CacheSetFloatResponse, error) { +func CacheSetFloat(key string, value float64, ttlSeconds int64) error { panic("host: CacheSetFloat is only available in WASM plugins") } @@ -209,7 +184,7 @@ func CacheGetFloat(key string) (*CacheGetFloatResponse, error) { // - ttlSeconds: Time-to-live in seconds (0 uses default of 24 hours) // // Returns an error if the operation fails. -func CacheSetBytes(key string, value []byte, ttlSeconds int64) (*CacheSetBytesResponse, error) { +func CacheSetBytes(key string, value []byte, ttlSeconds int64) error { panic("host: CacheSetBytes is only available in WASM plugins") } @@ -243,6 +218,6 @@ func CacheHas(key string) (*CacheHasResponse, error) { // - key: The cache key (will be namespaced with plugin ID) // // Returns an error if the operation fails. Does not return an error if the key doesn't exist. -func CacheRemove(key string) (*CacheRemoveResponse, error) { +func CacheRemove(key string) error { panic("host: CacheRemove is only available in WASM plugins") } diff --git a/plugins/pdk/go/host/nd_host_kvstore.go b/plugins/pdk/go/host/nd_host_kvstore.go index 92c141db6..c482ffcc3 100644 --- a/plugins/pdk/go/host/nd_host_kvstore.go +++ b/plugins/pdk/go/host/nd_host_kvstore.go @@ -50,11 +50,6 @@ type KVStoreSetRequest struct { Value []byte `json:"value"` } -// KVStoreSetResponse is the response type for KVStore.Set. -type KVStoreSetResponse struct { - Error string `json:"error,omitempty"` -} - // KVStoreGetRequest is the request type for KVStore.Get. type KVStoreGetRequest struct { Key string `json:"key"` @@ -72,11 +67,6 @@ type KVStoreDeleteRequest struct { Key string `json:"key"` } -// KVStoreDeleteResponse is the response type for KVStore.Delete. -type KVStoreDeleteResponse struct { - Error string `json:"error,omitempty"` -} - // KVStoreHasRequest is the request type for KVStore.Has. type KVStoreHasRequest struct { Key string `json:"key"` @@ -113,7 +103,7 @@ type KVStoreGetStorageUsedResponse struct { // - value: The byte slice to store // // Returns an error if the storage limit would be exceeded or the operation fails. -func KVStoreSet(key string, value []byte) (*KVStoreSetResponse, error) { +func KVStoreSet(key string, value []byte) error { // Marshal request to JSON req := KVStoreSetRequest{ Key: key, @@ -121,7 +111,7 @@ func KVStoreSet(key string, value []byte) (*KVStoreSetResponse, error) { } reqBytes, err := json.Marshal(req) if err != nil { - return nil, err + return err } reqMem := pdk.AllocateBytes(reqBytes) defer reqMem.Free() @@ -133,18 +123,17 @@ func KVStoreSet(key string, value []byte) (*KVStoreSetResponse, error) { responseMem := pdk.FindMemory(responsePtr) responseBytes := responseMem.ReadBytes() - // Parse the response - var response KVStoreSetResponse + // Parse error-only response + var response struct { + Error string `json:"error,omitempty"` + } if err := json.Unmarshal(responseBytes, &response); err != nil { - return nil, err + return err } - - // Convert Error field to Go error if response.Error != "" { - return nil, errors.New(response.Error) + return errors.New(response.Error) } - - return &response, nil + return nil } // KVStoreGet calls the kvstore_get host function. @@ -194,14 +183,14 @@ func KVStoreGet(key string) (*KVStoreGetResponse, error) { // - key: The storage key // // Returns an error if the operation fails. Does not return an error if the key doesn't exist. -func KVStoreDelete(key string) (*KVStoreDeleteResponse, error) { +func KVStoreDelete(key string) error { // Marshal request to JSON req := KVStoreDeleteRequest{ Key: key, } reqBytes, err := json.Marshal(req) if err != nil { - return nil, err + return err } reqMem := pdk.AllocateBytes(reqBytes) defer reqMem.Free() @@ -213,18 +202,17 @@ func KVStoreDelete(key string) (*KVStoreDeleteResponse, error) { responseMem := pdk.FindMemory(responsePtr) responseBytes := responseMem.ReadBytes() - // Parse the response - var response KVStoreDeleteResponse + // Parse error-only response + var response struct { + Error string `json:"error,omitempty"` + } if err := json.Unmarshal(responseBytes, &response); err != nil { - return nil, err + return err } - - // Convert Error field to Go error if response.Error != "" { - return nil, errors.New(response.Error) + return errors.New(response.Error) } - - return &response, nil + return nil } // KVStoreHas calls the kvstore_has host function. diff --git a/plugins/pdk/go/host/nd_host_kvstore_stub.go b/plugins/pdk/go/host/nd_host_kvstore_stub.go index 3b3fe494a..a3a83f634 100644 --- a/plugins/pdk/go/host/nd_host_kvstore_stub.go +++ b/plugins/pdk/go/host/nd_host_kvstore_stub.go @@ -14,11 +14,6 @@ type KVStoreSetRequest struct { Value []byte `json:"value"` } -// KVStoreSetResponse is the response type for KVStore.Set. -type KVStoreSetResponse struct { - Error string `json:"error,omitempty"` -} - // KVStoreGetRequest is the request type for KVStore.Get. type KVStoreGetRequest struct { Key string `json:"key"` @@ -36,11 +31,6 @@ type KVStoreDeleteRequest struct { Key string `json:"key"` } -// KVStoreDeleteResponse is the response type for KVStore.Delete. -type KVStoreDeleteResponse struct { - Error string `json:"error,omitempty"` -} - // KVStoreHasRequest is the request type for KVStore.Has. type KVStoreHasRequest struct { Key string `json:"key"` @@ -77,7 +67,7 @@ type KVStoreGetStorageUsedResponse struct { // - value: The byte slice to store // // Returns an error if the storage limit would be exceeded or the operation fails. -func KVStoreSet(key string, value []byte) (*KVStoreSetResponse, error) { +func KVStoreSet(key string, value []byte) error { panic("host: KVStoreSet is only available in WASM plugins") } @@ -99,7 +89,7 @@ func KVStoreGet(key string) (*KVStoreGetResponse, error) { // - key: The storage key // // Returns an error if the operation fails. Does not return an error if the key doesn't exist. -func KVStoreDelete(key string) (*KVStoreDeleteResponse, error) { +func KVStoreDelete(key string) error { panic("host: KVStoreDelete is only available in WASM plugins") } diff --git a/plugins/pdk/go/host/nd_host_scheduler.go b/plugins/pdk/go/host/nd_host_scheduler.go index 144ca26fc..f1bfd3129 100644 --- a/plugins/pdk/go/host/nd_host_scheduler.go +++ b/plugins/pdk/go/host/nd_host_scheduler.go @@ -60,11 +60,6 @@ type SchedulerCancelScheduleRequest struct { ScheduleID string `json:"scheduleId"` } -// SchedulerCancelScheduleResponse is the response type for Scheduler.CancelSchedule. -type SchedulerCancelScheduleResponse struct { - Error string `json:"error,omitempty"` -} - // SchedulerScheduleOneTime calls the scheduler_scheduleonetime host function. // ScheduleOneTime schedules a one-time event to be triggered after the specified delay. // Plugins that use this function must also implement the SchedulerCallback capability @@ -162,14 +157,14 @@ func SchedulerScheduleRecurring(cronExpression string, payload string, scheduleI // any future events. // // Returns an error if the schedule ID is not found or if cancellation fails. -func SchedulerCancelSchedule(scheduleID string) (*SchedulerCancelScheduleResponse, error) { +func SchedulerCancelSchedule(scheduleID string) error { // Marshal request to JSON req := SchedulerCancelScheduleRequest{ ScheduleID: scheduleID, } reqBytes, err := json.Marshal(req) if err != nil { - return nil, err + return err } reqMem := pdk.AllocateBytes(reqBytes) defer reqMem.Free() @@ -181,16 +176,15 @@ func SchedulerCancelSchedule(scheduleID string) (*SchedulerCancelScheduleRespons responseMem := pdk.FindMemory(responsePtr) responseBytes := responseMem.ReadBytes() - // Parse the response - var response SchedulerCancelScheduleResponse + // Parse error-only response + var response struct { + Error string `json:"error,omitempty"` + } if err := json.Unmarshal(responseBytes, &response); err != nil { - return nil, err + return err } - - // Convert Error field to Go error if response.Error != "" { - return nil, errors.New(response.Error) + return errors.New(response.Error) } - - return &response, nil + return nil } diff --git a/plugins/pdk/go/host/nd_host_scheduler_stub.go b/plugins/pdk/go/host/nd_host_scheduler_stub.go index dde181810..37a67c55e 100644 --- a/plugins/pdk/go/host/nd_host_scheduler_stub.go +++ b/plugins/pdk/go/host/nd_host_scheduler_stub.go @@ -39,11 +39,6 @@ type SchedulerCancelScheduleRequest struct { ScheduleID string `json:"scheduleId"` } -// SchedulerCancelScheduleResponse is the response type for Scheduler.CancelSchedule. -type SchedulerCancelScheduleResponse struct { - Error string `json:"error,omitempty"` -} - // SchedulerScheduleOneTime is a stub that panics on non-WASM platforms. // ScheduleOneTime schedules a one-time event to be triggered after the specified delay. // Plugins that use this function must also implement the SchedulerCallback capability @@ -79,6 +74,6 @@ func SchedulerScheduleRecurring(cronExpression string, payload string, scheduleI // any future events. // // Returns an error if the schedule ID is not found or if cancellation fails. -func SchedulerCancelSchedule(scheduleID string) (*SchedulerCancelScheduleResponse, error) { +func SchedulerCancelSchedule(scheduleID string) error { panic("host: SchedulerCancelSchedule is only available in WASM plugins") } diff --git a/plugins/pdk/go/host/nd_host_websocket.go b/plugins/pdk/go/host/nd_host_websocket.go index cb653488b..cea22712f 100644 --- a/plugins/pdk/go/host/nd_host_websocket.go +++ b/plugins/pdk/go/host/nd_host_websocket.go @@ -53,22 +53,12 @@ type WebSocketSendTextRequest struct { Message string `json:"message"` } -// WebSocketSendTextResponse is the response type for WebSocket.SendText. -type WebSocketSendTextResponse struct { - Error string `json:"error,omitempty"` -} - // WebSocketSendBinaryRequest is the request type for WebSocket.SendBinary. type WebSocketSendBinaryRequest struct { ConnectionID string `json:"connectionId"` Data []byte `json:"data"` } -// WebSocketSendBinaryResponse is the response type for WebSocket.SendBinary. -type WebSocketSendBinaryResponse struct { - Error string `json:"error,omitempty"` -} - // WebSocketCloseConnectionRequest is the request type for WebSocket.CloseConnection. type WebSocketCloseConnectionRequest struct { ConnectionID string `json:"connectionId"` @@ -76,11 +66,6 @@ type WebSocketCloseConnectionRequest struct { Reason string `json:"reason"` } -// WebSocketCloseConnectionResponse is the response type for WebSocket.CloseConnection. -type WebSocketCloseConnectionResponse struct { - Error string `json:"error,omitempty"` -} - // WebSocketConnect calls the websocket_connect host function. // Connect establishes a WebSocket connection to the specified URL. // @@ -137,7 +122,7 @@ func WebSocketConnect(url string, headers map[string]string, connectionID string // - message: The text message to send // // Returns an error if the connection is not found or if sending fails. -func WebSocketSendText(connectionID string, message string) (*WebSocketSendTextResponse, error) { +func WebSocketSendText(connectionID string, message string) error { // Marshal request to JSON req := WebSocketSendTextRequest{ ConnectionID: connectionID, @@ -145,7 +130,7 @@ func WebSocketSendText(connectionID string, message string) (*WebSocketSendTextR } reqBytes, err := json.Marshal(req) if err != nil { - return nil, err + return err } reqMem := pdk.AllocateBytes(reqBytes) defer reqMem.Free() @@ -157,18 +142,17 @@ func WebSocketSendText(connectionID string, message string) (*WebSocketSendTextR responseMem := pdk.FindMemory(responsePtr) responseBytes := responseMem.ReadBytes() - // Parse the response - var response WebSocketSendTextResponse + // Parse error-only response + var response struct { + Error string `json:"error,omitempty"` + } if err := json.Unmarshal(responseBytes, &response); err != nil { - return nil, err + return err } - - // Convert Error field to Go error if response.Error != "" { - return nil, errors.New(response.Error) + return errors.New(response.Error) } - - return &response, nil + return nil } // WebSocketSendBinary calls the websocket_sendbinary host function. @@ -179,7 +163,7 @@ func WebSocketSendText(connectionID string, message string) (*WebSocketSendTextR // - data: The binary data to send // // Returns an error if the connection is not found or if sending fails. -func WebSocketSendBinary(connectionID string, data []byte) (*WebSocketSendBinaryResponse, error) { +func WebSocketSendBinary(connectionID string, data []byte) error { // Marshal request to JSON req := WebSocketSendBinaryRequest{ ConnectionID: connectionID, @@ -187,7 +171,7 @@ func WebSocketSendBinary(connectionID string, data []byte) (*WebSocketSendBinary } reqBytes, err := json.Marshal(req) if err != nil { - return nil, err + return err } reqMem := pdk.AllocateBytes(reqBytes) defer reqMem.Free() @@ -199,18 +183,17 @@ func WebSocketSendBinary(connectionID string, data []byte) (*WebSocketSendBinary responseMem := pdk.FindMemory(responsePtr) responseBytes := responseMem.ReadBytes() - // Parse the response - var response WebSocketSendBinaryResponse + // Parse error-only response + var response struct { + Error string `json:"error,omitempty"` + } if err := json.Unmarshal(responseBytes, &response); err != nil { - return nil, err + return err } - - // Convert Error field to Go error if response.Error != "" { - return nil, errors.New(response.Error) + return errors.New(response.Error) } - - return &response, nil + return nil } // WebSocketCloseConnection calls the websocket_closeconnection host function. @@ -222,7 +205,7 @@ func WebSocketSendBinary(connectionID string, data []byte) (*WebSocketSendBinary // - reason: Optional human-readable reason for closing // // Returns an error if the connection is not found or if closing fails. -func WebSocketCloseConnection(connectionID string, code int32, reason string) (*WebSocketCloseConnectionResponse, error) { +func WebSocketCloseConnection(connectionID string, code int32, reason string) error { // Marshal request to JSON req := WebSocketCloseConnectionRequest{ ConnectionID: connectionID, @@ -231,7 +214,7 @@ func WebSocketCloseConnection(connectionID string, code int32, reason string) (* } reqBytes, err := json.Marshal(req) if err != nil { - return nil, err + return err } reqMem := pdk.AllocateBytes(reqBytes) defer reqMem.Free() @@ -243,16 +226,15 @@ func WebSocketCloseConnection(connectionID string, code int32, reason string) (* responseMem := pdk.FindMemory(responsePtr) responseBytes := responseMem.ReadBytes() - // Parse the response - var response WebSocketCloseConnectionResponse + // Parse error-only response + var response struct { + Error string `json:"error,omitempty"` + } if err := json.Unmarshal(responseBytes, &response); err != nil { - return nil, err + return err } - - // Convert Error field to Go error if response.Error != "" { - return nil, errors.New(response.Error) + return errors.New(response.Error) } - - return &response, nil + return nil } diff --git a/plugins/pdk/go/host/nd_host_websocket_stub.go b/plugins/pdk/go/host/nd_host_websocket_stub.go index 27a453fdb..42f86421a 100644 --- a/plugins/pdk/go/host/nd_host_websocket_stub.go +++ b/plugins/pdk/go/host/nd_host_websocket_stub.go @@ -27,22 +27,12 @@ type WebSocketSendTextRequest struct { Message string `json:"message"` } -// WebSocketSendTextResponse is the response type for WebSocket.SendText. -type WebSocketSendTextResponse struct { - Error string `json:"error,omitempty"` -} - // WebSocketSendBinaryRequest is the request type for WebSocket.SendBinary. type WebSocketSendBinaryRequest struct { ConnectionID string `json:"connectionId"` Data []byte `json:"data"` } -// WebSocketSendBinaryResponse is the response type for WebSocket.SendBinary. -type WebSocketSendBinaryResponse struct { - Error string `json:"error,omitempty"` -} - // WebSocketCloseConnectionRequest is the request type for WebSocket.CloseConnection. type WebSocketCloseConnectionRequest struct { ConnectionID string `json:"connectionId"` @@ -50,11 +40,6 @@ type WebSocketCloseConnectionRequest struct { Reason string `json:"reason"` } -// WebSocketCloseConnectionResponse is the response type for WebSocket.CloseConnection. -type WebSocketCloseConnectionResponse struct { - Error string `json:"error,omitempty"` -} - // WebSocketConnect is a stub that panics on non-WASM platforms. // Connect establishes a WebSocket connection to the specified URL. // @@ -80,7 +65,7 @@ func WebSocketConnect(url string, headers map[string]string, connectionID string // - message: The text message to send // // Returns an error if the connection is not found or if sending fails. -func WebSocketSendText(connectionID string, message string) (*WebSocketSendTextResponse, error) { +func WebSocketSendText(connectionID string, message string) error { panic("host: WebSocketSendText is only available in WASM plugins") } @@ -92,7 +77,7 @@ func WebSocketSendText(connectionID string, message string) (*WebSocketSendTextR // - data: The binary data to send // // Returns an error if the connection is not found or if sending fails. -func WebSocketSendBinary(connectionID string, data []byte) (*WebSocketSendBinaryResponse, error) { +func WebSocketSendBinary(connectionID string, data []byte) error { panic("host: WebSocketSendBinary is only available in WASM plugins") } @@ -105,6 +90,6 @@ func WebSocketSendBinary(connectionID string, data []byte) (*WebSocketSendBinary // - reason: Optional human-readable reason for closing // // Returns an error if the connection is not found or if closing fails. -func WebSocketCloseConnection(connectionID string, code int32, reason string) (*WebSocketCloseConnectionResponse, error) { +func WebSocketCloseConnection(connectionID string, code int32, reason string) error { panic("host: WebSocketCloseConnection is only available in WASM plugins") } diff --git a/plugins/testdata/test-cache-plugin/main.go b/plugins/testdata/test-cache-plugin/main.go index 5db5e1d64..13aa12ecb 100644 --- a/plugins/testdata/test-cache-plugin/main.go +++ b/plugins/testdata/test-cache-plugin/main.go @@ -41,7 +41,7 @@ func ndTestCache() int32 { switch input.Operation { case "set_string": - _, err := host.CacheSetString(input.Key, input.StringVal, input.TTLSeconds) + err := host.CacheSetString(input.Key, input.StringVal, input.TTLSeconds) if err != nil { errStr := err.Error() pdk.OutputJSON(TestCacheOutput{Error: &errStr}) @@ -61,7 +61,7 @@ func ndTestCache() int32 { return 0 case "set_int": - _, err := host.CacheSetInt(input.Key, input.IntVal, input.TTLSeconds) + err := host.CacheSetInt(input.Key, input.IntVal, input.TTLSeconds) if err != nil { errStr := err.Error() pdk.OutputJSON(TestCacheOutput{Error: &errStr}) @@ -81,7 +81,7 @@ func ndTestCache() int32 { return 0 case "set_float": - _, err := host.CacheSetFloat(input.Key, input.FloatVal, input.TTLSeconds) + err := host.CacheSetFloat(input.Key, input.FloatVal, input.TTLSeconds) if err != nil { errStr := err.Error() pdk.OutputJSON(TestCacheOutput{Error: &errStr}) @@ -101,7 +101,7 @@ func ndTestCache() int32 { return 0 case "set_bytes": - _, err := host.CacheSetBytes(input.Key, input.BytesVal, input.TTLSeconds) + err := host.CacheSetBytes(input.Key, input.BytesVal, input.TTLSeconds) if err != nil { errStr := err.Error() pdk.OutputJSON(TestCacheOutput{Error: &errStr}) @@ -131,7 +131,7 @@ func ndTestCache() int32 { return 0 case "remove": - _, err := host.CacheRemove(input.Key) + err := host.CacheRemove(input.Key) if err != nil { errStr := err.Error() pdk.OutputJSON(TestCacheOutput{Error: &errStr}) diff --git a/plugins/testdata/test-kvstore/main.go b/plugins/testdata/test-kvstore/main.go index 48f9a2b2f..cef5afc2c 100644 --- a/plugins/testdata/test-kvstore/main.go +++ b/plugins/testdata/test-kvstore/main.go @@ -37,7 +37,7 @@ func ndTestKVStore() int32 { switch input.Operation { case "set": - _, err := host.KVStoreSet(input.Key, input.Value) + err := host.KVStoreSet(input.Key, input.Value) if err != nil { errStr := err.Error() pdk.OutputJSON(TestKVStoreOutput{Error: &errStr}) @@ -57,7 +57,7 @@ func ndTestKVStore() int32 { return 0 case "delete": - _, err := host.KVStoreDelete(input.Key) + err := host.KVStoreDelete(input.Key) if err != nil { errStr := err.Error() pdk.OutputJSON(TestKVStoreOutput{Error: &errStr}) diff --git a/plugins/testdata/test-websocket/main.go b/plugins/testdata/test-websocket/main.go index bd5a2b6c4..a59800048 100644 --- a/plugins/testdata/test-websocket/main.go +++ b/plugins/testdata/test-websocket/main.go @@ -28,12 +28,12 @@ func (t *testWebSocket) OnTextMessage(input websocket.OnTextMessageRequest) erro switch input.Message { case "echo": - if _, err := host.WebSocketSendText(input.ConnectionID, "echo:"+input.Message); err != nil { + if err := host.WebSocketSendText(input.ConnectionID, "echo:"+input.Message); err != nil { return err } case "close": - if _, err := host.WebSocketCloseConnection(input.ConnectionID, 1000, "closed by plugin"); err != nil { + if err := host.WebSocketCloseConnection(input.ConnectionID, 1000, "closed by plugin"); err != nil { return err }