Change the store interface to remove variadic args (#1095)
This commit is contained in:
@@ -106,15 +106,19 @@ func (w *workersKV) Init(opts ...store.Option) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// In the cloudflare workers KV implemention, List() doesn't guarantee
|
||||
// anything as the workers API is eventually consistent.
|
||||
func (w *workersKV) List() ([]*store.Record, error) {
|
||||
func (w *workersKV) list(prefix string) ([]string, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
path := fmt.Sprintf("accounts/%s/storage/kv/namespaces/%s/keys", w.account, w.namespace)
|
||||
|
||||
response, _, _, err := w.request(ctx, http.MethodGet, path, nil, make(http.Header))
|
||||
body := make(map[string]string)
|
||||
|
||||
if len(prefix) > 0 {
|
||||
body["prefix"] = prefix
|
||||
}
|
||||
|
||||
response, _, _, err := w.request(ctx, http.MethodGet, path, body, make(http.Header))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -138,13 +142,51 @@ func (w *workersKV) List() ([]*store.Record, error) {
|
||||
keys = append(keys, r.Name)
|
||||
}
|
||||
|
||||
return w.Read(keys...)
|
||||
return keys, nil
|
||||
}
|
||||
|
||||
func (w *workersKV) Read(keys ...string) ([]*store.Record, error) {
|
||||
// In the cloudflare workers KV implemention, List() doesn't guarantee
|
||||
// anything as the workers API is eventually consistent.
|
||||
func (w *workersKV) List() ([]*store.Record, error) {
|
||||
keys, err := w.list("")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var gerr error
|
||||
var records []*store.Record
|
||||
|
||||
for _, key := range keys {
|
||||
r, err := w.Read(key)
|
||||
if err != nil {
|
||||
gerr = err
|
||||
continue
|
||||
}
|
||||
records = append(records, r...)
|
||||
}
|
||||
|
||||
return records, gerr
|
||||
}
|
||||
|
||||
func (w *workersKV) Read(key string, opts ...store.ReadOption) ([]*store.Record, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
var options store.ReadOptions
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
keys := []string{key}
|
||||
|
||||
if options.Prefix {
|
||||
k, err := w.list(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
keys = k
|
||||
}
|
||||
|
||||
//nolint:prealloc
|
||||
var records []*store.Record
|
||||
|
||||
@@ -174,65 +216,61 @@ func (w *workersKV) Read(keys ...string) ([]*store.Record, error) {
|
||||
return records, nil
|
||||
}
|
||||
|
||||
func (w *workersKV) Write(records ...*store.Record) error {
|
||||
func (w *workersKV) Write(r *store.Record) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
for _, r := range records {
|
||||
path := fmt.Sprintf("accounts/%s/storage/kv/namespaces/%s/values/%s", w.account, w.namespace, url.PathEscape(r.Key))
|
||||
if r.Expiry != 0 {
|
||||
// Minimum cloudflare TTL is 60 Seconds
|
||||
exp := int(math.Max(60, math.Round(r.Expiry.Seconds())))
|
||||
path = path + "?expiration_ttl=" + strconv.Itoa(exp)
|
||||
}
|
||||
path := fmt.Sprintf("accounts/%s/storage/kv/namespaces/%s/values/%s", w.account, w.namespace, url.PathEscape(r.Key))
|
||||
if r.Expiry != 0 {
|
||||
// Minimum cloudflare TTL is 60 Seconds
|
||||
exp := int(math.Max(60, math.Round(r.Expiry.Seconds())))
|
||||
path = path + "?expiration_ttl=" + strconv.Itoa(exp)
|
||||
}
|
||||
|
||||
headers := make(http.Header)
|
||||
headers := make(http.Header)
|
||||
|
||||
resp, _, _, err := w.request(ctx, http.MethodPut, path, r.Value, headers)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp, _, _, err := w.request(ctx, http.MethodPut, path, r.Value, headers)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
a := &apiResponse{}
|
||||
if err := json.Unmarshal(resp, a); err != nil {
|
||||
return err
|
||||
}
|
||||
a := &apiResponse{}
|
||||
if err := json.Unmarshal(resp, a); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !a.Success {
|
||||
messages := ""
|
||||
for _, m := range a.Errors {
|
||||
messages += strconv.Itoa(m.Code) + " " + m.Message + "\n"
|
||||
}
|
||||
return errors.New(messages)
|
||||
if !a.Success {
|
||||
messages := ""
|
||||
for _, m := range a.Errors {
|
||||
messages += strconv.Itoa(m.Code) + " " + m.Message + "\n"
|
||||
}
|
||||
return errors.New(messages)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *workersKV) Delete(keys ...string) error {
|
||||
func (w *workersKV) Delete(key string) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
for _, k := range keys {
|
||||
path := fmt.Sprintf("accounts/%s/storage/kv/namespaces/%s/values/%s", w.account, w.namespace, url.PathEscape(k))
|
||||
resp, _, _, err := w.request(ctx, http.MethodDelete, path, nil, make(http.Header))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
path := fmt.Sprintf("accounts/%s/storage/kv/namespaces/%s/values/%s", w.account, w.namespace, url.PathEscape(key))
|
||||
resp, _, _, err := w.request(ctx, http.MethodDelete, path, nil, make(http.Header))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
a := &apiResponse{}
|
||||
if err := json.Unmarshal(resp, a); err != nil {
|
||||
return err
|
||||
}
|
||||
a := &apiResponse{}
|
||||
if err := json.Unmarshal(resp, a); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !a.Success {
|
||||
messages := ""
|
||||
for _, m := range a.Errors {
|
||||
messages += strconv.Itoa(m.Code) + " " + m.Message + "\n"
|
||||
}
|
||||
return errors.New(messages)
|
||||
if !a.Success {
|
||||
messages := ""
|
||||
for _, m := range a.Errors {
|
||||
messages += strconv.Itoa(m.Code) + " " + m.Message + "\n"
|
||||
}
|
||||
return errors.New(messages)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@@ -33,17 +33,18 @@ func TestCloudflare(t *testing.T) {
|
||||
t.Log("Listed " + strconv.Itoa(len(records)) + " records")
|
||||
}
|
||||
|
||||
err = wkv.Write(
|
||||
&store.Record{
|
||||
Key: randomK,
|
||||
Value: []byte(randomV),
|
||||
},
|
||||
&store.Record{
|
||||
Key: "expirationtest",
|
||||
Value: []byte("This message will self destruct"),
|
||||
Expiry: 75 * time.Second,
|
||||
},
|
||||
)
|
||||
err = wkv.Write(&store.Record{
|
||||
Key: randomK,
|
||||
Value: []byte(randomV),
|
||||
})
|
||||
if err != nil {
|
||||
t.Errorf("Write: %s", err.Error())
|
||||
}
|
||||
err = wkv.Write(&store.Record{
|
||||
Key: "expirationtest",
|
||||
Value: []byte("This message will self destruct"),
|
||||
Expiry: 75 * time.Second,
|
||||
})
|
||||
if err != nil {
|
||||
t.Errorf("Write: %s", err.Error())
|
||||
}
|
||||
|
Reference in New Issue
Block a user