Skip to content

MapToWithMapper deletes default values #274

Closed

Description

@normen

Describe the bug
When using MapToWithMapper() non-existing values are deleted, the expected behavior would be to leave those values intact, like advertised in the MapTo() documentation.

To Reproduce
The following code should add any values from defaultCfg that don't exist in the config file to said file, instead it adds all non-existing values as empty.

                defaultCfg := &MyIniStuct{
                  Blah: "value"
                  &SectionStruct:{
                    Meh: "value"
                  }
                }
		if err = ini.MapToWithMapper(defaultCfg, ini.TitleUnderscore, configFilePath); err == nil {
			cfg = ini.Empty()
			if err = ini.ReflectFromWithMapper(cfg, defaultCfg, ini.TitleUnderscore); err == nil {
				err = cfg.SaveTo(configFilePath)
			}
		}

Expected behavior
The function should leave the values that don't exist in the config file alone.

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context
Add any other context about the problem here, or any suggestion to fix the problem.

Thanks for this software, it helps a lot!
Cheers,
Normen

Activity

  1. changed the title [-]MapToWithMapper deletes non-existing values[/-] [+]MapToWithMapper deletes default values[/+] on Nov 24, 2020
  2. normen commented on Nov 24, 2020

    @normen
    Author

    As an update, this does NOT happen when the single sections of the file are MapTo'd

  3. arojoal commented on Sep 7, 2021

    @arojoal

    +1

  4. added
    bug
    on Nov 12, 2021
  5. Narek-Khachikyan commented on Nov 8, 2026

    @Narek-Khachikyan

    I submitted PR #422 to preserve defaults in preinitialized embedded pointers when MapToWithMapper maps an INI file, while still applying keys present in the file. I missed the contribution guide's requirement to comment here before starting.

  6. unknwon commented on Nov 11, 2026

    @unknwon
    Member

    Fixed in v2. MapTo now keeps an embedded struct pointer that is already set, so values the file doesn't have stay as they were. v1 replaced the pointer with a new zero struct.

    cfg := &MyIni{Blah: "value", SectionStruct: &SectionStruct{Meh: "value"}}
    err := ini.MapTo(cfg, configFilePath, ini.NameMapping(ini.TitleUnderscore)) // replaces MapToWithMapper
    // cfg.Meh is still "value" when the file doesn't set it

    Fixed in unknwon.dev/ini/v2, v2.0.0-beta.1 and later. See the migration guide to move from v1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions