give me a review of this func func processOnlySearchableResourceOfType(types ...string)…
- User
give me a review of this func
func processOnlySearchableResourceOfType(types ...string) func(ctx context.Context, entity lumappsevent.Entity) error { return func(ctx context.Context, entity lumappsevent.Entity) error { event, ok := entity.Event.(*lumappsevent.EventSearchableResource) if !ok { return fmt.Errorf( "error while converting eventSearchableResource from %s, entity
%+v, actual type:%+v", entity.Reference.ToString(), entity.Event, reflect.TypeOf(entity.Event).Elem(), ) }if slices.Contains(types, event.Type) { return nil } log.Debug().Msgf("dropping event %s as it is not in the process only list", entity.Reference.Type) return messaging.NewDropError() }}
Assistant
Review of
processOnlySearchableResourceOfType-
Purpose
The function is a factory that returns a handler.
The handler keeps only events whoseTypeis not in the supplied list and drops the rest.
The name therefore feels backwards: it actually drops the listed types and keeps everything else.
If the intention is the opposite (keep only the listed types), the logic is inverted. -
Correctness
• The type-assertion is correct (*lumappsevent.EventSearchableResource).
•slices.Containsis used correctly.
•reflect.TypeOf(entity.Event).Elem()will panic ifentity.Eventisnil; add a nil check or use%T.
•entity.Reference.Typeis logged, but the message says “event … as it is not in the process only list”; the reader might expect the event’s own type (event.Type) to be shown. -
Error handling
• Returning a typed error (messaging.NewDropError()) is idiomatic for a message bus.
• The error returned on failed type assertion is fine, but the format string is noisy; consider a simpler message. -
Naming & readability
• Rename to something that matches the behaviour, e.g.dropSearchableResourceTypesor invert the logic and call itallowOnlySearchableResourceTypes.
• The log message is misleading; log the actualevent.Typeinstead ofentity.Reference.Type. -
Minor style points
•reflect.TypeOf(entity.Event).Elem()can be replaced with%Tin the format string.
• Thetypesslice is captured by closure; if it can be large, passing amap[string]struct{}is more efficient.
Re-written (assuming the intention is to keep only the listed types):
func allowOnlySearchableResourceTypes(allowed ...string) func(ctx context.Context, e lumappsevent.Entity) error { allowedSet := make(map[string]struct{}, len(allowed)) for _, t := range allowed { allowedSet[t] = struct{}{} } return func(ctx context.Context, e lumappsevent.Entity) error { ev, ok := e.Event.(*lumappsevent.EventSearchableResource) if !ok { return fmt.Errorf("unexpected event type %T, want *EventSearchableResource", e.Event) } if _, ok := allowedSet[ev.Type]; ok { return nil // keep } log.Debug(). Str("eventType", ev.Type). Str("ref", e.Reference.ToString()). Msg("dropping event, type not in allow-list") return messaging.NewDropError() } }If the original behaviour (drop the listed types) is correct, rename the function and adjust the log message accordingly.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.