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

ytypes.Unmarshal() treating 50 as a float64 even though I am converting 50 to string before unmarshalling #972

Open
ruchakulkarni11 opened this issue May 3, 2024 · 7 comments

Comments

@ruchakulkarni11
Copy link

ruchakulkarni11 commented May 3, 2024

I have an enum with index 1 and value 50. schema required to convert the enum index to enum value (string).
I am converting 50 to string type but while ytypes.Unmarshal() sanitizeJson method is treating 50 as a float64 even though I am converting 50 to string before unmarshalling.

Is there any way I can convert the type of 50 to string?

I would appreciate any inputs!

@wenovus
Copy link
Collaborator

wenovus commented May 3, 2024

Enums are marshalled as strings per RFC7951: https://datatracker.ietf.org/doc/html/rfc7951#section-6.4

@ruchakulkarni11
Copy link
Author

yes, thats right but my string value is 50, and failing while unmarshalling.

@wenovus
Copy link
Collaborator

wenovus commented May 3, 2024

Can you provide your YANG schema as well as a reproducing example? I have to go for today but I can take a look with those information when I get a chance.

I'm not quite sure what you mean by the string value is 50. This means the JSON needs to be "50" rather than 50. I'm not immediately seeing how sanitizeJSON could be the issue in any case assuming the leaf is an enum.

@ruchakulkarni11
Copy link
Author

Hello, Sorry for late reply.

Suppose enum is defined as follow,

typedef example_enum {
    type enumeration {
        enum 25 { value 1; }
        enum 50 { value 2; }
        enum 75 { value 3; }
        enum 100 { value 4; }
    }
}

Now here when enum index 2 is converted to name, its 50. Here enum name is string so 50 must be of type string, but the ytypes.Unmarshal returns error as "got float64 type for field, expect string"

@robshakir
Copy link
Contributor

Hi,

Could you provide the JSON input you're using? This sounds like you might have this field set as foo: 50 rather than foo: "50".

Thanks,
r.

@ruchakulkarni11
Copy link
Author

If I set foo: "50", unmarshalling fails because "50" does not match with the enum-name 50.

ytypes.Unmarshal: "50" is not a valid value for enum field Id

@robshakir
Copy link
Contributor

Can you provide both the JSON and the YANG module? It's important how the leaf is defined to be able to debug this.

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

3 participants