Last active
May 24, 2022 13:22
-
-
Save bbrodriges/cf45c5e82ec995e3379ff6963b758709 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package store | |
import ( | |
"context" | |
"errors" | |
"fmt" | |
"net/url" | |
"github.com/gofrs/uuid" | |
) | |
type InMemory struct { | |
store map[string]*url.URL | |
userStore map[string]map[string]*url.URL | |
} | |
// NewInMemory create new InMemory instance | |
func NewInMemory() *InMemory { | |
return &InMemory{ | |
store: make(map[string]*url.URL), | |
userStore: make(map[string]map[string]*url.URL), | |
} | |
} | |
func (m *InMemory) Save(_ context.Context, u *url.URL) (id string, err error) { | |
id = fmt.Sprintf("%x", len(m.store)) | |
m.store[id] = u | |
return id, nil | |
} | |
func (m *InMemory) SaveUser(ctx context.Context, uid uuid.UUID, u *url.URL) (id string, err error) { | |
id, err = m.Save(ctx, u) | |
if err != nil { | |
return "", fmt.Errorf("cannot save URL to shared store: %w", err) | |
} | |
if _, ok := m.userStore[uid.String()]; !ok { | |
m.userStore[uid.String()] = make(map[string]*url.URL) | |
} | |
m.userStore[uid.String()][id] = u | |
return id, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment