-
Notifications
You must be signed in to change notification settings - Fork 115
/
util.go
38 lines (34 loc) · 957 Bytes
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package util
import (
"crypto/rand"
"fmt"
"io"
"io/ioutil"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/types/descriptorpb"
)
//ReadProtoSet - Reads protobuf files from disk
func ReadProtoSet(path string) (*protoregistry.Files, error) {
protoSet, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
var fileSet descriptorpb.FileDescriptorSet
if err := proto.Unmarshal(protoSet, &fileSet); err != nil {
return nil, err
}
return protodesc.NewFiles(&fileSet)
}
//UUID4 Generates a new universally unique identifier
func UUID4() (string, error) {
bs := make([]byte, 16)
n, err := io.ReadFull(rand.Reader, bs)
if n != len(bs) || err != nil {
return "", err
}
bs[8] = bs[8]&^0xc0 | 0x80
bs[6] = bs[6]&^0xf0 | 0x40
return fmt.Sprintf("%x-%x-%x-%x-%x", bs[0:4], bs[4:6], bs[6:8], bs[8:10], bs[10:]), nil
}