You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First, I want to say that I really love this project – it has become my default go-to to handle config files.
Thanks!
Describe the bug
When I'm using my typact library, koanf seems to "forget" some fields.
Given the following config struct:
// Config is the configuration structure.typeConfigstruct {
// Version holds the config version.VersionVersion`koanf:"version"`// ChangelogDir is the relative path to the config file// where all the changelog files are stored.ChangelogDirstring`koanf:"changelog_dir"`// OutputPath is the path to the file where the resulting// file should be stored.//// Defaults to "CHANGELOG.md".OutputPath typact.Option[string] `koanf:"output_path"`
}
When I'm now marshaling the config, the OutputPath will be null regardless whether it contains a value or not.
To Reproduce
// CreateDefault creates a file at path with the contents// of [Default].funcCreateDefault(pathstring, forcebool) error {
k:=koanf.New(".")
defaultCfg:=Config{
Version: "2",
ChangelogDir: ".changelogs",
OutputPath: typact.Some("CHANGELOG.md"),
}
iferr:=k.Load(structs.Provider(defaultCfg, "koanf"), nil); err!=nil {
returnerr
}
tomlRaw, err:=k.Marshal(toml.Parser())
iferr!=nil {
returnerr
}
fmt.Printf("%s\n", string(tomlRaw))
}
The maps.Copy function clones the map but skips over unexported fields.
This behavior is documented in the copystructure module:
// See https://github.com/mitchellh/copystructure/blob/d4ce1f938f7a7ea2a40bff4544b56be9c00b5e84/copystructure.go#L15-L17// ...// Copy returns a deep copy of v.//// Copy is unable to copy unexported fields in a struct (lowercase field names).// Unexported fields can't be reflected by the Go runtime and therefore// copystructure can't perform any data copies.//// ...
This could be addressed by allowing the callee to define a custom map clone function or using a Cloner interface to clone values – if implemented.
The text was updated successfully, but these errors were encountered:
koanf has switched away from the obsolete mitchellh/mapstructure lib to the go-viper fork #331. From the linked comment, it looks like it's not a bug, but documented behaviour.
That said, the structs provider is a simple abstraction on top of fatih/structs -> mapstructure (related: #299). I think the better approach is to write a custom Provider that uses custom cloning logic rather.
First, I want to say that I really love this project – it has become my default go-to to handle config files.
Thanks!
Describe the bug
When I'm using my
typact
library,koanf
seems to "forget" some fields.Given the following config struct:
When I'm now marshaling the config, the
OutputPath
will benull
regardless whether it contains a value or not.To Reproduce
The output will be:
Expected behavior
The output should be
Please provide the following information):
Linux <REDACTED> 6.11.5-arch1-1 #1 SMP PREEMPT_DYNAMIC Tue, 22 Oct 2024 18:31:38 +0000 x86_64 GNU/Linux
Additional context
I debugged the problem and traced it down to the following code:
Koanf.Marshal
method callsko.Raw
Koanf.Raw
method callsmaps.Copy
.maps.Copy
function clones the map but skips over unexported fields.This behavior is documented in the
copystructure
module:This could be addressed by allowing the callee to define a custom map clone function or using a
Cloner
interface to clone values – if implemented.The text was updated successfully, but these errors were encountered: