-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
47 lines (39 loc) · 1.55 KB
/
proxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright 2020-2022 CYBERCRYPT
package io
import (
"context"
)
// Proxy is an IO Provider that wraps other IO Providers
// By default, it forwards calls directly to the implementation,
// but allows you to customize the behavior as you see fit by
// changing the individual functions as you see fit.
type Proxy struct {
Implementation Provider
PutFunc func(ctx context.Context, id []byte, dataType DataType, data []byte) error
GetFunc func(ctx context.Context, id []byte, dataType DataType) ([]byte, error)
UpdateFunc func(ctx context.Context, id []byte, dataType DataType, data []byte) error
DeleteFunc func(ctx context.Context, id []byte, dataType DataType) error
}
func (o *Proxy) Put(ctx context.Context, id []byte, dataType DataType, data []byte) error {
return o.PutFunc(ctx, id, dataType, data)
}
func (o *Proxy) Get(ctx context.Context, id []byte, dataType DataType) ([]byte, error) {
return o.GetFunc(ctx, id, dataType)
}
func (o *Proxy) Update(ctx context.Context, id []byte, dataType DataType, data []byte) error {
return o.UpdateFunc(ctx, id, dataType, data)
}
func (o *Proxy) Delete(ctx context.Context, id []byte, dataType DataType) error {
return o.DeleteFunc(ctx, id, dataType)
}
// NewProxy returns a basic implementation of Proxy that can be used as a basis
// for tests.
func NewProxy(implementation Provider) Proxy {
return Proxy{
Implementation: implementation,
PutFunc: implementation.Put,
GetFunc: implementation.Get,
UpdateFunc: implementation.Update,
DeleteFunc: implementation.Delete,
}
}