mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-29 03:41:45 +00:00
wallet: marshal before writing to file
Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
parent
50d99464e0
commit
8f196c8222
1 changed files with 16 additions and 5 deletions
|
@ -143,20 +143,31 @@ func (w *Wallet) Path() string {
|
||||||
// that is responsible for saving the data. This can
|
// that is responsible for saving the data. This can
|
||||||
// be a buffer, file, etc..
|
// be a buffer, file, etc..
|
||||||
func (w *Wallet) Save() error {
|
func (w *Wallet) Save() error {
|
||||||
if err := w.rewind(); err != nil {
|
data, err := json.Marshal(w)
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return json.NewEncoder(w.rw).Encode(w)
|
|
||||||
|
return w.writeRaw(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
// savePretty saves wallet in a beautiful JSON.
|
// savePretty saves wallet in a beautiful JSON.
|
||||||
func (w *Wallet) savePretty() error {
|
func (w *Wallet) savePretty() error {
|
||||||
|
data, err := json.MarshalIndent(w, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return w.writeRaw(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Wallet) writeRaw(data []byte) error {
|
||||||
if err := w.rewind(); err != nil {
|
if err := w.rewind(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
enc := json.NewEncoder(w.rw)
|
|
||||||
enc.SetIndent("", " ")
|
_, err := w.rw.Write(data)
|
||||||
return enc.Encode(w)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Wallet) rewind() error {
|
func (w *Wallet) rewind() error {
|
||||||
|
|
Loading…
Reference in a new issue