Skip to content

Commit 0fef5d3

Browse files
author
Mike Vanbuskirk
authored
Merge pull request #10 from trufflesecurity/make-struct-field-exportable
struct field needs to be exported
2 parents 40ea18d + 0fe0d22 commit 0fef5d3

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

disk_buffer_reader.go

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,30 @@ type DiskBufferReader struct {
2121
index int64
2222
}
2323

24-
// For making New a variadic function
25-
type Options struct {
26-
bufferName string
24+
type config struct{ bufferName string }
25+
26+
// Options for creating a DiskBufferReader
27+
type Options func(*config)
28+
29+
// WithBufferName sets the name of the temporary file.
30+
func WithBufferName(bufferName string) Options {
31+
return func(c *config) {
32+
c.bufferName = bufferName
33+
}
2734
}
2835

2936
// New takes an io.Reader and creates returns an initialized DiskBufferReader.
30-
// Optionally, you can pass a string to give the tmpfile a custom name
37+
// Optionally, you can pass a string(via Options) to give the tempfile a custom name
3138
func New(r io.Reader, opts ...Options) (*DiskBufferReader, error) {
32-
var opt Options
33-
var tmpFile *os.File
34-
var err error
35-
if len(opts) > 0 {
36-
opt = opts[0]
37-
tmpFile, err = ioutil.TempFile(os.TempDir(), opt.bufferName)
38-
if err != nil {
39-
return nil, err
40-
}
41-
} else {
42-
tmpFile, err = ioutil.TempFile(os.TempDir(), "disk-buffer-file")
43-
if err != nil {
44-
return nil, err
45-
}
39+
const defaultBufferName = "disk-buffer-file"
40+
cfg := config{bufferName: defaultBufferName}
41+
for _, o := range opts {
42+
o(&cfg)
43+
}
44+
45+
tmpFile, err := ioutil.TempFile(os.TempDir(), cfg.bufferName)
46+
if err != nil {
47+
return nil, err
4648
}
4749

4850
return &DiskBufferReader{

0 commit comments

Comments
 (0)