Open zzxwill opened 3 years ago
If you replace this
mod := tfconfig.Module{Variables: map[string]*tfconfig.Variable{}}
with
mod := tfconfig.NewModule()
then your example should not panic.
@radeksimko Thanks for the reply. But there is no path
for .tf files as the configuration file is from an API and I don't want to write it to a local path in order to speed the process.
I think nil checking won't do anything bad in the PR, right?
func NewModule(path string) *Module {
I don't know how does your API work, but I believe that LoadModuleFromFilesystem(fs, dir)
is the function you are looking for.
Assuming that you can expose the directory through that interface you should be able to make it work:
The FS
interface is basically a superset of the Go 1.16 io/fs.FS
and File
is an exact copy of io/fs.File
. This is partially because these interfaces were introduced prior to Go 1.16 being released.
We use that exact function inside Terraform LS where files (and their content) also come via API.
I think nil checking won't do anything bad in the PR, right?
AFAICT with the break
statements as-is it would effectively prevent parsing anything when the Module
has uninitialised fields - which - again should not happen as long as you use that constructor. I admit that panic is not ideal, but IMO it's a better side effect than silently not parsing anything.
In Go constructors usually follow a particular naming convention - so I'd expect people to look for New*
functions before attempting to create the struct on their own and running into similar problems. I'm not aware of any decent/easy way of communicating that convention however.
Terraform Configuration
rds.tf
is as below.Try to get all
variable
information but panic happended.It seems that all fields of
tfconfig.Module
has to be set even though I justvariable
from it, or panic will happen.