feat(plugins): New Plugin System with multi-language PDK support (#4833)

* chore(plugins): remove the old plugins system implementation

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): implement new plugin system with using Extism

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): add capability detection for plugins based on exported functions

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): add auto-reload functionality for plugins with file watcher support

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): add auto-reload functionality for plugins with file watcher support

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(plugins): standardize variable names and remove superfluous wrapper functions

Signed-off-by: Deluan <deluan@navidrome.org>

* fix(plugins): improve error handling and logging in plugin manager

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(plugins): implement plugin function call helper and refactor MetadataAgent methods

Signed-off-by: Deluan <deluan@navidrome.org>

* fix(plugins): race condition in plugin manager

* tests(plugins): change BeforeEach to BeforeAll in MetadataAgent tests

Signed-off-by: Deluan <deluan@navidrome.org>

* tests(plugins): optimize tests

Signed-off-by: Deluan <deluan@navidrome.org>

* tests(plugins): more optimizations

Signed-off-by: Deluan <deluan@navidrome.org>

* test(plugins): ignore goroutine leaks from notify library in tests

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): add Wikimedia plugin for Navidrome to fetch artist metadata

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): enhance plugin logging and set User-Agent header

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): implement scrobbler plugin with authorization and scrobbling capabilities

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): integrate logs

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(plugins): clean up manifest struct and improve plugin loading logic

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): add metadata agent and scrobbler schemas for bootstrapping plugins

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(hostgen): add hostgen tool for generating Extism host function wrappers

- Implemented hostgen tool to generate wrappers from annotated Go interfaces.
- Added command-line flags for input/output directories and package name.
- Introduced parsing and code generation logic for host services.
- Created test data for various service interfaces and expected generated code.
- Added documentation for host services and annotations for code generation.
- Implemented SubsonicAPI service with corresponding generated code.

* feat(subsonicapi): update Call method to return JSON string response

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): implement SubsonicAPI host function integration with permissions

Signed-off-by: Deluan <deluan@navidrome.org>

* fix(generator): error-only methods in response handling

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): generate client wrappers for host functions

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(generator): remove error handling for response.Error in client templates

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(scheduler): add Scheduler service interface with host function wrappers for scheduling tasks

* feat(plugins): add WASI build constraints to client wrapper templates, to avoid lint errors

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(scheduler): implement Scheduler service with one-time and recurring scheduling capabilities

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(manifest): remove unused ConfigPermission from permissions schema

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(scheduler): add scheduler callback schema and implementation for plugins

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(scheduler): streamline scheduling logic and remove unused callback tracking

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(scheduler): add Close method for resource cleanup on plugin unload

Signed-off-by: Deluan <deluan@navidrome.org>

* docs(scheduler): clarify SchedulerCallback requirement for scheduling functions

Signed-off-by: Deluan <deluan@navidrome.org>

* fix: update wasm build rule to include all Go files in the directory

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: rewrite the wikimedia plugin using the XTP CLI

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(scheduler): replace uuid with id.NewRandom for schedule ID generation

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor: capabilities registration

Signed-off-by: Deluan <deluan@navidrome.org>

* test: add scheduler service isolation test for plugin instances

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor: update plugin manager initialization and encapsulate logic

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: add WebSocket service definitions for plugin communication

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: implement WebSocket service for plugin integration and connection management

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: add Crypto Ticker example plugin for real-time cryptocurrency price updates via Coinbase WebSocket API

Also add the lifecycle capability

Signed-off-by: Deluan <deluan@navidrome.org>

* fix: use context.Background() in invokeCallback for scheduled tasks

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor: rename plugin.create() to plugin.instance()

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor: rename pluginInstance to plugin for consistency across the codebase

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor: simplify schedule cloning in Close method and enhance plugin cleanup error handling

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: implement Artwork service for generating artwork URLs in Navidrome plugins - WIP

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor: moved public URL builders to avoid import cycles

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: add Cache service for in-memory TTL-based caching in plugins

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: add Discord Rich Presence example plugin for Navidrome integration

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor: host function wrappers to use structured request and response types

- Updated the host function signatures in `nd_host_artwork.go`, `nd_host_scheduler.go`, `nd_host_subsonicapi.go`, and `nd_host_websocket.go` to accept a single parameter for JSON requests.
- Introduced structured request and response types for various cache operations in `nd_host_cache.go`.
- Modified cache functions to marshal requests to JSON and unmarshal responses, improving error handling and code clarity.
- Removed redundant memory allocation for string parameters in favor of JSON marshaling.
- Enhanced error handling in WebSocket and cache operations to return structured error responses.

* refactor: error handling in various plugins to convert response.Error to Go errors

- Updated error handling in `nd_host_scheduler.go`, `nd_host_websocket.go`, `nd_host_artwork.go`, `nd_host_cache.go`, and `nd_host_subsonicapi.go` to convert string errors from responses into Go errors.
- Removed redundant error checks in test data plugins for cleaner code.
- Ensured consistent error handling across all plugins to improve reliability and maintainability.

* refactor: rename fake plugins to test plugins for clarity in integration tests

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: add help target to Makefile for plugin usage instructions

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: add Cover Art Archive plugin as an example of Python plugin

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: update Makefile and README to clarify Go plugin usage

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: include plugin capabilities in loading log message

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: add trace logging for plugin availability and error handling in agents

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: add Now Playing Logger plugin to showcase calling host functions from Python plugins

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: generate Python client wrappers for various host services

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: add generated host function wrappers for Scheduler and SubsonicAPI services

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: update Python plugin documentation and usage instructions for host function wrappers

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: add Webhook Scrobbler plugin in Rust to send HTTP notifications on scrobble events

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: enable parallel loading of plugins during startup

Signed-off-by: Deluan <deluan@navidrome.org>

* docs: update README to include WebSocket callback schema in plugin documentation

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: extend plugin watcher with improved logging and debounce duration adjustment

Signed-off-by: Deluan <deluan@navidrome.org>

* add trace message for plugin recompiles

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: implement plugin cache purging functionality

Signed-off-by: Deluan <deluan@navidrome.org>

* test: move purgeCacheBySize unit tests

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins UI): add plugin repository and database support

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins UI): add plugin management routes and middleware

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins UI): implement plugin synchronization with database for add, update, and remove actions

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins UI): add PluginList and PluginShow components with plugin management functionality

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): optimize plugin change detection

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(plugins UI): improve PluginList structure

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins UI): enhance PluginShow with author, website, and permissions display

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins UI): refactor to use MUI and RA components

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins UI): add error handling for plugin enable/disable actions

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(plugins): inject PluginManager into native API

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(plugins): update GetManager to accept DataStore parameter

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): add subsonicRouter to Manager and refactor host service registration

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(plugins): enhance debug logging for plugin actions and recompile logic

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(plugins): break manager.go into smaller, focused files

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(plugins): streamline error handling and improve plugin retrieval logic

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(plugins): update newWebSocketService to use WebSocketPermission for allowed hosts

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(plugins): introduce ToggleEnabledSwitch for managing plugin enable/disable state

Signed-off-by: Deluan <deluan@navidrome.org>

* docs: update READMEs

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(library): add Library service for metadata access and filesystem integration

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): add Library Inspector plugin for periodic library inspection and file size logging

Signed-off-by: Deluan <deluan@navidrome.org>

* docs: update README to reflect JSON configuration format for plugins

Signed-off-by: Deluan <deluan@navidrome.org>

* fix(build): update target to wasm32-wasip1 for improved WASI support

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): implement configuration management UI with key-value pairs support

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(ui): adjust grid layout in InfoRow component for improved responsiveness

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): rename ErrorIndicator to EnabledOrErrorField and enhance error handling logic

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(i18n): add Portuguese translations for plugin management and notifications

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): add support for .ndp plugin packages and update build process

Signed-off-by: Deluan <deluan@navidrome.org>

* docs: update README for .ndp plugin packaging and installation instructions

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): implement KVStore service for persistent key-value storage

Signed-off-by: Deluan <deluan@navidrome.org>

* docs: enhance README with Extism plugin development resources and recommendations

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): integrate event broker into plugin manager

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): update config handling in PluginShow to track last record state

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): add Rust host function library and example implementation of Discord Rich Presence plugin in Rust

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): generate Rust lib.rs file to expose host function wrappers

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(plugins): update JSON field names to camelCase for consistency

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor: reduce cyclomatic complexity by refactoring main function

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): enhance Rust code generation with typed struct support and improved type handling

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): add Go client library with host function wrappers and documentation

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): generate Go client stubs for non-WASM platforms

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): update client template file names for consistency

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): add initial implementation of the Navidrome Plugin Development Kit code generator - Pahse 1

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): implementation of the Navidrome Plugin Development Kit with generated client wrappers and service interfaces - Phase 2

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): implementation of the Navidrome Plugin Development Kit with generated client wrappers and service interfaces - Phase 2 (2)

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): implementation of the Navidrome Plugin Development Kit with generated client wrappers and service interfaces - Phase 3

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): implementation of the Navidrome Plugin Development Kit with generated client wrappers and service interfaces - Phase 4

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): implementation of the Navidrome Plugin Development Kit with generated client wrappers and service interfaces - Phase 5

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(plugins): consistent naming/types across PDK

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(plugins): streamline plugin function signatures and error handling

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(plugins): update scrobbler interface to return errors directly instead of response structs

Signed-off-by: Deluan <deluan@navidrome.org>

* test: make all test plugins use the PDK

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(plugins): reorganize and sort type definitions for consistency

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(plugins): update error handling for methods to return errors directly

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(plugins): update function signatures to return values directly instead of response structs

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(plugins): update request/response types to use private naming conventions

Signed-off-by: Deluan <deluan@navidrome.org>

* build: mark .wasm files as intermediate for cleanup after building .ndp

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(plugins): consolidate PDK module path and update Go version to 1.25

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: implement Rust PDK

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(plugins): reorganize Rust output structure to follow standard conventions

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(plugins): update Discord Rich Presence and Library Inspector plugins to use nd-pdk for service calls and implement lifecycle management

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(plugins): update macro names for websocket and metadata registration to improve clarity and consistency

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(plugins): rename scheduler callback methods for consistency and clarity

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(plugins): update export wrappers to use `//go:wasmexport` for WebAssembly compatibility

Signed-off-by: Deluan <deluan@navidrome.org>

* docs: update plugin registration docs

Signed-off-by: Deluan <deluan@navidrome.org>

* fix(plugins): generate host wrappers

Signed-off-by: Deluan <deluan@navidrome.org>

* test(plugins): conditionally run goleak checks based on CI environment

Signed-off-by: Deluan <deluan@navidrome.org>

* docs: update README to reflect changes in plugin import paths

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor(plugins): update plugin instance creation to accept context for cancellation support

Signed-off-by: Deluan <deluan@navidrome.org>

* fix(plugins): update return types in metadata interfaces to use pointers

Signed-off-by: Deluan <deluan@navidrome.org>

* fix(plugins): enhance type handling for Rust and XTP output in capability generation

Signed-off-by: Deluan <deluan@navidrome.org>

* fix(plugins): update IsAuthorized method to return boolean instead of response object

Signed-off-by: Deluan <deluan@navidrome.org>

* test(plugins): add unit tests for rustOutputType and isPrimitiveRustType functions

Signed-off-by: Deluan <deluan@navidrome.org>

* feat(plugins): implement XTP JSONSchema validation for generated schemas

Signed-off-by: Deluan <deluan@navidrome.org>

* fix(plugins): update response types in testMetadataAgent methods to use pointers

Signed-off-by: Deluan <deluan@navidrome.org>

* docs: update Go and Rust plugin developer sections for clarity

Signed-off-by: Deluan <deluan@navidrome.org>

* docs: correct example link for library inspector in README

Signed-off-by: Deluan <deluan@navidrome.org>

* docs: clarify artwork URL generation capabilities in service descriptions

Signed-off-by: Deluan <deluan@navidrome.org>

* docs: update README to include Rust PDK crate information for plugin developers

Signed-off-by: Deluan <deluan@navidrome.org>

* fix: handle URL parsing errors and use atomic upsert in plugin repository

Added proper error handling for url.Parse calls in PublicURL and AbsoluteURL
functions. When parsing fails, PublicURL now falls back to AbsoluteURL, and
AbsoluteURL logs the error and returns an empty string, preventing malformed
URLs from being generated.

Replaced the non-atomic UPDATE-then-INSERT pattern in plugin repository Put
method with a single atomic INSERT ... ON CONFLICT statement. This eliminates
potential race conditions and improves consistency with the upsert pattern
already used in host_kvstore.go.

* feat: implement mock service instances for non-WASM builds using testify/mock

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor: Discord RPC struct to encapsulate WebSocket logic

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: add support for experimental WebAssembly threads

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: add PDK abstraction layer with mock support for non-WASM builds

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: add unit tests for Discord plugin and RPC functionality

Signed-off-by: Deluan <deluan@navidrome.org>

* fix: update return types in minimalPlugin and wikimediaPlugin methods to use pointers

Signed-off-by: Deluan <deluan@navidrome.org>

* fix: context cancellation and implement WebSocket callback timeout for improved error handling

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: conditionally include error handling in generated client code templates

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: implement ConfigService for plugin configuration management

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: enhance plugin manager to support metrics recording

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor: make MockPDK private

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor: update interface types to use 'any' in plugin repository methods

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor: rename List method to Keys for clarity in configuration management

Signed-off-by: Deluan <deluan@navidrome.org>

* test: add ndpgen plugin tests in the pipeline and update Makefile

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: add users permission management to plugin system

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor: streamline users integration tests and enhance plugin user management

Signed-off-by: Deluan <deluan@navidrome.org>

* refactor: remove UserID from scrobbler request structure

Signed-off-by: Deluan <deluan@navidrome.org>

* test: add integration tests for UsersService enable gate behavior

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: implement user permissions for SubsonicAPI and scrobbler plugins

Signed-off-by: Deluan <deluan@navidrome.org>

* fix: show proper error in the UI when enabling a plugin fails

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: add library permission management to plugin system

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: add user permission for processing scrobbles in Discord Rich Presence plugin

Signed-off-by: Deluan <deluan@navidrome.org>

* fix: implement dynamic loading for buffered scrobbler plugins

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: add GetAdmins method to retrieve admin users from the plugin

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: update Portuguese translations for user and library permissions

Signed-off-by: Deluan <deluan@navidrome.org>

* reorder migrations

Signed-off-by: Deluan <deluan@navidrome.org>

* fix: remove unnecessary bulkActionButtons prop from PluginList component

* feat: add manual plugin rescan functionality and corresponding UI action

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: implement user/library and plugin management integration with cleanup on deletion

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: replace core mock services with test-specific implementations to avoid import cycles

* feat: add ID fields to Artist and Song structs and enhance track loading logic by prioritizing ID matches

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: update plugin permissions from allowedHosts to requiredHosts for better clarity and consistency

* feat: refactor plugin host permissions to use RequiredHosts directly for improved clarity

* fix: don't record metrics for plugin calls that aren't implemented at all

Signed-off-by: Deluan <deluan@navidrome.org>

* fix: enhance connection management with improved error handling and cleanup logic

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: introduce ArtistRef struct for better artist representation and update track metadata handling

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: update user configuration handling to use user key prefix for improved clarity

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: enhance ConfigCard input fields with multiline support and vertical resizing

Signed-off-by: Deluan <deluan@navidrome.org>

* fix: rust plugin compilation error

Signed-off-by: Deluan <deluan@navidrome.org>

* feat: implement IsOptionPattern method for better return type handling in Rust PDK generation

Signed-off-by: Deluan <deluan@navidrome.org>

---------

Signed-off-by: Deluan <deluan@navidrome.org>
This commit is contained in:
Deluan Quintão
2026-01-14 19:22:48 -05:00
committed by GitHub
parent fd4a04339e
commit 03a45753e9
518 changed files with 49456 additions and 34933 deletions
+859
View File
@@ -0,0 +1,859 @@
package internal
import (
"bytes"
"embed"
"fmt"
"strings"
"text/template"
)
//go:embed templates/*.tmpl
var templatesFS embed.FS
// hostFuncMap returns the template functions for host code generation.
func hostFuncMap(svc Service) template.FuncMap {
return template.FuncMap{
"lower": strings.ToLower,
"title": strings.Title,
"exportName": func(m Method) string { return m.FunctionName(svc.ExportPrefix()) },
"requestType": func(m Method) string { return m.RequestTypeName(svc.Name) },
"responseType": func(m Method) string { return m.ResponseTypeName(svc.Name) },
}
}
// clientFuncMap returns the template functions for client code generation.
// Uses private (lowercase) type names for request/response structs.
func clientFuncMap(svc Service) template.FuncMap {
return template.FuncMap{
"lower": strings.ToLower,
"title": strings.Title,
"exportName": func(m Method) string { return m.FunctionName(svc.ExportPrefix()) },
"requestType": func(m Method) string { return m.ClientRequestTypeName(svc.Name) },
"responseType": func(m Method) string { return m.ClientResponseTypeName(svc.Name) },
"formatDoc": formatDoc,
"mockReturnValues": mockReturnValues,
}
}
// mockReturnValues generates the testify mock return value accessors for a method.
// For example: args.String(0), args.Bool(1), args.Error(2)
func mockReturnValues(m Method) string {
var parts []string
idx := 0
for _, r := range m.Returns {
parts = append(parts, mockAccessor(r.Type, idx))
idx++
}
if m.HasError {
parts = append(parts, fmt.Sprintf("args.Error(%d)", idx))
}
return strings.Join(parts, ", ")
}
// mockAccessor returns the testify mock accessor call for a given type and index.
func mockAccessor(typ string, idx int) string {
switch {
case typ == "string":
return fmt.Sprintf("args.String(%d)", idx)
case typ == "bool":
return fmt.Sprintf("args.Bool(%d)", idx)
case typ == "int":
return fmt.Sprintf("args.Int(%d)", idx)
case typ == "int64":
return fmt.Sprintf("args.Get(%d).(int64)", idx)
case typ == "int32":
return fmt.Sprintf("args.Get(%d).(int32)", idx)
case typ == "float64":
return fmt.Sprintf("args.Get(%d).(float64)", idx)
case typ == "float32":
return fmt.Sprintf("args.Get(%d).(float32)", idx)
case typ == "[]byte":
return fmt.Sprintf("args.Get(%d).([]byte)", idx)
default:
// For slices, maps, pointers, and custom types, use Get with type assertion
return fmt.Sprintf("args.Get(%d).(%s)", idx, typ)
}
}
// pythonFuncMap returns the template functions for Python client code generation.
func pythonFuncMap(svc Service) template.FuncMap {
return template.FuncMap{
"lower": strings.ToLower,
"exportName": func(m Method) string { return m.FunctionName(svc.ExportPrefix()) },
"pythonFunc": func(m Method) string { return m.PythonFunctionName(svc.ExportPrefix()) },
"pythonResultType": func(m Method) string { return m.PythonResultTypeName(svc.Name) },
"pythonDefault": pythonDefaultValue,
}
}
// GenerateHost generates the host function wrapper code for a service.
func GenerateHost(svc Service, pkgName string) ([]byte, error) {
tmplContent, err := templatesFS.ReadFile("templates/host.go.tmpl")
if err != nil {
return nil, fmt.Errorf("reading host template: %w", err)
}
tmpl, err := template.New("host").Funcs(hostFuncMap(svc)).Parse(string(tmplContent))
if err != nil {
return nil, fmt.Errorf("parsing template: %w", err)
}
data := templateData{
Package: pkgName,
Service: svc,
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil {
return nil, fmt.Errorf("executing template: %w", err)
}
return buf.Bytes(), nil
}
// GenerateClientGo generates client wrapper code for plugins to call host functions.
func GenerateClientGo(svc Service, pkgName string) ([]byte, error) {
tmplContent, err := templatesFS.ReadFile("templates/client.go.tmpl")
if err != nil {
return nil, fmt.Errorf("reading client template: %w", err)
}
tmpl, err := template.New("client").Funcs(clientFuncMap(svc)).Parse(string(tmplContent))
if err != nil {
return nil, fmt.Errorf("parsing template: %w", err)
}
data := templateData{
Package: pkgName,
Service: svc,
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil {
return nil, fmt.Errorf("executing template: %w", err)
}
return buf.Bytes(), nil
}
// GenerateClientGoStub generates stub code for non-WASM platforms.
// These stubs provide type definitions and function signatures for IDE support,
// but panic at runtime since host functions are only available in WASM plugins.
func GenerateClientGoStub(svc Service, pkgName string) ([]byte, error) {
tmplContent, err := templatesFS.ReadFile("templates/client_stub.go.tmpl")
if err != nil {
return nil, fmt.Errorf("reading client stub template: %w", err)
}
tmpl, err := template.New("client_stub").Funcs(clientFuncMap(svc)).Parse(string(tmplContent))
if err != nil {
return nil, fmt.Errorf("parsing template: %w", err)
}
data := templateData{
Package: pkgName,
Service: svc,
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil {
return nil, fmt.Errorf("executing template: %w", err)
}
return buf.Bytes(), nil
}
type templateData struct {
Package string
Service Service
}
// formatDoc formats a documentation string for Go comments.
// It prefixes each line with "// " and trims trailing whitespace.
func formatDoc(doc string) string {
if doc == "" {
return ""
}
lines := strings.Split(strings.TrimSpace(doc), "\n")
var result []string
for _, line := range lines {
result = append(result, "// "+strings.TrimRight(line, " \t"))
}
return strings.Join(result, "\n")
}
// GenerateClientPython generates Python client wrapper code for plugins.
func GenerateClientPython(svc Service) ([]byte, error) {
tmplContent, err := templatesFS.ReadFile("templates/client.py.tmpl")
if err != nil {
return nil, fmt.Errorf("reading Python client template: %w", err)
}
tmpl, err := template.New("client_py").Funcs(pythonFuncMap(svc)).Parse(string(tmplContent))
if err != nil {
return nil, fmt.Errorf("parsing template: %w", err)
}
data := templateData{
Service: svc,
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil {
return nil, fmt.Errorf("executing template: %w", err)
}
return buf.Bytes(), nil
}
// pythonDefaultValue returns a Python default value for response.get() calls.
func pythonDefaultValue(p Param) string {
switch p.Type {
case "string":
return `, ""`
case "int", "int32", "int64":
return ", 0"
case "float32", "float64":
return ", 0.0"
case "bool":
return ", False"
case "[]byte":
return ", b\"\""
default:
return ", None"
}
}
// rustFuncMap returns the template functions for Rust client code generation.
func rustFuncMap(svc Service) template.FuncMap {
knownStructs := svc.KnownStructs()
return template.FuncMap{
"lower": strings.ToLower,
"exportName": func(m Method) string { return m.FunctionName(svc.ExportPrefix()) },
"requestType": func(m Method) string { return m.RequestTypeName(svc.Name) },
"responseType": func(m Method) string { return m.ResponseTypeName(svc.Name) },
"rustFunc": func(m Method) string { return m.RustFunctionName(svc.ExportPrefix()) },
"rustDocComment": RustDocComment,
"rustType": func(p Param) string { return p.RustTypeWithStructs(knownStructs) },
"rustParamType": func(p Param) string { return p.RustParamTypeWithStructs(knownStructs) },
"fieldRustType": func(f FieldDef) string { return f.RustType(knownStructs) },
}
}
// GenerateClientRust generates Rust client wrapper code for plugins.
func GenerateClientRust(svc Service) ([]byte, error) {
tmplContent, err := templatesFS.ReadFile("templates/client.rs.tmpl")
if err != nil {
return nil, fmt.Errorf("reading Rust client template: %w", err)
}
tmpl, err := template.New("client_rs").Funcs(rustFuncMap(svc)).Parse(string(tmplContent))
if err != nil {
return nil, fmt.Errorf("parsing template: %w", err)
}
data := templateData{
Service: svc,
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil {
return nil, fmt.Errorf("executing template: %w", err)
}
return buf.Bytes(), nil
}
// firstLine returns the first line of a multi-line string, with the first word removed.
func firstLine(s string) string {
line := s
if idx := strings.Index(s, "\n"); idx >= 0 {
line = s[:idx]
}
// Remove the first word (service name like "ArtworkService")
if idx := strings.Index(line, " "); idx >= 0 {
line = line[idx+1:]
}
return line
}
// GenerateRustLib generates the lib.rs file that exposes all service modules.
func GenerateRustLib(services []Service) ([]byte, error) {
tmplContent, err := templatesFS.ReadFile("templates/lib.rs.tmpl")
if err != nil {
return nil, fmt.Errorf("reading Rust lib template: %w", err)
}
tmpl, err := template.New("lib_rs").Funcs(template.FuncMap{
"lower": strings.ToLower,
"firstLine": firstLine,
}).Parse(string(tmplContent))
if err != nil {
return nil, fmt.Errorf("parsing template: %w", err)
}
data := struct {
Services []Service
}{
Services: services,
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil {
return nil, fmt.Errorf("executing template: %w", err)
}
return buf.Bytes(), nil
}
// GenerateGoDoc generates the doc.go file that provides package documentation.
func GenerateGoDoc(services []Service, pkgName string) ([]byte, error) {
tmplContent, err := templatesFS.ReadFile("templates/doc.go.tmpl")
if err != nil {
return nil, fmt.Errorf("reading Go doc template: %w", err)
}
tmpl, err := template.New("doc_go").Funcs(template.FuncMap{
"firstLine": firstLine,
}).Parse(string(tmplContent))
if err != nil {
return nil, fmt.Errorf("parsing template: %w", err)
}
data := struct {
Package string
Services []Service
}{
Package: pkgName,
Services: services,
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil {
return nil, fmt.Errorf("executing template: %w", err)
}
return buf.Bytes(), nil
}
// GenerateGoMod generates the go.mod file for the Go client library.
func GenerateGoMod() ([]byte, error) {
tmplContent, err := templatesFS.ReadFile("templates/go.mod.tmpl")
if err != nil {
return nil, fmt.Errorf("reading go.mod template: %w", err)
}
return tmplContent, nil
}
// capabilityTemplateData holds data for capability template execution.
type capabilityTemplateData struct {
Package string
Capability Capability
}
// capabilityFuncMap returns template functions for capability code generation.
func capabilityFuncMap(cap Capability) template.FuncMap {
return template.FuncMap{
"formatDoc": formatDoc,
"indent": indentText,
"agentName": capabilityAgentName,
"providerInterface": func(e Export) string { return e.ProviderInterfaceName() },
"implVar": func(e Export) string { return e.ImplVarName() },
"exportFunc": func(e Export) string { return e.ExportFuncName() },
}
}
// indentText adds n tabs to each line of text.
func indentText(n int, s string) string {
indent := strings.Repeat("\t", n)
lines := strings.Split(s, "\n")
for i, line := range lines {
if line != "" {
lines[i] = indent + line
}
}
return strings.Join(lines, "\n")
}
// capabilityAgentName returns the interface name for a capability.
// Uses the Go interface name stripped of common suffixes.
func capabilityAgentName(cap Capability) string {
name := cap.Interface
// Remove common suffixes to get a clean name
for _, suffix := range []string{"Agent", "Callback", "Service"} {
if strings.HasSuffix(name, suffix) {
name = name[:len(name)-len(suffix)]
break
}
}
// Use the shortened name or the original if no suffix found
if name == "" {
name = cap.Interface
}
return name
}
// GenerateCapabilityGo generates Go export wrapper code for a capability.
func GenerateCapabilityGo(cap Capability, pkgName string) ([]byte, error) {
tmplContent, err := templatesFS.ReadFile("templates/capability.go.tmpl")
if err != nil {
return nil, fmt.Errorf("reading capability template: %w", err)
}
tmpl, err := template.New("capability").Funcs(capabilityFuncMap(cap)).Parse(string(tmplContent))
if err != nil {
return nil, fmt.Errorf("parsing template: %w", err)
}
data := capabilityTemplateData{
Package: pkgName,
Capability: cap,
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil {
return nil, fmt.Errorf("executing template: %w", err)
}
return buf.Bytes(), nil
}
// GenerateCapabilityGoStub generates stub code for non-WASM platforms.
func GenerateCapabilityGoStub(cap Capability, pkgName string) ([]byte, error) {
tmplContent, err := templatesFS.ReadFile("templates/capability_stub.go.tmpl")
if err != nil {
return nil, fmt.Errorf("reading capability stub template: %w", err)
}
tmpl, err := template.New("capability_stub").Funcs(capabilityFuncMap(cap)).Parse(string(tmplContent))
if err != nil {
return nil, fmt.Errorf("parsing template: %w", err)
}
data := capabilityTemplateData{
Package: pkgName,
Capability: cap,
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil {
return nil, fmt.Errorf("executing template: %w", err)
}
return buf.Bytes(), nil
}
// rustCapabilityFuncMap returns template functions for Rust capability code generation.
func rustCapabilityFuncMap(cap Capability) template.FuncMap {
knownStructs := cap.KnownStructs()
return template.FuncMap{
"rustDocComment": RustDocComment,
"rustTypeAlias": rustTypeAlias,
"rustConstType": rustConstType,
"rustConstName": rustConstName,
"rustFieldName": func(name string) string { return ToSnakeCase(name) },
"rustMethodName": func(name string) string { return ToSnakeCase(name) },
"fieldRustType": func(f FieldDef) string { return f.RustType(knownStructs) },
"rustOutputType": rustOutputType,
"isPrimitiveRust": isPrimitiveRustType,
"skipSerializingFunc": skipSerializingFunc,
"hasHashMap": hasHashMap,
"agentName": capabilityAgentName,
"providerInterface": func(e Export) string { return e.ProviderInterfaceName() },
"registerMacroName": func(name string) string { return registerMacroName(cap.Name, name) },
"snakeCase": ToSnakeCase,
"indent": func(spaces int, s string) string {
indent := strings.Repeat(" ", spaces)
lines := strings.Split(s, "\n")
for i, line := range lines {
if line != "" {
lines[i] = indent + line
}
}
return strings.Join(lines, "\n")
},
}
}
// rustTypeAlias converts a Go type to its Rust equivalent for type aliases.
// For string types used as error sentinels/constants, we use &'static str
// since Rust consts can't be heap-allocated String values.
func rustTypeAlias(goType string) string {
switch goType {
case "string":
return "&'static str"
case "int", "int32":
return "i32"
case "int64":
return "i64"
default:
return goType
}
}
// rustConstType converts a Go type to its Rust equivalent for const declarations.
// For String types, it returns &'static str since Rust consts can't be heap-allocated.
func rustConstType(goType string) string {
switch goType {
case "string", "String":
return "&'static str"
case "int", "int32":
return "i32"
case "int64":
return "i64"
default:
return goType
}
}
// rustOutputType converts a Go type to Rust for capability method signatures.
// It handles pointer types specially - for capability outputs, pointers become the base type
// (not Option<T>) because Rust's Result<T, Error> already provides optional semantics.
//
// TODO: Pointer to primitive types (e.g., *string, *int32) are not handled correctly.
// Currently "*string" returns "string" instead of "String". This would generate invalid
// Rust code. No current capability uses this pattern, but it should be fixed if needed.
func rustOutputType(goType string) string {
// Strip pointer prefix - capability outputs use Result<T, Error> for optionality
if strings.HasPrefix(goType, "*") {
return goType[1:]
}
// Convert Go primitives to Rust primitives
switch goType {
case "bool":
return "bool"
case "string":
return "String"
case "int", "int32":
return "i32"
case "int64":
return "i64"
case "float32":
return "f32"
case "float64":
return "f64"
}
return goType
}
// isPrimitiveRustType returns true if the Go type maps to a Rust primitive type.
func isPrimitiveRustType(goType string) bool {
// Strip pointer prefix first
if strings.HasPrefix(goType, "*") {
goType = goType[1:]
}
switch goType {
case "bool", "string", "int", "int32", "int64", "float32", "float64":
return true
}
return false
}
// rustConstName converts a Go const name to Rust convention (SCREAMING_SNAKE_CASE).
func rustConstName(name string) string {
return strings.ToUpper(ToSnakeCase(name))
}
// skipSerializingFunc returns the appropriate skip_serializing_if function name.
func skipSerializingFunc(goType string) string {
if strings.HasPrefix(goType, "*") || strings.HasPrefix(goType, "[]") || strings.HasPrefix(goType, "map[") {
return "Option::is_none"
}
switch goType {
case "string":
return "String::is_empty"
case "bool":
return "std::ops::Not::not"
default:
return "Option::is_none"
}
}
// hasHashMap returns true if any struct in the capability uses HashMap.
func hasHashMap(cap Capability) bool {
for _, st := range cap.Structs {
for _, f := range st.Fields {
if strings.HasPrefix(f.Type, "map[") {
return true
}
}
}
return false
}
// registerMacroName returns the macro name for registering an optional method.
// For package "websocket" and method "OnClose", returns "register_websocket_close".
func registerMacroName(pkg, name string) string {
// Remove common prefixes from method name
for _, prefix := range []string{"Get", "On"} {
if strings.HasPrefix(name, prefix) {
name = name[len(prefix):]
break
}
}
return "register_" + ToSnakeCase(pkg) + "_" + ToSnakeCase(name)
}
// GenerateCapabilityRust generates Rust export wrapper code for a capability.
func GenerateCapabilityRust(cap Capability) ([]byte, error) {
tmplContent, err := templatesFS.ReadFile("templates/capability.rs.tmpl")
if err != nil {
return nil, fmt.Errorf("reading Rust capability template: %w", err)
}
tmpl, err := template.New("capability_rust").Funcs(rustCapabilityFuncMap(cap)).Parse(string(tmplContent))
if err != nil {
return nil, fmt.Errorf("parsing template: %w", err)
}
data := capabilityTemplateData{
Package: cap.Name,
Capability: cap,
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil {
return nil, fmt.Errorf("executing template: %w", err)
}
return buf.Bytes(), nil
}
// GenerateCapabilityRustLib generates the lib.rs file for the Rust capabilities crate.
func GenerateCapabilityRustLib(capabilities []Capability) ([]byte, error) {
var buf bytes.Buffer
buf.WriteString("// Code generated by ndpgen. DO NOT EDIT.\n\n")
buf.WriteString("//! Navidrome Plugin Development Kit - Capability Wrappers\n")
buf.WriteString("//!\n")
buf.WriteString("//! This crate provides type definitions, traits, and registration macros\n")
buf.WriteString("//! for implementing Navidrome plugin capabilities in Rust.\n\n")
// Module declarations
for _, cap := range capabilities {
moduleName := ToSnakeCase(cap.Name)
buf.WriteString(fmt.Sprintf("pub mod %s;\n", moduleName))
}
return buf.Bytes(), nil
}
// pdkFuncMap returns the template functions for PDK code generation.
func pdkFuncMap() template.FuncMap {
return template.FuncMap{
"firstSentence": firstSentence,
"paramList": pdkParamList,
"returnList": pdkReturnList,
"argList": pdkArgList,
"argListWithReceiver": pdkArgListWithReceiver,
"mockReturns": pdkMockReturns,
"constValue": pdkConstValue,
"stubTypeUnderlying": stubTypeUnderlying,
"methodReceiver": pdkMethodReceiver,
}
}
// stubTypeUnderlying returns the appropriate stub type for non-WASM builds.
// For types that reference internal packages (like memory.Memory), returns "struct{}".
func stubTypeUnderlying(t PDKType) string {
underlying := t.Underlying
// If the underlying type references a package (contains a dot), use a stub struct
if strings.Contains(underlying, ".") {
return "struct{}"
}
// For simple types like int, int32, return as-is
return underlying
}
// firstSentence returns the first sentence of a doc string, normalized to a single line.
func firstSentence(doc string) string {
if doc == "" {
return ""
}
// Normalize whitespace (replace newlines with spaces, collapse multiple spaces)
doc = strings.Join(strings.Fields(doc), " ")
// Find first period followed by space or end
for i, r := range doc {
if r == '.' && (i+1 >= len(doc) || doc[i+1] == ' ') {
return doc[:i+1]
}
}
return doc
}
// pdkParamList generates a parameter list string for function signature.
func pdkParamList(params []PDKParam) string {
var parts []string
for _, p := range params {
if p.Name != "" {
parts = append(parts, p.Name+" "+p.Type)
} else {
parts = append(parts, p.Type)
}
}
return strings.Join(parts, ", ")
}
// pdkReturnList generates a return list string for function signature.
func pdkReturnList(returns []PDKReturn) string {
if len(returns) == 0 {
return ""
}
if len(returns) == 1 && returns[0].Name == "" {
return " " + returns[0].Type
}
var parts []string
for _, r := range returns {
if r.Name != "" {
parts = append(parts, r.Name+" "+r.Type)
} else {
parts = append(parts, r.Type)
}
}
return " (" + strings.Join(parts, ", ") + ")"
}
// pdkArgList generates an argument list string for function call.
func pdkArgList(params []PDKParam) string {
var parts []string
for _, p := range params {
if p.Name != "" {
parts = append(parts, p.Name)
} else {
parts = append(parts, "_")
}
}
return strings.Join(parts, ", ")
}
// pdkArgListWithReceiver generates an argument list that includes the receiver variable
// as the first argument to PDKMock.Called(). This allows tests to verify which instance
// a method was called on.
func pdkArgListWithReceiver(params []PDKParam, typeName string) string {
// Use lowercase first letter of type name as receiver variable
receiverVar := strings.ToLower(typeName[:1])
parts := []string{receiverVar}
for _, p := range params {
if p.Name != "" {
parts = append(parts, p.Name)
} else {
parts = append(parts, "_")
}
}
return strings.Join(parts, ", ")
}
// pdkMethodReceiver generates the receiver declaration for a method.
// Example: "r *HTTPRequest" or "m Memory"
func pdkMethodReceiver(receiver, typeName string) string {
receiverVar := strings.ToLower(typeName[:1])
if strings.HasPrefix(receiver, "*") {
return receiverVar + " *" + typeName
}
return receiverVar + " " + typeName
}
// pdkMockReturns generates the mock return accessors for a function.
func pdkMockReturns(returns []PDKReturn) string {
var parts []string
for i, r := range returns {
parts = append(parts, mockAccessorForType(r.Type, i))
}
return strings.Join(parts, ", ")
}
// mockAccessorForType returns the testify mock accessor for a type.
func mockAccessorForType(typ string, idx int) string {
switch typ {
case "string":
return fmt.Sprintf("args.String(%d)", idx)
case "bool":
return fmt.Sprintf("args.Bool(%d)", idx)
case "int":
return fmt.Sprintf("args.Int(%d)", idx)
case "error":
return fmt.Sprintf("args.Error(%d)", idx)
case "[]byte":
return fmt.Sprintf("args.Get(%d).([]byte)", idx)
case "uint64":
return fmt.Sprintf("args.Get(%d).(uint64)", idx)
case "uint32":
return fmt.Sprintf("args.Get(%d).(uint32)", idx)
case "uint16":
return fmt.Sprintf("args.Get(%d).(uint16)", idx)
default:
return fmt.Sprintf("args.Get(%d).(%s)", idx, typ)
}
}
// pdkConstValue returns the value expression for a constant.
func pdkConstValue(c PDKConst) string {
if c.Value == "" || c.Value == "iota" {
return "iota"
}
return c.Value
}
// GeneratePDKGo generates the WASM implementation of the PDK wrapper package.
func GeneratePDKGo(symbols *PDKSymbols) ([]byte, error) {
tmplContent, err := templatesFS.ReadFile("templates/pdk.go.tmpl")
if err != nil {
return nil, fmt.Errorf("reading pdk template: %w", err)
}
tmpl, err := template.New("pdk").Funcs(pdkFuncMap()).Parse(string(tmplContent))
if err != nil {
return nil, fmt.Errorf("parsing template: %w", err)
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, symbols); err != nil {
return nil, fmt.Errorf("executing template: %w", err)
}
return buf.Bytes(), nil
}
// GeneratePDKGoStub generates the native stub implementation of the PDK wrapper package.
func GeneratePDKGoStub(symbols *PDKSymbols) ([]byte, error) {
tmplContent, err := templatesFS.ReadFile("templates/pdk_stub.go.tmpl")
if err != nil {
return nil, fmt.Errorf("reading pdk stub template: %w", err)
}
tmpl, err := template.New("pdk_stub").Funcs(pdkFuncMap()).Parse(string(tmplContent))
if err != nil {
return nil, fmt.Errorf("parsing template: %w", err)
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, symbols); err != nil {
return nil, fmt.Errorf("executing template: %w", err)
}
return buf.Bytes(), nil
}
// GeneratePDKTypesStub generates the native type definitions for the PDK wrapper package.
func GeneratePDKTypesStub(symbols *PDKSymbols) ([]byte, error) {
tmplContent, err := templatesFS.ReadFile("templates/types_stub.go.tmpl")
if err != nil {
return nil, fmt.Errorf("reading types stub template: %w", err)
}
tmpl, err := template.New("types_stub").Funcs(pdkFuncMap()).Parse(string(tmplContent))
if err != nil {
return nil, fmt.Errorf("parsing template: %w", err)
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, symbols); err != nil {
return nil, fmt.Errorf("executing template: %w", err)
}
return buf.Bytes(), nil
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,13 @@
package internal
import (
"testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestInternal(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "NDPGen Internal Suite")
}
+846
View File
@@ -0,0 +1,846 @@
package internal
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"maps"
"os"
"path/filepath"
"regexp"
"slices"
"strings"
)
// Annotation patterns
var (
// //nd:hostservice name=ServiceName permission=key
hostServicePattern = regexp.MustCompile(`//nd:hostservice\s+(.*)`)
// //nd:hostfunc [name=CustomName]
hostFuncPattern = regexp.MustCompile(`//nd:hostfunc(?:\s+(.*))?`)
// //nd:capability name=PackageName [required=true]
capabilityPattern = regexp.MustCompile(`//nd:capability\s+(.*)`)
// //nd:export name=ExportName
exportPattern = regexp.MustCompile(`//nd:export\s+(.*)`)
// key=value pairs
keyValuePattern = regexp.MustCompile(`(\w+)=(\S+)`)
)
// ParseDirectory parses all Go source files in a directory and extracts host services.
func ParseDirectory(dir string) ([]Service, error) {
entries, err := os.ReadDir(dir)
if err != nil {
return nil, fmt.Errorf("reading directory: %w", err)
}
var services []Service
fset := token.NewFileSet()
for _, entry := range entries {
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".go") {
continue
}
// Skip generated files and test files
if strings.HasSuffix(entry.Name(), "_gen.go") || strings.HasSuffix(entry.Name(), "_test.go") {
continue
}
path := filepath.Join(dir, entry.Name())
parsed, err := parseFile(fset, path)
if err != nil {
return nil, fmt.Errorf("parsing %s: %w", entry.Name(), err)
}
services = append(services, parsed...)
}
return services, nil
}
// ParseCapabilities parses all Go source files in a directory and extracts capabilities.
func ParseCapabilities(dir string) ([]Capability, error) {
entries, err := os.ReadDir(dir)
if err != nil {
return nil, fmt.Errorf("reading directory: %w", err)
}
fset := token.NewFileSet()
// First pass: collect all structs and type aliases from all files in the package
sharedStructMap := make(map[string]StructDef)
sharedAliasMap := make(map[string]TypeAlias)
var allConstGroups []ConstGroup
var goFiles []string
for _, entry := range entries {
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".go") {
continue
}
// Skip generated files, test files, and doc.go
if strings.HasSuffix(entry.Name(), "_gen.go") ||
strings.HasSuffix(entry.Name(), "_test.go") ||
entry.Name() == "doc.go" {
continue
}
goFiles = append(goFiles, filepath.Join(dir, entry.Name()))
}
for _, path := range goFiles {
f, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
if err != nil {
return nil, fmt.Errorf("parsing %s for types: %w", filepath.Base(path), err)
}
for _, s := range parseStructs(f) {
sharedStructMap[s.Name] = s
}
for _, a := range parseTypeAliases(f) {
sharedAliasMap[a.Name] = a
}
allConstGroups = append(allConstGroups, parseConstGroups(f)...)
}
// Second pass: parse capabilities using the shared type maps
var capabilities []Capability
for _, path := range goFiles {
parsed, err := parseCapabilityFile(fset, path, sharedStructMap, sharedAliasMap, allConstGroups)
if err != nil {
return nil, fmt.Errorf("parsing %s: %w", filepath.Base(path), err)
}
capabilities = append(capabilities, parsed...)
}
return capabilities, nil
}
// parseCapabilityFile parses a single Go source file and extracts capabilities.
func parseCapabilityFile(fset *token.FileSet, path string, structMap map[string]StructDef, aliasMap map[string]TypeAlias, allConstGroups []ConstGroup) ([]Capability, error) {
f, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
if err != nil {
return nil, err
}
var capabilities []Capability
for _, decl := range f.Decls {
genDecl, ok := decl.(*ast.GenDecl)
if !ok || genDecl.Tok != token.TYPE {
continue
}
for _, spec := range genDecl.Specs {
typeSpec, ok := spec.(*ast.TypeSpec)
if !ok {
continue
}
interfaceType, ok := typeSpec.Type.(*ast.InterfaceType)
if !ok {
continue
}
// Check for //nd:capability annotation in doc comment
docText, rawDoc := getDocComment(genDecl, typeSpec)
capAnnotation := parseCapabilityAnnotation(rawDoc)
if capAnnotation == nil {
continue
}
// Extract source file base name (e.g., "websocket_callback" from "websocket_callback.go")
baseName := filepath.Base(path)
sourceFile := strings.TrimSuffix(baseName, ".go")
capability := Capability{
Name: capAnnotation["name"],
Interface: typeSpec.Name.Name,
Required: capAnnotation["required"] == "true",
Doc: cleanDoc(docText),
SourceFile: sourceFile,
}
// Parse methods and collect referenced types
referencedTypes := make(map[string]bool)
for _, method := range interfaceType.Methods.List {
if len(method.Names) == 0 {
continue // Embedded interface
}
funcType, ok := method.Type.(*ast.FuncType)
if !ok {
continue
}
// Check for //nd:export annotation
methodDocText, methodRawDoc := getMethodDocComment(method)
exportAnnotation := parseExportAnnotation(methodRawDoc)
if exportAnnotation == nil {
continue
}
export, err := parseExport(method.Names[0].Name, funcType, exportAnnotation, cleanDoc(methodDocText))
if err != nil {
return nil, fmt.Errorf("parsing export %s.%s: %w", typeSpec.Name.Name, method.Names[0].Name, err)
}
capability.Methods = append(capability.Methods, export)
// Collect referenced types from input and output
collectReferencedTypes(export.Input.Type, referencedTypes)
collectReferencedTypes(export.Output.Type, referencedTypes)
}
// Recursively collect all struct dependencies
collectAllStructDependencies(referencedTypes, structMap)
// Sort type names for stable output order
sortedTypeNames := slices.Sorted(maps.Keys(referencedTypes))
// Attach referenced structs to the capability
for _, typeName := range sortedTypeNames {
if s, exists := structMap[typeName]; exists {
capability.Structs = append(capability.Structs, s)
}
}
// Attach referenced type aliases
for _, typeName := range sortedTypeNames {
if a, exists := aliasMap[typeName]; exists {
capability.TypeAliases = append(capability.TypeAliases, a)
}
}
// Also attach type aliases prefixed with interface name (e.g., ScrobblerError for Scrobbler interface)
// This supports error types that are not directly referenced in method signatures
interfaceName := typeSpec.Name.Name
for _, typeName := range slices.Sorted(maps.Keys(aliasMap)) {
a := aliasMap[typeName]
if strings.HasPrefix(typeName, interfaceName) && !referencedTypes[typeName] {
capability.TypeAliases = append(capability.TypeAliases, a)
referencedTypes[typeName] = true // Mark as referenced for const lookup
}
}
// Attach const groups that match referenced type aliases
for _, group := range allConstGroups {
if group.Type == "" {
continue
}
if referencedTypes[group.Type] {
capability.Consts = append(capability.Consts, group)
}
}
if len(capability.Methods) > 0 {
capabilities = append(capabilities, capability)
}
}
}
return capabilities, nil
}
// collectAllStructDependencies recursively collects all struct types referenced by other structs.
func collectAllStructDependencies(referencedTypes map[string]bool, structMap map[string]StructDef) {
// Keep iterating until no new types are added
for {
newTypes := make(map[string]bool)
for typeName := range referencedTypes {
if s, exists := structMap[typeName]; exists {
for _, field := range s.Fields {
collectReferencedTypes(field.Type, newTypes)
}
}
}
// Check if any new types were found
foundNew := false
for t := range newTypes {
if !referencedTypes[t] {
referencedTypes[t] = true
foundNew = true
}
}
if !foundNew {
break
}
}
}
// parseExport parses an export method signature into an Export struct.
func parseExport(name string, funcType *ast.FuncType, annotation map[string]string, doc string) (Export, error) {
export := Export{
Name: name,
ExportName: annotation["name"],
Doc: doc,
}
// Capability exports have exactly one input parameter (the struct type)
if funcType.Params != nil && len(funcType.Params.List) == 1 {
field := funcType.Params.List[0]
typeName := typeToString(field.Type)
paramName := "input"
if len(field.Names) > 0 {
paramName = field.Names[0].Name
}
export.Input = NewParam(paramName, typeName)
}
// Capability exports return (OutputType, error)
if funcType.Results != nil {
for _, field := range funcType.Results.List {
typeName := typeToString(field.Type)
if typeName == "error" {
continue // Skip error return
}
paramName := "output"
if len(field.Names) > 0 {
paramName = field.Names[0].Name
}
export.Output = NewParam(paramName, typeName)
break // Only take the first non-error return
}
}
return export, nil
}
// parseFile parses a single Go source file and extracts host services.
func parseFile(fset *token.FileSet, path string) ([]Service, error) {
f, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
if err != nil {
return nil, err
}
// First pass: collect all struct definitions in the file
allStructs := parseStructs(f)
structMap := make(map[string]StructDef)
for _, s := range allStructs {
structMap[s.Name] = s
}
var services []Service
for _, decl := range f.Decls {
genDecl, ok := decl.(*ast.GenDecl)
if !ok || genDecl.Tok != token.TYPE {
continue
}
for _, spec := range genDecl.Specs {
typeSpec, ok := spec.(*ast.TypeSpec)
if !ok {
continue
}
interfaceType, ok := typeSpec.Type.(*ast.InterfaceType)
if !ok {
continue
}
// Check for //nd:hostservice annotation in doc comment
docText, rawDoc := getDocComment(genDecl, typeSpec)
svcAnnotation := parseHostServiceAnnotation(rawDoc)
if svcAnnotation == nil {
continue
}
service := Service{
Name: svcAnnotation["name"],
Permission: svcAnnotation["permission"],
Interface: typeSpec.Name.Name,
Doc: cleanDoc(docText),
}
// Parse methods and collect referenced types
referencedTypes := make(map[string]bool)
for _, method := range interfaceType.Methods.List {
if len(method.Names) == 0 {
continue // Embedded interface
}
funcType, ok := method.Type.(*ast.FuncType)
if !ok {
continue
}
// Check for //nd:hostfunc annotation
methodDocText, methodRawDoc := getMethodDocComment(method)
methodAnnotation := parseHostFuncAnnotation(methodRawDoc)
if methodAnnotation == nil {
continue
}
m, err := parseMethod(method.Names[0].Name, funcType, methodAnnotation, cleanDoc(methodDocText))
if err != nil {
return nil, fmt.Errorf("parsing method %s.%s: %w", typeSpec.Name.Name, method.Names[0].Name, err)
}
service.Methods = append(service.Methods, m)
// Collect referenced types from params and returns
for _, p := range m.Params {
collectReferencedTypes(p.Type, referencedTypes)
}
for _, r := range m.Returns {
collectReferencedTypes(r.Type, referencedTypes)
}
}
// Attach referenced structs to the service (sorted for stable output)
for _, typeName := range slices.Sorted(maps.Keys(referencedTypes)) {
if s, exists := structMap[typeName]; exists {
service.Structs = append(service.Structs, s)
}
}
if len(service.Methods) > 0 {
services = append(services, service)
}
}
}
return services, nil
}
// parseStructs extracts all struct type definitions from a parsed Go file.
func parseStructs(f *ast.File) []StructDef {
var structs []StructDef
for _, decl := range f.Decls {
genDecl, ok := decl.(*ast.GenDecl)
if !ok || genDecl.Tok != token.TYPE {
continue
}
for _, spec := range genDecl.Specs {
typeSpec, ok := spec.(*ast.TypeSpec)
if !ok {
continue
}
structType, ok := typeSpec.Type.(*ast.StructType)
if !ok {
continue
}
docText, _ := getDocComment(genDecl, typeSpec)
s := StructDef{
Name: typeSpec.Name.Name,
Doc: cleanDoc(docText),
}
// Parse struct fields
for _, field := range structType.Fields.List {
if len(field.Names) == 0 {
continue // Embedded field
}
fieldDef := parseStructField(field)
s.Fields = append(s.Fields, fieldDef...)
}
structs = append(structs, s)
}
}
return structs
}
// parseTypeAliases extracts all type alias definitions from a parsed Go file.
// Type aliases are non-struct type declarations like: type MyType string
func parseTypeAliases(f *ast.File) []TypeAlias {
var aliases []TypeAlias
for _, decl := range f.Decls {
genDecl, ok := decl.(*ast.GenDecl)
if !ok || genDecl.Tok != token.TYPE {
continue
}
for _, spec := range genDecl.Specs {
typeSpec, ok := spec.(*ast.TypeSpec)
if !ok {
continue
}
// Skip struct and interface types
if _, isStruct := typeSpec.Type.(*ast.StructType); isStruct {
continue
}
if _, isInterface := typeSpec.Type.(*ast.InterfaceType); isInterface {
continue
}
docText, _ := getDocComment(genDecl, typeSpec)
aliases = append(aliases, TypeAlias{
Name: typeSpec.Name.Name,
Type: typeToString(typeSpec.Type),
Doc: cleanDoc(docText),
})
}
}
return aliases
}
// parseConstGroups extracts const groups from a parsed Go file.
func parseConstGroups(f *ast.File) []ConstGroup {
var groups []ConstGroup
for _, decl := range f.Decls {
genDecl, ok := decl.(*ast.GenDecl)
if !ok || genDecl.Tok != token.CONST {
continue
}
group := ConstGroup{}
for _, spec := range genDecl.Specs {
valueSpec, ok := spec.(*ast.ValueSpec)
if !ok {
continue
}
// Get type if specified
if valueSpec.Type != nil && group.Type == "" {
group.Type = typeToString(valueSpec.Type)
}
// Extract values
for i, name := range valueSpec.Names {
def := ConstDef{
Name: name.Name,
}
// Get value if present
if i < len(valueSpec.Values) {
def.Value = exprToString(valueSpec.Values[i])
}
// Get doc comment
if valueSpec.Doc != nil {
def.Doc = cleanDoc(valueSpec.Doc.Text())
} else if valueSpec.Comment != nil {
def.Doc = cleanDoc(valueSpec.Comment.Text())
}
group.Values = append(group.Values, def)
}
}
if len(group.Values) > 0 {
groups = append(groups, group)
}
}
return groups
}
// exprToString converts an AST expression to a Go source string.
func exprToString(expr ast.Expr) string {
switch e := expr.(type) {
case *ast.BasicLit:
return e.Value
case *ast.Ident:
return e.Name
default:
return ""
}
}
// parseStructField parses a struct field and returns FieldDef for each name.
func parseStructField(field *ast.Field) []FieldDef {
var fields []FieldDef
typeName := typeToString(field.Type)
// Parse struct tag for JSON field name and omitempty
jsonTag := ""
omitEmpty := false
if field.Tag != nil {
tag := field.Tag.Value
// Remove backticks
tag = strings.Trim(tag, "`")
// Parse json tag
jsonTag, omitEmpty = parseJSONTag(tag)
}
// Get doc comment
var doc string
if field.Doc != nil {
doc = cleanDoc(field.Doc.Text())
}
for _, name := range field.Names {
fieldJSONTag := jsonTag
if fieldJSONTag == "" {
// Default to field name with camelCase
fieldJSONTag = toJSONName(name.Name)
}
fields = append(fields, FieldDef{
Name: name.Name,
Type: typeName,
JSONTag: fieldJSONTag,
OmitEmpty: omitEmpty,
Doc: doc,
})
}
return fields
}
// parseJSONTag extracts the json field name and omitempty flag from a struct tag.
func parseJSONTag(tag string) (name string, omitEmpty bool) {
// Find json:"..." in the tag
for _, part := range strings.Split(tag, " ") {
if strings.HasPrefix(part, `json:"`) {
value := strings.TrimPrefix(part, `json:"`)
value = strings.TrimSuffix(value, `"`)
parts := strings.Split(value, ",")
if len(parts) > 0 && parts[0] != "-" {
name = parts[0]
}
for _, opt := range parts[1:] {
if opt == "omitempty" {
omitEmpty = true
}
}
return
}
}
return "", false
}
// collectReferencedTypes extracts custom type names from a Go type string.
// It handles pointers, slices, and maps, collecting base type names.
func collectReferencedTypes(goType string, refs map[string]bool) {
// Strip pointer
if strings.HasPrefix(goType, "*") {
collectReferencedTypes(goType[1:], refs)
return
}
// Strip slice
if strings.HasPrefix(goType, "[]") {
if goType != "[]byte" {
collectReferencedTypes(goType[2:], refs)
}
return
}
// Handle map
if strings.HasPrefix(goType, "map[") {
rest := goType[4:] // Remove "map["
depth := 1
keyEnd := 0
for i, r := range rest {
if r == '[' {
depth++
} else if r == ']' {
depth--
if depth == 0 {
keyEnd = i
break
}
}
}
keyType := rest[:keyEnd]
valueType := rest[keyEnd+1:]
collectReferencedTypes(keyType, refs)
collectReferencedTypes(valueType, refs)
return
}
// Check if it's a custom type (starts with uppercase, not a builtin)
if len(goType) > 0 && goType[0] >= 'A' && goType[0] <= 'Z' {
switch goType {
case "String", "Bool", "Int", "Int32", "Int64", "Float32", "Float64":
// Not custom types (just capitalized for some reason)
default:
refs[goType] = true
}
}
}
// toJSONName is imported from types.go via the same package
// getDocComment extracts the doc comment for a type spec.
// Returns both the readable doc text and the raw comment text (which includes pragma-style comments).
func getDocComment(genDecl *ast.GenDecl, typeSpec *ast.TypeSpec) (docText, rawText string) {
var docGroup *ast.CommentGroup
// First check the TypeSpec's own doc (when multiple types in one block)
if typeSpec.Doc != nil {
docGroup = typeSpec.Doc
} else if genDecl.Doc != nil {
// Fall back to GenDecl doc (single type declaration)
docGroup = genDecl.Doc
}
if docGroup == nil {
return "", ""
}
return docGroup.Text(), commentGroupRaw(docGroup)
}
// commentGroupRaw returns all comment text including pragma-style comments (//nd:...).
// Go's ast.CommentGroup.Text() strips comments without a space after //, so we need this.
func commentGroupRaw(cg *ast.CommentGroup) string {
if cg == nil {
return ""
}
var lines []string
for _, c := range cg.List {
lines = append(lines, c.Text)
}
return strings.Join(lines, "\n")
}
// getMethodDocComment extracts the doc comment for a method.
func getMethodDocComment(field *ast.Field) (docText, rawText string) {
if field.Doc == nil {
return "", ""
}
return field.Doc.Text(), commentGroupRaw(field.Doc)
}
// parseHostServiceAnnotation extracts //nd:hostservice annotation parameters.
func parseHostServiceAnnotation(doc string) map[string]string {
for _, line := range strings.Split(doc, "\n") {
line = strings.TrimSpace(line)
match := hostServicePattern.FindStringSubmatch(line)
if match != nil {
return parseKeyValuePairs(match[1])
}
}
return nil
}
// parseHostFuncAnnotation extracts //nd:hostfunc annotation parameters.
func parseHostFuncAnnotation(doc string) map[string]string {
for _, line := range strings.Split(doc, "\n") {
line = strings.TrimSpace(line)
match := hostFuncPattern.FindStringSubmatch(line)
if match != nil {
params := parseKeyValuePairs(match[1])
if params == nil {
params = make(map[string]string)
}
return params
}
}
return nil
}
// parseCapabilityAnnotation extracts //nd:capability annotation parameters.
func parseCapabilityAnnotation(doc string) map[string]string {
for _, line := range strings.Split(doc, "\n") {
line = strings.TrimSpace(line)
match := capabilityPattern.FindStringSubmatch(line)
if match != nil {
return parseKeyValuePairs(match[1])
}
}
return nil
}
// parseExportAnnotation extracts //nd:export annotation parameters.
func parseExportAnnotation(doc string) map[string]string {
for _, line := range strings.Split(doc, "\n") {
line = strings.TrimSpace(line)
match := exportPattern.FindStringSubmatch(line)
if match != nil {
return parseKeyValuePairs(match[1])
}
}
return nil
}
// parseKeyValuePairs extracts key=value pairs from annotation text.
func parseKeyValuePairs(text string) map[string]string {
matches := keyValuePattern.FindAllStringSubmatch(text, -1)
if len(matches) == 0 {
return nil
}
result := make(map[string]string)
for _, m := range matches {
result[m[1]] = m[2]
}
return result
}
// parseMethod parses a method signature into a Method struct.
func parseMethod(name string, funcType *ast.FuncType, annotation map[string]string, doc string) (Method, error) {
m := Method{
Name: name,
ExportName: annotation["name"],
Doc: doc,
}
// Parse parameters (skip context.Context)
if funcType.Params != nil {
for _, field := range funcType.Params.List {
typeName := typeToString(field.Type)
if typeName == "context.Context" {
continue // Skip context parameter
}
for _, name := range field.Names {
m.Params = append(m.Params, NewParam(name.Name, typeName))
}
}
}
// Parse return values
if funcType.Results != nil {
for _, field := range funcType.Results.List {
typeName := typeToString(field.Type)
if typeName == "error" {
m.HasError = true
continue // Track error but don't include in Returns
}
// Handle anonymous returns
if len(field.Names) == 0 {
// Generate a name based on position
m.Returns = append(m.Returns, NewParam("result", typeName))
} else {
for _, name := range field.Names {
m.Returns = append(m.Returns, NewParam(name.Name, typeName))
}
}
}
}
return m, nil
}
// typeToString converts an AST type expression to a string.
func typeToString(expr ast.Expr) string {
switch t := expr.(type) {
case *ast.Ident:
return t.Name
case *ast.SelectorExpr:
return typeToString(t.X) + "." + t.Sel.Name
case *ast.StarExpr:
return "*" + typeToString(t.X)
case *ast.ArrayType:
if t.Len == nil {
return "[]" + typeToString(t.Elt)
}
return fmt.Sprintf("[%s]%s", typeToString(t.Len), typeToString(t.Elt))
case *ast.MapType:
return fmt.Sprintf("map[%s]%s", typeToString(t.Key), typeToString(t.Value))
case *ast.BasicLit:
return t.Value
case *ast.InterfaceType:
// Empty interface (interface{} or any)
if t.Methods == nil || len(t.Methods.List) == 0 {
return "any"
}
// Non-empty interfaces can't be easily represented
return "any"
default:
return fmt.Sprintf("%T", expr)
}
}
// cleanDoc removes annotation lines from documentation.
func cleanDoc(doc string) string {
var lines []string
for _, line := range strings.Split(doc, "\n") {
trimmed := strings.TrimSpace(line)
if strings.HasPrefix(trimmed, "//nd:") {
continue
}
lines = append(lines, line)
}
return strings.TrimSpace(strings.Join(lines, "\n"))
}
+547
View File
@@ -0,0 +1,547 @@
package internal
import (
"os"
"path/filepath"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Parser", func() {
var tmpDir string
BeforeEach(func() {
var err error
tmpDir, err = os.MkdirTemp("", "ndpgen-test-*")
Expect(err).NotTo(HaveOccurred())
})
AfterEach(func() {
os.RemoveAll(tmpDir)
})
Describe("ParseDirectory", func() {
It("should parse a simple host service interface", func() {
src := `package host
import "context"
// SubsonicAPIService provides access to Navidrome's Subsonic API.
//nd:hostservice name=SubsonicAPI permission=subsonicapi
type SubsonicAPIService interface {
// Call executes a Subsonic API request.
//nd:hostfunc
Call(ctx context.Context, uri string) (response string, err error)
}
`
err := os.WriteFile(filepath.Join(tmpDir, "service.go"), []byte(src), 0600)
Expect(err).NotTo(HaveOccurred())
services, err := ParseDirectory(tmpDir)
Expect(err).NotTo(HaveOccurred())
Expect(services).To(HaveLen(1))
svc := services[0]
Expect(svc.Name).To(Equal("SubsonicAPI"))
Expect(svc.Permission).To(Equal("subsonicapi"))
Expect(svc.Interface).To(Equal("SubsonicAPIService"))
Expect(svc.Methods).To(HaveLen(1))
m := svc.Methods[0]
Expect(m.Name).To(Equal("Call"))
Expect(m.HasError).To(BeTrue())
Expect(m.Params).To(HaveLen(1))
Expect(m.Params[0].Name).To(Equal("uri"))
Expect(m.Params[0].Type).To(Equal("string"))
Expect(m.Returns).To(HaveLen(1))
Expect(m.Returns[0].Name).To(Equal("response"))
Expect(m.Returns[0].Type).To(Equal("string"))
})
It("should parse multiple methods", func() {
src := `package host
import "context"
// SchedulerService provides scheduling capabilities.
//nd:hostservice name=Scheduler permission=scheduler
type SchedulerService interface {
//nd:hostfunc
ScheduleRecurring(ctx context.Context, cronExpression string) (scheduleID string, err error)
//nd:hostfunc
ScheduleOneTime(ctx context.Context, delaySeconds int32) (scheduleID string, err error)
//nd:hostfunc
CancelSchedule(ctx context.Context, scheduleID string) (canceled bool, err error)
}
`
err := os.WriteFile(filepath.Join(tmpDir, "scheduler.go"), []byte(src), 0600)
Expect(err).NotTo(HaveOccurred())
services, err := ParseDirectory(tmpDir)
Expect(err).NotTo(HaveOccurred())
Expect(services).To(HaveLen(1))
svc := services[0]
Expect(svc.Name).To(Equal("Scheduler"))
Expect(svc.Methods).To(HaveLen(3))
Expect(svc.Methods[0].Name).To(Equal("ScheduleRecurring"))
Expect(svc.Methods[0].Params[0].Type).To(Equal("string"))
Expect(svc.Methods[1].Name).To(Equal("ScheduleOneTime"))
Expect(svc.Methods[1].Params[0].Type).To(Equal("int32"))
Expect(svc.Methods[2].Name).To(Equal("CancelSchedule"))
Expect(svc.Methods[2].Returns[0].Type).To(Equal("bool"))
})
It("should skip methods without hostfunc annotation", func() {
src := `package host
import "context"
//nd:hostservice name=Test permission=test
type TestService interface {
//nd:hostfunc
Exported(ctx context.Context) error
// This method is not exported
NotExported(ctx context.Context) error
}
`
err := os.WriteFile(filepath.Join(tmpDir, "test.go"), []byte(src), 0600)
Expect(err).NotTo(HaveOccurred())
services, err := ParseDirectory(tmpDir)
Expect(err).NotTo(HaveOccurred())
Expect(services).To(HaveLen(1))
Expect(services[0].Methods).To(HaveLen(1))
Expect(services[0].Methods[0].Name).To(Equal("Exported"))
})
It("should handle custom export name", func() {
src := `package host
import "context"
//nd:hostservice name=Test permission=test
type TestService interface {
//nd:hostfunc name=custom_export_name
MyMethod(ctx context.Context) error
}
`
err := os.WriteFile(filepath.Join(tmpDir, "test.go"), []byte(src), 0600)
Expect(err).NotTo(HaveOccurred())
services, err := ParseDirectory(tmpDir)
Expect(err).NotTo(HaveOccurred())
Expect(services[0].Methods[0].ExportName).To(Equal("custom_export_name"))
Expect(services[0].Methods[0].FunctionName("test")).To(Equal("custom_export_name"))
})
It("should skip generated files", func() {
regularSrc := `package host
import "context"
//nd:hostservice name=Test permission=test
type TestService interface {
//nd:hostfunc
Method(ctx context.Context) error
}
`
genSrc := `// Code generated. DO NOT EDIT.
package host
//nd:hostservice name=Generated permission=gen
type GeneratedService interface {
//nd:hostfunc
Method() error
}
`
err := os.WriteFile(filepath.Join(tmpDir, "test.go"), []byte(regularSrc), 0600)
Expect(err).NotTo(HaveOccurred())
err = os.WriteFile(filepath.Join(tmpDir, "test_gen.go"), []byte(genSrc), 0600)
Expect(err).NotTo(HaveOccurred())
services, err := ParseDirectory(tmpDir)
Expect(err).NotTo(HaveOccurred())
Expect(services).To(HaveLen(1))
Expect(services[0].Name).To(Equal("Test"))
})
It("should skip interfaces without hostservice annotation", func() {
src := `package host
import "context"
// Regular interface without annotation
type RegularInterface interface {
Method(ctx context.Context) error
}
//nd:hostservice name=Annotated permission=annotated
type AnnotatedService interface {
//nd:hostfunc
Method(ctx context.Context) error
}
`
err := os.WriteFile(filepath.Join(tmpDir, "test.go"), []byte(src), 0600)
Expect(err).NotTo(HaveOccurred())
services, err := ParseDirectory(tmpDir)
Expect(err).NotTo(HaveOccurred())
Expect(services).To(HaveLen(1))
Expect(services[0].Name).To(Equal("Annotated"))
})
It("should return empty slice for directory with no host services", func() {
src := `package host
type RegularInterface interface {
Method() error
}
`
err := os.WriteFile(filepath.Join(tmpDir, "test.go"), []byte(src), 0600)
Expect(err).NotTo(HaveOccurred())
services, err := ParseDirectory(tmpDir)
Expect(err).NotTo(HaveOccurred())
Expect(services).To(BeEmpty())
})
})
Describe("parseKeyValuePairs", func() {
It("should parse key=value pairs", func() {
result := parseKeyValuePairs("name=Test permission=test")
Expect(result).To(HaveKeyWithValue("name", "Test"))
Expect(result).To(HaveKeyWithValue("permission", "test"))
})
It("should return nil for empty input", func() {
result := parseKeyValuePairs("")
Expect(result).To(BeNil())
})
})
Describe("typeToString", func() {
It("should handle basic types", func() {
src := `package test
type T interface {
Method(s string, i int, b bool) ([]byte, error)
}
`
err := os.WriteFile(filepath.Join(tmpDir, "types.go"), []byte(src), 0600)
Expect(err).NotTo(HaveOccurred())
// Parse and verify type conversion works
// This is implicitly tested through ParseDirectory
})
It("should convert interface{} to any", func() {
src := `package test
import "context"
//nd:hostservice name=Test permission=test
type TestService interface {
//nd:hostfunc
GetMetadata(ctx context.Context) (data map[string]interface{}, err error)
}
`
err := os.WriteFile(filepath.Join(tmpDir, "test.go"), []byte(src), 0600)
Expect(err).NotTo(HaveOccurred())
services, err := ParseDirectory(tmpDir)
Expect(err).NotTo(HaveOccurred())
Expect(services).To(HaveLen(1))
Expect(services[0].Methods[0].Returns[0].Type).To(Equal("map[string]any"))
})
})
Describe("Method helpers", func() {
It("should generate correct function names", func() {
m := Method{Name: "Call"}
Expect(m.FunctionName("subsonicapi")).To(Equal("subsonicapi_call"))
m.ExportName = "custom_name"
Expect(m.FunctionName("subsonicapi")).To(Equal("custom_name"))
})
It("should generate correct type names", func() {
m := Method{Name: "Call"}
// Host-side types are public
Expect(m.RequestTypeName("SubsonicAPI")).To(Equal("SubsonicAPICallRequest"))
Expect(m.ResponseTypeName("SubsonicAPI")).To(Equal("SubsonicAPICallResponse"))
// Client/PDK types are private
Expect(m.ClientRequestTypeName("SubsonicAPI")).To(Equal("subsonicAPICallRequest"))
Expect(m.ClientResponseTypeName("SubsonicAPI")).To(Equal("subsonicAPICallResponse"))
})
})
Describe("Service helpers", func() {
It("should generate correct output file name", func() {
s := Service{Name: "SubsonicAPI"}
Expect(s.OutputFileName()).To(Equal("subsonicapi_gen.go"))
})
It("should generate correct export prefix", func() {
s := Service{Name: "SubsonicAPI"}
Expect(s.ExportPrefix()).To(Equal("subsonicapi"))
})
})
Describe("ParseCapabilities", func() {
It("should parse a simple capability interface", func() {
src := `package capabilities
// MetadataAgent provides metadata retrieval.
//nd:capability name=metadata
type MetadataAgent interface {
// GetArtistBiography returns artist biography.
//nd:export name=nd_get_artist_biography
GetArtistBiography(ArtistInput) (ArtistBiographyOutput, error)
}
// ArtistInput is the input for artist-related functions.
type ArtistInput struct {
// ID is the artist ID.
ID string ` + "`json:\"id\"`" + `
// Name is the artist name.
Name string ` + "`json:\"name\"`" + `
}
// ArtistBiographyOutput is the output for GetArtistBiography.
type ArtistBiographyOutput struct {
// Biography is the biography text.
Biography string ` + "`json:\"biography\"`" + `
}
`
err := os.WriteFile(filepath.Join(tmpDir, "metadata.go"), []byte(src), 0600)
Expect(err).NotTo(HaveOccurred())
capabilities, err := ParseCapabilities(tmpDir)
Expect(err).NotTo(HaveOccurred())
Expect(capabilities).To(HaveLen(1))
cap := capabilities[0]
Expect(cap.Name).To(Equal("metadata"))
Expect(cap.Interface).To(Equal("MetadataAgent"))
Expect(cap.Required).To(BeFalse())
Expect(cap.Doc).To(ContainSubstring("MetadataAgent provides metadata retrieval"))
Expect(cap.Methods).To(HaveLen(1))
m := cap.Methods[0]
Expect(m.Name).To(Equal("GetArtistBiography"))
Expect(m.ExportName).To(Equal("nd_get_artist_biography"))
Expect(m.Input.Type).To(Equal("ArtistInput"))
Expect(m.Output.Type).To(Equal("ArtistBiographyOutput"))
// Check structs were collected
Expect(cap.Structs).To(HaveLen(2))
})
It("should parse a required capability", func() {
src := `package capabilities
// Scrobbler requires all methods to be implemented.
//nd:capability name=scrobbler required=true
type Scrobbler interface {
//nd:export name=nd_scrobbler_is_authorized
IsAuthorized(AuthInput) (AuthOutput, error)
//nd:export name=nd_scrobbler_scrobble
Scrobble(ScrobbleInput) (ScrobblerOutput, error)
}
type AuthInput struct {
UserID string ` + "`json:\"userId\"`" + `
}
type AuthOutput struct {
Authorized bool ` + "`json:\"authorized\"`" + `
}
type ScrobbleInput struct {
UserID string ` + "`json:\"userId\"`" + `
}
type ScrobblerOutput struct {
Error *string ` + "`json:\"error,omitempty\"`" + `
}
`
err := os.WriteFile(filepath.Join(tmpDir, "scrobbler.go"), []byte(src), 0600)
Expect(err).NotTo(HaveOccurred())
capabilities, err := ParseCapabilities(tmpDir)
Expect(err).NotTo(HaveOccurred())
Expect(capabilities).To(HaveLen(1))
cap := capabilities[0]
Expect(cap.Name).To(Equal("scrobbler"))
Expect(cap.Required).To(BeTrue())
Expect(cap.Methods).To(HaveLen(2))
})
It("should parse type aliases and consts", func() {
src := `package capabilities
//nd:capability name=scrobbler required=true
type Scrobbler interface {
//nd:export name=nd_scrobble
Scrobble(ScrobbleInput) (ScrobblerOutput, error)
}
type ScrobbleInput struct {
UserID string ` + "`json:\"userId\"`" + `
}
// ScrobblerErrorType indicates error handling behavior.
type ScrobblerErrorType string
const (
// ScrobblerErrorNone indicates no error.
ScrobblerErrorNone ScrobblerErrorType = "none"
// ScrobblerErrorRetry indicates retry later.
ScrobblerErrorRetry ScrobblerErrorType = "retry"
)
type ScrobblerOutput struct {
ErrorType *ScrobblerErrorType ` + "`json:\"errorType,omitempty\"`" + `
}
`
err := os.WriteFile(filepath.Join(tmpDir, "scrobbler.go"), []byte(src), 0600)
Expect(err).NotTo(HaveOccurred())
capabilities, err := ParseCapabilities(tmpDir)
Expect(err).NotTo(HaveOccurred())
Expect(capabilities).To(HaveLen(1))
cap := capabilities[0]
// Type alias should be collected
Expect(cap.TypeAliases).To(HaveLen(1))
Expect(cap.TypeAliases[0].Name).To(Equal("ScrobblerErrorType"))
Expect(cap.TypeAliases[0].Type).To(Equal("string"))
// Consts should be collected
Expect(cap.Consts).To(HaveLen(1))
Expect(cap.Consts[0].Type).To(Equal("ScrobblerErrorType"))
Expect(cap.Consts[0].Values).To(HaveLen(2))
Expect(cap.Consts[0].Values[0].Name).To(Equal("ScrobblerErrorNone"))
Expect(cap.Consts[0].Values[0].Value).To(Equal(`"none"`))
})
It("should collect nested struct dependencies", func() {
src := `package capabilities
//nd:capability name=metadata
type MetadataAgent interface {
//nd:export name=nd_get_images
GetImages(ArtistInput) (ImagesOutput, error)
}
type ArtistInput struct {
ID string ` + "`json:\"id\"`" + `
}
type ImagesOutput struct {
Images []ImageInfo ` + "`json:\"images\"`" + `
}
type ImageInfo struct {
URL string ` + "`json:\"url\"`" + `
Size int32 ` + "`json:\"size\"`" + `
}
`
err := os.WriteFile(filepath.Join(tmpDir, "metadata.go"), []byte(src), 0600)
Expect(err).NotTo(HaveOccurred())
capabilities, err := ParseCapabilities(tmpDir)
Expect(err).NotTo(HaveOccurred())
Expect(capabilities).To(HaveLen(1))
cap := capabilities[0]
// Should collect all 3 structs: ArtistInput, ImagesOutput, and ImageInfo
Expect(cap.Structs).To(HaveLen(3))
structNames := make([]string, len(cap.Structs))
for i, s := range cap.Structs {
structNames[i] = s.Name
}
Expect(structNames).To(ContainElements("ArtistInput", "ImagesOutput", "ImageInfo"))
})
It("should return empty slice for directory with no capabilities", func() {
src := `package capabilities
type RegularInterface interface {
Method() error
}
`
err := os.WriteFile(filepath.Join(tmpDir, "test.go"), []byte(src), 0600)
Expect(err).NotTo(HaveOccurred())
capabilities, err := ParseCapabilities(tmpDir)
Expect(err).NotTo(HaveOccurred())
Expect(capabilities).To(BeEmpty())
})
It("should ignore methods without export annotation", func() {
src := `package capabilities
//nd:capability name=test
type TestCapability interface {
//nd:export name=nd_exported
ExportedMethod(Input) (Output, error)
// This method has no export annotation
NotExportedMethod(Input) (Output, error)
}
type Input struct {
Value string ` + "`json:\"value\"`" + `
}
type Output struct {
Result string ` + "`json:\"result\"`" + `
}
`
err := os.WriteFile(filepath.Join(tmpDir, "test.go"), []byte(src), 0600)
Expect(err).NotTo(HaveOccurred())
capabilities, err := ParseCapabilities(tmpDir)
Expect(err).NotTo(HaveOccurred())
Expect(capabilities).To(HaveLen(1))
// Only the exported method should be captured
Expect(capabilities[0].Methods).To(HaveLen(1))
Expect(capabilities[0].Methods[0].Name).To(Equal("ExportedMethod"))
})
})
Describe("Export helpers", func() {
It("should generate correct provider interface name", func() {
e := Export{Name: "GetArtistBiography"}
Expect(e.ProviderInterfaceName()).To(Equal("ArtistBiographyProvider"))
e = Export{Name: "OnInit"}
Expect(e.ProviderInterfaceName()).To(Equal("InitProvider"))
})
It("should generate correct impl variable name", func() {
e := Export{Name: "GetArtistBiography"}
Expect(e.ImplVarName()).To(Equal("artistBiographyImpl"))
e = Export{Name: "OnInit"}
Expect(e.ImplVarName()).To(Equal("initImpl"))
})
It("should generate correct export function name", func() {
e := Export{Name: "GetArtistBiography", ExportName: "nd_get_artist_biography"}
Expect(e.ExportFuncName()).To(Equal("_NdGetArtistBiography"))
})
})
})
+441
View File
@@ -0,0 +1,441 @@
package internal
import (
"fmt"
"go/ast"
"go/token"
"sort"
"strings"
"golang.org/x/tools/go/packages"
)
// PDKSymbols contains all exported symbols parsed from extism/go-pdk.
type PDKSymbols struct {
Types []PDKType
Consts []PDKConst
Functions []PDKFunc
}
// PDKType represents an exported type from extism/go-pdk.
type PDKType struct {
Name string
Underlying string // The underlying type (e.g., "int" for LogLevel)
IsAlias bool // True if it's a type alias (type X = Y)
Doc string // Documentation comment
Methods []PDKFunc // Methods on this type
Fields []PDKField // Struct fields (if it's a struct type)
}
// PDKField represents a struct field.
type PDKField struct {
Name string
Type string
Tag string // Struct tag (e.g., `json:"name"`)
}
// PDKConst represents an exported constant from extism/go-pdk.
type PDKConst struct {
Name string
Type string // The type name (may be empty for untyped consts)
Value string // The value expression
Doc string
}
// PDKFunc represents an exported function from extism/go-pdk.
type PDKFunc struct {
Name string
Doc string
Receiver string // Empty for package-level functions
Params []PDKParam
Returns []PDKReturn
IsVariadic bool
}
// PDKParam represents a function parameter.
type PDKParam struct {
Name string
Type string
}
// PDKReturn represents a function return value.
type PDKReturn struct {
Name string // May be empty for unnamed returns
Type string
}
// ParseExtismPDK parses the extism/go-pdk package and extracts all exported symbols.
func ParseExtismPDK() (*PDKSymbols, error) {
// Load both packages with syntax trees in one call
cfg := &packages.Config{
Mode: packages.NeedName | packages.NeedSyntax | packages.NeedFiles,
}
pkgs, err := packages.Load(cfg,
"github.com/extism/go-pdk",
"github.com/extism/go-pdk/internal/memory",
)
if err != nil {
return nil, fmt.Errorf("loading extism/go-pdk: %w", err)
}
// Find both packages
var pdkPkg, memoryPkg *packages.Package
for _, pkg := range pkgs {
if len(pkg.Errors) > 0 {
return nil, fmt.Errorf("loading %s: %v", pkg.PkgPath, pkg.Errors[0])
}
switch pkg.Name {
case "pdk":
pdkPkg = pkg
case "memory":
memoryPkg = pkg
}
}
if pdkPkg == nil {
return nil, fmt.Errorf("package github.com/extism/go-pdk not found")
}
if memoryPkg == nil {
return nil, fmt.Errorf("package github.com/extism/go-pdk/internal/memory not found")
}
symbols := &PDKSymbols{}
seenTypes := make(map[string]bool)
// Extract Memory type from internal/memory package first
extractMemorySymbols(memoryPkg.Syntax, symbols, seenTypes)
// First pass: collect types from pdk package (skip if already found in internal packages)
for _, file := range pdkPkg.Syntax {
for _, decl := range file.Decls {
if genDecl, ok := decl.(*ast.GenDecl); ok {
for _, spec := range genDecl.Specs {
if typeSpec, ok := spec.(*ast.TypeSpec); ok {
if !typeSpec.Name.IsExported() {
continue
}
// Skip if we already have this type (from internal packages)
if seenTypes[typeSpec.Name.Name] {
continue
}
seenTypes[typeSpec.Name.Name] = true
pdkType := extractType(typeSpec, genDecl.Doc)
symbols.Types = append(symbols.Types, pdkType)
}
}
}
}
}
// Build typeMap from the final slice (after all types are added)
typeMap := make(map[string]*PDKType)
for i := range symbols.Types {
typeMap[symbols.Types[i].Name] = &symbols.Types[i]
}
// Second pass: collect functions and methods from pdk package
for _, file := range pdkPkg.Syntax {
for _, decl := range file.Decls {
switch d := decl.(type) {
case *ast.GenDecl:
if d.Tok == token.CONST {
consts := extractConsts(d)
symbols.Consts = append(symbols.Consts, consts...)
}
case *ast.FuncDecl:
if !d.Name.IsExported() {
continue
}
fn := extractFunc(d)
if fn.Receiver != "" {
// It's a method, associate with type
typeName := fn.Receiver
if strings.HasPrefix(typeName, "*") {
typeName = typeName[1:]
}
if t, ok := typeMap[typeName]; ok {
t.Methods = append(t.Methods, fn)
}
} else {
symbols.Functions = append(symbols.Functions, fn)
}
}
}
}
// Sort for consistent output
sort.Slice(symbols.Types, func(i, j int) bool {
return symbols.Types[i].Name < symbols.Types[j].Name
})
sort.Slice(symbols.Consts, func(i, j int) bool {
return symbols.Consts[i].Name < symbols.Consts[j].Name
})
sort.Slice(symbols.Functions, func(i, j int) bool {
return symbols.Functions[i].Name < symbols.Functions[j].Name
})
return symbols, nil
}
func extractType(spec *ast.TypeSpec, doc *ast.CommentGroup) PDKType {
t := PDKType{
Name: spec.Name.Name,
Doc: extractDoc(doc),
}
// Check if it's an alias (type X = Y)
t.IsAlias = spec.Assign.IsValid()
// Extract underlying type
t.Underlying = typeString(spec.Type)
// Extract struct fields if it's a struct type
if structType, ok := spec.Type.(*ast.StructType); ok {
t.Fields = extractStructFields(structType)
}
return t
}
func extractStructFields(st *ast.StructType) []PDKField {
var fields []PDKField
if st.Fields == nil {
return fields
}
for _, field := range st.Fields.List {
fieldType := typeString(field.Type)
tag := ""
if field.Tag != nil {
tag = field.Tag.Value
}
if len(field.Names) == 0 {
// Embedded field
fields = append(fields, PDKField{
Name: fieldType, // Use type name as field name for embedded
Type: fieldType,
Tag: tag,
})
} else {
for _, name := range field.Names {
// Skip unexported fields
if !name.IsExported() {
continue
}
fields = append(fields, PDKField{
Name: name.Name,
Type: fieldType,
Tag: tag,
})
}
}
}
return fields
}
func extractConsts(decl *ast.GenDecl) []PDKConst {
var consts []PDKConst
var currentType string // For iota-style const blocks
for i, spec := range decl.Specs {
valSpec, ok := spec.(*ast.ValueSpec)
if !ok {
continue
}
// Update type if specified
if valSpec.Type != nil {
currentType = typeString(valSpec.Type)
}
for j, name := range valSpec.Names {
if !name.IsExported() {
continue
}
c := PDKConst{
Name: name.Name,
Type: currentType,
}
// Extract value
if j < len(valSpec.Values) {
c.Value = exprString(valSpec.Values[j])
} else if i == 0 && j == 0 {
// First const with no value - likely iota
c.Value = "iota"
}
// Extract doc
if valSpec.Doc != nil {
c.Doc = extractDoc(valSpec.Doc)
} else if i == 0 && decl.Doc != nil {
c.Doc = extractDoc(decl.Doc)
}
consts = append(consts, c)
}
}
return consts
}
func extractFunc(decl *ast.FuncDecl) PDKFunc {
fn := PDKFunc{
Name: decl.Name.Name,
Doc: extractDoc(decl.Doc),
}
// Extract receiver
if decl.Recv != nil && len(decl.Recv.List) > 0 {
fn.Receiver = typeString(decl.Recv.List[0].Type)
}
// Extract parameters
if decl.Type.Params != nil {
for _, field := range decl.Type.Params.List {
paramType := typeString(field.Type)
// Check for variadic
if _, ok := field.Type.(*ast.Ellipsis); ok {
fn.IsVariadic = true
}
if len(field.Names) == 0 {
// Unnamed parameter
fn.Params = append(fn.Params, PDKParam{Type: paramType})
} else {
for _, name := range field.Names {
fn.Params = append(fn.Params, PDKParam{
Name: name.Name,
Type: paramType,
})
}
}
}
}
// Extract returns
if decl.Type.Results != nil {
for _, field := range decl.Type.Results.List {
retType := typeString(field.Type)
if len(field.Names) == 0 {
// Unnamed return
fn.Returns = append(fn.Returns, PDKReturn{Type: retType})
} else {
for _, name := range field.Names {
fn.Returns = append(fn.Returns, PDKReturn{
Name: name.Name,
Type: retType,
})
}
}
}
}
return fn
}
func extractDoc(doc *ast.CommentGroup) string {
if doc == nil {
return ""
}
return strings.TrimSpace(doc.Text())
}
func typeString(expr ast.Expr) string {
switch t := expr.(type) {
case *ast.Ident:
return t.Name
case *ast.StarExpr:
return "*" + typeString(t.X)
case *ast.SelectorExpr:
return typeString(t.X) + "." + t.Sel.Name
case *ast.ArrayType:
if t.Len == nil {
return "[]" + typeString(t.Elt)
}
return fmt.Sprintf("[%s]%s", exprString(t.Len), typeString(t.Elt))
case *ast.MapType:
return fmt.Sprintf("map[%s]%s", typeString(t.Key), typeString(t.Value))
case *ast.InterfaceType:
return "any" // Simplified
case *ast.Ellipsis:
return "..." + typeString(t.Elt)
case *ast.StructType:
return "struct{}" // Simplified for anonymous structs
case *ast.FuncType:
return "func()" // Simplified
default:
return fmt.Sprintf("%T", expr)
}
}
func exprString(expr ast.Expr) string {
switch e := expr.(type) {
case *ast.Ident:
return e.Name
case *ast.BasicLit:
return e.Value
case *ast.BinaryExpr:
return exprString(e.X) + " " + e.Op.String() + " " + exprString(e.Y)
case *ast.UnaryExpr:
return e.Op.String() + exprString(e.X)
case *ast.CallExpr:
return typeString(e.Fun) + "(...)"
default:
return fmt.Sprintf("%T", expr)
}
}
// extractMemorySymbols extracts the Memory type and its methods from already-parsed syntax trees.
// This is needed because Memory is defined in internal/memory but re-exported by the pdk package.
func extractMemorySymbols(files []*ast.File, symbols *PDKSymbols, seenTypes map[string]bool) {
// Collect the Memory type
for _, file := range files {
for _, decl := range file.Decls {
if genDecl, ok := decl.(*ast.GenDecl); ok {
for _, spec := range genDecl.Specs {
if typeSpec, ok := spec.(*ast.TypeSpec); ok {
// Only interested in Memory type
if typeSpec.Name.Name == "Memory" {
pdkType := extractType(typeSpec, genDecl.Doc)
symbols.Types = append(symbols.Types, pdkType)
seenTypes["Memory"] = true
}
}
}
}
}
}
// Build local type map for method association
localTypeMap := make(map[string]*PDKType)
for i := range symbols.Types {
localTypeMap[symbols.Types[i].Name] = &symbols.Types[i]
}
// Collect methods for Memory
for _, file := range files {
for _, decl := range file.Decls {
if funcDecl, ok := decl.(*ast.FuncDecl); ok {
if !funcDecl.Name.IsExported() {
continue
}
fn := extractFunc(funcDecl)
if fn.Receiver != "" {
typeName := fn.Receiver
if strings.HasPrefix(typeName, "*") {
typeName = typeName[1:]
}
if typeName == "Memory" {
if t, ok := localTypeMap["Memory"]; ok {
t.Methods = append(t.Methods, fn)
}
}
}
}
}
}
}
@@ -0,0 +1,223 @@
// Code generated by ndpgen. DO NOT EDIT.
//
// This file contains export wrappers for the {{.Capability.Interface}} capability.
// It is intended for use in Navidrome plugins built with TinyGo.
//
//go:build wasip1
package {{.Package}}
import (
"github.com/navidrome/navidrome/plugins/pdk/go/pdk"
)
{{- /* Generate type alias definitions */ -}}
{{- range .Capability.TypeAliases}}
{{- if .Doc}}
{{formatDoc .Doc}}
{{- end}}
type {{.Name}} {{.Type}}
{{- end}}
{{- /* Generate const definitions */ -}}
{{- range .Capability.Consts}}
{{- if .Values}}
const (
{{- $type := .Type}}
{{- range $i, $v := .Values}}
{{- if $v.Doc}}
{{formatDoc $v.Doc | indent 1}}
{{- end}}
{{- if $type}}
{{$v.Name}} {{$type}} = {{$v.Value}}
{{- else}}
{{$v.Name}} = {{$v.Value}}
{{- end}}
{{- end}}
)
{{- end}}
{{- end}}
{{- /* Generate Error() methods for string type aliases with const values (implements error interface) */ -}}
{{- $consts := .Capability.Consts}}
{{- range .Capability.TypeAliases}}
{{- if eq .Type "string"}}
{{- $typeName := .Name}}
{{- range $consts}}
{{- if eq .Type $typeName}}
// Error implements the error interface for {{$typeName}}.
func (e {{$typeName}}) Error() string { return string(e) }
{{- end}}
{{- end}}
{{- end}}
{{- end}}
{{- /* Generate struct definitions */ -}}
{{- range .Capability.Structs}}
{{- if .Doc}}
{{formatDoc .Doc}}
{{- else}}
// {{.Name}} represents the {{.Name}} data structure.
{{- end}}
type {{.Name}} struct {
{{- range .Fields}}
{{- if .Doc}}
{{formatDoc .Doc | indent 1}}
{{- end}}
{{.Name}} {{.Type}} `json:"{{.JSONTag}}{{if .OmitEmpty}},omitempty{{end}}"`
{{- end}}
}
{{- end}}
{{- /* Generate main interface based on required flag */ -}}
{{if .Capability.Required}}
// {{agentName .Capability}} requires all methods to be implemented.
{{- if .Capability.Doc}}
{{formatDoc .Capability.Doc}}
{{- end}}
type {{agentName .Capability}} interface {
{{- range .Capability.Methods}}
// {{.Name}}{{if .Doc}} - {{.Doc}}{{end}}
{{- if and .HasInput .HasOutput}}
{{.Name}}({{.Input.Type}}) ({{.Output.Type}}, error)
{{- else if .HasInput}}
{{.Name}}({{.Input.Type}}) error
{{- else if .HasOutput}}
{{.Name}}() ({{.Output.Type}}, error)
{{- else}}
{{.Name}}() error
{{- end}}
{{- end}}
}
{{- else}}
// {{agentName .Capability}} is the marker interface for {{.Package}} plugins.
// Implement one or more of the provider interfaces below.
{{- if .Capability.Doc}}
{{formatDoc .Capability.Doc}}
{{- end}}
type {{agentName .Capability}} interface{}
{{- end}}
{{- /* Generate optional provider interfaces for non-required capabilities */ -}}
{{- if not .Capability.Required}}
{{- range .Capability.Methods}}
// {{providerInterface .}} provides the {{.Name}} function.
type {{providerInterface .}} interface {
{{- if and .HasInput .HasOutput}}
{{.Name}}({{.Input.Type}}) ({{.Output.Type}}, error)
{{- else if .HasInput}}
{{.Name}}({{.Input.Type}}) error
{{- else if .HasOutput}}
{{.Name}}() ({{.Output.Type}}, error)
{{- else}}
{{.Name}}() error
{{- end}}
}
{{- end}}
{{- end}}
{{- /* Generate implementation function holders */ -}}
// Internal implementation holders
var (
{{- range .Capability.Methods}}
{{- if and .HasInput .HasOutput}}
{{implVar .}} func({{.Input.Type}}) ({{.Output.Type}}, error)
{{- else if .HasInput}}
{{implVar .}} func({{.Input.Type}}) error
{{- else if .HasOutput}}
{{implVar .}} func() ({{.Output.Type}}, error)
{{- else}}
{{implVar .}} func() error
{{- end}}
{{- end}}
)
// Register registers a {{.Package}} implementation.
{{- if .Capability.Required}}
// All methods are required.
func Register(impl {{agentName .Capability}}) {
{{- range .Capability.Methods}}
{{implVar .}} = impl.{{.Name}}
{{- end}}
}
{{- else}}
// The implementation is checked for optional provider interfaces.
func Register(impl {{agentName .Capability}}) {
{{- range .Capability.Methods}}
if p, ok := impl.({{providerInterface .}}); ok {
{{implVar .}} = p.{{.Name}}
}
{{- end}}
}
{{- end}}
// NotImplementedCode is the standard return code for unimplemented functions.
// The host recognizes this and skips the plugin gracefully.
const NotImplementedCode int32 = -2
{{- /* Generate export wrappers */ -}}
{{range .Capability.Methods}}
//go:wasmexport {{.ExportName}}
func {{exportFunc .}}() int32 {
if {{implVar .}} == nil {
// Return standard code - host will skip this plugin gracefully
return NotImplementedCode
}
{{- if .HasInput}}
var input {{.Input.Type}}
if err := pdk.InputJSON(&input); err != nil {
pdk.SetError(err)
return -1
}
{{- end}}
{{- if and .HasInput .HasOutput}}
output, err := {{implVar .}}(input)
if err != nil {
pdk.SetError(err)
return -1
}
if err := pdk.OutputJSON(output); err != nil {
pdk.SetError(err)
return -1
}
{{- else if .HasInput}}
if err := {{implVar .}}(input); err != nil {
pdk.SetError(err)
return -1
}
{{- else if .HasOutput}}
output, err := {{implVar .}}()
if err != nil {
pdk.SetError(err)
return -1
}
if err := pdk.OutputJSON(output); err != nil {
pdk.SetError(err)
return -1
}
{{- else}}
if err := {{implVar .}}(); err != nil {
pdk.SetError(err)
return -1
}
{{- end}}
return 0
}
{{- end}}
@@ -0,0 +1,184 @@
// Code generated by ndpgen. DO NOT EDIT.
//
// This file contains export wrappers for the {{.Capability.Interface}} capability.
// It is intended for use in Navidrome plugins built with extism-pdk.
{{if .Capability.Structs}}
use serde::{Deserialize, Serialize};
{{- if hasHashMap .Capability}}
use std::collections::HashMap;
{{- end}}
{{- end}}
{{- /* Generate type alias definitions */ -}}
{{- range .Capability.TypeAliases}}
{{- if .Doc}}
{{rustDocComment .Doc}}
{{- end}}
pub type {{.Name}} = {{rustTypeAlias .Type}};
{{- end}}
{{- /* Generate const definitions */ -}}
{{- range .Capability.Consts}}
{{- if .Values}}
{{- $type := .Type}}
{{- range $i, $v := .Values}}
{{- if $v.Doc}}
{{rustDocComment $v.Doc}}
{{- end}}
{{- /* Use the type alias name if a named type is provided, otherwise use &'static str */ -}}
{{- if $type}}
pub const {{rustConstName $v.Name}}: {{$type}} = {{$v.Value}};
{{- else}}
pub const {{rustConstName $v.Name}}: &'static str = {{$v.Value}};
{{- end}}
{{- end}}
{{- end}}
{{- end}}
{{- /* Generate struct definitions */ -}}
{{- range .Capability.Structs}}
{{- if .Doc}}
{{rustDocComment .Doc}}
{{- else}}
/// {{.Name}} represents the {{.Name}} data structure.
{{- end}}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct {{.Name}} {
{{- range .Fields}}
{{- if .Doc}}
{{rustDocComment .Doc | indent 4}}
{{- end}}
{{- if .OmitEmpty}}
#[serde(default, skip_serializing_if = "{{skipSerializingFunc .Type}}")]
{{- else}}
#[serde(default)]
{{- end}}
pub {{rustFieldName .Name}}: {{fieldRustType .}},
{{- end}}
}
{{- end}}
/// Error represents an error from a capability method.
#[derive(Debug)]
pub struct Error {
pub message: String,
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.message)
}
}
impl std::error::Error for Error {}
impl Error {
pub fn new(message: impl Into<String>) -> Self {
Self { message: message.into() }
}
}
{{- /* Generate main interface based on required flag */ -}}
{{if .Capability.Required}}
/// {{agentName .Capability}} requires all methods to be implemented.
{{- if .Capability.Doc}}
{{rustDocComment .Capability.Doc}}
{{- end}}
pub trait {{agentName .Capability}} {
{{- range .Capability.Methods}}
/// {{.Name}}{{if .Doc}} - {{.Doc}}{{end}}
{{- if and .HasInput .HasOutput}}
fn {{rustMethodName .Name}}(&self, req: {{rustOutputType .Input.Type}}) -> Result<{{rustOutputType .Output.Type}}, Error>;
{{- else if .HasInput}}
fn {{rustMethodName .Name}}(&self, req: {{rustOutputType .Input.Type}}) -> Result<(), Error>;
{{- else if .HasOutput}}
fn {{rustMethodName .Name}}(&self) -> Result<{{rustOutputType .Output.Type}}, Error>;
{{- else}}
fn {{rustMethodName .Name}}(&self) -> Result<(), Error>;
{{- end}}
{{- end}}
}
/// Register all exports for the {{agentName .Capability}} capability.
/// This macro generates the WASM export functions for all trait methods.
#[macro_export]
macro_rules! register_{{snakeCase .Package}} {
($plugin_type:ty) => {
{{- range .Capability.Methods}}
#[extism_pdk::plugin_fn]
pub fn {{.ExportName}}(
{{- if .HasInput}}
req: extism_pdk::Json<$crate::{{snakeCase $.Package}}::{{rustOutputType .Input.Type}}>
{{- end}}
) -> extism_pdk::FnResult<{{if .HasOutput}}extism_pdk::Json<{{if isPrimitiveRust .Output.Type}}{{rustOutputType .Output.Type}}{{else}}$crate::{{snakeCase $.Package}}::{{rustOutputType .Output.Type}}{{end}}>{{else}}(){{end}}> {
let plugin = <$plugin_type>::default();
{{- if and .HasInput .HasOutput}}
let result = $crate::{{snakeCase $.Package}}::{{agentName $.Capability}}::{{rustMethodName .Name}}(&plugin, req.into_inner())?;
Ok(extism_pdk::Json(result))
{{- else if .HasInput}}
$crate::{{snakeCase $.Package}}::{{agentName $.Capability}}::{{rustMethodName .Name}}(&plugin, req.into_inner())?;
Ok(())
{{- else if .HasOutput}}
let result = $crate::{{snakeCase $.Package}}::{{agentName $.Capability}}::{{rustMethodName .Name}}(&plugin)?;
Ok(extism_pdk::Json(result))
{{- else}}
$crate::{{snakeCase $.Package}}::{{agentName $.Capability}}::{{rustMethodName .Name}}(&plugin)?;
Ok(())
{{- end}}
}
{{- end}}
};
}
{{- else}}
{{- /* Generate optional provider interfaces for non-required capabilities */ -}}
{{- range .Capability.Methods}}
/// {{providerInterface .}} provides the {{.Name}} function.
pub trait {{providerInterface .}} {
{{- if and .HasInput .HasOutput}}
fn {{rustMethodName .Name}}(&self, req: {{rustOutputType .Input.Type}}) -> Result<{{rustOutputType .Output.Type}}, Error>;
{{- else if .HasInput}}
fn {{rustMethodName .Name}}(&self, req: {{rustOutputType .Input.Type}}) -> Result<(), Error>;
{{- else if .HasOutput}}
fn {{rustMethodName .Name}}(&self) -> Result<{{rustOutputType .Output.Type}}, Error>;
{{- else}}
fn {{rustMethodName .Name}}(&self) -> Result<(), Error>;
{{- end}}
}
/// Register the {{rustMethodName .Name}} export.
/// This macro generates the WASM export function for this method.
#[macro_export]
macro_rules! {{registerMacroName .Name}} {
($plugin_type:ty) => {
#[extism_pdk::plugin_fn]
pub fn {{.ExportName}}(
{{- if .HasInput}}
req: extism_pdk::Json<$crate::{{snakeCase $.Package}}::{{rustOutputType .Input.Type}}>
{{- end}}
) -> extism_pdk::FnResult<{{if .HasOutput}}extism_pdk::Json<{{if isPrimitiveRust .Output.Type}}{{rustOutputType .Output.Type}}{{else}}$crate::{{snakeCase $.Package}}::{{rustOutputType .Output.Type}}{{end}}>{{else}}(){{end}}> {
let plugin = <$plugin_type>::default();
{{- if and .HasInput .HasOutput}}
let result = $crate::{{snakeCase $.Package}}::{{providerInterface .}}::{{rustMethodName .Name}}(&plugin, req.into_inner())?;
Ok(extism_pdk::Json(result))
{{- else if .HasInput}}
$crate::{{snakeCase $.Package}}::{{providerInterface .}}::{{rustMethodName .Name}}(&plugin, req.into_inner())?;
Ok(())
{{- else if .HasOutput}}
let result = $crate::{{snakeCase $.Package}}::{{providerInterface .}}::{{rustMethodName .Name}}(&plugin)?;
Ok(extism_pdk::Json(result))
{{- else}}
$crate::{{snakeCase $.Package}}::{{providerInterface .}}::{{rustMethodName .Name}}(&plugin)?;
Ok(())
{{- end}}
}
};
}
{{- end}}
{{- end}}
@@ -0,0 +1,132 @@
// Code generated by ndpgen. DO NOT EDIT.
//
// This file provides stub implementations for non-WASM platforms.
// It allows Go plugins to compile and run tests outside of WASM,
// but the actual functionality is only available in WASM builds.
//
//go:build !wasip1
package {{.Package}}
{{- /* Generate type alias definitions */ -}}
{{- range .Capability.TypeAliases}}
{{- if .Doc}}
{{formatDoc .Doc}}
{{- end}}
type {{.Name}} {{.Type}}
{{- end}}
{{- /* Generate const definitions */ -}}
{{- range .Capability.Consts}}
{{- if .Values}}
const (
{{- $type := .Type}}
{{- range $i, $v := .Values}}
{{- if $v.Doc}}
{{formatDoc $v.Doc | indent 1}}
{{- end}}
{{- if $type}}
{{$v.Name}} {{$type}} = {{$v.Value}}
{{- else}}
{{$v.Name}} = {{$v.Value}}
{{- end}}
{{- end}}
)
{{- end}}
{{- end}}
{{- /* Generate Error() methods for string type aliases with const values (implements error interface) */ -}}
{{- $consts := .Capability.Consts}}
{{- range .Capability.TypeAliases}}
{{- if eq .Type "string"}}
{{- $typeName := .Name}}
{{- range $consts}}
{{- if eq .Type $typeName}}
// Error implements the error interface for {{$typeName}}.
func (e {{$typeName}}) Error() string { return string(e) }
{{- end}}
{{- end}}
{{- end}}
{{- end}}
{{- /* Generate struct definitions */ -}}
{{- range .Capability.Structs}}
{{- if .Doc}}
{{formatDoc .Doc}}
{{- else}}
// {{.Name}} represents the {{.Name}} data structure.
{{- end}}
type {{.Name}} struct {
{{- range .Fields}}
{{- if .Doc}}
{{formatDoc .Doc | indent 1}}
{{- end}}
{{.Name}} {{.Type}} `json:"{{.JSONTag}}{{if .OmitEmpty}},omitempty{{end}}"`
{{- end}}
}
{{- end}}
{{- /* Generate main interface based on required flag */ -}}
{{if .Capability.Required}}
// {{agentName .Capability}} requires all methods to be implemented.
{{- if .Capability.Doc}}
{{formatDoc .Capability.Doc}}
{{- end}}
type {{agentName .Capability}} interface {
{{- range .Capability.Methods}}
// {{.Name}}{{if .Doc}} - {{.Doc}}{{end}}
{{- if and .HasInput .HasOutput}}
{{.Name}}({{.Input.Type}}) ({{.Output.Type}}, error)
{{- else if .HasInput}}
{{.Name}}({{.Input.Type}}) error
{{- else if .HasOutput}}
{{.Name}}() ({{.Output.Type}}, error)
{{- else}}
{{.Name}}() error
{{- end}}
{{- end}}
}
{{- else}}
// {{agentName .Capability}} is the marker interface for {{.Package}} plugins.
// Implement one or more of the provider interfaces below.
{{- if .Capability.Doc}}
{{formatDoc .Capability.Doc}}
{{- end}}
type {{agentName .Capability}} interface{}
{{- end}}
{{- /* Generate optional provider interfaces for non-required capabilities */ -}}
{{- if not .Capability.Required}}
{{- range .Capability.Methods}}
// {{providerInterface .}} provides the {{.Name}} function.
type {{providerInterface .}} interface {
{{- if and .HasInput .HasOutput}}
{{.Name}}({{.Input.Type}}) ({{.Output.Type}}, error)
{{- else if .HasInput}}
{{.Name}}({{.Input.Type}}) error
{{- else if .HasOutput}}
{{.Name}}() ({{.Output.Type}}, error)
{{- else}}
{{.Name}}() error
{{- end}}
}
{{- end}}
{{- end}}
// NotImplementedCode is the standard return code for unimplemented functions.
const NotImplementedCode int32 = -2
// Register is a no-op on non-WASM platforms.
// This stub allows code to compile outside of WASM.
{{- if .Capability.Required}}
func Register(_ {{agentName .Capability}}) {}
{{- else}}
func Register(_ {{agentName .Capability}}) {}
{{- end}}
@@ -0,0 +1,129 @@
// Code generated by ndpgen. DO NOT EDIT.
//
// This file contains client wrappers for the {{.Service.Name}} host service.
// It is intended for use in Navidrome plugins built with TinyGo.
//
//go:build wasip1
package {{.Package}}
import (
"encoding/json"
{{- if .Service.HasErrors}}
"errors"
{{- end}}
"github.com/navidrome/navidrome/plugins/pdk/go/pdk"
)
{{- /* Generate struct definitions */ -}}
{{- range .Service.Structs}}
// {{.Name}} represents the {{.Name}} data structure.
{{- if .Doc}}
{{formatDoc .Doc}}
{{- end}}
type {{.Name}} struct {
{{- range .Fields}}
{{.Name}} {{.Type}} `json:"{{.JSONTag}}"`
{{- end}}
}
{{- end}}
{{- /* Generate wasmimport declarations for each method */ -}}
{{range .Service.Methods}}
// {{exportName .}} is the host function provided by Navidrome.
//
//go:wasmimport extism:host/user {{exportName .}}
func {{exportName .}}(uint64) uint64
{{- end}}
{{- /* Generate request/response types for all methods (private) */ -}}
{{range .Service.Methods}}
{{- if .HasParams}}
type {{requestType .}} struct {
{{- range .Params}}
{{title .Name}} {{.Type}} `json:"{{.JSONName}}"`
{{- end}}
}
{{- end}}
{{- if not .IsErrorOnly}}
type {{responseType .}} struct {
{{- range .Returns}}
{{title .Name}} {{.Type}} `json:"{{.JSONName}},omitempty"`
{{- end}}
{{- if .HasError}}
Error string `json:"error,omitempty"`
{{- end}}
}
{{- end}}
{{- end}}
{{- /* Generate wrapper functions */ -}}
{{range .Service.Methods}}
// {{$.Service.Name}}{{.Name}} calls the {{exportName .}} host function.
{{- if .Doc}}
{{formatDoc .Doc}}
{{- end}}
func {{$.Service.Name}}{{.Name}}({{range $i, $p := .Params}}{{if $i}}, {{end}}{{$p.Name}} {{$p.Type}}{{end}}) {{.ReturnSignature}} {
{{- if .HasParams}}
// Marshal request to JSON
req := {{requestType .}}{
{{- range .Params}}
{{title .Name}}: {{.Name}},
{{- end}}
}
reqBytes, err := json.Marshal(req)
if err != nil {
return {{if .HasReturns}}{{.ZeroValues}}{{end}}{{if and .HasReturns .HasError}}, {{end}}{{if .HasError}}err{{end}}
}
reqMem := pdk.AllocateBytes(reqBytes)
defer reqMem.Free()
{{- else}}
// No parameters - allocate empty JSON object
reqMem := pdk.AllocateBytes([]byte("{}"))
defer reqMem.Free()
{{- end}}
// Call the host function
responsePtr := {{exportName .}}(reqMem.Offset())
// 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 .}}
if err := json.Unmarshal(responseBytes, &response); err != nil {
return {{if .HasReturns}}{{.ZeroValues}}{{end}}{{if and .HasReturns .HasError}}, {{end}}{{if .HasError}}err{{end}}
}
{{- if .HasError}}
// Convert Error field to Go error
if response.Error != "" {
return {{if .HasReturns}}{{.ZeroValues}}, {{end}}errors.New(response.Error)
}
{{- end}}
return {{range $i, $r := .Returns}}{{if $i}}, {{end}}response.{{title $r.Name}}{{end}}{{if and .HasReturns .HasError}}, {{end}}{{if .HasError}}nil{{end}}
{{- end}}
}
{{- end}}
@@ -0,0 +1,96 @@
# Code generated by ndpgen. DO NOT EDIT.
#
# This file contains client wrappers for the {{.Service.Name}} host service.
# It is intended for use in Navidrome plugins built with extism-py.
#
# IMPORTANT: Due to a limitation in extism-py, you cannot import this file directly.
# The @extism.import_fn decorators are only detected when defined in the plugin's
# main __init__.py file. Copy the needed functions from this file into your plugin.
from dataclasses import dataclass
from typing import Any
import extism
import json
class HostFunctionError(Exception):
"""Raised when a host function returns an error."""
pass
{{- /* Generate raw host function imports */ -}}
{{range .Service.Methods}}
@extism.import_fn("extism:host/user", "{{exportName .}}")
def _{{exportName .}}(offset: int) -> int:
"""Raw host function - do not call directly."""
...
{{- end}}
{{- /* Generate dataclasses for multi-value returns */ -}}
{{range .Service.Methods}}
{{- if .NeedsResultClass}}
@dataclass
class {{pythonResultType .}}:
"""Result type for {{pythonFunc .}}."""
{{- range .Returns}}
{{.PythonName}}: {{.PythonType}}
{{- end}}
{{- end}}
{{- end}}
{{- /* Generate wrapper functions */ -}}
{{range .Service.Methods}}
def {{pythonFunc .}}({{range $i, $p := .Params}}{{if $i}}, {{end}}{{$p.PythonName}}: {{$p.PythonType}}{{end}}){{if .NeedsResultClass}} -> {{pythonResultType .}}{{else if .HasReturns}} -> {{(index .Returns 0).PythonType}}{{else}} -> None{{end}}:
"""{{if .Doc}}{{.Doc}}{{else}}Call the {{exportName .}} host function.{{end}}
{{- if .HasParams}}
Args:
{{- range .Params}}
{{.PythonName}}: {{.PythonType}} parameter.
{{- end}}
{{- end}}
{{- if .HasReturns}}
Returns:
{{- if .NeedsResultClass}}
{{pythonResultType .}} containing{{range .Returns}} {{.PythonName}},{{end}}.
{{- else}}
{{(index .Returns 0).PythonType}}: The result value.
{{- end}}
{{- end}}
Raises:
HostFunctionError: If the host function returns an error.
"""
{{- if .HasParams}}
request = {
{{- range .Params}}
"{{.JSONName}}": {{.PythonName}},
{{- end}}
}
request_bytes = json.dumps(request).encode("utf-8")
{{- else}}
request_bytes = b"{}"
{{- end}}
request_mem = extism.memory.alloc(request_bytes)
response_offset = _{{exportName .}}(request_mem.offset)
response_mem = extism.memory.find(response_offset)
response = json.loads(extism.memory.string(response_mem))
{{if .HasError}}
if response.get("error"):
raise HostFunctionError(response["error"])
{{end}}
{{- if .NeedsResultClass}}
return {{pythonResultType .}}(
{{- range .Returns}}
{{.PythonName}}=response.get("{{.JSONName}}"{{pythonDefault .}}),
{{- end}}
)
{{- else if .HasReturns}}
return response.get("{{(index .Returns 0).JSONName}}"{{pythonDefault (index .Returns 0)}})
{{- end}}
{{- end}}
@@ -0,0 +1,134 @@
// Code generated by ndpgen. DO NOT EDIT.
//
// This file contains client wrappers for the {{.Service.Name}} host service.
// It is intended for use in Navidrome plugins built with extism-pdk.
use extism_pdk::*;
use serde::{Deserialize, Serialize};
{{- /* Generate struct definitions */ -}}
{{- range .Service.Structs}}
{{if .Doc}}
{{rustDocComment .Doc}}
{{else}}
{{end}}#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct {{.Name}} {
{{- range .Fields}}
{{- if .NeedsDefault}}
#[serde(default)]
{{- end}}
pub {{.RustName}}: {{fieldRustType .}},
{{- end}}
}
{{- end}}
{{- /* Generate request/response types */ -}}
{{- range .Service.Methods}}
{{- if .HasParams}}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct {{requestType .}} {
{{- range .Params}}
{{.RustName}}: {{rustType .}},
{{- end}}
}
{{- end}}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct {{responseType .}} {
{{- range .Returns}}
#[serde(default)]
{{.RustName}}: {{rustType .}},
{{- end}}
{{- if .HasError}}
#[serde(default)]
error: Option<String>,
{{- end}}
}
{{- end}}
#[host_fn]
extern "ExtismHost" {
{{- range .Service.Methods}}
fn {{exportName .}}(input: Json<{{if .HasParams}}{{requestType .}}{{else}}serde_json::Value{{end}}>) -> Json<{{responseType .}}>;
{{- end}}
}
{{- /* Generate wrapper functions */ -}}
{{range .Service.Methods}}
{{if .Doc}}{{rustDocComment .Doc}}{{else}}/// Calls the {{exportName .}} host function.{{end}}
{{- if .HasParams}}
///
/// # Arguments
{{- range .Params}}
/// * `{{.RustName}}` - {{rustType .}} parameter.
{{- end}}
{{- end}}
{{- if .HasReturns}}
///
/// # Returns
{{- if .IsOptionPattern}}
/// `Some({{(index .Returns 0).RustName}})` if found, `None` otherwise.
{{- else if eq (len .Returns) 1}}
/// The {{(index .Returns 0).RustName}} value.
{{- else}}
/// A tuple of ({{range $i, $r := .Returns}}{{if $i}}, {{end}}{{$r.RustName}}{{end}}).
{{- end}}
{{- end}}
///
/// # Errors
/// Returns an error if the host function call fails.
{{- if .IsOptionPattern}}
pub fn {{rustFunc .}}({{range $i, $p := .Params}}{{if $i}}, {{end}}{{$p.RustName}}: {{rustParamType $p}}{{end}}) -> Result<Option<{{rustType (index .Returns 0)}}>, Error> {
let response = unsafe {
{{- if .HasParams}}
{{exportName .}}(Json({{requestType .}} {
{{- range .Params}}
{{.RustName}}: {{.RustName}}{{if .NeedsToOwned}}.to_owned(){{end}},
{{- end}}
}))?
{{- else}}
{{exportName .}}(Json(serde_json::json!({})))?
{{- end}}
};
{{if .HasError}}
if let Some(err) = response.0.error {
return Err(Error::msg(err));
}
{{end}}
if response.0.{{(index .Returns 1).RustName}} {
Ok(Some(response.0.{{(index .Returns 0).RustName}}))
} else {
Ok(None)
}
}
{{- else}}
pub fn {{rustFunc .}}({{range $i, $p := .Params}}{{if $i}}, {{end}}{{$p.RustName}}: {{rustParamType $p}}{{end}}) -> Result<{{if eq (len .Returns) 0}}(){{else if eq (len .Returns) 1}}{{rustType (index .Returns 0)}}{{else}}({{range $i, $r := .Returns}}{{if $i}}, {{end}}{{rustType $r}}{{end}}){{end}}, Error> {
let response = unsafe {
{{- if .HasParams}}
{{exportName .}}(Json({{requestType .}} {
{{- range .Params}}
{{.RustName}}: {{.RustName}}{{if .NeedsToOwned}}.to_owned(){{end}},
{{- end}}
}))?
{{- else}}
{{exportName .}}(Json(serde_json::json!({})))?
{{- end}}
};
{{if .HasError}}
if let Some(err) = response.0.error {
return Err(Error::msg(err));
}
{{end}}
{{- if eq (len .Returns) 0}}
Ok(())
{{- else if eq (len .Returns) 1}}
Ok(response.0.{{(index .Returns 0).RustName}})
{{- else}}
Ok(({{range $i, $r := .Returns}}{{if $i}}, {{end}}response.0.{{$r.RustName}}{{end}}))
{{- end}}
}
{{- end}}
{{- end}}
@@ -0,0 +1,50 @@
// Code generated by ndpgen. DO NOT EDIT.
//
// This file contains mock implementations for non-WASM builds.
// These mocks allow IDE support, compilation, and unit testing on non-WASM platforms.
// Plugin authors can use the exported mock instances to set expectations in tests.
//
//go:build !wasip1
package {{.Package}}
import "github.com/stretchr/testify/mock"
{{- /* Generate struct definitions (same as main file, needed for type references in function signatures) */ -}}
{{- range .Service.Structs}}
// {{.Name}} represents the {{.Name}} data structure.
{{- if .Doc}}
{{formatDoc .Doc}}
{{- end}}
type {{.Name}} struct {
{{- range .Fields}}
{{.Name}} {{.Type}} `json:"{{.JSONTag}}"`
{{- end}}
}
{{- end}}
// mock{{.Service.Name}}Service is the mock implementation for testing.
type mock{{.Service.Name}}Service struct {
mock.Mock
}
// {{.Service.Name}}Mock is the auto-instantiated mock instance for testing.
// Use this to set expectations: host.{{.Service.Name}}Mock.On("MethodName", args...).Return(values...)
var {{.Service.Name}}Mock = &mock{{.Service.Name}}Service{}
{{range .Service.Methods}}
// {{.Name}} is the mock method for {{$.Service.Name}}{{.Name}}.
func (m *mock{{$.Service.Name}}Service) {{.Name}}({{range $i, $p := .Params}}{{if $i}}, {{end}}{{$p.Name}} {{$p.Type}}{{end}}) {{.ReturnSignature}} {
args := m.Called({{range $i, $p := .Params}}{{if $i}}, {{end}}{{$p.Name}}{{end}})
return {{mockReturnValues .}}
}
// {{$.Service.Name}}{{.Name}} delegates to the mock instance.
{{- if .Doc}}
{{formatDoc .Doc}}
{{- end}}
func {{$.Service.Name}}{{.Name}}({{range $i, $p := .Params}}{{if $i}}, {{end}}{{$p.Name}} {{$p.Type}}{{end}}) {{.ReturnSignature}} {
return {{$.Service.Name}}Mock.{{.Name}}({{range $i, $p := .Params}}{{if $i}}, {{end}}{{$p.Name}}{{end}})
}
{{- end}}
@@ -0,0 +1,49 @@
// Code generated by ndpgen. DO NOT EDIT.
/*
Package {{.Package}} provides Navidrome Plugin Development Kit wrappers for Go/TinyGo plugins.
This package is auto-generated by the ndpgen tool and should not be edited manually.
# Usage
Add this module as a dependency in your plugin's go.mod:
require github.com/navidrome/navidrome/plugins/pdk/go/host v0.0.0
Then import the package in your plugin code:
import {{.Package}} "github.com/navidrome/navidrome/plugins/pdk/go/host"
func myPluginFunction() error {
// Use the cache service
_, err := {{.Package}}.CacheSetString("my_key", "my_value", 3600)
if err != nil {
return err
}
// Schedule a recurring task
_, err = {{.Package}}.SchedulerScheduleRecurring("@every 5m", "payload", "task_id")
if err != nil {
return err
}
return nil
}
# Available Services
The following host services are available:
{{range .Services}}
- {{.Name}}: {{if .Doc}}{{.Doc | firstLine}}{{else}}{{.Name}} service{{end}}
{{- end}}
# Building Plugins
Go plugins must be compiled to WebAssembly using TinyGo:
tinygo build -o plugin.wasm -target=wasip1 -buildmode=c-shared .
See the examples directory for complete plugin implementations.
*/
package {{.Package}}
@@ -0,0 +1,8 @@
module github.com/navidrome/navidrome/plugins/pdk/go
go 1.25
require (
github.com/extism/go-pdk v1.1.3
github.com/stretchr/testify v1.11.1
)
@@ -0,0 +1,121 @@
// Code generated by ndpgen. DO NOT EDIT.
package {{.Package}}
import (
"context"
"encoding/json"
extism "github.com/extism/go-sdk"
)
{{- /* Generate request/response types for all methods */ -}}
{{range .Service.Methods}}
{{- if .HasParams}}
// {{requestType .}} is the request type for {{$.Service.Name}}.{{.Name}}.
type {{requestType .}} struct {
{{- range .Params}}
{{title .Name}} {{.Type}} `json:"{{.JSONName}}"`
{{- end}}
}
{{- end}}
// {{responseType .}} is the response type for {{$.Service.Name}}.{{.Name}}.
type {{responseType .}} struct {
{{- range .Returns}}
{{title .Name}} {{.Type}} `json:"{{.JSONName}},omitempty"`
{{- end}}
{{- if .HasError}}
Error string `json:"error,omitempty"`
{{- end}}
}
{{end}}
// Register{{.Service.Name}}HostFunctions registers {{.Service.Name}} service host functions.
// The returned host functions should be added to the plugin's configuration.
func Register{{.Service.Name}}HostFunctions(service {{.Service.Interface}}) []extism.HostFunction {
return []extism.HostFunction{
{{- range .Service.Methods}}
new{{$.Service.Name}}{{.Name}}HostFunction(service),
{{- end}}
}
}
{{range .Service.Methods}}
func new{{$.Service.Name}}{{.Name}}HostFunction(service {{$.Service.Interface}}) extism.HostFunction {
return extism.NewHostFunctionWithStack(
"{{exportName .}}",
func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
{{- if .HasParams}}
// Read JSON request from plugin memory
reqBytes, err := p.ReadBytes(stack[0])
if err != nil {
{{$.Service.Name | lower}}WriteError(p, stack, err)
return
}
var req {{requestType .}}
if err := json.Unmarshal(reqBytes, &req); err != nil {
{{$.Service.Name | lower}}WriteError(p, stack, err)
return
}
{{- end}}
// Call the service method
{{- if .HasReturns}}
{{- if .HasError}}
{{range $i, $r := .Returns}}{{if $i}}, {{end}}{{lower $r.Name}}{{end}}, svcErr := service.{{.Name}}(ctx{{range .Params}}, req.{{title .Name}}{{end}})
if svcErr != nil {
{{$.Service.Name | lower}}WriteError(p, stack, svcErr)
return
}
{{- else}}
{{range $i, $r := .Returns}}{{if $i}}, {{end}}{{lower $r.Name}}{{end}} := service.{{.Name}}(ctx{{range .Params}}, req.{{title .Name}}{{end}})
{{- end}}
{{- else if .HasError}}
if svcErr := service.{{.Name}}(ctx{{range .Params}}, req.{{title .Name}}{{end}}); svcErr != nil {
{{$.Service.Name | lower}}WriteError(p, stack, svcErr)
return
}
{{- else}}
service.{{.Name}}(ctx{{range .Params}}, req.{{title .Name}}{{end}})
{{- end}}
// Write JSON response to plugin memory
resp := {{responseType .}}{
{{- range .Returns}}
{{title .Name}}: {{lower .Name}},
{{- end}}
}
{{$.Service.Name | lower}}WriteResponse(p, stack, resp)
},
[]extism.ValueType{extism.ValueTypePTR},
[]extism.ValueType{extism.ValueTypePTR},
)
}
{{end}}
// {{.Service.Name | lower}}WriteResponse writes a JSON response to plugin memory.
func {{.Service.Name | lower}}WriteResponse(p *extism.CurrentPlugin, stack []uint64, resp any) {
respBytes, err := json.Marshal(resp)
if err != nil {
{{.Service.Name | lower}}WriteError(p, stack, err)
return
}
respPtr, err := p.WriteBytes(respBytes)
if err != nil {
stack[0] = 0
return
}
stack[0] = respPtr
}
// {{.Service.Name | lower}}WriteError writes an error response to plugin memory.
func {{.Service.Name | lower}}WriteError(p *extism.CurrentPlugin, stack []uint64, err error) {
errResp := struct {
Error string `json:"error"`
}{Error: err.Error()}
respBytes, _ := json.Marshal(errResp)
respPtr, _ := p.WriteBytes(respBytes)
stack[0] = respPtr
}
@@ -0,0 +1,47 @@
// Code generated by ndpgen. DO NOT EDIT.
//
//! Navidrome Host Function Wrappers for Rust Plugins
//!
//! This crate provides idiomatic Rust wrappers for all Navidrome host services.
//! It is auto-generated by the ndpgen tool and should not be edited manually.
//!
//! # Usage
//!
//! Add this crate as a dependency in your plugin's Cargo.toml:
//!
//! ```toml
//! [dependencies]
//! nd-host = { path = "../../host/rust" }
//! ```
//!
//! Then import the services you need:
//!
//! ```ignore
//! use nd_host::{cache, scheduler};
//!
//! fn my_plugin_function() -> Result<(), extism_pdk::Error> {
//! // Use the cache service
//! cache::set_string("my_key", "my_value", 3600)?;
//!
//! // Schedule a recurring task
//! scheduler::schedule_recurring("@every 5m", "payload", "task_id")?;
//!
//! Ok(())
//! }
//! ```
//!
//! # Available Services
//!
{{- range .Services}}
//! - [`{{.Name | lower}}`] - {{if .Doc}}{{.Doc | firstLine}}{{else}}{{.Name}} service{{end}}
{{- end}}
{{range .Services}}
#[doc(hidden)]
mod nd_host_{{.Name | lower}};
/// {{if .Doc}}{{.Doc | firstLine}}{{else}}{{.Name}} host service wrappers.{{end}}
pub mod {{.Name | lower}} {
pub use super::nd_host_{{.Name | lower}}::*;
}
{{end}}
// Re-export commonly used types from extism-pdk for convenience
pub use extism_pdk::Error;
@@ -0,0 +1,50 @@
// Code generated by ndpgen. DO NOT EDIT.
//
// This file contains wrapper functions for the extism/go-pdk package.
// For WASM builds, it provides type aliases and function wrappers that delegate
// to the real extism/go-pdk package with zero overhead.
//
//go:build wasip1
package pdk
import (
extism "github.com/extism/go-pdk"
)
// Type aliases - zero overhead, full compatibility
{{- range .Types}}
type {{.Name}} = extism.{{.Name}}
{{- end}}
// Constants
{{- $prevType := ""}}
{{- range .Consts}}
{{- if ne .Type $prevType}}
{{- if ne $prevType ""}}
)
{{- end}}
const (
{{- end}}
{{.Name}} = extism.{{.Name}}
{{- $prevType = .Type}}
{{- end}}
{{- if ne $prevType ""}}
)
{{- end}}
// Functions
{{- range .Functions}}
{{- if .Doc}}
// {{.Name}} {{firstSentence .Doc}}
{{- end}}
func {{.Name}}({{paramList .Params}}){{returnList .Returns}} {
{{- if .Returns}}
return extism.{{.Name}}({{argList .Params}})
{{- else}}
extism.{{.Name}}({{argList .Params}})
{{- end}}
}
{{- end}}
@@ -0,0 +1,42 @@
// Code generated by ndpgen. DO NOT EDIT.
//
// This file contains mock implementations for non-WASM builds.
// These mocks allow IDE support, compilation, and unit testing on non-WASM platforms.
// Plugin authors can use the exported PDKMock instance to set expectations in tests.
//
//go:build !wasip1
package pdk
import "github.com/stretchr/testify/mock"
// mockPDK is the mock implementation for testing PDK functions.
type mockPDK struct {
mock.Mock
}
// PDKMock is the auto-instantiated mock instance for testing.
// Use this to set expectations: pdk.PDKMock.On("GetConfig", "key").Return("value", true)
var PDKMock = &mockPDK{}
// ResetMock resets the mock to its initial state.
// Call this in test setup/teardown to ensure clean state between tests.
func ResetMock() {
PDKMock = &mockPDK{}
}
// Functions
{{- range .Functions}}
{{- if .Doc}}
// {{.Name}} {{firstSentence .Doc}}
{{- end}}
func {{.Name}}({{paramList .Params}}){{returnList .Returns}} {
{{- if .Returns}}
args := PDKMock.Called({{argList .Params}})
return {{mockReturns .Returns}}
{{- else}}
PDKMock.Called({{argList .Params}})
{{- end}}
}
{{- end}}
@@ -0,0 +1,192 @@
// Code generated by ndpgen. DO NOT EDIT.
//
// This file contains type definitions for non-WASM builds.
// These types match the extism/go-pdk signatures to allow compilation and testing
// on native platforms without importing the WASM-only extism package.
//
//go:build !wasip1
package pdk
// LogLevel represents a logging level.
type LogLevel int
// Log level constants
const (
LogTrace LogLevel = iota
LogDebug
LogInfo
LogWarn
LogError
)
// HTTPMethod represents an HTTP method.
type HTTPMethod int32
// HTTP method constants
const (
MethodGet HTTPMethod = iota
MethodHead
MethodPost
MethodPut
MethodPatch
MethodDelete
MethodConnect
MethodOptions
MethodTrace
)
// String returns the string representation of the HTTP method.
func (m HTTPMethod) String() string {
switch m {
case MethodGet:
return "GET"
case MethodHead:
return "HEAD"
case MethodPost:
return "POST"
case MethodPut:
return "PUT"
case MethodPatch:
return "PATCH"
case MethodDelete:
return "DELETE"
case MethodConnect:
return "CONNECT"
case MethodOptions:
return "OPTIONS"
case MethodTrace:
return "TRACE"
default:
return "UNKNOWN"
}
}
// Memory represents memory allocated by (and shared with) the host.
// This is a stub implementation for non-WASM platforms.
type Memory struct {
offset uint64
length uint64
data []byte
}
// Offset returns the offset of the memory block.
func (m Memory) Offset() uint64 {
return m.offset
}
// Length returns the length of the memory block.
func (m Memory) Length() uint64 {
return m.length
}
// ReadBytes reads all bytes from the memory block.
func (m Memory) ReadBytes() []byte {
return m.data
}
// Load reads the memory block into the provided buffer.
func (m *Memory) Load(buffer []byte) {
copy(buffer, m.data)
}
// Store writes data to the memory block.
func (m *Memory) Store(data []byte) {
m.data = make([]byte, len(data))
copy(m.data, data)
m.length = uint64(len(data))
}
// Free frees the memory block.
func (m *Memory) Free() {
m.data = nil
m.length = 0
}
// NewStubMemory creates a new stub Memory for testing.
// This is a helper function not present in the real PDK.
func NewStubMemory(offset, length uint64, data []byte) Memory {
return Memory{
offset: offset,
length: length,
data: data,
}
}
// HTTPRequest represents an HTTP request sent by the host.
// This is a stub implementation for non-WASM platforms.
type HTTPRequest struct {
method HTTPMethod
url string
headers map[string]string
body []byte
}
// SetHeader sets an HTTP header key to value.
func (r *HTTPRequest) SetHeader(key string, value string) *HTTPRequest {
if r.headers == nil {
r.headers = make(map[string]string)
}
r.headers[key] = value
return r
}
// SetBody sets the HTTP request body.
func (r *HTTPRequest) SetBody(body []byte) *HTTPRequest {
r.body = body
return r
}
// Send sends the HTTP request and returns the response.
// In the stub implementation, this delegates to the mock.
func (r *HTTPRequest) Send() HTTPResponse {
args := PDKMock.Called(r)
return args.Get(0).(HTTPResponse)
}
// HTTPRequestMeta represents the metadata associated with an HTTP request.
type HTTPRequestMeta struct {
URL string `json:"url"`
Method string `json:"method"`
Headers map[string]string `json:"headers"`
}
// HTTPResponse represents an HTTP response returned from the host.
// This is a stub implementation for non-WASM platforms.
type HTTPResponse struct {
status uint16
headers map[string]string
body []byte
memory Memory
}
// Status returns the status code from the response.
func (r HTTPResponse) Status() uint16 {
return r.status
}
// Headers returns the HTTP response headers.
func (r *HTTPResponse) Headers() map[string]string {
return r.headers
}
// Body returns the body byte slice from the response.
func (r HTTPResponse) Body() []byte {
return r.body
}
// Memory returns the memory associated with the response.
func (r HTTPResponse) Memory() Memory {
return r.memory
}
// NewStubHTTPResponse creates a new stub HTTPResponse for testing.
// This is a helper function not present in the real PDK.
func NewStubHTTPResponse(status uint16, headers map[string]string, body []byte) HTTPResponse {
return HTTPResponse{
status: status,
headers: headers,
body: body,
memory: NewStubMemory(0, uint64(len(body)), body),
}
}
+621
View File
@@ -0,0 +1,621 @@
package internal
import (
"strings"
"unicode"
)
// Service represents a parsed host service interface.
type Service struct {
Name string // Service name from annotation (e.g., "SubsonicAPI")
Permission string // Manifest permission key (e.g., "subsonicapi")
Interface string // Go interface name (e.g., "SubsonicAPIService")
Methods []Method // Methods marked with //nd:hostfunc
Doc string // Documentation comment for the service
Structs []StructDef // Structs used by this service
}
// Capability represents a parsed capability interface for plugin exports.
type Capability struct {
Name string // Package name from annotation (e.g., "metadata")
Interface string // Go interface name (e.g., "MetadataAgent")
Required bool // If true, all methods must be implemented
Methods []Export // Methods marked with //nd:export
Doc string // Documentation comment for the capability
Structs []StructDef // Structs used by this capability
TypeAliases []TypeAlias // Type aliases used by this capability
Consts []ConstGroup // Const groups used by this capability
SourceFile string // Base name of source file without extension (e.g., "websocket_callback")
}
// TypeAlias represents a type alias definition (e.g., type ScrobblerErrorType string).
type TypeAlias struct {
Name string // Type name
Type string // Underlying type
Doc string // Documentation comment
}
// ConstGroup represents a group of const definitions.
type ConstGroup struct {
Type string // Type name for typed consts (empty for untyped)
Values []ConstDef // Const definitions
}
// ConstDef represents a single const definition.
type ConstDef struct {
Name string // Const name
Value string // Const value
Doc string // Documentation comment
}
// KnownStructs returns a map of struct names defined in this capability.
func (c Capability) KnownStructs() map[string]bool {
result := make(map[string]bool)
for _, st := range c.Structs {
result[st.Name] = true
}
return result
}
// Export represents an exported WASM function within a capability.
type Export struct {
Name string // Go method name (e.g., "GetArtistBiography")
ExportName string // WASM export name (e.g., "nd_get_artist_biography")
Input Param // Single input parameter (the struct type)
Output Param // Single output return value (the struct type)
Doc string // Documentation comment for the method
}
// ProviderInterfaceName returns the optional provider interface name.
// For a method "GetArtistBiography", returns "ArtistBiographyProvider".
func (e Export) ProviderInterfaceName() string {
// Remove "Get", "On", etc. prefixes and add "Provider" suffix
name := e.Name
for _, prefix := range []string{"Get", "On"} {
if strings.HasPrefix(name, prefix) {
name = name[len(prefix):]
break
}
}
return name + "Provider"
}
// ImplVarName returns the internal implementation variable name.
// For "GetArtistBiography", returns "artistBiographyImpl".
func (e Export) ImplVarName() string {
name := e.Name
for _, prefix := range []string{"Get", "On"} {
if strings.HasPrefix(name, prefix) {
name = name[len(prefix):]
break
}
}
// Convert to camelCase
if len(name) > 0 {
name = strings.ToLower(string(name[0])) + name[1:]
}
return name + "Impl"
}
// ExportFuncName returns the unexported WASM export function name.
// For "nd_get_artist_biography", returns "_ndGetArtistBiography".
func (e Export) ExportFuncName() string {
// Convert snake_case to PascalCase
parts := strings.Split(e.ExportName, "_")
var result strings.Builder
result.WriteString("_")
for _, part := range parts {
if len(part) > 0 {
result.WriteString(strings.ToUpper(string(part[0])))
result.WriteString(part[1:])
}
}
return result.String()
}
// HasInput returns true if the method has an input parameter.
func (e Export) HasInput() bool {
return e.Input.Type != ""
}
// HasOutput returns true if the method has a non-error return value.
func (e Export) HasOutput() bool {
return e.Output.Type != ""
}
// IsPointerOutput returns true if the output type is a pointer.
func (e Export) IsPointerOutput() bool {
return strings.HasPrefix(e.Output.Type, "*")
}
// StructDef represents a Go struct type definition.
type StructDef struct {
Name string // Go struct name (e.g., "Library")
Fields []FieldDef // Struct fields
Doc string // Documentation comment
}
// FieldDef represents a field within a struct.
type FieldDef struct {
Name string // Go field name (e.g., "TotalSongs")
Type string // Go type (e.g., "int32", "*string", "[]User")
JSONTag string // JSON tag value (e.g., "totalSongs,omitempty")
OmitEmpty bool // Whether the field has omitempty tag
Doc string // Field documentation
}
// OutputFileName returns the generated file name for this service.
func (s Service) OutputFileName() string {
return strings.ToLower(s.Name) + "_gen.go"
}
// ExportPrefix returns the prefix for exported host function names.
func (s Service) ExportPrefix() string {
return strings.ToLower(s.Name)
}
// KnownStructs returns a map of struct names defined in this service.
func (s Service) KnownStructs() map[string]bool {
result := make(map[string]bool)
for _, st := range s.Structs {
result[st.Name] = true
}
return result
}
// HasErrors returns true if any method in the service returns an error.
func (s Service) HasErrors() bool {
for _, m := range s.Methods {
if m.HasError {
return true
}
}
return false
}
// Method represents a host function method within a service.
type Method struct {
Name string // Go method name (e.g., "Call")
ExportName string // Optional override for export name
Params []Param // Method parameters (excluding context.Context)
Returns []Param // Return values (excluding error)
HasError bool // Whether the method returns an error
Doc string // Documentation comment for the method
}
// FunctionName returns the Extism host function export name.
func (m Method) FunctionName(servicePrefix string) string {
if m.ExportName != "" {
return m.ExportName
}
return servicePrefix + "_" + strings.ToLower(m.Name)
}
// RequestTypeName returns the generated request type name (public, for host-side code).
func (m Method) RequestTypeName(serviceName string) string {
return serviceName + m.Name + "Request"
}
// ResponseTypeName returns the generated response type name (public, for host-side code).
func (m Method) ResponseTypeName(serviceName string) string {
return serviceName + m.Name + "Response"
}
// ClientRequestTypeName returns the generated request type name (private, for client/PDK code).
func (m Method) ClientRequestTypeName(serviceName string) string {
return lowerFirst(serviceName) + m.Name + "Request"
}
// ClientResponseTypeName returns the generated response type name (private, for client/PDK code).
func (m Method) ClientResponseTypeName(serviceName string) string {
return lowerFirst(serviceName) + m.Name + "Response"
}
// lowerFirst returns the string with the first letter lowercased.
func lowerFirst(s string) string {
if s == "" {
return s
}
r := []rune(s)
r[0] = unicode.ToLower(r[0])
return string(r)
}
// HasParams returns true if the method has input parameters.
func (m Method) HasParams() bool {
return len(m.Params) > 0
}
// HasReturns returns true if the method has return values (excluding error).
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()
}
// IsSingleReturn returns true if the method has exactly one return value (excluding error).
func (m Method) IsSingleReturn() bool {
return len(m.Returns) == 1
}
// IsMultiReturn returns true if the method has multiple return values (excluding error).
func (m Method) IsMultiReturn() bool {
return len(m.Returns) > 1
}
// IsOptionPattern returns true if the method returns (value, bool) where the bool
// indicates existence (named "exists", "ok", or "found"). This pattern is used to
// generate Option<T> in Rust instead of a tuple.
func (m Method) IsOptionPattern() bool {
if len(m.Returns) != 2 {
return false
}
if m.Returns[1].Type != "bool" {
return false
}
// Only treat as option pattern if the first return has a meaningful value type
// (not just a bool check like Has())
if m.Returns[0].Type == "bool" {
return false
}
name := strings.ToLower(m.Returns[1].Name)
return name == "exists" || name == "ok" || name == "found"
}
// ReturnSignature returns the Go return type signature for the wrapper function.
// For error-only: "error"
// For single return with error: "(Type, error)"
// For single return no error: "Type"
// For multi return: "(Type1, Type2, ..., error)"
func (m Method) ReturnSignature() string {
if m.IsErrorOnly() {
return "error"
}
var parts []string
for _, r := range m.Returns {
parts = append(parts, r.Type)
}
if m.HasError {
parts = append(parts, "error")
}
// Single return without error doesn't need parentheses
if len(parts) == 1 {
return parts[0]
}
return "(" + strings.Join(parts, ", ") + ")"
}
// ZeroValues returns the zero value expressions for all return types (excluding error).
// Used for error return statements like "return "", false, err".
func (m Method) ZeroValues() string {
var zeros []string
for _, r := range m.Returns {
zeros = append(zeros, zeroValue(r.Type))
}
return strings.Join(zeros, ", ")
}
// zeroValue returns the zero value for a Go type.
func zeroValue(typ string) string {
switch {
case typ == "string":
return `""`
case typ == "bool":
return "false"
case typ == "int", typ == "int8", typ == "int16", typ == "int32", typ == "int64",
typ == "uint", typ == "uint8", typ == "uint16", typ == "uint32", typ == "uint64",
typ == "float32", typ == "float64":
return "0"
case typ == "[]byte":
return "nil"
case strings.HasPrefix(typ, "[]"):
return "nil"
case strings.HasPrefix(typ, "map["):
return "nil"
case strings.HasPrefix(typ, "*"):
return "nil"
case typ == "any", typ == "interface{}":
return "nil"
default:
// For custom struct types, return empty struct
return typ + "{}"
}
}
// Param represents a method parameter or return value.
type Param struct {
Name string // Parameter name
Type string // Go type (e.g., "string", "int32", "[]byte")
JSONName string // JSON field name (camelCase)
}
// NewParam creates a Param with auto-generated JSON name.
func NewParam(name, typ string) Param {
return Param{
Name: name,
Type: typ,
JSONName: toJSONName(name),
}
}
// toJSONName converts a Go identifier to camelCase JSON field name.
// This matches Rust serde's rename_all = "camelCase" behavior.
// Examples: "ConnectionID" -> "connectionId", "NewConnectionID" -> "newConnectionId"
func toJSONName(name string) string {
if name == "" {
return ""
}
runes := []rune(name)
result := make([]rune, 0, len(runes))
for i, r := range runes {
if i == 0 {
// First character is always lowercase
result = append(result, unicode.ToLower(r))
} else if unicode.IsUpper(r) {
// Check if this is part of an acronym (consecutive uppercase)
// or a word boundary
prevIsUpper := unicode.IsUpper(runes[i-1])
nextIsLower := i+1 < len(runes) && unicode.IsLower(runes[i+1])
if prevIsUpper && !nextIsLower {
// Middle of an acronym - lowercase it
result = append(result, unicode.ToLower(r))
} else if prevIsUpper && nextIsLower {
// End of acronym followed by lowercase - this starts a new word
// Keep uppercase
result = append(result, r)
} else {
// Regular word boundary - keep uppercase
result = append(result, r)
}
} else {
result = append(result, r)
}
}
return string(result)
}
// ToPythonType converts a Go type to its Python equivalent.
func ToPythonType(goType string) string {
switch goType {
case "string":
return "str"
case "int", "int32", "int64":
return "int"
case "float32", "float64":
return "float"
case "bool":
return "bool"
case "[]byte":
return "bytes"
default:
return "Any"
}
}
// ToSnakeCase converts a PascalCase or camelCase string to snake_case.
// It handles consecutive uppercase letters correctly (e.g., "ScheduleID" -> "schedule_id").
func ToSnakeCase(s string) string {
var result strings.Builder
runes := []rune(s)
for i, r := range runes {
if i > 0 && r >= 'A' && r <= 'Z' {
// Add underscore before uppercase, but not if:
// - Previous char was uppercase AND next char is uppercase or end of string
// (this handles acronyms like "ID" in "NewScheduleID")
prevUpper := runes[i-1] >= 'A' && runes[i-1] <= 'Z'
nextUpper := i+1 < len(runes) && runes[i+1] >= 'A' && runes[i+1] <= 'Z'
atEnd := i+1 == len(runes)
// Only skip underscore if we're in the middle of an acronym
if !prevUpper || (!nextUpper && !atEnd) {
result.WriteByte('_')
}
}
result.WriteRune(r)
}
return strings.ToLower(result.String())
}
// PythonFunctionName returns the Python function name for a method.
func (m Method) PythonFunctionName(servicePrefix string) string {
return ToSnakeCase(servicePrefix + m.Name)
}
// PythonResultTypeName returns the Python dataclass name for multi-value returns.
func (m Method) PythonResultTypeName(serviceName string) string {
return serviceName + m.Name + "Result"
}
// NeedsResultClass returns true if the method needs a dataclass for returns.
func (m Method) NeedsResultClass() bool {
return len(m.Returns) > 1
}
// PythonType returns the Python type for this parameter.
func (p Param) PythonType() string {
return ToPythonType(p.Type)
}
// PythonName returns the snake_case Python name for this parameter.
func (p Param) PythonName() string {
return ToSnakeCase(p.Name)
}
// ToRustType converts a Go type to its Rust equivalent.
func ToRustType(goType string) string {
return ToRustTypeWithStructs(goType, nil)
}
// RustParamType returns the Rust type for a function parameter (uses &str for strings).
func RustParamType(goType string) string {
if goType == "string" {
return "&str"
}
return ToRustType(goType)
}
// RustDefaultValue returns the default value for a Rust type.
func RustDefaultValue(goType string) string {
switch goType {
case "string":
return `String::new()`
case "int", "int32":
return "0"
case "int64":
return "0"
case "float32", "float64":
return "0.0"
case "bool":
return "false"
default:
if strings.HasPrefix(goType, "[]") {
return "Vec::new()"
}
if strings.HasPrefix(goType, "map[") {
return "std::collections::HashMap::new()"
}
if strings.HasPrefix(goType, "*") {
return "None"
}
return "serde_json::Value::Null"
}
}
// RustFunctionName returns the Rust function name for a method (snake_case).
// Uses just the method name without service prefix since the module provides namespacing.
func (m Method) RustFunctionName(_ string) string {
return ToSnakeCase(m.Name)
}
// RustDocComment returns a properly formatted Rust doc comment.
// Each line of the input doc string is prefixed with "/// ".
func RustDocComment(doc string) string {
if doc == "" {
return ""
}
lines := strings.Split(doc, "\n")
var result []string
for _, line := range lines {
result = append(result, "/// "+line)
}
return strings.Join(result, "\n")
}
// RustType returns the Rust type for this parameter.
func (p Param) RustType() string {
return ToRustType(p.Type)
}
// RustTypeWithStructs returns the Rust type using known struct names.
func (p Param) RustTypeWithStructs(knownStructs map[string]bool) string {
return ToRustTypeWithStructs(p.Type, knownStructs)
}
// RustParamType returns the Rust type for this parameter when used as a function argument.
func (p Param) RustParamType() string {
return RustParamType(p.Type)
}
// RustParamTypeWithStructs returns the Rust param type using known struct names.
func (p Param) RustParamTypeWithStructs(knownStructs map[string]bool) string {
if p.Type == "string" {
return "&str"
}
return ToRustTypeWithStructs(p.Type, knownStructs)
}
// RustName returns the snake_case Rust name for this parameter.
func (p Param) RustName() string {
return ToSnakeCase(p.Name)
}
// NeedsToOwned returns true if the parameter needs .to_owned() when used.
func (p Param) NeedsToOwned() bool {
return p.Type == "string"
}
// RustType returns the Rust type for this field, using known struct names.
func (f FieldDef) RustType(knownStructs map[string]bool) string {
return ToRustTypeWithStructs(f.Type, knownStructs)
}
// RustName returns the snake_case Rust name for this field.
func (f FieldDef) RustName() string {
return ToSnakeCase(f.Name)
}
// NeedsDefault returns true if the field needs #[serde(default)] attribute.
// This is true for fields with omitempty tag.
func (f FieldDef) NeedsDefault() bool {
return f.OmitEmpty
}
// ToRustTypeWithStructs converts a Go type to its Rust equivalent,
// using known struct names instead of serde_json::Value.
func ToRustTypeWithStructs(goType string, knownStructs map[string]bool) string {
// Handle pointer types
if strings.HasPrefix(goType, "*") {
inner := ToRustTypeWithStructs(goType[1:], knownStructs)
return "Option<" + inner + ">"
}
// Handle slice types
if strings.HasPrefix(goType, "[]") {
if goType == "[]byte" {
return "Vec<u8>"
}
inner := ToRustTypeWithStructs(goType[2:], knownStructs)
return "Vec<" + inner + ">"
}
// Handle map types
if strings.HasPrefix(goType, "map[") {
// Extract key and value types from map[K]V
rest := goType[4:] // Remove "map["
depth := 1
keyEnd := 0
for i, r := range rest {
if r == '[' {
depth++
} else if r == ']' {
depth--
if depth == 0 {
keyEnd = i
break
}
}
}
keyType := rest[:keyEnd]
valueType := rest[keyEnd+1:]
return "std::collections::HashMap<" + ToRustTypeWithStructs(keyType, knownStructs) + ", " + ToRustTypeWithStructs(valueType, knownStructs) + ">"
}
switch goType {
case "string":
return "String"
case "int", "int32":
return "i32"
case "int64":
return "i64"
case "float32":
return "f32"
case "float64":
return "f64"
case "bool":
return "bool"
case "interface{}", "any":
return "serde_json::Value"
default:
// Check if this is a known struct type
if knownStructs != nil && knownStructs[goType] {
return goType
}
// For unknown custom types, fall back to Value
return "serde_json::Value"
}
}
+327
View File
@@ -0,0 +1,327 @@
package internal
import (
"sort"
"strings"
"gopkg.in/yaml.v3"
)
// XTP Schema types for YAML marshalling
type (
xtpSchema struct {
Version string `yaml:"version"`
Exports yaml.Node `yaml:"exports,omitempty"`
Components *xtpComponents `yaml:"components,omitempty"`
}
xtpComponents struct {
Schemas yaml.Node `yaml:"schemas"`
}
xtpExport struct {
Description string `yaml:"description,omitempty"`
Input *xtpIOParam `yaml:"input,omitempty"`
Output *xtpIOParam `yaml:"output,omitempty"`
}
xtpIOParam struct {
Ref string `yaml:"$ref,omitempty"`
Type string `yaml:"type,omitempty"`
ContentType string `yaml:"contentType"`
}
// xtpObjectSchema represents an object schema in XTP.
// Per the XTP JSON Schema, ObjectSchema has properties, required, and description
// but NOT a type field.
xtpObjectSchema struct {
Description string `yaml:"description,omitempty"`
Properties yaml.Node `yaml:"properties"`
Required []string `yaml:"required,omitempty"`
}
xtpEnumSchema struct {
Description string `yaml:"description,omitempty"`
Type string `yaml:"type"`
Enum []string `yaml:"enum"`
}
xtpProperty struct {
Ref string `yaml:"$ref,omitempty"`
Type string `yaml:"type,omitempty"`
Format string `yaml:"format,omitempty"`
Description string `yaml:"description,omitempty"`
Nullable bool `yaml:"nullable,omitempty"`
Items *xtpProperty `yaml:"items,omitempty"`
}
)
// GenerateSchema generates an XTP YAML schema from a capability.
func GenerateSchema(cap Capability) ([]byte, error) {
schema := xtpSchema{Version: "v1-draft"}
// Build exports as ordered map
if len(cap.Methods) > 0 {
schema.Exports = yaml.Node{Kind: yaml.MappingNode}
for _, export := range cap.Methods {
addToMap(&schema.Exports, export.ExportName, buildExport(export))
}
}
// Build components/schemas
schemas := buildSchemas(cap)
if len(schemas.Content) > 0 {
schema.Components = &xtpComponents{Schemas: schemas}
}
return yaml.Marshal(schema)
}
func buildExport(export Export) xtpExport {
e := xtpExport{Description: cleanDocForYAML(export.Doc)}
if export.Input.Type != "" {
e.Input = &xtpIOParam{
Ref: "#/components/schemas/" + strings.TrimPrefix(export.Input.Type, "*"),
ContentType: "application/json",
}
}
if export.Output.Type != "" {
outputType := strings.TrimPrefix(export.Output.Type, "*")
// Check if output is a primitive type
if isPrimitiveGoType(outputType) {
e.Output = &xtpIOParam{
Type: goTypeToXTPType(outputType),
ContentType: "application/json",
}
} else {
e.Output = &xtpIOParam{
Ref: "#/components/schemas/" + outputType,
ContentType: "application/json",
}
}
}
return e
}
// isPrimitiveGoType returns true if the Go type is a primitive type.
func isPrimitiveGoType(goType string) bool {
switch goType {
case "bool", "string", "int", "int32", "int64", "float32", "float64", "[]byte":
return true
}
return false
}
func buildSchemas(cap Capability) yaml.Node {
schemas := yaml.Node{Kind: yaml.MappingNode}
knownTypes := cap.KnownStructs()
for _, alias := range cap.TypeAliases {
knownTypes[alias.Name] = true
}
// Collect types that are actually used by exports
usedTypes := collectUsedTypes(cap, knownTypes)
// Sort structs by name for consistent output
structNames := make([]string, 0, len(cap.Structs))
structMap := make(map[string]StructDef)
for _, st := range cap.Structs {
if usedTypes[st.Name] {
structNames = append(structNames, st.Name)
structMap[st.Name] = st
}
}
sort.Strings(structNames)
for _, name := range structNames {
st := structMap[name]
addToMap(&schemas, name, buildObjectSchema(st, knownTypes))
}
// Build enum types from type aliases (only if used by exports)
for _, alias := range cap.TypeAliases {
if !usedTypes[alias.Name] {
continue
}
if alias.Type == "string" {
for _, cg := range cap.Consts {
if cg.Type == alias.Name {
addToMap(&schemas, alias.Name, buildEnumSchema(alias, cg))
break
}
}
}
}
return schemas
}
// collectUsedTypes returns a set of type names that are reachable from exports.
func collectUsedTypes(cap Capability, knownTypes map[string]bool) map[string]bool {
used := make(map[string]bool)
// Start with types directly referenced by exports
for _, export := range cap.Methods {
if export.Input.Type != "" {
addTypeAndDeps(strings.TrimPrefix(export.Input.Type, "*"), cap, knownTypes, used)
}
if export.Output.Type != "" {
outputType := strings.TrimPrefix(export.Output.Type, "*")
if !isPrimitiveGoType(outputType) {
addTypeAndDeps(outputType, cap, knownTypes, used)
}
}
}
return used
}
// addTypeAndDeps adds a type and all its dependencies to the used set.
func addTypeAndDeps(typeName string, cap Capability, knownTypes map[string]bool, used map[string]bool) {
if used[typeName] || !knownTypes[typeName] {
return
}
used[typeName] = true
// Find the struct and add its field types
for _, st := range cap.Structs {
if st.Name == typeName {
for _, field := range st.Fields {
fieldType := strings.TrimPrefix(field.Type, "*")
fieldType = strings.TrimPrefix(fieldType, "[]")
if knownTypes[fieldType] {
addTypeAndDeps(fieldType, cap, knownTypes, used)
}
}
return
}
}
}
func buildObjectSchema(st StructDef, knownTypes map[string]bool) xtpObjectSchema {
schema := xtpObjectSchema{
Description: cleanDocForYAML(st.Doc),
Properties: yaml.Node{Kind: yaml.MappingNode},
}
for _, field := range st.Fields {
propName := getJSONFieldName(field)
addToMap(&schema.Properties, propName, buildProperty(field, knownTypes))
if !strings.HasPrefix(field.Type, "*") && !field.OmitEmpty {
schema.Required = append(schema.Required, propName)
}
}
return schema
}
func buildEnumSchema(alias TypeAlias, cg ConstGroup) xtpEnumSchema {
values := make([]string, 0, len(cg.Values))
for _, cv := range cg.Values {
values = append(values, strings.Trim(cv.Value, `"`))
}
return xtpEnumSchema{
Description: cleanDocForYAML(alias.Doc),
Type: "string",
Enum: values,
}
}
func buildProperty(field FieldDef, knownTypes map[string]bool) xtpProperty {
goType := field.Type
isPointer := strings.HasPrefix(goType, "*")
if isPointer {
goType = goType[1:]
}
prop := xtpProperty{
Description: cleanDocForYAML(field.Doc),
Nullable: isPointer,
}
// Handle reference types (use $ref instead of type)
if isKnownType(goType, knownTypes) && !strings.HasPrefix(goType, "[]") {
prop.Ref = "#/components/schemas/" + goType
return prop
}
// Handle slice types
if strings.HasPrefix(goType, "[]") {
elemType := goType[2:]
prop.Type = "array"
prop.Items = &xtpProperty{}
if isKnownType(elemType, knownTypes) {
prop.Items.Ref = "#/components/schemas/" + elemType
} else {
prop.Items.Type = goTypeToXTPType(elemType)
}
return prop
}
// Handle primitive types
prop.Type, prop.Format = goTypeToXTPTypeAndFormat(goType)
return prop
}
// addToMap adds a key-value pair to a yaml.Node map, preserving insertion order.
func addToMap[T any](node *yaml.Node, key string, value T) {
var valNode yaml.Node
_ = valNode.Encode(value)
node.Content = append(node.Content, &yaml.Node{Kind: yaml.ScalarNode, Value: key}, &valNode)
}
func getJSONFieldName(field FieldDef) string {
propName := field.JSONTag
if idx := strings.Index(propName, ","); idx >= 0 {
propName = propName[:idx]
}
if propName == "" {
propName = field.Name
}
return propName
}
// isKnownType checks if a type is a known struct or type alias.
func isKnownType(typeName string, knownTypes map[string]bool) bool {
return knownTypes[typeName]
}
// goTypeToXTPType converts a Go type to an XTP schema type.
func goTypeToXTPType(goType string) string {
typ, _ := goTypeToXTPTypeAndFormat(goType)
return typ
}
// goTypeToXTPTypeAndFormat converts a Go type to XTP type and format.
func goTypeToXTPTypeAndFormat(goType string) (typ, format string) {
switch goType {
case "string":
return "string", ""
case "int", "int32":
return "integer", "int32"
case "int64":
return "integer", "int64"
case "float32":
return "number", "float"
case "float64":
return "number", "float"
case "bool":
return "boolean", ""
case "[]byte":
return "string", "byte"
default:
return "object", ""
}
}
// cleanDocForYAML cleans documentation for YAML output.
func cleanDocForYAML(doc string) string {
doc = strings.TrimSpace(doc)
// Remove leading "// " from each line if present
lines := strings.Split(doc, "\n")
for i, line := range lines {
lines[i] = strings.TrimPrefix(strings.TrimSpace(line), "// ")
}
return strings.TrimSpace(strings.Join(lines, "\n"))
}
+549
View File
@@ -0,0 +1,549 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"version": {
"$ref": "#/$defs/XtpVersion"
}
},
"required": [
"version"
],
"allOf": [
{
"if": {
"properties": {
"version": {
"const": "v0"
}
}
},
"then": {
"properties": {
"exports": {
"type": "array",
"items": {
"type": "string",
"pattern": "^[a-zA-Z_$][a-zA-Z0-9_$]*$"
}
},
"version": {
"const": "v0"
}
},
"required": [
"exports"
],
"additionalProperties": false
}
},
{
"if": {
"properties": {
"version": {
"const": "v1-draft"
}
}
},
"then": {
"properties": {
"version": {
"$ref": "#/$defs/XtpVersion"
},
"exports": {
"type": "object",
"patternProperties": {
"^[a-zA-Z_$][a-zA-Z0-9_$]*$": {
"$ref": "#/$defs/Export"
}
},
"additionalProperties": false
},
"imports": {
"type": "object",
"patternProperties": {
"^[a-zA-Z_$][a-zA-Z0-9_$]*$": {
"$ref": "#/$defs/Import"
}
},
"additionalProperties": false
},
"components": {
"type": "object",
"properties": {
"schemas": {
"type": "object",
"patternProperties": {
"^[a-zA-Z_$][a-zA-Z0-9_$]*$": {
"$ref": "#/$defs/Schema"
}
},
"additionalProperties": false
}
},
"required": [
"schemas"
],
"additionalProperties": false
}
},
"required": [
"exports"
],
"additionalProperties": false
}
}
],
"$defs": {
"XtpVersion": {
"type": "string",
"enum": [
"v0",
"v1-draft"
]
},
"Export": {
"type": "object",
"properties": {
"description": {
"type": "string"
},
"codeSamples": {
"type": "array",
"items": {
"$ref": "#/$defs/CodeSample"
}
},
"input": {
"$ref": "#/$defs/Parameter"
},
"output": {
"$ref": "#/$defs/Parameter"
}
},
"additionalProperties": false
},
"CodeSample": {
"type": "object",
"properties": {
"lang": {
"anyOf": [
{
"type": "string",
"enum": [
"typescript",
"csharp",
"zig",
"rust",
"go",
"python",
"c++"
]
},
{
"type": "string"
}
]
},
"source": {
"type": "string"
},
"label": {
"type": "string"
}
},
"required": [
"lang",
"source"
],
"additionalProperties": false
},
"Import": {
"type": "object",
"properties": {
"description": {
"type": "string"
},
"input": {
"$ref": "#/$defs/Parameter"
},
"output": {
"$ref": "#/$defs/Parameter"
}
},
"additionalProperties": false
},
"Schema": {
"oneOf": [
{
"$ref": "#/$defs/ObjectSchema"
},
{
"$ref": "#/$defs/EnumSchema"
}
]
},
"ObjectSchema": {
"type": "object",
"properties": {
"description": {
"type": "string"
},
"properties": {
"type": "object",
"patternProperties": {
"^[a-zA-Z_$][a-zA-Z0-9_$]*$": {
"$ref": "#/$defs/Property"
}
},
"additionalProperties": false
},
"required": {
"type": "array",
"items": {
"type": "string"
}
}
},
"required": [
"properties"
],
"additionalProperties": false
},
"EnumSchema": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"string"
]
},
"description": {
"type": "string"
},
"enum": {
"type": "array",
"items": {
"type": "string",
"pattern": "^[a-zA-Z_$][a-zA-Z0-9_$]*$"
}
}
},
"required": [
"enum"
],
"additionalProperties": false
},
"Parameter": {
"oneOf": [
{
"$ref": "#/$defs/ValueParameter"
},
{
"$ref": "#/$defs/RefParameter"
},
{
"$ref": "#/$defs/MapParameter"
}
]
},
"RefParameter": {
"type": "object",
"properties": {
"$ref": {
"$ref": "#/$defs/SchemaReference"
},
"description": {
"type": "string"
},
"nullable": {
"type": "boolean",
"default": false
},
"contentType": {
"$ref": "#/$defs/ContentType"
}
},
"required": [
"$ref",
"contentType"
],
"additionalProperties": false
},
"ValueParameter": {
"type": "object",
"properties": {
"contentType": {
"$ref": "#/$defs/ContentType"
},
"type": {
"$ref": "#/$defs/XtpType"
},
"format": {
"$ref": "#/$defs/XtpFormat"
},
"nullable": {
"type": "boolean",
"default": false
},
"description": {
"type": "string"
},
"items": {
"type": "object",
"$ref": "#/$defs/ArrayItem"
}
},
"required": [
"type",
"contentType"
],
"additionalProperties": false
},
"MapParameter": {
"type": "object",
"properties": {
"type": {
"const": "object"
},
"description": {
"type": "string"
},
"additionalProperties": {
"allOf": [
{
"$ref": "#/$defs/NonMapProperty"
},
{
"type": "object",
"properties": {
"description": false
},
"additionalProperties": false
}
]
},
"nullable": {
"type": "boolean",
"default": false
},
"contentType": {
"$ref": "#/$defs/ContentType"
}
},
"required": [
"additionalProperties",
"contentType"
]
},
"NonMapProperty": {
"oneOf": [
{
"$ref": "#/$defs/ValueProperty"
},
{
"$ref": "#/$defs/RefProperty"
}
]
},
"Property": {
"oneOf": [
{
"$ref": "#/$defs/ValueProperty"
},
{
"$ref": "#/$defs/RefProperty"
},
{
"$ref": "#/$defs/MapProperty"
}
]
},
"ValueProperty": {
"type": "object",
"properties": {
"type": {
"$ref": "#/$defs/XtpType"
},
"format": {
"$ref": "#/$defs/XtpFormat"
},
"nullable": {
"type": "boolean",
"default": false
},
"description": {
"type": "string"
},
"items": {
"type": "object",
"$ref": "#/$defs/ArrayItem"
}
},
"required": [
"type"
],
"additionalProperties": false
},
"MapProperty": {
"type": "object",
"properties": {
"type": {
"const": "object"
},
"description": {
"type": "string"
},
"additionalProperties": {
"allOf": [
{
"$ref": "#/$defs/NonMapProperty"
},
{
"not": {
"type": "object",
"required": ["description"]
}
}
]
},
"nullable": {
"type": "boolean",
"default": false
}
},
"required": [
"additionalProperties"
],
"additionalProperties": false
},
"RefProperty": {
"type": "object",
"properties": {
"$ref": {
"$ref": "#/$defs/SchemaReference"
},
"description": {
"type": "string"
},
"nullable": {
"type": "boolean",
"default": false
}
},
"required": [
"$ref"
],
"additionalProperties": false
},
"ContentType": {
"type": "string",
"enum": [
"application/json",
"application/x-binary",
"text/plain; charset=utf-8"
]
},
"SchemaReference": {
"type": "string",
"pattern": "^#/components/schemas/[^/]+$"
},
"XtpType": {
"type": "string",
"enum": [
"integer",
"string",
"number",
"boolean",
"object",
"array",
"buffer"
]
},
"XtpFormat": {
"type": "string",
"enum": [
"int32",
"int64",
"float",
"double",
"date-time",
"byte"
]
},
"ArrayItem": {
"type": "object",
"oneOf": [
{
"$ref": "#/$defs/ValueArrayItem"
},
{
"$ref": "#/$defs/RefArrayItem"
},
{
"$ref": "#/$defs/MapArrayItem"
}
]
},
"ValueArrayItem": {
"type": "object",
"properties": {
"type": {
"$ref": "#/$defs/XtpType"
},
"format": {
"$ref": "#/$defs/XtpFormat"
},
"nullable": {
"type": "boolean"
}
},
"required": [
"type"
],
"additionalProperties": false
},
"RefArrayItem": {
"type": "object",
"properties": {
"$ref": {
"$ref": "#/$defs/SchemaReference"
},
"nullable": {
"type": "boolean",
"default": false
}
},
"required": [
"$ref"
],
"additionalProperties": false
},
"MapArrayItem": {
"type": "object",
"properties": {
"type": {
"const": "object"
},
"additionalProperties": {
"allOf": [
{
"$ref": "#/$defs/NonMapProperty"
},
{
"not": {
"type": "object",
"required": ["description"]
}
}
]
}
},
"required": [
"additionalProperties"
],
"additionalProperties": false
}
}
}
@@ -0,0 +1,722 @@
package internal
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"gopkg.in/yaml.v3"
)
var _ = Describe("XTP Schema Generation", func() {
parseSchema := func(schema []byte) map[string]any {
var doc map[string]any
Expect(yaml.Unmarshal(schema, &doc)).To(Succeed())
return doc
}
Describe("GenerateSchema", func() {
Context("basic capability with one export", func() {
var schema []byte
BeforeEach(func() {
capability := Capability{
Name: "test",
Doc: "Test capability",
SourceFile: "test",
Methods: []Export{
{
ExportName: "test_method",
Doc: "Test method does something",
Input: NewParam("input", "TestInput"),
Output: NewParam("output", "TestOutput"),
},
},
Structs: []StructDef{
{
Name: "TestInput",
Doc: "Input for test",
Fields: []FieldDef{
{Name: "Name", Type: "string", JSONTag: "name", Doc: "The name"},
{Name: "Count", Type: "int", JSONTag: "count", Doc: "The count"},
},
},
{
Name: "TestOutput",
Doc: "Output for test",
Fields: []FieldDef{
{Name: "Result", Type: "string", JSONTag: "result", Doc: "The result"},
},
},
},
}
var err error
schema, err = GenerateSchema(capability)
Expect(err).NotTo(HaveOccurred())
Expect(schema).NotTo(BeEmpty())
})
It("should validate against XTP JSONSchema", func() {
Expect(ValidateXTPSchema(schema)).To(Succeed())
})
It("should have correct version", func() {
doc := parseSchema(schema)
Expect(doc["version"]).To(Equal("v1-draft"))
})
It("should include exports with description", func() {
doc := parseSchema(schema)
exports := doc["exports"].(map[string]any)
Expect(exports).To(HaveKey("test_method"))
method := exports["test_method"].(map[string]any)
Expect(method["description"]).To(Equal("Test method does something"))
})
It("should include schemas for input and output types", func() {
doc := parseSchema(schema)
components := doc["components"].(map[string]any)
schemas := components["schemas"].(map[string]any)
Expect(schemas).To(HaveKey("TestInput"))
Expect(schemas).To(HaveKey("TestOutput"))
})
It("should define input schema with correct properties", func() {
doc := parseSchema(schema)
components := doc["components"].(map[string]any)
schemas := components["schemas"].(map[string]any)
input := schemas["TestInput"].(map[string]any)
// Per XTP spec, ObjectSchema does NOT have a type field - only properties, required, description
Expect(input).NotTo(HaveKey("type"))
props := input["properties"].(map[string]any)
Expect(props).To(HaveKey("name"))
Expect(props).To(HaveKey("count"))
})
It("should mark non-pointer, non-omitempty fields as required", func() {
doc := parseSchema(schema)
components := doc["components"].(map[string]any)
schemas := components["schemas"].(map[string]any)
input := schemas["TestInput"].(map[string]any)
required := input["required"].([]any)
Expect(required).To(ContainElement("name"))
Expect(required).To(ContainElement("count"))
})
})
Context("capability with pointer fields (nullable)", func() {
var schema []byte
BeforeEach(func() {
capability := Capability{
Name: "nullable_test",
SourceFile: "nullable_test",
Methods: []Export{
{ExportName: "test", Input: NewParam("input", "Input"), Output: NewParam("output", "Output")},
},
Structs: []StructDef{
{
Name: "Input",
Fields: []FieldDef{
{Name: "Required", Type: "string", JSONTag: "required"},
{Name: "Optional", Type: "*string", JSONTag: "optional,omitempty", OmitEmpty: true},
},
},
{
Name: "Output",
Fields: []FieldDef{
{Name: "Value", Type: "string", JSONTag: "value"},
},
},
},
}
var err error
schema, err = GenerateSchema(capability)
Expect(err).NotTo(HaveOccurred())
})
It("should validate against XTP JSONSchema", func() {
Expect(ValidateXTPSchema(schema)).To(Succeed())
})
It("should not mark required field as nullable", func() {
doc := parseSchema(schema)
components := doc["components"].(map[string]any)
schemas := components["schemas"].(map[string]any)
input := schemas["Input"].(map[string]any)
props := input["properties"].(map[string]any)
requiredField := props["required"].(map[string]any)
Expect(requiredField).NotTo(HaveKey("nullable"))
})
It("should mark optional pointer field as nullable", func() {
doc := parseSchema(schema)
components := doc["components"].(map[string]any)
schemas := components["schemas"].(map[string]any)
input := schemas["Input"].(map[string]any)
props := input["properties"].(map[string]any)
optionalField := props["optional"].(map[string]any)
Expect(optionalField["nullable"]).To(BeTrue())
})
It("should only include non-pointer fields in required array", func() {
doc := parseSchema(schema)
components := doc["components"].(map[string]any)
schemas := components["schemas"].(map[string]any)
input := schemas["Input"].(map[string]any)
required := input["required"].([]any)
Expect(required).To(ContainElement("required"))
Expect(required).NotTo(ContainElement("optional"))
})
})
Context("capability with enum", func() {
var schema []byte
BeforeEach(func() {
capability := Capability{
Name: "enum_test",
SourceFile: "enum_test",
Methods: []Export{
{ExportName: "test", Input: NewParam("input", "Input"), Output: NewParam("output", "Output")},
},
Structs: []StructDef{
{
Name: "Input",
Fields: []FieldDef{
{Name: "Status", Type: "Status", JSONTag: "status"},
},
},
{
Name: "Output",
Fields: []FieldDef{
{Name: "Value", Type: "string", JSONTag: "value"},
},
},
},
TypeAliases: []TypeAlias{
{Name: "Status", Type: "string", Doc: "Status type"},
},
Consts: []ConstGroup{
{
Type: "Status",
Values: []ConstDef{
{Name: "StatusPending", Value: `"pending"`},
{Name: "StatusActive", Value: `"active"`},
{Name: "StatusDone", Value: `"done"`},
},
},
},
}
var err error
schema, err = GenerateSchema(capability)
Expect(err).NotTo(HaveOccurred())
})
It("should validate against XTP JSONSchema", func() {
Expect(ValidateXTPSchema(schema)).To(Succeed())
})
It("should define enum type with correct values", func() {
doc := parseSchema(schema)
components := doc["components"].(map[string]any)
schemas := components["schemas"].(map[string]any)
Expect(schemas).To(HaveKey("Status"))
status := schemas["Status"].(map[string]any)
Expect(status["type"]).To(Equal("string"))
enum := status["enum"].([]any)
Expect(enum).To(ConsistOf("pending", "active", "done"))
})
It("should use $ref for enum field in struct", func() {
doc := parseSchema(schema)
components := doc["components"].(map[string]any)
schemas := components["schemas"].(map[string]any)
input := schemas["Input"].(map[string]any)
props := input["properties"].(map[string]any)
statusRef := props["status"].(map[string]any)
Expect(statusRef["$ref"]).To(Equal("#/components/schemas/Status"))
})
})
Context("capability with array types", func() {
var schema []byte
BeforeEach(func() {
capability := Capability{
Name: "array_test",
SourceFile: "array_test",
Methods: []Export{
{ExportName: "test", Input: NewParam("input", "Input"), Output: NewParam("output", "Output")},
},
Structs: []StructDef{
{
Name: "Input",
Fields: []FieldDef{
{Name: "Tags", Type: "[]string", JSONTag: "tags"},
{Name: "Items", Type: "[]Item", JSONTag: "items"},
},
},
{
Name: "Output",
Fields: []FieldDef{
{Name: "Value", Type: "string", JSONTag: "value"},
},
},
{
Name: "Item",
Fields: []FieldDef{
{Name: "ID", Type: "string", JSONTag: "id"},
},
},
},
}
var err error
schema, err = GenerateSchema(capability)
Expect(err).NotTo(HaveOccurred())
})
It("should validate against XTP JSONSchema", func() {
Expect(ValidateXTPSchema(schema)).To(Succeed())
})
It("should define string array with primitive type", func() {
doc := parseSchema(schema)
components := doc["components"].(map[string]any)
schemas := components["schemas"].(map[string]any)
input := schemas["Input"].(map[string]any)
props := input["properties"].(map[string]any)
tags := props["tags"].(map[string]any)
Expect(tags["type"]).To(Equal("array"))
tagItems := tags["items"].(map[string]any)
Expect(tagItems["type"]).To(Equal("string"))
})
It("should define struct array with $ref", func() {
doc := parseSchema(schema)
components := doc["components"].(map[string]any)
schemas := components["schemas"].(map[string]any)
input := schemas["Input"].(map[string]any)
props := input["properties"].(map[string]any)
items := props["items"].(map[string]any)
Expect(items["type"]).To(Equal("array"))
itemItems := items["items"].(map[string]any)
Expect(itemItems["$ref"]).To(Equal("#/components/schemas/Item"))
})
})
Context("capability with nullable ref", func() {
It("should mark pointer to enum as nullable with $ref", func() {
capability := Capability{
Name: "nullable_ref_test",
SourceFile: "nullable_ref_test",
Methods: []Export{
{ExportName: "test", Input: NewParam("input", "Input"), Output: NewParam("output", "Output")},
},
Structs: []StructDef{
{
Name: "Input",
Fields: []FieldDef{
{Name: "Value", Type: "string", JSONTag: "value"},
},
},
{
Name: "Output",
Fields: []FieldDef{
{Name: "Status", Type: "*ErrorType", JSONTag: "status,omitempty", OmitEmpty: true},
},
},
},
TypeAliases: []TypeAlias{
{Name: "ErrorType", Type: "string"},
},
Consts: []ConstGroup{
{
Type: "ErrorType",
Values: []ConstDef{
{Name: "ErrorNone", Value: `"none"`},
{Name: "ErrorFatal", Value: `"fatal"`},
},
},
},
}
schema, err := GenerateSchema(capability)
Expect(err).NotTo(HaveOccurred())
// Validate against XTP JSONSchema
Expect(ValidateXTPSchema(schema)).To(Succeed())
doc := parseSchema(schema)
components := doc["components"].(map[string]any)
schemas := components["schemas"].(map[string]any)
output := schemas["Output"].(map[string]any)
props := output["properties"].(map[string]any)
status := props["status"].(map[string]any)
Expect(status["$ref"]).To(Equal("#/components/schemas/ErrorType"))
Expect(status["nullable"]).To(BeTrue())
})
})
})
Describe("goTypeToXTPTypeAndFormat", func() {
DescribeTable("should convert Go types to XTP types",
func(goType, wantType, wantFormat string) {
gotType, gotFormat := goTypeToXTPTypeAndFormat(goType)
Expect(gotType).To(Equal(wantType))
Expect(gotFormat).To(Equal(wantFormat))
},
Entry("string", "string", "string", ""),
Entry("int", "int", "integer", "int32"),
Entry("int32", "int32", "integer", "int32"),
Entry("int64", "int64", "integer", "int64"),
Entry("float32", "float32", "number", "float"),
Entry("float64", "float64", "number", "float"),
Entry("bool", "bool", "boolean", ""),
Entry("[]byte", "[]byte", "string", "byte"),
Entry("unknown types default to object", "CustomType", "object", ""),
)
})
Describe("cleanDocForYAML", func() {
DescribeTable("should clean documentation strings",
func(doc, want string) {
Expect(cleanDocForYAML(doc)).To(Equal(want))
},
Entry("empty", "", ""),
Entry("single line", "Simple description", "Simple description"),
Entry("multiline", "First line\nSecond line", "First line\nSecond line"),
Entry("trailing newline", "Description\n", "Description"),
Entry("whitespace", " Description ", "Description"),
)
})
Describe("isPrimitiveGoType", func() {
DescribeTable("should identify primitive Go types",
func(goType string, want bool) {
Expect(isPrimitiveGoType(goType)).To(Equal(want))
},
Entry("bool", "bool", true),
Entry("string", "string", true),
Entry("int", "int", true),
Entry("int32", "int32", true),
Entry("int64", "int64", true),
Entry("float32", "float32", true),
Entry("float64", "float64", true),
Entry("[]byte", "[]byte", true),
Entry("custom type", "CustomType", false),
Entry("struct type", "MyStruct", false),
Entry("slice of string", "[]string", false),
Entry("map type", "map[string]int", false),
)
})
Describe("GenerateSchema with primitive output types", func() {
inputStruct := StructDef{
Name: "Input",
Fields: []FieldDef{{Name: "ID", Type: "string", JSONTag: "id"}},
}
Context("export with primitive string output", func() {
It("should use type instead of $ref and validate against XTP JSONSchema", func() {
capability := Capability{
Name: "test",
SourceFile: "test",
Methods: []Export{
{ExportName: "get_name", Input: NewParam("input", "Input"), Output: NewParam("output", "string")},
},
Structs: []StructDef{inputStruct},
}
schema, err := GenerateSchema(capability)
Expect(err).NotTo(HaveOccurred())
Expect(schema).NotTo(BeEmpty())
Expect(ValidateXTPSchema(schema)).To(Succeed())
doc := parseSchema(schema)
exports := doc["exports"].(map[string]any)
method := exports["get_name"].(map[string]any)
output := method["output"].(map[string]any)
Expect(output["type"]).To(Equal("string"))
Expect(output).NotTo(HaveKey("$ref"))
Expect(output["contentType"]).To(Equal("application/json"))
})
})
Context("export with primitive bool output", func() {
It("should use boolean type and validate against XTP JSONSchema", func() {
capability := Capability{
Name: "test",
SourceFile: "test",
Methods: []Export{
{ExportName: "is_valid", Input: NewParam("input", "Input"), Output: NewParam("output", "bool")},
},
Structs: []StructDef{inputStruct},
}
schema, err := GenerateSchema(capability)
Expect(err).NotTo(HaveOccurred())
Expect(ValidateXTPSchema(schema)).To(Succeed())
doc := parseSchema(schema)
exports := doc["exports"].(map[string]any)
method := exports["is_valid"].(map[string]any)
output := method["output"].(map[string]any)
Expect(output["type"]).To(Equal("boolean"))
Expect(output).NotTo(HaveKey("$ref"))
})
})
Context("export with primitive int output", func() {
It("should use integer type and validate against XTP JSONSchema", func() {
capability := Capability{
Name: "test",
SourceFile: "test",
Methods: []Export{
{ExportName: "get_count", Input: NewParam("input", "Input"), Output: NewParam("output", "int32")},
},
Structs: []StructDef{inputStruct},
}
schema, err := GenerateSchema(capability)
Expect(err).NotTo(HaveOccurred())
Expect(ValidateXTPSchema(schema)).To(Succeed())
doc := parseSchema(schema)
exports := doc["exports"].(map[string]any)
method := exports["get_count"].(map[string]any)
output := method["output"].(map[string]any)
Expect(output["type"]).To(Equal("integer"))
Expect(output).NotTo(HaveKey("$ref"))
})
})
Context("export with pointer to primitive output", func() {
It("should strip pointer and use primitive type and validate against XTP JSONSchema", func() {
capability := Capability{
Name: "test",
SourceFile: "test",
Methods: []Export{
{ExportName: "get_optional_string", Input: NewParam("input", "Input"), Output: NewParam("output", "*string")},
},
Structs: []StructDef{inputStruct},
}
schema, err := GenerateSchema(capability)
Expect(err).NotTo(HaveOccurred())
Expect(ValidateXTPSchema(schema)).To(Succeed())
doc := parseSchema(schema)
exports := doc["exports"].(map[string]any)
method := exports["get_optional_string"].(map[string]any)
output := method["output"].(map[string]any)
Expect(output["type"]).To(Equal("string"))
Expect(output).NotTo(HaveKey("$ref"))
})
})
Context("export with struct output", func() {
It("should still use $ref and validate against XTP JSONSchema", func() {
capability := Capability{
Name: "test",
SourceFile: "test",
Methods: []Export{
{ExportName: "get_result", Input: NewParam("input", "Input"), Output: NewParam("output", "Output")},
},
Structs: []StructDef{
inputStruct,
{Name: "Output", Fields: []FieldDef{{Name: "Value", Type: "string", JSONTag: "value"}}},
},
}
schema, err := GenerateSchema(capability)
Expect(err).NotTo(HaveOccurred())
Expect(ValidateXTPSchema(schema)).To(Succeed())
doc := parseSchema(schema)
exports := doc["exports"].(map[string]any)
method := exports["get_result"].(map[string]any)
output := method["output"].(map[string]any)
Expect(output["$ref"]).To(Equal("#/components/schemas/Output"))
Expect(output).NotTo(HaveKey("type"))
})
})
})
Describe("collectUsedTypes", func() {
getSchemas := func(schema []byte) map[string]any {
doc := parseSchema(schema)
components, hasComponents := doc["components"].(map[string]any)
if !hasComponents {
return make(map[string]any)
}
schemas, ok := components["schemas"].(map[string]any)
if !ok {
return make(map[string]any)
}
return schemas
}
It("should only include types referenced by exports", func() {
capability := Capability{
Name: "test",
SourceFile: "test",
Methods: []Export{
{ExportName: "test", Input: NewParam("input", "UsedInput"), Output: NewParam("output", "UsedOutput")},
},
Structs: []StructDef{
{Name: "UsedInput", Fields: []FieldDef{{Name: "ID", Type: "string", JSONTag: "id"}}},
{Name: "UsedOutput", Fields: []FieldDef{{Name: "Value", Type: "string", JSONTag: "value"}}},
{Name: "UnusedStruct", Fields: []FieldDef{{Name: "Foo", Type: "string", JSONTag: "foo"}}},
},
}
schema, err := GenerateSchema(capability)
Expect(err).NotTo(HaveOccurred())
Expect(ValidateXTPSchema(schema)).To(Succeed())
schemas := getSchemas(schema)
Expect(schemas).To(HaveKey("UsedInput"))
Expect(schemas).To(HaveKey("UsedOutput"))
Expect(schemas).NotTo(HaveKey("UnusedStruct"))
})
It("should include transitively referenced types", func() {
capability := Capability{
Name: "test",
SourceFile: "test",
Methods: []Export{
{ExportName: "test", Input: NewParam("input", "Input"), Output: NewParam("output", "Output")},
},
Structs: []StructDef{
{Name: "Input", Fields: []FieldDef{{Name: "ID", Type: "string", JSONTag: "id"}}},
{Name: "Output", Fields: []FieldDef{{Name: "Nested", Type: "NestedType", JSONTag: "nested"}}},
{Name: "NestedType", Fields: []FieldDef{{Name: "Value", Type: "string", JSONTag: "value"}}},
},
}
schema, err := GenerateSchema(capability)
Expect(err).NotTo(HaveOccurred())
Expect(ValidateXTPSchema(schema)).To(Succeed())
schemas := getSchemas(schema)
Expect(schemas).To(HaveKey("Input"))
Expect(schemas).To(HaveKey("Output"))
Expect(schemas).To(HaveKey("NestedType"))
})
It("should include array element types", func() {
capability := Capability{
Name: "test",
SourceFile: "test",
Methods: []Export{
{ExportName: "test", Input: NewParam("input", "Input"), Output: NewParam("output", "Output")},
},
Structs: []StructDef{
{Name: "Input", Fields: []FieldDef{{Name: "ID", Type: "string", JSONTag: "id"}}},
{Name: "Output", Fields: []FieldDef{{Name: "Items", Type: "[]Item", JSONTag: "items"}}},
{Name: "Item", Fields: []FieldDef{{Name: "Name", Type: "string", JSONTag: "name"}}},
},
}
schema, err := GenerateSchema(capability)
Expect(err).NotTo(HaveOccurred())
Expect(ValidateXTPSchema(schema)).To(Succeed())
schemas := getSchemas(schema)
Expect(schemas).To(HaveKey("Input"))
Expect(schemas).To(HaveKey("Output"))
Expect(schemas).To(HaveKey("Item"))
})
It("should include pointer types", func() {
capability := Capability{
Name: "test",
SourceFile: "test",
Methods: []Export{
{ExportName: "test", Input: NewParam("input", "Input"), Output: NewParam("output", "Output")},
},
Structs: []StructDef{
{Name: "Input", Fields: []FieldDef{{Name: "ID", Type: "string", JSONTag: "id"}}},
{Name: "Output", Fields: []FieldDef{{Name: "Optional", Type: "*OptionalType", JSONTag: "optional"}}},
{Name: "OptionalType", Fields: []FieldDef{{Name: "Value", Type: "string", JSONTag: "value"}}},
},
}
schema, err := GenerateSchema(capability)
Expect(err).NotTo(HaveOccurred())
Expect(ValidateXTPSchema(schema)).To(Succeed())
schemas := getSchemas(schema)
Expect(schemas).To(HaveKey("Input"))
Expect(schemas).To(HaveKey("Output"))
Expect(schemas).To(HaveKey("OptionalType"))
})
It("should exclude primitive output types from schema", func() {
capability := Capability{
Name: "test",
SourceFile: "test",
Methods: []Export{
{ExportName: "test", Input: NewParam("input", "Input"), Output: NewParam("output", "string")},
},
Structs: []StructDef{
{Name: "Input", Fields: []FieldDef{{Name: "ID", Type: "string", JSONTag: "id"}}},
},
}
schema, err := GenerateSchema(capability)
Expect(err).NotTo(HaveOccurred())
Expect(ValidateXTPSchema(schema)).To(Succeed())
schemas := getSchemas(schema)
Expect(schemas).To(HaveKey("Input"))
})
})
Describe("GenerateSchema enum filtering", func() {
It("should only include enums that are actually used by exports", func() {
capability := Capability{
Name: "test",
SourceFile: "test",
Methods: []Export{
{ExportName: "test", Input: NewParam("input", "Input"), Output: NewParam("output", "Output")},
},
Structs: []StructDef{
{
Name: "Input",
Fields: []FieldDef{{Name: "Status", Type: "UsedStatus", JSONTag: "status"}},
},
{
Name: "Output",
Fields: []FieldDef{{Name: "Value", Type: "string", JSONTag: "value"}},
},
},
TypeAliases: []TypeAlias{
{Name: "UsedStatus", Type: "string"},
{Name: "UnusedStatus", Type: "string"},
},
Consts: []ConstGroup{
{
Type: "UsedStatus",
Values: []ConstDef{
{Name: "StatusActive", Value: `"active"`},
{Name: "StatusInactive", Value: `"inactive"`},
},
},
{
Type: "UnusedStatus",
Values: []ConstDef{
{Name: "UnusedPending", Value: `"pending"`},
},
},
},
}
schema, err := GenerateSchema(capability)
Expect(err).NotTo(HaveOccurred())
Expect(ValidateXTPSchema(schema)).To(Succeed())
doc := parseSchema(schema)
components := doc["components"].(map[string]any)
schemas := components["schemas"].(map[string]any)
// UsedStatus should be included because it's referenced by Input
Expect(schemas).To(HaveKey("UsedStatus"))
usedStatus := schemas["UsedStatus"].(map[string]any)
Expect(usedStatus["type"]).To(Equal("string"))
enum := usedStatus["enum"].([]any)
Expect(enum).To(ConsistOf("active", "inactive"))
// UnusedStatus should NOT be included
Expect(schemas).NotTo(HaveKey("UnusedStatus"))
})
})
})
@@ -0,0 +1,51 @@
package internal
import (
_ "embed"
"encoding/json"
"fmt"
"strings"
"github.com/xeipuuv/gojsonschema"
"gopkg.in/yaml.v3"
)
// XTP JSONSchema specification, from
// https://raw.githubusercontent.com/dylibso/xtp-bindgen/5090518dd86ba5e734dc225a33066ecc0ed2e12d/plugin/schema.json
//
//go:embed xtp_schema.json
var xtpSchemaJSON string
// ValidateXTPSchema validates that the generated schema conforms to the XTP JSONSchema specification.
// Returns nil if valid, or an error with validation details if invalid.
func ValidateXTPSchema(generatedSchema []byte) error {
// Parse the YAML schema to JSON for validation
var schemaDoc map[string]any
if err := yaml.Unmarshal(generatedSchema, &schemaDoc); err != nil {
return fmt.Errorf("failed to parse generated schema as YAML: %w", err)
}
// Convert to JSON for the validator
jsonBytes, err := json.Marshal(schemaDoc)
if err != nil {
return fmt.Errorf("failed to convert schema to JSON: %w", err)
}
schemaLoader := gojsonschema.NewStringLoader(xtpSchemaJSON)
documentLoader := gojsonschema.NewBytesLoader(jsonBytes)
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
if err != nil {
return fmt.Errorf("schema validation failed: %w", err)
}
if !result.Valid() {
var errs []string
for _, desc := range result.Errors() {
errs = append(errs, fmt.Sprintf("- %s", desc))
}
return fmt.Errorf("schema validation errors:\n%s", strings.Join(errs, "\n"))
}
return nil
}