Patch bot only if changed (#17766)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
4b04defea6
Коммит
6bb1cbca63
19
model/bot.go
19
model/bot.go
@@ -129,6 +129,8 @@ func BotFromJson(data io.Reader) *Bot {
|
||||
}
|
||||
|
||||
// Patch modifies an existing bot with optional fields from the given patch.
|
||||
// TODO 6.0: consider returning a boolean to indicate whether or not the patch
|
||||
// applied any changes.
|
||||
func (b *Bot) Patch(patch *BotPatch) {
|
||||
if patch.Username != nil {
|
||||
b.Username = *patch.Username
|
||||
@@ -143,6 +145,23 @@ func (b *Bot) Patch(patch *BotPatch) {
|
||||
}
|
||||
}
|
||||
|
||||
// WouldPatch returns whether or not the given patch would be applied or not.
|
||||
func (b *Bot) WouldPatch(patch *BotPatch) bool {
|
||||
if patch == nil {
|
||||
return false
|
||||
}
|
||||
if patch.Username != nil && *patch.Username != b.Username {
|
||||
return true
|
||||
}
|
||||
if patch.DisplayName != nil && *patch.DisplayName != b.DisplayName {
|
||||
return true
|
||||
}
|
||||
if patch.Description != nil && *patch.Description != b.Description {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// ToJson serializes the bot patch to json.
|
||||
func (b *BotPatch) ToJson() []byte {
|
||||
data, err := json.Marshal(b)
|
||||
|
||||
@@ -491,6 +491,40 @@ func TestBotPatch(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBotWouldPatch(t *testing.T) {
|
||||
b := &Bot{
|
||||
UserId: NewId(),
|
||||
}
|
||||
|
||||
t.Run("nil patch", func(t *testing.T) {
|
||||
ok := b.WouldPatch(nil)
|
||||
require.False(t, ok)
|
||||
})
|
||||
|
||||
t.Run("nil patch fields", func(t *testing.T) {
|
||||
patch := &BotPatch{}
|
||||
ok := b.WouldPatch(patch)
|
||||
require.False(t, ok)
|
||||
})
|
||||
|
||||
t.Run("patch", func(t *testing.T) {
|
||||
patch := &BotPatch{
|
||||
DisplayName: NewString("BotName"),
|
||||
}
|
||||
ok := b.WouldPatch(patch)
|
||||
require.True(t, ok)
|
||||
})
|
||||
|
||||
t.Run("no patch", func(t *testing.T) {
|
||||
patch := &BotPatch{
|
||||
DisplayName: NewString("BotName"),
|
||||
}
|
||||
b.Patch(patch)
|
||||
ok := b.WouldPatch(patch)
|
||||
require.False(t, ok)
|
||||
})
|
||||
}
|
||||
|
||||
func TestBotPatchToAndFromJson(t *testing.T) {
|
||||
botPatch1 := &BotPatch{
|
||||
Username: sToP("username"),
|
||||
|
||||
Ссылка в новой задаче
Block a user