-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.go
90 lines (81 loc) · 2.1 KB
/
utils.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package protobuf
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"unsafe"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
func UnsafeBytes(str string) []byte {
hdr := *(*reflect.StringHeader)(unsafe.Pointer(&str))
return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
Data: hdr.Data,
Len: hdr.Len,
Cap: hdr.Len,
}))
}
func UnsafeString(bytes []byte) string {
hdr := *(*reflect.SliceHeader)(unsafe.Pointer(&bytes))
return *(*string)(unsafe.Pointer(&reflect.StringHeader{
Data: hdr.Data,
Len: hdr.Len,
}))
}
func bool2char(b bool) byte {
if b {
return 1
}
return 0
}
func MessageToJson(v proto.Message, pretty ...bool) string {
ops := protojson.MarshalOptions{Multiline: len(pretty) > 0 && pretty[0]}
return ops.Format(v)
}
func NewProtobufDiskSourceTree(dir string) (map[string][]byte, error) {
return NewDiskSourceTree(dir, func(filename string) bool {
return filepath.Ext(filename) == ".proto"
})
}
func NewDiskSourceTree(dir string, filter func(filename string) bool) (map[string][]byte, error) {
dir, err := filepath.Abs(dir)
if err != nil {
return nil, fmt.Errorf(`filepath.Abs("%s") return err: %v`, dir, err)
}
files, err := lookupFiles(dir, filter)
if err != nil {
return nil, err
}
r := make(map[string][]byte, len(files))
for _, filename := range files {
rel, err := filepath.Rel(dir, filename)
if err != nil {
return nil, fmt.Errorf(`filepath.Rel("%s","%s") return err: %v`, dir, filename, err)
}
fileContent, err := ioutil.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf(`ioutil.ReadFile("%s") return err: %v`, filename, err)
}
r[rel] = fileContent
}
return r, nil
}
// dir: must abs path
func lookupFiles(dir string, filter func(filename string) bool) ([]string, error) {
files := make([]string, 0)
if err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if info == nil || info.IsDir() {
return nil
}
if filter != nil && !filter(path) {
return nil
}
files = append(files, path)
return nil
}); err != nil {
return nil, err
}
return files, nil
}