47 lines
822 B
Go
47 lines
822 B
Go
package gateway
|
|
|
|
import (
|
|
"sync"
|
|
|
|
mntxv1 "github.com/tech/sendico/pkg/proto/gateway/mntx/v1"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
type payoutStore struct {
|
|
mu sync.RWMutex
|
|
payouts map[string]*mntxv1.Payout
|
|
}
|
|
|
|
func newPayoutStore() *payoutStore {
|
|
return &payoutStore{
|
|
payouts: make(map[string]*mntxv1.Payout),
|
|
}
|
|
}
|
|
|
|
func (s *payoutStore) Save(p *mntxv1.Payout) {
|
|
if p == nil {
|
|
return
|
|
}
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
s.payouts[p.GetPayoutRef()] = clonePayout(p)
|
|
}
|
|
|
|
func (s *payoutStore) Get(ref string) (*mntxv1.Payout, bool) {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
p, ok := s.payouts[ref]
|
|
return clonePayout(p), ok
|
|
}
|
|
|
|
func clonePayout(p *mntxv1.Payout) *mntxv1.Payout {
|
|
if p == nil {
|
|
return nil
|
|
}
|
|
cloned := proto.Clone(p)
|
|
if cp, ok := cloned.(*mntxv1.Payout); ok {
|
|
return cp
|
|
}
|
|
return nil
|
|
}
|