Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ForceAllTypes to Options #63

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func Generate(input http.FileSystem, opt Options) error {
}

var toc toc
toc.AllTypes = opt.ForceAllTypes
err = findAndWriteFiles(buf, input, &toc)
if err != nil {
return err
Expand All @@ -56,6 +57,7 @@ func Generate(input http.FileSystem, opt Options) error {
type toc struct {
dirs []*dirInfo

AllTypes bool // Force include all types
HasCompressedFile bool // There's at least one compressedFile.
HasFile bool // There's at least one uncompressed file.
}
Expand Down Expand Up @@ -291,7 +293,7 @@ func (fs vfsgen۰FS) Open(path string) (http.File, error) {
return nil, &os.PathError{Op: "open", Path: path, Err: os.ErrNotExist}
}

switch f := f.(type) {{"{"}}{{if .HasCompressedFile}}
switch f := f.(type) {{"{"}}{{if or .AllTypes .HasCompressedFile}}
case *vfsgen۰CompressedFileInfo:
gr, err := gzip.NewReader(bytes.NewReader(f.compressedContent))
if err != nil {
Expand All @@ -301,7 +303,7 @@ func (fs vfsgen۰FS) Open(path string) (http.File, error) {
return &vfsgen۰CompressedFile{
vfsgen۰CompressedFileInfo: f,
gr: gr,
}, nil{{end}}{{if .HasFile}}
}, nil{{end}}{{if or .AllTypes .HasFile}}
case *vfsgen۰FileInfo:
return &vfsgen۰File{
vfsgen۰FileInfo: f,
Expand All @@ -316,7 +318,7 @@ func (fs vfsgen۰FS) Open(path string) (http.File, error) {
panic(fmt.Sprintf("unexpected type %T", f))
}
}
{{if .HasCompressedFile}}
{{if or .AllTypes .HasCompressedFile}}
// vfsgen۰CompressedFileInfo is a static definition of a gzip compressed file.
type vfsgen۰CompressedFileInfo struct {
name string
Expand Down Expand Up @@ -391,7 +393,7 @@ func (f *vfsgen۰CompressedFile) Close() error {
// We already imported "compress/gzip" and "io/ioutil", but ended up not using them. Avoid unused import error.
var _ = gzip.Reader{}
var _ = ioutil.Discard
{{end}}{{if .HasFile}}
{{end}}{{if or .AllTypes .HasFile}}
// vfsgen۰FileInfo is a static definition of an uncompressed file (because it's not worth gzip compressing).
type vfsgen۰FileInfo struct {
name string
Expand Down Expand Up @@ -422,7 +424,7 @@ type vfsgen۰File struct {
func (f *vfsgen۰File) Close() error {
return nil
}
{{else if not .HasCompressedFile}}
{{else if and (not .AllTypes) (not .HasCompressedFile)}}
// We already imported "bytes", but ended up not using it. Avoid unused import error.
var _ = bytes.Reader{}
{{end}}
Expand Down
12 changes: 10 additions & 2 deletions generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,19 @@ func TestGenerate_buildAndGofmt(t *testing.T) {
filename string
fs http.FileSystem
wantError func(error) bool // Nil function means want nil error.
force bool
}{
{
// Empty.
filename: "empty.go",
fs: union.New(nil),
},
{
// Force all types.
filename: "forceall.go",
fs: union.New(nil),
force: true,
},
{
// Test that vfsgen.Generate returns an error when there is
// an error reading from the input filesystem.
Expand Down Expand Up @@ -90,8 +97,9 @@ func TestGenerate_buildAndGofmt(t *testing.T) {
filename := filepath.Join(tempDir, test.filename)

err := vfsgen.Generate(test.fs, vfsgen.Options{
Filename: filename,
PackageName: "test",
Filename: filename,
PackageName: "test",
ForceAllTypes: test.force,
})
switch {
case test.wantError == nil && err != nil:
Expand Down
3 changes: 3 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type Options struct {
// VariableComment is the comment of the http.FileSystem variable in the generated code.
// If left empty, it defaults to "{{.VariableName}} statically implements the virtual filesystem provided to vfsgen.".
VariableComment string

// ForceAllTypes forces the generator to generate all vfsgen types.
ForceAllTypes bool
}

// fillMissing sets default values for mandatory options that are left empty.
Expand Down