From 287a38aa02ac6a34ea15dcbc4324d3f51b99c30d Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Mon, 31 Jan 2022 01:50:08 +0100 Subject: [PATCH] [counter] Add template support for counter step Signed-off-by: Knut Ahlers --- action_counter.go | 16 ++++++++++++---- plugins/fieldcollection.go | 2 +- plugins/fieldcollection_test.go | 13 +++++++++++++ 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/action_counter.go b/action_counter.go index 567cb2c..0ed6709 100644 --- a/action_counter.go +++ b/action_counter.go @@ -36,8 +36,8 @@ func init() { Key: "counter_step", Name: "Counter Step", Optional: true, - SupportTemplate: false, - Type: plugins.ActionDocumentationFieldTypeInt64, + SupportTemplate: true, + Type: plugins.ActionDocumentationFieldTypeString, }, { Default: "", @@ -132,8 +132,16 @@ func (a ActorCounter) Execute(c *irc.Client, m *irc.Message, r *plugins.Rule, ev } var counterStep int64 = 1 - if s := attrs.MustInt64("counter_step", ptrIntZero); s != 0 { - counterStep = s + if s := attrs.MustString("counter_step", ptrStringEmpty); s != "" { + parseStep, err := formatMessage(s, m, r, eventData) + if err != nil { + return false, errors.Wrap(err, "execute counter step template") + } + + counterStep, err = strconv.ParseInt(parseStep, 10, 64) + if err != nil { + return false, errors.Wrap(err, "parse counter step") + } } return false, errors.Wrap( diff --git a/plugins/fieldcollection.go b/plugins/fieldcollection.go index 023ab32..e7488fb 100644 --- a/plugins/fieldcollection.go +++ b/plugins/fieldcollection.go @@ -289,7 +289,7 @@ func (f *FieldCollection) String(name string) (string, error) { return iv.String(), nil } - return "", ErrValueMismatch + return fmt.Sprintf("%v", v), nil } // StringSlice tries to read key name as []string diff --git a/plugins/fieldcollection_test.go b/plugins/fieldcollection_test.go index 6331cd1..05b872a 100644 --- a/plugins/fieldcollection_test.go +++ b/plugins/fieldcollection_test.go @@ -78,3 +78,16 @@ func TestFieldCollectionNilDataGet(t *testing.T) { } } } + +func TestFieldCollectionIntToString(t *testing.T) { + var val int = 123 + fc := FieldCollectionFromData(map[string]interface{}{"test": val}) + + if !fc.CanString("test") { + t.Fatalf("cannot convert %T to string", val) + } + + if v := fc.MustString("test", nil); v != "123" { + t.Errorf("unexpected value: 123 != %s", v) + } +}