Skip to content

Commit 5a9259a

Browse files
authored
Merge pull request #415 from Gor027/as_udt
Add possibility to borrow udt fields
2 parents 0571ffc + 51063e3 commit 5a9259a

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

scylla/src/frame/response/result.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,13 @@ impl CqlValue {
269269
}
270270
}
271271

272+
pub fn as_udt(&self) -> Option<&Vec<(String, Option<CqlValue>)>> {
273+
match self {
274+
Self::UserDefinedType { fields, .. } => Some(fields),
275+
_ => None,
276+
}
277+
}
278+
272279
pub fn into_vec(self) -> Option<Vec<CqlValue>> {
273280
match self {
274281
Self::List(s) => Some(s),
@@ -284,6 +291,13 @@ impl CqlValue {
284291
}
285292
}
286293

294+
pub fn into_udt_pair_vec(self) -> Option<Vec<(String, Option<CqlValue>)>> {
295+
match self {
296+
Self::UserDefinedType { fields, .. } => Some(fields),
297+
_ => None,
298+
}
299+
}
300+
287301
pub fn into_varint(self) -> Option<BigInt> {
288302
match self {
289303
Self::Varint(i) => Some(i),
@@ -1103,6 +1117,37 @@ mod tests {
11031117
assert_eq!(CqlValue::Int(3), decoded[1].1);
11041118
}
11051119

1120+
#[test]
1121+
fn test_udt_from_cql() {
1122+
let my_fields: Vec<(String, Option<CqlValue>)> = vec![
1123+
("fst".to_string(), Some(CqlValue::Int(10))),
1124+
("snd".to_string(), Some(CqlValue::Boolean(true))),
1125+
];
1126+
1127+
let cql: CqlValue = CqlValue::UserDefinedType {
1128+
keyspace: "".to_string(),
1129+
type_name: "".to_string(),
1130+
fields: my_fields,
1131+
};
1132+
1133+
// Test borrowing.
1134+
let decoded = cql.as_udt().unwrap();
1135+
1136+
assert_eq!("fst".to_string(), decoded[0].0);
1137+
assert_eq!(Some(CqlValue::Int(10)), decoded[0].1);
1138+
1139+
assert_eq!("snd".to_string(), decoded[1].0);
1140+
assert_eq!(Some(CqlValue::Boolean(true)), decoded[1].1);
1141+
1142+
let decoded = cql.into_udt_pair_vec().unwrap();
1143+
1144+
assert_eq!("fst".to_string(), decoded[0].0);
1145+
assert_eq!(Some(CqlValue::Int(10)), decoded[0].1);
1146+
1147+
assert_eq!("snd".to_string(), decoded[1].0);
1148+
assert_eq!(Some(CqlValue::Boolean(true)), decoded[1].1);
1149+
}
1150+
11061151
#[test]
11071152
fn date_deserialize() {
11081153
// Date is correctly parsed from a 4 byte array

0 commit comments

Comments
 (0)