mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-04-22 11:09:02 -05:00
f725ddaa65
git-subtree-dir: settings git-subtree-mainline:c26f7b390agit-subtree-split:230545a4a7
40 lines
737 B
Go
40 lines
737 B
Go
package store
|
|
|
|
import (
|
|
"os"
|
|
|
|
"github.com/golang/protobuf/jsonpb"
|
|
"github.com/golang/protobuf/proto"
|
|
)
|
|
|
|
// Unmarshal file into record
|
|
func (s Store) parseRecordFromFile(record proto.Message, filePath string) error {
|
|
file, err := os.Open(filePath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer file.Close()
|
|
|
|
decoder := jsonpb.Unmarshaler{}
|
|
if err = decoder.Unmarshal(file, record); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Marshal record into file
|
|
func (s Store) writeRecordToFile(record proto.Message, filePath string) error {
|
|
file, err := os.Create(filePath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer file.Close()
|
|
|
|
encoder := jsonpb.Marshaler{}
|
|
if err = encoder.Marshal(file, record); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|