-
Notifications
You must be signed in to change notification settings - Fork 83
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
WIP: Generate separate data and common files. #25
base: main
Are you sure you want to change the base?
Conversation
The goal is to resolve #23, to make it possible to use vfsgen multiple times in same package. Previously, that would cause an error because the same types would be defined in each generated file. The approach to solve that is to generate two files: - VFS data file - common type definitions file This avoids the multiple declaration issues in an efficient way. A downside is having 2 files as generated output, instead of one.
Some open questions remaining:
|
💃 💃 💃 |
@shurcooL can we make this an option in vfsgen.Generate(http.Dir("templates"), vfsgen.Options{
PackageName: "static",
Filename: "templates_static.go",
CommonFilename: "vfsgen_common.go",
BuildTags: "production",
VariableName: "TemplatesFS",
}
vfsgen.Generate(http.Dir("css"), vfsgen.Options{
PackageName: "static",
Filename: "css_files_static.go",
CommonFilename: "vfsgen_common.go",
BuildTags: "production",
VariableName: "CssFS",
} and by default, the common stuff would be appended to a single generated file? |
I'm ok with a fixed file name for common parts and as many files as the number of vfsgen.Generate calls. I don't think it's necessary, but maybe you could make the common name configurable by package level variable. |
I'm sure you guys thought of this (so I'm probably misunderstanding the issue), but bear with me. Assuming the common part stays the same (structure definitions and behavior) why isn't it put in a package in this repo and imported from however many generated files? I mean any variability in the trailer seems to be removing some code happens not to be used (no directories, no uncompressed). Are the performance gains of bundling this worth the complexity? I'm perfectly willing to do the legwork if any of this makes sense to you. |
I see in #63 you're considering this. |
I totally agree with @ncruces. There is no point in regenerating structures that are fixed for every instance. And an import of the generating package in a generated file feels pretty natural to me. This is the approach they took for https://github.com/golang/protobuf for instance. I will try to submit a proposal in a few days. |
Please complete this MR, really cumbersome to work around this limitation. Thank you for working on this! 👍 |
I would like to complete the general idea started here, but it will happen no earlier than a few weeks from now, and I plan to start relying on modules to implement the idea. It may make sense to make it a feature of a v2 API, and keep v1 as is. I'll post updates in #23 when I make progress. |
Also looking forward to this (as well as modules support). Thank you! |
I ended up doing my own thing because I had slightly different requirements, but this is the gist of it: A package implementing an in memory file system: github.com/ncruces/go-fs/memfs A command that generates a The generated code imports the above package, declares a single variable, and has an The only slightly ugly bit is that the file system package exports a slightly uglier than I wished function that is intended to be used by static generators, and which skips most validation for performance. Using it incorrectly can lead to panics, and seemingly unrelated errors. |
Anything one can do to help this move forward? Here is my very nasty workaround that I just added after the generator code: // nasty hack to strip duplicate type information
err = exec.Command("sed", "-i", "/^type vfsgen۰FS/,$d", "assets_vfsdata.go").Run()
if err != nil {
log.Fatalln(err)
}
// clean up the unused imports
err = exec.Command("goimports", "-w", ".").Run()
if err != nil {
log.Fatalln(err)
} |
It seems like most people are using separate packages for separate embedded
filesystems.
Go 1.16 is supposed to release this month and will have file embedding,
which does a lot of what vfsgen does.
…On Fri, Feb 5, 2021, 00:17 Henry ***@***.***> wrote:
Anything one can do to help this move forward?
Here is my very nasty workaround that I just added after the generator
code:
// nasty hack to strip duplicate type information
err = exec.Command("sed", "-i", "/^type vfsgen۰FS/,$d", "assets_vfsdata.go").Run()
if err != nil {
log.Fatalln(err)
}
// clean up the unused imports
err = exec.Command("goimports", "-w", ".").Run()
if err != nil {
log.Fatalln(err)
}
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#25 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABOMUPJNB3NCUB6WOKWOQETS5OSS7ANCNFSM4C2SDLWQ>
.
|
@cryptix I expect major future development changes (such as this one) for this project will need to be rebased to consider the Go 1.16's |
Oh wow.. I totally missed the embed feature. Thanks. |
The goal is to resolve #23, to make it possible to use vfsgen multiple times in same package.
Previously, that would cause an error because the same types would be defined in each generated file.
The approach to solve that is to generate two files:
This avoids the multiple declaration issues in an efficient way.
A downside is having 2 files as generated output, instead of one.