Skip to content

Commit

Permalink
fix(repositories): correctly handle slices in squirrel update builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Satont committed Dec 19, 2024
1 parent 69da021 commit c4aadf1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 33 deletions.
37 changes: 26 additions & 11 deletions apps/api-gql/internal/delivery/gql/resolvers/commands.resolver.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -144,29 +144,31 @@ func (c *Service) Update(

newCmd.Command = newDbCmd

for _, r := range cmd.Responses {
err := c.responsesRepository.Delete(trCtx, r.ID)
if err != nil {
return err
if input.Responses != nil {
for _, r := range cmd.Responses {
err := c.responsesRepository.Delete(trCtx, r.ID)
if err != nil {
return err
}
}
}

newCmd.Responses = make([]responsemodel.Response, 0, len(cmd.Responses))
for _, r := range input.Responses {
newResponse, err := c.responsesRepository.Create(
trCtx,
commands_response.CreateInput{
CommandID: newDbCmd.ID,
Text: r.Text,
Order: r.Order,
TwitchCategoryIDs: r.TwitchCategoryIDs,
},
)
if err != nil {
return err
newCmd.Responses = make([]responsemodel.Response, 0, len(cmd.Responses))
for _, r := range input.Responses {
newResponse, err := c.responsesRepository.Create(
trCtx,
commands_response.CreateInput{
CommandID: newDbCmd.ID,
Text: r.Text,
Order: r.Order,
TwitchCategoryIDs: r.TwitchCategoryIDs,
},
)
if err != nil {
return err
}

newCmd.Responses = append(newCmd.Responses, newResponse)
}

newCmd.Responses = append(newCmd.Responses, newResponse)
}

return nil
Expand Down
12 changes: 10 additions & 2 deletions libs/repositories/squirrel_patch_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ func SquirrelApplyPatch(

// isNil checks if an interface value is nil or contains a nil dynamic value
func isNil(value interface{}) bool {
// Use reflection to detect nil dynamic value
// Use reflection to detect nil dynamic value or nil slice
v := reflect.ValueOf(value)
return !v.IsValid() || (v.Kind() == reflect.Ptr && v.IsNil())
if !v.IsValid() {
return true
}
switch v.Kind() {
case reflect.Ptr, reflect.Interface, reflect.Slice, reflect.Map, reflect.Chan, reflect.Func:
return v.IsNil()
default:
return false
}
}

0 comments on commit c4aadf1

Please sign in to comment.