freeconf / yang

Standards-based management for Golang microservices
Apache License 2.0
38 stars 14 forks source link

Setting default value through typedef for leaf-list generates panic #92

Closed raghavendramayagundi closed 1 year ago

raghavendramayagundi commented 1 year ago

If leaf-list uses type from a typedef which contains a default value, we get panic error while loading yang module:

panic: expected []string

goroutine 1 [running]:
github.com/freeconf/yang/meta.(*LeafList).setDefaultValue(0xc00049a09e?, {0xf77b80?, 0xc0004022c0?})
        C:/Users/mayagundi/go/pkg/mod/github.com/freeconf/yang@v0.0.0-20230922115330-84b345f6a2d1/meta/core_gen.go:1674 +0x85
github.com/freeconf/yang/meta.(*compiler).compileType(0xf63a7f?, 0xc00062a580, {0x106be80?, 0xc000630c60}, 0x0)
        C:/Users/mayagundi/go/pkg/mod/github.com/freeconf/yang@v0.0.0-20230922115330-84b345f6a2d1/meta/compile.go:256 +0x174
github.com/freeconf/yang/meta.(*compiler).compile(0xc000630a20?, {0xfc9020?, 0xc000630c60?})
        C:/Users/mayagundi/go/pkg/mod/github.com/freeconf/yang@v0.0.0-20230922115330-84b345f6a2d1/meta/compile.go:79 +0x16a
github.com/freeconf/yang/meta.(*compiler).compile(0xc000084680?, {0xfc8d00?, 0xc000630a20?})
        C:/Users/mayagundi/go/pkg/mod/github.com/freeconf/yang@v0.0.0-20230922115330-84b345f6a2d1/meta/compile.go:135 +0x685
github.com/freeconf/yang/meta.(*compiler).compile(0xc0001b22d0?, {0xfc96c0?, 0xc000084680?})
        C:/Users/mayagundi/go/pkg/mod/github.com/freeconf/yang@v0.0.0-20230922115330-84b345f6a2d1/meta/compile.go:135 +0x685
github.com/freeconf/yang/meta.(*compiler).module(0x10?, 0xc000084680)
        C:/Users/mayagundi/go/pkg/mod/github.com/freeconf/yang@v0.0.0-20230922115330-84b345f6a2d1/meta/compile.go:52 +0x206
github.com/freeconf/yang/meta.Compile(0xc000084680)
        C:/Users/mayagundi/go/pkg/mod/github.com/freeconf/yang@v0.0.0-20230922115330-84b345f6a2d1/meta/compile.go:21 +0x4a
github.com/freeconf/yang/parser.LoadModuleWithOptions(0xc00046a120, {0xc00000e100, 0xc}, {{0x0?, 0x0?}, {0x0?, 0xc00051bc50?}})
        C:/Users/mayagundi/go/pkg/mod/github.com/freeconf/yang@v0.0.0-20230922115330-84b345f6a2d1/parser/loader.go:72 +0x1fd
github.com/freeconf/yang/parser.LoadModule(...)

YANG file is attached (as a txt file). type_leafref.txt

HRogge commented 1 year ago

This could be related to the issue I wrote a testcase for in #86... its also related to typedef-leaflist combination.

@raghavendramayagundi could you test if the error goes away if you "inline" the typedef ?

raghavendramayagundi commented 1 year ago

@HRogge by "inline" if you mean to use "type string; default 7;" directly instead of "typedef", then yes it works. Locally I tried the below fix in core_gen.go by adding "else if":

func (m *LeafList) setDefaultValue(d interface{}) {
    if s, valid := d.([]string); valid {
        m.defaultVals = s
    } else if s, valid := d.(string); valid {
        m.defaultVals = []string{s}
    } else {
        panic("expected []string or string")
    }
}
HRogge commented 1 year ago

I have a similar "fix" for a related problem somewhere else, so my guess is that the real problem is somewhere in the yang file-parser, some bad interaction between "leaf-list" (and maybe list?) and "typedef"... but I have yet to find the underlying issue.

HRogge commented 1 year ago

Could you test if #93 solves your issue?

raghavendramayagundi commented 1 year ago

The fix did not help, got the below stack:

panic: expected []string

goroutine 1 [running]:
github.com/freeconf/yang/meta.(*LeafList).setDefaultValue(0xc0004be09e?, {0x389d40?, 0xc0004903b0?})
        C:/MyData/Projects/ESO/YANGtoJSON/Code/YangTest/freeconf/meta/core_gen.go:1675 +0x85
github.com/freeconf/yang/meta.(*compiler).compileType(0xc000028390?, 0xc00054e9a0, {0x47e080?, 0xc0001e2240}, 0x0)
        C:/MyData/Projects/ESO/YANGtoJSON/Code/YangTest/freeconf/meta/compile.go:262 +0x175
github.com/freeconf/yang/meta.(*compiler).compile(0xc0004419d0, {0x3db500?, 0xc0001e2240?})
        C:/MyData/Projects/ESO/YANGtoJSON/Code/YangTest/freeconf/meta/compile.go:81 +0x16a
github.com/freeconf/yang/meta.(*compiler).compile(0xc0004419d0, {0x3db1e0?, 0xc0001e2000?})
        C:/MyData/Projects/ESO/YANGtoJSON/Code/YangTest/freeconf/meta/compile.go:138 +0x713
github.com/freeconf/yang/meta.(*compiler).compile(0xc0004419d0, {0x3dbbc0?, 0xc00004b1e0?})
        C:/MyData/Projects/ESO/YANGtoJSON/Code/YangTest/freeconf/meta/compile.go:138 +0x713
github.com/freeconf/yang/meta.(*compiler).module(0x331d8b?, 0xc00004b1e0)
        C:/MyData/Projects/ESO/YANGtoJSON/Code/YangTest/freeconf/meta/compile.go:54 +0x206
github.com/freeconf/yang/meta.Compile(0xc00004b1e0)
        C:/MyData/Projects/ESO/YANGtoJSON/Code/YangTest/freeconf/meta/compile.go:22 +0xb8
github.com/freeconf/yang/parser.LoadModuleWithOptions(0xc0004921e0, {0xc00009e0a0, 0xc}, {{0x0?, 0x0?}, {0x0?, 0xc000441c50?}})
        C:/MyData/Projects/ESO/YANGtoJSON/Code/YangTest/freeconf/parser/loader.go:72 +0x1fd
github.com/freeconf/yang/parser.LoadModule(...)
        C:/MyData/Projects/ESO/YANGtoJSON/Code/YangTest/freeconf/parser/loader.go:16
e2eso-json-schema-generator/generator/yangparser.(*YangParser).ParseAndGenerateJsonSchema(0xc000441cb0)
        C:/MyData/Projects/ESO/YANGtoJSON/Code/e2eso-json-schema-generator/generator/yangparser/parser.go:34 +0x5f
e2eso-json-schema-generator/generator/yangparser.ConvertYangFilesToJsonSchema({0x47bd98, 0xc00063f2c0}, {0xc00000e17a?, 0x477c50?}, {0xc00009e0a0, 0xc}, {0xc000441e08, 0x2}, 0xa8?)
        C:/MyData/Projects/ESO/YANGtoJSON/Code/e2eso-json-schema-generator/generator/yangparser/parser.go:283 +0xde
main.main()
        C:/MyData/Projects/ESO/YANGtoJSON/Code/e2eso-json-schema-generator/main.go:90 +0x686
HRogge commented 1 year ago

okay, so we are looking at different problems...

dhubler commented 1 year ago

should be fixed now