-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🍦 serve: Rework NewServer and jig CLI for stacked dirs (#27)
Rework NewServer and jig CLI for stacked dirs. Instead of specifying --protoset=FILE and --method-dir=DIR separately rework `jig server` to load all *.pb files as FileDescriptorSets from the given directories and search for all method stubs in the given directories too. Allow for specifying several directories where the first found match is served. To this end create StackedFS, an implementation of fs.FS, which combines several fs.FS as if they were a single one, again returning the first found match. Pull-Request: #27
- Loading branch information
1 parent
c728565
commit eb2f9c7
Showing
11 changed files
with
186 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package serve | ||
|
||
import ( | ||
"io/fs" | ||
"sort" | ||
) | ||
|
||
type stackedFS []fs.FS | ||
|
||
// Open opens the the first occurrence of named file. | ||
func (s stackedFS) Open(name string) (f fs.File, err error) { | ||
for _, vfs := range s { | ||
if f, err = vfs.Open(name); err == nil { | ||
return f, nil | ||
} | ||
} | ||
return nil, err | ||
} | ||
|
||
// ReadDir combines all files on the stack, sorted by stack order first | ||
// and alphabetically within the stack second. Directories are not merged. | ||
func (s stackedFS) ReadDir(name string) (result []fs.DirEntry, err error) { | ||
seen := map[string]bool{} | ||
for _, vfs := range s { | ||
entries, err := fs.ReadDir(vfs, name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
byName := func(i, j int) bool { return entries[i].Name() < entries[j].Name() } | ||
sort.Slice(entries, byName) | ||
for _, entry := range entries { | ||
if !seen[entry.Name()] { | ||
seen[entry.Name()] = true | ||
result = append(result, entry) | ||
} | ||
} | ||
} | ||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package serve | ||
|
||
import ( | ||
"io/fs" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestStackedFS(t *testing.T) { | ||
aFS := os.DirFS("testdata/stackedfs/a") | ||
bFS := os.DirFS("testdata/stackedfs/b") | ||
stacked := stackedFS{aFS, bFS} | ||
|
||
b, err := fs.ReadFile(stacked, "1.txt") | ||
require.NoError(t, err) | ||
require.Equal(t, "1 in a\n", string(b)) | ||
b, err = fs.ReadFile(stacked, "3.txt") | ||
require.NoError(t, err) | ||
require.Equal(t, "3 in b\n", string(b)) | ||
b, err = fs.ReadFile(stacked, "2.txt") | ||
require.NoError(t, err) | ||
require.Equal(t, "2 in a\n", string(b)) | ||
|
||
entries, err := fs.ReadDir(stacked, ".") | ||
require.NoError(t, err) | ||
require.Equal(t, 4, len(entries)) | ||
var got []string | ||
for _, e := range entries { | ||
got = append(got, e.Name()) | ||
} | ||
want := []string{"1.txt", "2.txt", "4.txt", "3.txt"} | ||
require.Equal(t, want, got) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1 in a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
2 in a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
4 in a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
2 in b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3 in b |