withInstance extracts the tenant ID from the context, gets the instance, and calls the provided function if the instance exists. If the instance doesn't exist, it returns the zero value.
(ctx context.Context, s *LiveStore, fn func(*instance) (*T, error))
| 919 | // and calls the provided function if the instance exists. If the instance |
| 920 | // doesn't exist, it returns the zero value. |
| 921 | func withInstance[T any](ctx context.Context, s *LiveStore, fn func(*instance) (*T, error)) (*T, error) { |
| 922 | var defaultValue T |
| 923 | |
| 924 | // Check readiness before processing query |
| 925 | if err := s.CheckReady(ctx); err != nil { |
| 926 | return &defaultValue, err |
| 927 | } |
| 928 | |
| 929 | instanceID, err := validation.ExtractValidTenantID(ctx) |
| 930 | if err != nil { |
| 931 | return &defaultValue, err |
| 932 | } |
| 933 | |
| 934 | inst, found := s.getInstance(instanceID) |
| 935 | if inst == nil || !found { |
| 936 | return &defaultValue, nil |
| 937 | } |
| 938 | |
| 939 | return fn(inst) |
| 940 | } |
no test coverage detected