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

LoadData doesn't merge configs like LoadFiles #166

Closed
Adzikus opened this issue Sep 11, 2023 · 2 comments
Closed

LoadData doesn't merge configs like LoadFiles #166

Adzikus opened this issue Sep 11, 2023 · 2 comments
Assignees
Labels
question Further information is requested resolved

Comments

@Adzikus
Copy link

Adzikus commented Sep 11, 2023

System (please complete the following information):

  • OS: windows
  • GO Version: 1.20
  • Pkg Version: 2.2.3

Describe the bug

When loading config from Files

paths := []string{"base.json", "mod.json"}
err = tmpCfg.LoadFiles(paths...)

configuration is appended but when using LoadData() is overriding by last source

To Reproduce

// MODELS

type PostgresConfig struct {
// cut for simplicity
	Postgres *Postgres
}

type Postgres struct {
	Host         string
	Port         int
	User         string
	Password     string
	PasswordFile string
	Database     string
	SearchPath   string
	MinPoolSize  int32
	MaxPoolSize  int32 
}

// GOT
initConfig := map[string]any{
 "Postgres": &Postgres{
 Host:         "init-host",
 Port:         1000,
 User:         "init-user",
 Password:     "init-password",
 PasswordFile: "init-password-file",
 Database:     "init-database",
 SearchPath:   "init-search-path",
 MinPoolSize:  10,
 MaxPoolSize:  100,
 }}
appendConfig := map[string]any{
 "Postgres": &Postgres{Host: "append-host"}}

cfg := config.New("")
result := &PostgresConfig{}
cfg.LoadData(initConfig, appendConfig)
cfg.Decode(result)

Current behavior

image

Expected behavior

LoadData Merges all data loaded by parameter list
image

Additional context

If needed some more explanation, code or screens, fell free to write.
If there is any other method to load data like from files but from source maps or maybe there is possibility to create something like Merge(any...)

@inhere inhere added the bug Something isn't working label Sep 11, 2023
@yz3358
Copy link

yz3358 commented Sep 28, 2023

I don't think this is a bug. The equivalent of LoadData here should be loading map[string]any, without pointer. Becasue when you create struct, zero values are assigned automatically.

When LoadFile, contents are converted to map[string]any instead of a predefined struct.

LoadData uses mergo.Merge(..., merge.WithOverride). It will override "non-empty" attributes, which is desired in most use cases. (e.g. intentional override some int field to 0).

I did not find a way to differentiate "user assigned zero value" vs "automatic zero value" in a struct. If there is, then adding a custom mergo.Transformers might do.

@inhere inhere added question Further information is requested resolved and removed bug Something isn't working labels Jan 13, 2024
@inhere
Copy link
Member

inhere commented Jan 13, 2024

hi @yz3358 @Adzikus On https://github.com/gookit/config/releases/tag/v2.2.5, add new option: MergeOptions support custom the data merge options

@inhere inhere closed this as completed Jan 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested resolved
Projects
None yet
Development

No branches or pull requests

3 participants