34 lines
674 B
Go
34 lines
674 B
Go
|
package pb
|
||
|
|
||
|
import (
|
||
|
"io/fs"
|
||
|
"io/ioutil"
|
||
|
|
||
|
"google.golang.org/protobuf/proto"
|
||
|
"google.golang.org/protobuf/reflect/protoreflect"
|
||
|
)
|
||
|
|
||
|
// Read protocol buffer from file
|
||
|
func ReadFile(path string, message protoreflect.ProtoMessage) error {
|
||
|
file, err := ioutil.ReadFile(path)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if err := proto.Unmarshal(file, message); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// Write protocol buffer to file
|
||
|
func WriteFile(path string, message protoreflect.ProtoMessage, perm fs.FileMode) error {
|
||
|
msg, err := proto.Marshal(message)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if err := ioutil.WriteFile(path, msg, perm); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|