-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Iskandarov Lev Andreevich
committed
Apr 20, 2021
1 parent
7deb6c3
commit 5b550fa
Showing
8 changed files
with
239 additions
and
265 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,65 @@ | ||
package offset | ||
|
||
import ( | ||
"io" | ||
"os" | ||
) | ||
|
||
type LoadSaver interface { | ||
Load(io.Reader) error | ||
Save(io.Writer) error | ||
} | ||
|
||
type Offset struct { | ||
Callback LoadSaver | ||
|
||
path string | ||
} | ||
|
||
func NewOffset(path string) *Offset { | ||
return &Offset{path: path} | ||
} | ||
|
||
func (o *Offset) Load() error { | ||
file, err := os.Open(o.path) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
return nil | ||
} | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
if err := o.Callback.Load(file); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (o *Offset) getTmpPath() string { | ||
return o.path + ".tmp" | ||
} | ||
|
||
func (o *Offset) saveToTmp() error { | ||
file, err := os.Create(o.getTmpPath()) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
if err := o.Callback.Save(file); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (o *Offset) Save() error { | ||
if err := o.saveToTmp(); err != nil { | ||
return err | ||
} | ||
if err := os.Rename(o.getTmpPath(), o.path); err != nil { | ||
return err | ||
} | ||
|
||
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,85 @@ | ||
package offset | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type testOffset struct { | ||
Name string `json:"name"` | ||
Value int `json:"value"` | ||
} | ||
|
||
func (o *testOffset) set(name string, value int) { | ||
o.Name = name | ||
o.Value = value | ||
} | ||
|
||
func getTmpPath(t *testing.T, file string) string { | ||
res, err := os.MkdirTemp("", "file.d") | ||
assert.NoError(t, err) | ||
return filepath.Join(res, file) | ||
} | ||
|
||
func TestYAML(t *testing.T) { | ||
offset := testOffset{} | ||
offset.set("some_name", 123) | ||
|
||
buffer := &bytes.Buffer{} | ||
err := (&yamlValue{&offset}).Save(buffer) | ||
assert.NoError(t, err) | ||
|
||
fmt.Println(buffer.String()) | ||
|
||
loaded := testOffset{} | ||
err = (&yamlValue{&loaded}).Load(buffer) | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, offset, loaded) | ||
} | ||
|
||
func TestSaveLoad(t *testing.T) { | ||
path := getTmpPath(t, "offset.yaml") | ||
offset := testOffset{} | ||
offset.set("some_name", 123) | ||
|
||
err := SaveYAML(path, &offset) | ||
assert.NoError(t, err) | ||
|
||
loaded := testOffset{} | ||
err = LoadYAML(path, &loaded) | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, offset, loaded) | ||
} | ||
|
||
func TestAppendFile(t *testing.T) { | ||
path := getTmpPath(t, "offset.yaml") | ||
for i := 1; i < 5; i++ { | ||
offset := testOffset{} | ||
offset.set(fmt.Sprintf("iter_%d", i), i) | ||
|
||
err := SaveYAML(path, &offset) | ||
assert.NoError(t, err) | ||
|
||
loaded := testOffset{} | ||
err = LoadYAML(path, &loaded) | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, offset, loaded) | ||
} | ||
} | ||
|
||
// check, that no errors will happen | ||
func TestNoFile(t *testing.T) { | ||
path := getTmpPath(t, "offset.yaml") | ||
|
||
loaded := testOffset{} | ||
err := LoadYAML(path, &loaded) | ||
assert.NoError(t, err) | ||
} |
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,48 @@ | ||
package offset | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/ghodss/yaml" | ||
) | ||
|
||
type yamlValue struct { | ||
value interface{} | ||
} | ||
|
||
func (o *yamlValue) Load(r io.Reader) error { | ||
b, err := io.ReadAll(r) | ||
if err != nil { | ||
return err | ||
} | ||
if err := yaml.Unmarshal(b, o.value); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (o *yamlValue) Save(w io.Writer) error { | ||
b, err := yaml.Marshal(o.value) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = w.Write(b) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func newYAMLOffset(path string, value interface{}) *Offset { | ||
res := NewOffset(path) | ||
res.Callback = &yamlValue{value} | ||
return res | ||
} | ||
|
||
func LoadYAML(path string, value interface{}) error { | ||
return newYAMLOffset(path, value).Load() | ||
} | ||
|
||
func SaveYAML(path string, value interface{}) error { | ||
return newYAMLOffset(path, value).Save() | ||
} |
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
Oops, something went wrong.