ygot icon indicating copy to clipboard operation
ygot copied to clipboard

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

Open ruchakulkarni11 opened this issue 9 months ago • 7 comments

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!

ruchakulkarni11 avatar May 03 '24 00:05 ruchakulkarni11

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

wenovus avatar May 03 '24 00:05 wenovus

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

ruchakulkarni11 avatar May 03 '24 00:05 ruchakulkarni11

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.

wenovus avatar May 03 '24 00:05 wenovus

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"

ruchakulkarni11 avatar May 09 '24 22:05 ruchakulkarni11

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.

robshakir avatar Jun 11 '24 19:06 robshakir

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

ruchakulkarni11 avatar Jul 15 '24 23:07 ruchakulkarni11

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

robshakir avatar Jul 26 '24 01:07 robshakir