Skip to content

Commit

Permalink
feat(plugin): semplify the plugin API by using the callback directly
Browse files Browse the repository at this point in the history
Signed-off-by: Vincenzo Palazzo <[email protected]>
  • Loading branch information
vincenzopalazzo committed Apr 13, 2023
1 parent e3e06a2 commit ebe83ef
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 30 deletions.
14 changes: 7 additions & 7 deletions bench/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ type benchResult struct {
}

type bench struct {
Name string `json:"name"`
Runs int `json:"runs"`
Times time.Duration `json:"times"`
TimeStr string `json:"time_str"`
Name string `json:"name"`
Runs int `json:"runs"`
Times time.Duration `json:"times"`
TimeStr string `json:"time_str"`
}

// runBencmarks - entry point of the benchmarks!
Expand All @@ -41,9 +41,9 @@ func runBenchmarks(benchs map[string]func(*testing.B)) {
for name, bench_fn := range benchs {
res := testing.Benchmark(bench_fn)
b := bench{
Name: name,
Runs: res.N,
Times: res.T,
Name: name,
Runs: res.N,
Times: res.T,
TimeStr: fmt.Sprintf("%s", res.T),
}
log.Infof("----------------- %s -----------------", name)
Expand Down
12 changes: 4 additions & 8 deletions plugin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import (
"github.com/vincenzopalazzo/cln4go/comm"
)

// GetManifest is a method to generate the manfest object needed by Core Damon
// GetManifest - is a method to generate the manfest object needed by Core Damon
// to register the plugin. https://lightning.readthedocs.io/PLUGINS.html
type getManifest[T any] struct{}

func (instance *getManifest[T]) Call(plugin *Plugin[T], request map[string]any) (map[string]any, error) {
func GetManifest[T any](plugin *Plugin[T], request map[string]any) (map[string]any, error) {
result := make(map[string]any)
result["options"] = comm.GenerateArray(plugin.Options, map[string]bool{})
result["rpcmethods"] = comm.GenerateArray(plugin.RpcMethods, map[string]bool{"getmanifest": true, "init": true})
Expand All @@ -22,11 +20,9 @@ func (instance *getManifest[T]) Call(plugin *Plugin[T], request map[string]any)
return result, nil
}

// initMethod method is called by Core Damon after the command line options has been
// InitCall - method is called by Core Damon after the command line options has been
// parsed and the plugin has been loaded.
type initMethod[T any] struct{}

func (instance *initMethod[T]) Call(plugin *Plugin[T], request map[string]any) (map[string]any, error) {
func InitCall[T any](plugin *Plugin[T], request map[string]any) (map[string]any, error) {
plugin.Configuration, _ = request["configuration"].(map[string]any)
opts := request["options"]
for key, value := range opts.(map[string]any) {
Expand Down
20 changes: 10 additions & 10 deletions plugin/command.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
package plugin

type rpcMethod[T any] struct {
Name string `json:"name"`
Usage string `json:"usage"`
Description string `json:"description"`
LongDescription string `json:"long_description"`
callback RPCCommand[T] `json:"-"`
Name string `json:"name"`
Usage string `json:"usage"`
Description string `json:"description"`
LongDescription string `json:"long_description"`
callback func(plugin *Plugin[T], request map[string]any) (map[string]any, error) `json:"-"`
}

func (instance *rpcMethod[T]) Call(plugin *Plugin[T], request map[string]any) (map[string]any, error) {
return instance.callback.Call(plugin, request)
return instance.callback(plugin, request)
}

type rpcNotification[T any] struct {
onEvent string
callback RPCEvent[T]
callback func(plugin *Plugin[T], request map[string]any)
}

func (instance *rpcNotification[T]) Call(plugin *Plugin[T], request map[string]any) {
instance.callback.Call(plugin, request)
instance.callback(plugin, request)
}

type rpcHook[T any] struct {
name string
before []string
after []string
callback RPCCommand[T]
callback func(plugin *Plugin[T], request map[string]any) (map[string]any, error)
}

func (instance *rpcHook[T]) Call(plugin *Plugin[T], request map[string]any) {
if _, err := instance.callback.Call(plugin, request); err != nil {
if _, err := instance.callback(plugin, request); err != nil {
plugin.tracer.Infof("%s", err)
}
}
10 changes: 5 additions & 5 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (self *Plugin[T]) Decode(payload map[string]any, destination any) error {
}

// Method to add a new rpc method to the plugin.
func (instance *Plugin[T]) RegisterRPCMethod(name string, usage string, description string, callback RPCCommand[T]) {
func (instance *Plugin[T]) RegisterRPCMethod(name string, usage string, description string, callback func(plugin *Plugin[T], request Map) (Map, error)) {
instance.RpcMethods[name] = &rpcMethod[T]{
Name: name,
Usage: usage,
Expand All @@ -106,15 +106,15 @@ func (instance *Plugin[T]) RegisterOption(name string, typ string, def string, d
}

// Method to add a new rpc notification to the plugin.
func (instance *Plugin[T]) RegisterNotification(name string, callback RPCEvent[T]) {
func (instance *Plugin[T]) RegisterNotification(name string, callback func(plugin *Plugin[T], request Map)) {
instance.Notifications[name] = &rpcNotification[T]{
onEvent: name,
callback: callback,
}
}

// Method to add a new rpc hook to the plugin.
func (instance *Plugin[T]) RegisterHook(name string, before []string, after []string, callback RPCCommand[T]) {
func (instance *Plugin[T]) RegisterHook(name string, before []string, after []string, callback func(plugin *Plugin[T], request Map) (Map, error)) {
instance.Hooks[name] = &rpcHook[T]{
name: name,
before: before,
Expand Down Expand Up @@ -179,8 +179,8 @@ func (instance *Plugin[T]) Log(level string, message string) {

// Configuring a plugin with the default rpc methods Core Lightning needs to work.
func (instance *Plugin[T]) configurePlugin() {
instance.RegisterRPCMethod("getmanifest", "", "", &getManifest[T]{})
instance.RegisterRPCMethod("init", "", "", &initMethod[T]{})
instance.RegisterRPCMethod("getmanifest", "", "", GetManifest[T])
instance.RegisterRPCMethod("init", "", "", InitCall[T])
}

func (instance *Plugin[T]) Start() {
Expand Down

0 comments on commit ebe83ef

Please sign in to comment.