8000 decode struct with default embed.FS value results into empty FS · Issue #352 · knadh/koanf · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
8000
Skip to content
decode struct with default embed.FS value results into empty FS #352
Open
@jkroepke

Description

@jkroepke

Describe the bug
I have a configuration struct with has a field with type interface fs.FS.

Users can input file path as string and with a custom decode function, the string is translated into os.DirFS

If the users does not specific a value, the default embed.FS value be used. However after decode the config into a struct, the embed.FS is empty.

To Reproduce

Go code to reproduce
package main

import (
	"embed"
	"fmt"
	"io/fs"

	"github.com/knadh/koanf/providers/structs"
	"github.com/knadh/koanf/v2"
)

//go:embed *
var testFile embed.FS

type Config struct {
	FS fs.FS `koanf:"dir"`
}

var Defaults = Config{
	FS: testFile,
}

func main() {
	k := koanf.New(".")
	if err := k.Load(structs.Provider(Defaults, "koanf"), nil); err != nil {
		fmt.Println("Error loading defaults:", err)
		return
	}

	var conf Config

	if err := k.Unmarshal("", &conf); err != nil {
		fmt.Println("Error unmarshalling:", err)
		return
	}

	fmt.Println("Config loaded successfully.")

	fmt.Printf("FS: %+v\n", conf.FS)
	fmt.Printf("FS: %+v\n", Defaults.FS)
}

Expected behavior
If a user does not enter a value, the default value should be given.

Please provide the following information):

  • OS: OSX
  • Koanf Version [e.g. v1.0.0]
	github.com/go-viper/mapstructure/v2 v2.2.1
	github.com/knadh/koanf/parsers/yaml v1.0.0
	github.com/knadh/koanf/providers/basicflag v1.1.0
	github.com/knadh/koanf/providers/env v1.1.0
	github.com/knadh/koanf/providers/file v1.2.0
	github.com/knadh/koanf/providers/structs v1.0.0
	github.com/knadh/koanf/v2 v2.2.0

Additional context

I guess the issue is somewhere in ko.Get(path). My assumten is that somewhat from github.com/mitchellh/copystructure can't copy the struct, since the field is not exported.

Here is a screenshot from debugger:

Image

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0