toml icon indicating copy to clipboard operation
toml copied to clipboard

Behavior change between 0.1.0 and 0.1.1

Open davidnewhall opened this issue 7 years ago • 3 comments

If this were a major version change, I'd accept that "things change" but since it was a minor revision, I feel compelled to point out my now-broken use-case with this library. Maybe someone can correct my flaws and show me a valid way to do this. Or we can identify this as some sort of regression.

type tomlConfig struct {
	IncludeDir string
	Services []*Config
}

type Config struct {
	Name string
	Paths []string
}

func loadConfigFile(configFile string) (tomlConfig, error) {
	var config tomlConfig
	if buf, err := ioutil.ReadFile(configFile); err != nil {
		return config, err
	} else if err := toml.Unmarshal(buf, &config); err != nil {
		return config, err
	} else if config.IncludeDir == "" {
		return config, nil
	}

	// include_dir is not empty, read in more config files from a conf.d folder.
	includedConfigFiles, err := ioutil.ReadDir(config.IncludeDir)
	if err != nil {
		return config, err
	}

	for _, includeFile := range includedConfigFiles {
		filePath := filepath.Join(config.IncludeDir, includeFile.Name())
		if !strings.HasSuffix(filePath, ".conf") {
			continue // Only read files that end with .conf.
		} else if buf, err := ioutil.ReadFile(filePath); err != nil {
			return config, err
		} else if err := toml.Unmarshal(buf, &config); err != nil {
			return config, err
		}
	}
	return config, nil
}

The above code works great with toml library 0.1.0. The Services slice is appended to for each [[services]] found in the included config files. In version 0.1.1 the slice only contains the data from the last config file Unmarshal()'d. Thoughts?

davidnewhall avatar May 04 '18 01:05 davidnewhall

I've worked around this by using a new tomlConfig for each loop iteration and appending the things I care about to the originally unmarshal'd dataset. I assume I was using a bug in the original code that was failing to clear out defaults. Providing a toml.UnmarshalAppend() function would be nifty so the above pattern can still be used. No priority from me here. Thanks!

davidnewhall avatar May 04 '18 17:05 davidnewhall

Just checked and you're right: the behavior did change. I think the new behavior is more correct, but a config option could be added to disable 'clearing' of slices and maps. Are you up for implementing that?

fjl avatar Jul 24 '18 13:07 fjl

Sorry, I just wound up using burnt sushi.

davidnewhall avatar Feb 27 '21 06:02 davidnewhall