-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-85: DataSource basic implementations
- Loading branch information
Showing
8 changed files
with
199 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (c) 2018 Yandex LLC. All rights reserved. | ||
// Use of this source code is governed by a MPL 2.0 | ||
// license that can be found in the LICENSE file. | ||
// Author: Vladimir Skipor <[email protected]> | ||
|
||
package coretest | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/spf13/afero" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/yandex/pandora/core" | ||
"github.com/yandex/pandora/lib/testutil2" | ||
) | ||
|
||
func AssertSourceEqualStdStream(t *testing.T, expectedPtr **os.File, getSource func() core.DataSource) { | ||
temp, err := ioutil.TempFile("", "") | ||
require.NoError(t, err) | ||
|
||
backup := *expectedPtr | ||
defer func() { | ||
*expectedPtr = backup | ||
}() | ||
*expectedPtr = temp | ||
const testdata = "abcd" | ||
_, err = io.WriteString(temp, testdata) | ||
require.NoError(t, err) | ||
|
||
rc, err := getSource().OpenSource() | ||
require.NoError(t, err) | ||
|
||
err = rc.Close() | ||
require.NoError(t, err, "std stream should not be closed") | ||
|
||
temp.Seek(0, io.SeekStart) | ||
data, err := ioutil.ReadAll(temp) | ||
assert.Equal(t, testdata, string(data)) | ||
} | ||
|
||
func AssertSourceEqualFile(t *testing.T, fs afero.Fs, filename string, source core.DataSource) { | ||
const testdata = "abcd" | ||
afero.WriteFile(fs, filename, []byte(testdata), 644) | ||
|
||
rc, err := source.OpenSource() | ||
require.NoError(t, err) | ||
|
||
data := testutil2.ReadString(t, rc) | ||
err = rc.Close() | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, testdata, data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) 2018 Yandex LLC. All rights reserved. | ||
// Use of this source code is governed by a MPL 2.0 | ||
// license that can be found in the LICENSE file. | ||
// Author: Vladimir Skipor <[email protected]> | ||
|
||
package datasource | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
"github.com/spf13/afero" | ||
|
||
"github.com/yandex/pandora/core" | ||
) | ||
|
||
// TODO(skipor): auto unzip | ||
type FileConfig struct { | ||
Path string `config:"path" validate:"required"` | ||
} | ||
|
||
func NewFile(fs afero.Fs, conf FileConfig) core.DataSource { | ||
return &fileSource{afero.Afero{fs}, conf} | ||
} | ||
|
||
type fileSource struct { | ||
fs afero.Afero | ||
conf FileConfig | ||
} | ||
|
||
func (s *fileSource) OpenSource() (wc io.ReadCloser, err error) { | ||
return s.fs.Open(s.conf.Path) | ||
} | ||
|
||
func NewStdin() core.DataSource { | ||
return hideCloseFileSource{os.Stdin} | ||
} | ||
|
||
type hideCloseFileSource struct{ afero.File } | ||
|
||
func (f hideCloseFileSource) OpenSource() (wc io.ReadCloser, err error) { | ||
return f, nil | ||
} | ||
|
||
func (f hideCloseFileSource) Close() error { return nil } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) 2018 Yandex LLC. All rights reserved. | ||
// Use of this source code is governed by a MPL 2.0 | ||
// license that can be found in the LICENSE file. | ||
// Author: Vladimir Skipor <[email protected]> | ||
|
||
package datasource | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/spf13/afero" | ||
"github.com/yandex/pandora/core/coretest" | ||
) | ||
|
||
func TestFileSource(t *testing.T) { | ||
const filename = "/xxx/yyy" | ||
fs := afero.NewMemMapFs() | ||
source := NewFile(fs, FileConfig{Path: filename}) | ||
coretest.AssertSourceEqualFile(t, fs, filename, source) | ||
} | ||
|
||
func TestStdin(t *testing.T) { | ||
coretest.AssertSourceEqualStdStream(t, &os.Stdout, NewStdin) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) 2018 Yandex LLC. All rights reserved. | ||
// Use of this source code is governed by a MPL 2.0 | ||
// license that can be found in the LICENSE file. | ||
// Author: Vladimir Skipor <[email protected]> | ||
|
||
package datasource | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"io/ioutil" | ||
|
||
"github.com/yandex/pandora/core" | ||
"github.com/yandex/pandora/lib/ioutil2" | ||
) | ||
|
||
func NewBuffer(buf *bytes.Buffer) core.DataSource { | ||
return buffer{Buffer: buf} | ||
} | ||
|
||
type buffer struct { | ||
*bytes.Buffer | ||
ioutil2.NopCloser | ||
} | ||
|
||
func (b buffer) OpenSource() (wc io.ReadCloser, err error) { | ||
return b, nil | ||
} | ||
|
||
// NewReader returns dummy core.DataSource that returns it on OpenSource call, wrapping it | ||
// ioutil.NopCloser if r is not io.Closer. | ||
// NONE(skipor): such wrapping hide | ||
func NewReader(r io.Reader) core.DataSource { | ||
return &reader{r} | ||
} | ||
|
||
type reader struct { | ||
source io.Reader | ||
} | ||
|
||
func (r *reader) OpenSource() (rc io.ReadCloser, err error) { | ||
if rc, ok := r.source.(io.ReadCloser); ok { | ||
return rc, nil | ||
} | ||
return ioutil.NopCloser(r.source), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) 2018 Yandex LLC. All rights reserved. | ||
// Use of this source code is governed by a MPL 2.0 | ||
// license that can be found in the LICENSE file. | ||
// Author: Vladimir Skipor <[email protected]> | ||
|
||
package ioutil2 | ||
|
||
// NopCloser may be embedded to any struct to implement io.Closer doing nothing on closer. | ||
type NopCloser struct{} | ||
|
||
func (NopCloser) Close() error { return nil } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters