ygot icon indicating copy to clipboard operation
ygot copied to clipboard

Union field has nil value after xml.Unmarshal

Open Sneh-Prabha opened this issue 10 months ago • 5 comments

I have generated go code using ygot generator for my yang files.

Here is the sample generated go struct which I am using to Unmarshal. Union is always converted as nil. Note: "Type" is union of two enums (and each enums has multiple identity) in my yang

package main

import ( "encoding/xml" "fmt" ) type State struct { Description *string Type Type_Union }

type Type_Union interface { Is_Type_Union() } type Type_Union_E_HARDWARE struct { E_HARDWARE E_HARDWARE } func (*Type_Union_E_HARDWARE) Is_Type_Union() {} type Type_Union_E_SOFTWARE struct { E_SOFTWARE E_SOFTWARE } func (*Type_Union_E_SOFTWARE) Is_Type_Union() {}

type E_HARDWARE int64 func (E_HARDWARE) IsYANGGoEnum() {} const ( HARDWARE_UNSET E_HARDWARE = 0 HARDWARE_A E_HARDWARE = 1 HARDWARE_B E_HARDWARE = 2 ) type E_SOFTWARE int64 func (E_SOFTWARE) IsYANGGoEnum() {} const ( SOFTWARE_UNSET E_SOFTWARE = 0 SOFTWARE_C E_SOFTWARE = 1 SOFTWARE_D E_SOFTWARE = 2 )

func main() { xmlData := <abc> <Description>Hello</Description> <Type>2</Type> </abc>

var gostruct State

err := xml.Unmarshal([]byte(xmlData), &gostruct)
if err != nil {
    fmt.Println("Error parsing XML:", err)
    return
}
fmt.Println("Description:", *gostruct.Description)
fmt.Println("type:", gostruct.Type)

}

Output: Description: Hello type:

Let me know what modification is needed in the input to get the correct value.

Sneh-Prabha avatar Mar 29 '24 11:03 Sneh-Prabha

identities and enumerations are represented as strings per https://datatracker.ietf.org/doc/html/rfc7951, 2 is ambiguous.

wenovus avatar Mar 29 '24 16:03 wenovus

Yes, value is generated using ygot. I also dont know how to distinguish between values as if it for first enum or second and also how to give value in <Type> tag as whatever I am giving it is not accepting and converting to nil after unmarshal.

Sneh-Prabha avatar Apr 01 '24 08:04 Sneh-Prabha

Here I would be expecting ygot to accept the string "A", "B", "C", or "D" since these seem to be the enum names in YANG. How did you get 2 in the output? ygot would not be able to unmarshal that.

wenovus avatar Apr 01 '24 21:04 wenovus

Inside each enums we have Identity. That might be causing values to be generated as 0,1,2 ?

Sneh-Prabha avatar Apr 04 '24 04:04 Sneh-Prabha

In your code you have

err := xml.Unmarshal([]byte(xmlData), &gostruct)

I'm not quite sure where the xml library is, nor where xmlData comes from or why those two things has to do with ygot. The data is not RFC7951 compliant, so I would recommend understanding how it was generated.

wenovus avatar Apr 04 '24 16:04 wenovus

ygot doesn't support XML marshalling and unmarshalling, solely RFC7951 JSON. If you would like to add support for this, we would welcome a contribution.

robshakir avatar Jul 26 '24 01:07 robshakir