Skip to content

Commit 711d9a9

Browse files
amwolffStorj Robot
authored and
Storj Robot
committed
testrand: allow for Metadata().JSON() calls
testrand.Metadata() is never used in any tests. One of the reasons for that is probably because it misses an easy way to transform it into raw bytes. This change adds a JSON method to allow for easy chaining, such as m: = testrand.Metadata().JSON() Change-Id: I89e7638ce4dc33a951f3aca8db03a7392e3707ad
1 parent ada9642 commit 711d9a9

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

testrand/rand.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package testrand
66

77
import (
8+
"encoding/json"
89
"io"
910
"math/rand"
1011

@@ -142,14 +143,28 @@ func BucketName() string {
142143
return string(b)
143144
}
144145

146+
// TestMetadata represents randomly generated metadata. Combined with
147+
// its Metadata constructor and JSON method, it allows for constructions
148+
// in tests such as `m := testrand.Metadata().JSON()`.
149+
type TestMetadata map[string]string
150+
151+
// JSON encodes t to a slice of bytes.
152+
func (t TestMetadata) JSON() []byte {
153+
b, err := json.Marshal(t)
154+
if err != nil {
155+
panic(err)
156+
}
157+
return b
158+
}
159+
145160
// Metadata creates random metadata mostly conforming to the restrictions of S3:
146161
// https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html#object-metadata
147162
//
148163
// NOTE: This may not generate values that cover all valid values (for Storj or
149164
// S3). This is a best effort to cover most cases we believe our design
150165
// requires and will need to be revisited when a more explicit design spec is
151166
// created.
152-
func Metadata() map[string]string {
167+
func Metadata() TestMetadata {
153168
const (
154169
// The actual limit is 2KiB, but there are overheads to encoding and encryption.
155170
max = 1 * 1024

0 commit comments

Comments
 (0)