Skip to content

Commit 957b39b

Browse files
authored
✨ Add a NoOp store (#76)
1 parent 4177ebd commit 957b39b

File tree

5 files changed

+169
-119
lines changed

5 files changed

+169
-119
lines changed

store/content_encoding.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package store
2+
3+
import "fmt"
4+
5+
type ContentEncoding int
6+
7+
const (
8+
ContentEncodingPlain ContentEncoding = iota
9+
ContentEncodingGzip
10+
ContentEncodingZlib
11+
ContentEncodingFlate
12+
)
13+
14+
var contentEncodingStrings = [...]string{
15+
"plain",
16+
"gzip",
17+
"zlib",
18+
"flate",
19+
}
20+
21+
var contentEncodings = map[string]ContentEncoding{
22+
contentEncodingStrings[ContentEncodingPlain]: ContentEncodingPlain,
23+
contentEncodingStrings[ContentEncodingGzip]: ContentEncodingGzip,
24+
contentEncodingStrings[ContentEncodingZlib]: ContentEncodingZlib,
25+
contentEncodingStrings[ContentEncodingFlate]: ContentEncodingFlate,
26+
}
27+
28+
func (ce ContentEncoding) String() string {
29+
if ce < 0 || int(ce) >= len(contentEncodingStrings) {
30+
return unknown
31+
}
32+
return contentEncodingStrings[ce]
33+
}
34+
35+
var contentEncodingFileExtensions = map[ContentEncoding]string{
36+
ContentEncodingPlain: "",
37+
ContentEncodingGzip: "gz",
38+
ContentEncodingZlib: "zlib",
39+
ContentEncodingFlate: "flate",
40+
}
41+
42+
func (ce ContentEncoding) FileExtension() string {
43+
ext, ok := contentEncodingFileExtensions[ce]
44+
if !ok {
45+
return ""
46+
}
47+
return ext
48+
}
49+
50+
func (ce ContentEncoding) FilePath(key string) string {
51+
ext := ce.FileExtension()
52+
if ext == "" {
53+
return key
54+
}
55+
return fmt.Sprintf("%s.%s", key, ext)
56+
}
57+
58+
func ParseContentEncoding(compression string) (ContentEncoding, error) {
59+
if ce, ok := contentEncodings[compression]; ok {
60+
return ce, nil
61+
}
62+
return -1, fmt.Errorf("invalid compression: %s", compression)
63+
}

store/content_type.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package store
2+
3+
import "fmt"
4+
5+
var unknown = "unknown"
6+
7+
type ContentType int
8+
9+
const (
10+
ContentTypeUnknown ContentType = iota
11+
ContentTypeText
12+
ContentTypeJSON
13+
ContentTypeProtobuf
14+
)
15+
16+
var contentTypeStrings = [...]string{
17+
"",
18+
"text/plain",
19+
"application/json",
20+
"application/protobuf",
21+
}
22+
23+
func (ct ContentType) String() string {
24+
if ct < 0 || int(ct) >= len(contentTypeStrings) {
25+
return unknown
26+
}
27+
return contentTypeStrings[ct]
28+
}
29+
30+
var contentTypeFileExtensions = map[ContentType]string{
31+
ContentTypeText: "",
32+
ContentTypeJSON: "json",
33+
ContentTypeProtobuf: "protobuf",
34+
}
35+
36+
func (ct ContentType) FileExtension() string {
37+
ext, ok := contentTypeFileExtensions[ct]
38+
if !ok {
39+
return ""
40+
}
41+
return ext
42+
}
43+
44+
func (ct ContentType) FilePath(key string) string {
45+
ext := ct.FileExtension()
46+
if ext == "" {
47+
return key
48+
}
49+
return fmt.Sprintf("%s.%s", key, ext)
50+
}
51+
52+
var contentTypes = map[string]ContentType{
53+
contentTypeStrings[ContentTypeJSON]: ContentTypeJSON,
54+
contentTypeStrings[ContentTypeProtobuf]: ContentTypeProtobuf,
55+
}
56+
57+
func ParseContentType(contentType string) (ContentType, error) {
58+
if ct, ok := contentTypes[contentType]; ok {
59+
return ct, nil
60+
}
61+
return -1, fmt.Errorf("invalid content type: %s", contentType)
62+
}

store/noop.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package store
2+
3+
import (
4+
"context"
5+
"io"
6+
)
7+
8+
type noOpStore struct{}
9+
10+
// NewNoOpStore returns a new no-op store.
11+
func NewNoOpStore() Store {
12+
return &noOpStore{}
13+
}
14+
15+
func (s *noOpStore) Store(_ context.Context, _ string, _ io.Reader, _ *Headers) error {
16+
return nil
17+
}
18+
func (s *noOpStore) Load(_ context.Context, _ string) (io.ReadCloser, *Headers, error) {
19+
return nil, nil, nil
20+
}
21+
func (s *noOpStore) Delete(_ context.Context, _ string) error { return nil }
22+
func (s *noOpStore) Copy(_ context.Context, _, _ string) error { return nil }

store/noop_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package store
2+
3+
import (
4+
"context"
5+
"strings"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestNoOpStore(t *testing.T) {
13+
store := NewNoOpStore()
14+
require.NotNil(t, store)
15+
16+
assert.NoError(t, store.Store(context.Background(), "test", strings.NewReader("test"), nil))
17+
18+
_, _, err := store.Load(context.Background(), "test")
19+
assert.NoError(t, err)
20+
assert.NoError(t, store.Delete(context.Background(), "test"))
21+
assert.NoError(t, store.Copy(context.Background(), "test", "test2"))
22+
}

store/store.go

Lines changed: 0 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -63,122 +63,3 @@ func (h *Headers) GetContentEncoding() (ContentEncoding, error) {
6363
}
6464
return -1, fmt.Errorf("invalid compression: %s", h.ContentEncoding)
6565
}
66-
67-
var unknown = "unknown"
68-
69-
type ContentType int
70-
71-
const (
72-
ContentTypeUnknown ContentType = iota
73-
ContentTypeText
74-
ContentTypeJSON
75-
ContentTypeProtobuf
76-
)
77-
78-
var contentTypeStrings = [...]string{
79-
"",
80-
"text/plain",
81-
"application/json",
82-
"application/protobuf",
83-
}
84-
85-
func (ct ContentType) String() string {
86-
if ct < 0 || int(ct) >= len(contentTypeStrings) {
87-
return unknown
88-
}
89-
return contentTypeStrings[ct]
90-
}
91-
92-
var contentTypeFileExtensions = map[ContentType]string{
93-
ContentTypeText: "",
94-
ContentTypeJSON: "json",
95-
ContentTypeProtobuf: "protobuf",
96-
}
97-
98-
func (ct ContentType) FileExtension() string {
99-
ext, ok := contentTypeFileExtensions[ct]
100-
if !ok {
101-
return ""
102-
}
103-
return ext
104-
}
105-
106-
func (ct ContentType) FilePath(key string) string {
107-
ext := ct.FileExtension()
108-
if ext == "" {
109-
return key
110-
}
111-
return fmt.Sprintf("%s.%s", key, ext)
112-
}
113-
114-
var contentTypes = map[string]ContentType{
115-
contentTypeStrings[ContentTypeJSON]: ContentTypeJSON,
116-
contentTypeStrings[ContentTypeProtobuf]: ContentTypeProtobuf,
117-
}
118-
119-
func ParseContentType(contentType string) (ContentType, error) {
120-
if ct, ok := contentTypes[contentType]; ok {
121-
return ct, nil
122-
}
123-
return -1, fmt.Errorf("invalid content type: %s", contentType)
124-
}
125-
126-
type ContentEncoding int
127-
128-
const (
129-
ContentEncodingPlain ContentEncoding = iota
130-
ContentEncodingGzip
131-
ContentEncodingZlib
132-
ContentEncodingFlate
133-
)
134-
135-
var contentEncodingStrings = [...]string{
136-
"plain",
137-
"gzip",
138-
"zlib",
139-
"flate",
140-
}
141-
142-
var contentEncodings = map[string]ContentEncoding{
143-
contentEncodingStrings[ContentEncodingPlain]: ContentEncodingPlain,
144-
contentEncodingStrings[ContentEncodingGzip]: ContentEncodingGzip,
145-
contentEncodingStrings[ContentEncodingZlib]: ContentEncodingZlib,
146-
contentEncodingStrings[ContentEncodingFlate]: ContentEncodingFlate,
147-
}
148-
149-
func (ce ContentEncoding) String() string {
150-
if ce < 0 || int(ce) >= len(contentEncodingStrings) {
151-
return unknown
152-
}
153-
return contentEncodingStrings[ce]
154-
}
155-
156-
var contentEncodingFileExtensions = map[ContentEncoding]string{
157-
ContentEncodingPlain: "",
158-
ContentEncodingGzip: "gz",
159-
ContentEncodingZlib: "zlib",
160-
ContentEncodingFlate: "flate",
161-
}
162-
163-
func (ce ContentEncoding) FileExtension() string {
164-
ext, ok := contentEncodingFileExtensions[ce]
165-
if !ok {
166-
return ""
167-
}
168-
return ext
169-
}
170-
171-
func (ce ContentEncoding) FilePath(key string) string {
172-
ext := ce.FileExtension()
173-
if ext == "" {
174-
return key
175-
}
176-
return fmt.Sprintf("%s.%s", key, ext)
177-
}
178-
179-
func ParseContentEncoding(compression string) (ContentEncoding, error) {
180-
if ce, ok := contentEncodings[compression]; ok {
181-
return ce, nil
182-
}
183-
return -1, fmt.Errorf("invalid compression: %s", compression)
184-
}

0 commit comments

Comments
 (0)