mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-01-05 11:51:16 -06:00
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
|
|
}
|