Skip to content

Draft: Unable to read configuration from yaml file #157

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

Open
sxwebdev opened this issue Mar 11, 2025 · 4 comments · May be fixed by #159
Open

Draft: Unable to read configuration from yaml file #157

sxwebdev opened this issue Mar 11, 2025 · 4 comments · May be fixed by #159

Comments

@sxwebdev
Copy link

sxwebdev commented Mar 11, 2025

Unable to read configuration from yaml file:

main.go

package main

import (
	"log"

	"github.com/cristalhq/aconfig"
	"github.com/cristalhq/aconfig/aconfigdotenv"
	"github.com/cristalhq/aconfig/aconfigyaml"
)

type StringType string

type NestedStruct struct {
	Key string
}

type NestedArr struct {
	Enabled      bool
	Str          StringType
	NestedStruct NestedStruct `yaml:"nested_struct"`
	Data         map[string]any
}

type Items struct {
	Arrs []NestedArr
}

type MyConfig struct {
	Items Items `yaml:"items"`
}

func main() {
	var cfg MyConfig
	loader := aconfig.LoaderFor(&cfg, aconfig.Config{
		Files: []string{"config.yaml"},
		FileDecoders: map[string]aconfig.FileDecoder{
			".yaml": aconfigyaml.New(),
			".yml":  aconfigyaml.New(),
			".env":  aconfigdotenv.New(),
		},
	})

	if err := loader.Load(); err != nil {
		log.Fatalf("could not load config: %v", err)
	}

	log.Printf("Config: %+v", cfg)
}

config.yaml

items:
  arrs:
    - enabled: true
      str: asdasd
      nest_struct:
        key: value
      data:
        key: value
        bool: true

Errors:

  • 2025/03/11 09:02:24 could not load config: load config: load files: provided value type do not match struct field type (main.StringType and string)
  • 2025/03/11 09:01:44 could not load config: load config: load files: no such field "Nested_struct" in struct
@cristaloleg
Copy link
Member

Hey, sorry for the bug. Wanna make a PR ?

@sxwebdev
Copy link
Author

Hey, sorry for the bug. Wanna make a PR ?

At the moment I have no opportunity because I am busy, but I would be very grateful if you could fix the issue.

@cristaloleg
Copy link
Member

Same for me now.

sxwebdev added a commit to sxwebdev/aconfig that referenced this issue Mar 25, 2025
@sxwebdev sxwebdev changed the title Unable to read configuration from yaml file Draft: Unable to read configuration from yaml file Mar 25, 2025
@d-enk
Copy link

d-enk commented Apr 15, 2025

aconfig/reflection.go

Lines 384 to 409 in 7111e04

if structFieldValue.Type() != val.Type() {
if structFieldValue.Kind() == reflect.Slice && val.Kind() == reflect.Slice {
vals := value.([]interface{})
slice := reflect.MakeSlice(structFieldValue.Type(), len(vals), len(vals))
if isPrimitive(structFieldValue.Type().Elem()) {
for i := 0; i < len(vals); i++ {
fd := l.newFieldData(reflect.StructField{}, slice.Index(i), nil)
if err := l.setFieldData(fd, vals[i]); err != nil {
return fmt.Errorf("incorrect slice item %q: %w", vals[i], err)
}
}
} else {
for i := 0; i < len(vals); i++ {
a := mii(vals[i])
b := slice.Index(i)
if err := l.m2s(a, b); err != nil {
return err
}
}
}
structFieldValue.Set(slice)
continue
} else {
return fmt.Errorf("provided value type do not match struct field type (%v and %v)", structFieldValue.Type(), val.Type())
}
}

The whole problem is this function is too complicated and does not do its job...

@d-enk d-enk linked a pull request Apr 15, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants