Skip to content

Commit 9c31ed9

Browse files
committed
Switch to fs.FS
1 parent fc431ec commit 9c31ed9

File tree

2 files changed

+10
-41
lines changed

2 files changed

+10
-41
lines changed

sourceinfo.go

+9-40
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package protoschema
1616

1717
import (
18-
"embed"
18+
"io/fs"
1919
"os"
2020
"path/filepath"
2121
"strings"
@@ -37,17 +37,22 @@ var (
3737

3838
// RegisterAllSourceInfo registers all sourceinfo files under the given output path.
3939
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 {
4146
if err != nil {
4247
return err
4348
}
44-
if info.IsDir() {
49+
if d.IsDir() {
4550
return nil
4651
}
4752
if !strings.HasSuffix(path, pluginsourceinfo.FileExtension) {
4853
return nil
4954
}
50-
data, err := os.ReadFile(path)
55+
data, err := fs.ReadFile(fsys, path)
5156
if err != nil {
5257
return err
5358
}
@@ -59,15 +64,6 @@ func RegisterAllSourceInfo(root string) error {
5964
})
6065
}
6166

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-
7167
func registerSourceInfoData(path string, data []byte) error {
7268
sourceInfo := &descriptorpb.SourceCodeInfo{}
7369
if err := proto.Unmarshal(data, sourceInfo); err != nil {
@@ -79,30 +75,3 @@ func registerSourceInfoData(path string, data []byte) error {
7975
sourceinfo.RegisterSourceInfo(protoPath, sourceInfo)
8076
return nil
8177
}
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-
}

sourceinfo_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var sourceInfoTestData embed.FS
2828

2929
func TestEmbeddedSourceInfo(t *testing.T) {
3030
t.Parallel()
31-
err := RegisterEmbeddedSourceInfo(sourceInfoTestData, "internal/testdata/sourceinfo")
31+
err := RegisterAllSourceInfoFS(sourceInfoTestData, "internal/testdata/sourceinfo")
3232
require.NoError(t, err)
3333

3434
msgType, err := SourceInfoGlobalTypes.FindMessageByName(

0 commit comments

Comments
 (0)