slacknotifier: optimize object name for comparison summary

This commit is contained in:
c9s 2024-11-14 12:06:18 +08:00
parent 230d7e117e
commit 86ac4ee673
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 39 additions and 1 deletions

View File

@ -521,7 +521,7 @@ func diffsToComment(obj any, diffs []dynamic.Diff) (text string) {
return text return text
} }
text += fmt.Sprintf("%T updated\n", obj) text += fmt.Sprintf("_%s_ updated\n", objectName(obj))
for _, diff := range diffs { for _, diff := range diffs {
text += fmt.Sprintf("- %s: `%s` transited to `%s`\n", diff.Field, diff.Before, diff.After) text += fmt.Sprintf("- %s: `%s` transited to `%s`\n", diff.Field, diff.Before, diff.After)
@ -529,3 +529,19 @@ func diffsToComment(obj any, diffs []dynamic.Diff) (text string) {
return text return text
} }
var typeNamePrefixRE = regexp.MustCompile("^\\*?([a-zA-Z0-9_]+\\.)?")
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
}

View File

@ -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))
})
}