@@ -21,28 +21,30 @@ type DiskBufferReader struct {
21
21
index int64
22
22
}
23
23
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
+ }
27
34
}
28
35
29
36
// 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
31
38
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
46
48
}
47
49
48
50
return & DiskBufferReader {
0 commit comments