15
15
package protoschema
16
16
17
17
import (
18
- "embed "
18
+ "io/fs "
19
19
"os"
20
20
"path/filepath"
21
21
"strings"
@@ -37,17 +37,22 @@ var (
37
37
38
38
// RegisterAllSourceInfo registers all sourceinfo files under the given output path.
39
39
func RegisterAllSourceInfo (root string ) error {
40
- return filepath .Walk (root , func (path string , info os.FileInfo , err error ) error {
40
+ return RegisterAllSourceInfoFS (os .DirFS (root ), "." )
41
+ }
42
+
43
+ // RegisterAllSourceInfoFS registers all sourceinfo files under the given output path, using the given fs.FS.
44
+ func RegisterAllSourceInfoFS (fsys fs.FS , root string ) error {
45
+ return fs .WalkDir (fsys , root , func (path string , d fs.DirEntry , err error ) error {
41
46
if err != nil {
42
47
return err
43
48
}
44
- if info .IsDir () {
49
+ if d .IsDir () {
45
50
return nil
46
51
}
47
52
if ! strings .HasSuffix (path , pluginsourceinfo .FileExtension ) {
48
53
return nil
49
54
}
50
- data , err := os .ReadFile (path )
55
+ data , err := fs .ReadFile (fsys , path )
51
56
if err != nil {
52
57
return err
53
58
}
@@ -59,15 +64,6 @@ func RegisterAllSourceInfo(root string) error {
59
64
})
60
65
}
61
66
62
- // RegisterEmbeddedSourceInfo registers all sourceinfo files embedded in the given FS.
63
- func RegisterEmbeddedSourceInfo (files embed.FS , root string ) error {
64
- contents , err := files .ReadDir (root )
65
- if err != nil {
66
- return err
67
- }
68
- return registerEmbeddedDirs (files , root , contents , "" )
69
- }
70
-
71
67
func registerSourceInfoData (path string , data []byte ) error {
72
68
sourceInfo := & descriptorpb.SourceCodeInfo {}
73
69
if err := proto .Unmarshal (data , sourceInfo ); err != nil {
@@ -79,30 +75,3 @@ func registerSourceInfoData(path string, data []byte) error {
79
75
sourceinfo .RegisterSourceInfo (protoPath , sourceInfo )
80
76
return nil
81
77
}
82
-
83
- func registerEmbeddedDirs (files embed.FS , root string , dir []os.DirEntry , prefix string ) error {
84
- for _ , entry := range dir {
85
- fullName := filepath .Join (prefix , entry .Name ())
86
- if entry .IsDir () {
87
- subDir , err := files .ReadDir (filepath .Join (root , fullName ))
88
- if err != nil {
89
- return err
90
- }
91
- if err := registerEmbeddedDirs (files , root , subDir , fullName ); err != nil {
92
- return err
93
- }
94
- continue
95
- }
96
- if ! strings .HasSuffix (fullName , pluginsourceinfo .FileExtension ) {
97
- continue
98
- }
99
- data , err := files .ReadFile (filepath .Join (root , fullName ))
100
- if err != nil {
101
- return err
102
- }
103
- if err := registerSourceInfoData (fullName , data ); err != nil {
104
- return err
105
- }
106
- }
107
- return nil
108
- }
0 commit comments