Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Union field has nil value after xml.Unmarshal #961

Open
Sneh-Prabha opened this issue Mar 29, 2024 · 5 comments
Open

Union field has nil value after xml.Unmarshal #961

Sneh-Prabha opened this issue Mar 29, 2024 · 5 comments

Comments

@Sneh-Prabha
Copy link

Sneh-Prabha commented Mar 29, 2024

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.

@wenovus
Copy link
Collaborator

wenovus commented Mar 29, 2024

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

@Sneh-Prabha
Copy link
Author

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
Copy link
Collaborator

wenovus commented Apr 1, 2024

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
Copy link
Author

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

@wenovus
Copy link
Collaborator

wenovus commented Apr 4, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants