Skip to content

Commit

Permalink
slacknotifier: optimize object name for comparison summary
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Nov 14, 2024
1 parent 230d7e1 commit 86ac4ee
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
18 changes: 17 additions & 1 deletion pkg/notifier/slacknotifier/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,11 +521,27 @@ func diffsToComment(obj any, diffs []dynamic.Diff) (text string) {
return text
}

text += fmt.Sprintf("%T updated\n", obj)
text += fmt.Sprintf("_%s_ updated\n", objectName(obj))

for _, diff := range diffs {
text += fmt.Sprintf("- %s: `%s` transited to `%s`\n", diff.Field, diff.Before, diff.After)
}

return text
}

var typeNamePrefixRE = regexp.MustCompile("^\\*?([a-zA-Z0-9_]+\\.)?")

Check failure on line 533 in pkg/notifier/slacknotifier/slack.go

View workflow job for this annotation

GitHub Actions / lint

S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple)

func objectName(obj any) string {
type labelInf interface {
Label() string
}

if ll, ok := obj.(labelInf); ok {
return ll.Label()
}

typeName := fmt.Sprintf("%T", obj)
typeName = typeNamePrefixRE.ReplaceAllString(typeName, "")
return typeName
}
22 changes: 22 additions & 0 deletions pkg/notifier/slacknotifier/slack_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package slacknotifier

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/c9s/bbgo/pkg/types"
)

func Test_objectName(t *testing.T) {
t.Run("deposit", func(t *testing.T) {
var deposit = &types.Deposit{}
assert.Equal(t, "Deposit", objectName(deposit))
})

t.Run("local type", func(t *testing.T) {
type A struct{}
var obj = &A{}
assert.Equal(t, "A", objectName(obj))
})
}

0 comments on commit 86ac4ee

Please sign in to comment.