Skip to content

Draft: Unable to read configuration from yaml file #157

@sxwebdev

Description

@sxwebdev

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions