@@ -1078,38 +1078,37 @@ def cdata_leaf_value(cdata, context: "libyang.Context" = None) -> Any:
10781078 return None
10791079
10801080 val = c2str (val )
1081- term_node = ffi .cast ("struct lyd_node_term *" , cdata )
1082- val_type = ffi .new ("const struct lysc_type **" , ffi .NULL )
1083-
1084- # get real value type
1085- ctx = context .cdata if context else ffi .NULL
1086- ret = lib .lyd_value_validate (
1087- ctx ,
1088- term_node .schema ,
1089- str2c (val ),
1090- len (val ),
1091- ffi .NULL ,
1092- val_type ,
1093- ffi .NULL ,
1094- )
10951081
1096- if ret in (lib .LY_SUCCESS , lib .LY_EINCOMPLETE ):
1097- val_type = val_type [0 ].basetype
1098- if val_type in Type .STR_TYPES :
1099- return val
1100- if val_type in Type .NUM_TYPES :
1101- return int (val )
1102- if val_type == Type .BOOL :
1103- return val == "true"
1104- if val_type == Type .DEC64 :
1105- return float (val )
1106- if val_type == Type .LEAFREF :
1107- return DLeaf .cdata_leaf_value (cdata .value .leafref , context )
1108- if val_type == Type .EMPTY :
1109- return None
1082+ if cdata .schema == ffi .NULL :
1083+ # opaq node
11101084 return val
11111085
1112- raise TypeError ("value type validation error" )
1086+ if cdata .schema .nodetype == SNode .LEAF :
1087+ snode = ffi .cast ("struct lysc_node_leaf *" , cdata .schema )
1088+ elif cdata .schema .nodetype == SNode .LEAFLIST :
1089+ snode = ffi .cast ("struct lysc_node_leaflist *" , cdata .schema )
1090+
1091+ # find the real type used
1092+ cdata = ffi .cast ("struct lyd_node_term *" , cdata )
1093+ curr_type = snode .type
1094+ while curr_type .basetype in (Type .LEAFREF , Type .UNION ):
1095+ if curr_type .basetype == Type .LEAFREF :
1096+ curr_type = cdata .value .realtype
1097+ if curr_type .basetype == Type .UNION :
1098+ curr_type = cdata .value .subvalue .value .realtype
1099+
1100+ val_type = curr_type .basetype
1101+ if val_type in Type .STR_TYPES :
1102+ return val
1103+ if val_type in Type .NUM_TYPES :
1104+ return int (val )
1105+ if val_type == Type .BOOL :
1106+ return val == "true"
1107+ if val_type == Type .DEC64 :
1108+ return float (val )
1109+ if val_type == Type .EMPTY :
1110+ return None
1111+ return val
11131112
11141113
11151114# -------------------------------------------------------------------------------------
0 commit comments