mirror of
https://github.com/hanzo-fi/stack.git
synced 2026-10-30 20:34:43 +00:00
40 lines
803 B
Go
40 lines
803 B
Go
package task
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/formancehq/payments/cmd/connectors/internal/storage"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type StateResolver interface {
|
|
ResolveTo(ctx context.Context, v any) error
|
|
}
|
|
type StateResolverFn func(ctx context.Context, v any) error
|
|
|
|
func (fn StateResolverFn) ResolveTo(ctx context.Context, v any) error {
|
|
return fn(ctx, v)
|
|
}
|
|
|
|
func ResolveTo[State any](ctx context.Context, resolver StateResolver, to *State) (*State, error) {
|
|
err := resolver.ResolveTo(ctx, to)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return to, nil
|
|
}
|
|
|
|
func MustResolveTo[State any](ctx context.Context, resolver StateResolver, to State) State {
|
|
state, err := ResolveTo(ctx, resolver, &to)
|
|
if errors.Is(err, storage.ErrNotFound) {
|
|
return to
|
|
}
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return *state
|
|
}
|