Skip to content

Commit

Permalink
Enhance yaml file reading (dynamic instead of fixed buffer size)
Browse files Browse the repository at this point in the history
  • Loading branch information
mengdaming committed Jun 15, 2023
1 parent 27fdd7e commit 30bd6b6
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/utils/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ import (
)

const (
yamlIndent = 2
yamlExtension = "yml"
yamlFileMaxLength = 1024 // Sufficient for language and toolchain config files
yamlIndent = 2
yamlExtension = "yml"
)

// LoadFromYAMLFile loads a structure configuration from a YAML file
Expand All @@ -51,7 +50,19 @@ func LoadFromYAMLFile(filesystem fs.FS, filename string, out any) error {
Trace("Error while opening file: ", errOpen)
return errOpen
}
data := make([]byte, yamlFileMaxLength)
defer func() {
_ = f.Close()
}()

// Retrieve file size
info, errStat := f.Stat()
if errStat != nil {
Trace("Error while retrieving file info: ", errStat)
return errStat
}
size := info.Size() + 1 // one byte for final read at EOF

data := make([]byte, size)
l, errRead := f.Read(data)
if errRead != nil {
Trace("Error while reading file: ", errRead)
Expand Down

0 comments on commit 30bd6b6

Please sign in to comment.