Skip to content

Commit

Permalink
feat: add ToMap function in the Metric interface
Browse files Browse the repository at this point in the history
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
  • Loading branch information
vincenzopalazzo committed Jan 30, 2023
1 parent faff772 commit 3d9d240
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
17 changes: 2 additions & 15 deletions internal/plugin/diagnostic.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package plugin

import (
"encoding/json"
"fmt"

cln4go "github.com/vincenzopalazzo/cln4go/plugin"
Expand All @@ -14,23 +13,11 @@ func NewRawLocalScoreRPC[T MetricsPluginState]() *RawLocalScoreRPC[T] {
}

func (instance *RawLocalScoreRPC[T]) Call(plugin *cln4go.Plugin[T], payload map[string]any) (map[string]any, error) {
metricOne, found := plugin.GetState().GetMetrics()[MetricOneID]
var result map[string]any
metric, found := plugin.GetState().GetMetrics()[RawLocalScoreID]
if !found {
return nil, fmt.Errorf("Metric with id %d not found", 1)
}

// FIXME: improve the metric API to include the ToMap call
resultStr, err := json.Marshal(metricOne)
if err != nil {
return nil, err
}

if err != json.Unmarshal(resultStr, &result) {
return nil, err
}

return result, nil
return metric.ToMap()
}

// ForceUpdateRPC enable the force update command
Expand Down
5 changes: 4 additions & 1 deletion internal/plugin/metric_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

const (
MetricOneID int = 1
RawLocalScoreID int = 1
)

// MetricsSupported mapping the internal id with the name of the metrics.
Expand Down Expand Up @@ -54,6 +54,9 @@ type Metric interface {
// UpdateWithMsg update the metric with the last information fo the node with some msg info
UpdateWithMsg(message *Msg, lightning client.Client) error

// ToMap convert the Metric object into a map
ToMap() (map[string]any, error)

// ToJSON convert the object into a json
ToJSON() (string, error)

Expand Down
14 changes: 14 additions & 0 deletions internal/plugin/metrics_one.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,20 @@ func (instance *MetricOne) OnStop(msg *Msg, lightning cln4go.Client) error {
return nil
}

// ToMap encode the object into a map
func (self *MetricOne) ToMap() (map[string]any, error) {
var result map[string]any
bytes, err := self.Encoder.EncodeToByte(self)
if err != nil {
return nil, err
}

if err != self.Encoder.DecodeFromBytes(bytes, &result) {
return nil, err
}
return result, nil
}

// ToJSON Convert the MetricOne structure to a JSON string.
func (instance *MetricOne) ToJSON() (string, error) {
json, err := instance.Encoder.EncodeToByte(&instance)
Expand Down

0 comments on commit 3d9d240

Please sign in to comment.