Skip to content

Panic on parsing YAML configuration with empty lists #111

@ic2hrmk

Description

@ic2hrmk

Hi,

I encounter panic when a list in the YAML configuration is empty:

package config_test

import (
	"testing"

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

type (
	ResourcesConfiguration struct {
		ResourcesA []ResourceA `yaml:"resources_a"`
		ResourcesB []ResourceB `yaml:"resources_b"`
	}

	ResourceA struct {
		Field string `yaml:"field"`
	}

	ResourceB struct {
		Field int `yaml:"field"`
	}
)

func TestLoadResources(t *testing.T) {
	resourcesConfiguration := new(ResourcesConfiguration)

	resourcesLoader := aconfig.LoaderFor(resourcesConfiguration,
		aconfig.Config{
			SkipFlags:          true,
			Files:              []string{"asset_resources.yaml"},
			FailOnFileNotFound: true,
			FileDecoders: map[string]aconfig.FileDecoder{
				".yaml": aconfigyaml.New(),
			},
		})
	if err := resourcesLoader.Load(); err != nil {
		t.Errorf("failed to load resources configurations [err=%s]", err)
	}

	t.Logf("%v", *resourcesConfiguration)
}

Test file: asset_resources.yaml

resources_a:

resources_b:

Panic:

=== RUN   TestLoadResources
--- FAIL: TestLoadResources (0.00s)
panic: <nil> <nil> [recovered]
	panic: <nil> <nil>

goroutine 20 [running]:
testing.tRunner.func1.2({0x1147b60, 0xc00009e680})
	/usr/local/go/src/testing/testing.go:1209 +0x24e
testing.tRunner.func1()
	/usr/local/go/src/testing/testing.go:1212 +0x218
panic({0x1147b60, 0xc00009e680})
	/usr/local/go/src/runtime/panic.go:1038 +0x215
github.com/cristalhq/aconfig.(*Loader).setFieldData(0x11496c0, 0xc0000ce210, {0x0, 0x0})
	/Users/user/go/pkg/mod/github.com/cristalhq/aconfig@v0.16.7/reflection.go:187 +0x9a5
github.com/cristalhq/aconfig.(*Loader).loadFile(0xc000102000, {0x116b3c9, 0x14})
	/Users/user/go/pkg/mod/github.com/cristalhq/aconfig@v0.16.7/aconfig.go:330 +0x2f1
github.com/cristalhq/aconfig.(*Loader).loadFiles(0xc000102000)
	/Users/user/go/pkg/mod/github.com/cristalhq/aconfig@v0.16.7/aconfig.go:290 +0xea
github.com/cristalhq/aconfig.(*Loader).loadSources(0xc000102000)
	/Users/user/go/pkg/mod/github.com/cristalhq/aconfig@v0.16.7/aconfig.go:235 +0x7f
github.com/cristalhq/aconfig.(*Loader).loadConfig(0xc000102000)
	/Users/user/go/pkg/mod/github.com/cristalhq/aconfig@v0.16.7/aconfig.go:211 +0x65
github.com/cristalhq/aconfig.(*Loader).Load(0x1000000)
	/Users/user/go/pkg/mod/github.com/cristalhq/aconfig@v0.16.7/aconfig.go:201 +0x76
github.com/ic2hrmk/project/subdir/test/config_test.TestLoadResources(0xc000083860)
	/Users/user/go/src/github.com/ic2hrmk/project/subdir/test/config/load_resources_test.go:37 +0x176
testing.tRunner(0xc000083860, 0x11741d0)
	/usr/local/go/src/testing/testing.go:1259 +0x102
created by testing.(*T).Run
	/usr/local/go/src/testing/testing.go:1306 +0x35a

Expected:

=== RUN   TestLoadResources
    load_resources_test.go:38: {[] []}
--- PASS: TestLoadResources (0.00s)
PASS

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions