openconfig / ygot

A YANG-centric Go toolkit - Go/Protobuf Code Generation; Validation; Marshaling/Unmarshaling
Apache License 2.0
286 stars 107 forks source link

Union field has nil value after xml.Unmarshal #961

Closed Sneh-Prabha closed 4 months ago

Sneh-Prabha commented 8 months ago

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 := `

Hello
               <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.

wenovus commented 8 months ago

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

Sneh-Prabha commented 7 months ago

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 tag as whatever I am giving it is not accepting and converting to nil after unmarshal.

wenovus commented 7 months ago

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.

Sneh-Prabha commented 7 months ago

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

wenovus commented 7 months ago

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.

robshakir commented 4 months ago

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.