Skip to content

Commit 6801fe4

Browse files
committed
feat(inspect): add inspect for all types
also add `double` custom type Signed-off-by: Daniel Boll <danielboll.academico@gmail.com>
1 parent a241902 commit 6801fe4

File tree

14 files changed

+146
-24
lines changed

14 files changed

+146
-24
lines changed

examples/custom-types/double.mts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Cluster, Double } from "../../index.js";
2+
3+
const nodes = process.env.CLUSTER_NODES?.split(",") ?? ["127.0.0.1:9042"];
4+
5+
console.log(`Connecting to ${nodes}`);
6+
7+
const cluster = new Cluster({ nodes });
8+
const session = await cluster.connect();
9+
10+
await session.execute(
11+
"CREATE KEYSPACE IF NOT EXISTS double WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }",
12+
);
13+
await session.useKeyspace("double");
14+
15+
await session.execute(
16+
"CREATE TABLE IF NOT EXISTS double (a double, primary key (a))",
17+
);
18+
19+
const input = new Double(1.1127830921);
20+
const _ = await session.execute("INSERT INTO double (a) VALUES (?)", [input]);
21+
console.log(_);
22+
23+
const results = await session.execute("SELECT a FROM double");
24+
console.log(`${input} -> ${results[0].a}`);

examples/custom-types/floats.mts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ await session.execute(
1616
"CREATE TABLE IF NOT EXISTS floats (a float, primary key (a))",
1717
);
1818

19-
await session.execute("INSERT INTO floats (a) VALUES (?)", [new Float(1.1)]);
19+
const input = new Float(1.1127830921);
20+
await session.execute("INSERT INTO floats (a) VALUES (?)", [input]);
2021

2122
const results = await session.execute("SELECT a FROM floats");
22-
console.log(results);
23+
console.log(`${input} -> ${results[0].a}`);

index.d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ export declare class Decimal {
3838
toString(): string
3939
}
4040

41+
/**
42+
* A double precision float number.
43+
*
44+
* Due to the nature of numbers in JavaScript, it's hard to distinguish between integers and floats, so this type is used to represent
45+
* double precision float numbers while any other JS number will be treated as an integer. (This is not the case for BigInts, which are always treated as BigInts).
46+
*/
47+
export declare class Double {
48+
constructor(inner: number)
49+
toString(): string
50+
}
51+
4152
export declare class Duration {
4253
months: number
4354
days: number
@@ -55,16 +66,19 @@ export declare class Duration {
5566
*/
5667
export declare class Float {
5768
constructor(inner: number)
69+
toString(): string
5870
}
5971

6072
/** A list of any CqlType */
6173
export declare class List<T = NativeTypes> {
6274
constructor(values: T[])
75+
toString(): string
6376
}
6477

6578
/** A map of any CqlType to any CqlType */
6679
export declare class Map<T = NativeTypes, U = NativeTypes> {
6780
constructor(values: Array<Array<T | U>>)
81+
toString(): string
6882
}
6983

7084
export declare class Metrics {
@@ -242,6 +256,7 @@ export declare class ScyllaSession {
242256
/** A list of any CqlType */
243257
export declare class Set<T = NativeTypes> {
244258
constructor(values: T[])
259+
toString(): string
245260
}
246261

247262
export declare class Uuid {
@@ -272,6 +287,7 @@ export declare class Uuid {
272287
*/
273288
export declare class Varint {
274289
constructor(inner: Array<number>)
290+
toString(): string
275291
}
276292

277293
export interface Auth {

index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ module.exports.ScyllaBatchStatement = nativeBinding.ScyllaBatchStatement
366366
module.exports.Cluster = nativeBinding.Cluster
367367
module.exports.ScyllaCluster = nativeBinding.ScyllaCluster
368368
module.exports.Decimal = nativeBinding.Decimal
369+
module.exports.Double = nativeBinding.Double
369370
module.exports.Duration = nativeBinding.Duration
370371
module.exports.Float = nativeBinding.Float
371372
module.exports.List = nativeBinding.List
@@ -387,4 +388,10 @@ const customInspectSymbol = Symbol.for('nodejs.util.inspect.custom')
387388

388389
nativeBinding.Uuid.prototype[customInspectSymbol] = function () { return this.toString(); }
389390
nativeBinding.Duration.prototype[customInspectSymbol] = function () { return this.toString(); }
390-
nativeBinding.Decimal.prototype[customInspectSymbol] = function () { return this.toString(); }
391+
nativeBinding.Decimal.prototype[customInspectSymbol] = function () { return this.toString(); }
392+
nativeBinding.Float.prototype[customInspectSymbol] = function () { return this.toString(); }
393+
nativeBinding.Double.prototype[customInspectSymbol] = function () { return this.toString(); }
394+
nativeBinding.List.prototype[customInspectSymbol] = function () { return this.toString(); }
395+
nativeBinding.Set.prototype[customInspectSymbol] = function () { return this.toString(); }
396+
nativeBinding.Map.prototype[customInspectSymbol] = function () { return this.toString(); }
397+
nativeBinding.Varint.prototype[customInspectSymbol] = function () { return this.toString(); }

scripts/fix-files.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ const customInspectSymbol = Symbol.for('nodejs.util.inspect.custom')
3131
nativeBinding.Uuid.prototype[customInspectSymbol] = function () { return this.toString(); }
3232
nativeBinding.Duration.prototype[customInspectSymbol] = function () { return this.toString(); }
3333
nativeBinding.Decimal.prototype[customInspectSymbol] = function () { return this.toString(); }
34+
nativeBinding.Float.prototype[customInspectSymbol] = function () { return this.toString(); }
35+
nativeBinding.Double.prototype[customInspectSymbol] = function () { return this.toString(); }
36+
nativeBinding.List.prototype[customInspectSymbol] = function () { return this.toString(); }
37+
nativeBinding.Set.prototype[customInspectSymbol] = function () { return this.toString(); }
38+
nativeBinding.Map.prototype[customInspectSymbol] = function () { return this.toString(); }
39+
nativeBinding.Varint.prototype[customInspectSymbol] = function () { return this.toString(); }
3440
`,
3541
)
3642
.trim(),

src/helpers/cql_value_bridge.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
use napi::bindgen_prelude::{BigInt, Either13, Either14};
1+
use napi::bindgen_prelude::{BigInt, Either14, Either15};
22
use scylla::frame::response::result::CqlValue;
33

44
use std::collections::HashMap;
55

66
use crate::types::{
7-
decimal::Decimal, duration::Duration, float::Float, list::List, map::Map, set::Set, uuid::Uuid,
8-
varint::Varint,
7+
decimal::Decimal, double::Double, duration::Duration, float::Float, list::List, map::Map,
8+
set::Set, uuid::Uuid, varint::Varint,
99
};
1010

1111
use super::to_cql_value::ToCqlValue;
1212

1313
macro_rules! define_expected_type {
1414
($lifetime:lifetime, $($t:ty),+) => {
15-
pub type ParameterNativeTypes<$lifetime> = Either13<$($t),+>;
16-
pub type ParameterWithMapType<$lifetime> = Either14<$($t),+, HashMap<String, ParameterNativeTypes<$lifetime>>>;
15+
pub type ParameterNativeTypes<$lifetime> = Either14<$($t),+>;
16+
pub type ParameterWithMapType<$lifetime> = Either15<$($t),+, HashMap<String, ParameterNativeTypes<$lifetime>>>;
1717
pub type JSQueryParameters<$lifetime> = napi::Result<Vec<HashMap<String, ParameterWithMapType<$lifetime>>>>;
1818
};
1919
}
2020

21-
define_expected_type!('a, u32, String, &'a Uuid, BigInt, &'a Duration, &'a Decimal, bool, Vec<u32>, &'a Float, &'a Varint, &'a List, &'a Set, &'a Map);
21+
define_expected_type!('a, u32, String, &'a Uuid, BigInt, &'a Duration, &'a Decimal, bool, Vec<u32>, &'a Float, &'a Varint, &'a List, &'a Set, &'a Map, &'a Double);
2222

2323
impl<'a> ToCqlValue for ParameterWithMapType<'a> {
2424
fn to_cql_value(&self) -> CqlValue {
@@ -36,7 +36,8 @@ impl<'a> ToCqlValue for ParameterWithMapType<'a> {
3636
ParameterWithMapType::K(list) => list.to_cql_value(),
3737
ParameterWithMapType::L(set) => set.to_cql_value(),
3838
ParameterWithMapType::M(map) => map.to_cql_value(),
39-
ParameterWithMapType::N(map) => CqlValue::UserDefinedType {
39+
ParameterWithMapType::N(double) => double.to_cql_value(),
40+
ParameterWithMapType::O(map) => CqlValue::UserDefinedType {
4041
// TODO: think a better way to fill this info here
4142
keyspace: "keyspace".to_string(),
4243
type_name: "type_name".to_string(),
@@ -65,6 +66,7 @@ impl<'a> ToCqlValue for ParameterNativeTypes<'a> {
6566
ParameterNativeTypes::K(list) => list.to_cql_value(),
6667
ParameterNativeTypes::L(set) => set.to_cql_value(),
6768
ParameterNativeTypes::M(map) => map.to_cql_value(),
69+
ParameterNativeTypes::N(double) => double.to_cql_value(),
6870
}
6971
}
7072
}

src/helpers/to_cql_value.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use napi::bindgen_prelude::BigInt;
22
use scylla::frame::response::result::CqlValue;
33

44
use crate::types::{
5-
decimal::Decimal, duration::Duration, float::Float, list::List, map::Map, set::Set, uuid::Uuid,
6-
varint::Varint,
5+
decimal::Decimal, double::Double, duration::Duration, float::Float, list::List, map::Map,
6+
set::Set, uuid::Uuid, varint::Varint,
77
};
88

99
// Trait to abstract the conversion to CqlValue
@@ -60,6 +60,12 @@ impl ToCqlValue for &Float {
6060
}
6161
}
6262

63+
impl ToCqlValue for &Double {
64+
fn to_cql_value(&self) -> CqlValue {
65+
CqlValue::Double((*self).into())
66+
}
67+
}
68+
6369
impl ToCqlValue for &Varint {
6470
fn to_cql_value(&self) -> CqlValue {
6571
CqlValue::Varint((*self).into())

src/types/double.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/// A double precision float number.
2+
///
3+
/// Due to the nature of numbers in JavaScript, it's hard to distinguish between integers and floats, so this type is used to represent
4+
/// double precision float numbers while any other JS number will be treated as an integer. (This is not the case for BigInts, which are always treated as BigInts).
5+
#[napi]
6+
#[derive(Debug, Copy, Clone, PartialEq)]
7+
pub struct Double {
8+
pub(crate) inner: f64,
9+
}
10+
11+
impl From<f64> for Double {
12+
fn from(inner: f64) -> Self {
13+
Self { inner }
14+
}
15+
}
16+
17+
impl From<&Double> for f64 {
18+
fn from(float: &Double) -> Self {
19+
float.inner
20+
}
21+
}
22+
23+
#[napi]
24+
impl Double {
25+
#[napi(constructor)]
26+
pub fn new_float(inner: f64) -> Double {
27+
Double::from(inner)
28+
}
29+
30+
#[napi]
31+
#[allow(clippy::inherent_to_string)]
32+
pub fn to_string(&self) -> String {
33+
self.inner.to_string()
34+
}
35+
}

src/types/float.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,31 @@
55
#[napi]
66
#[derive(Debug, Copy, Clone, PartialEq)]
77
pub struct Float {
8-
pub(crate) inner: f64,
8+
pub(crate) inner: f32,
99
}
1010

11-
impl From<f64> for Float {
12-
fn from(inner: f64) -> Self {
11+
impl From<f32> for Float {
12+
fn from(inner: f32) -> Self {
1313
Self { inner }
1414
}
1515
}
1616

17-
impl From<Float> for f64 {
18-
fn from(float: Float) -> Self {
19-
float.inner
20-
}
21-
}
22-
2317
impl From<&Float> for f32 {
2418
fn from(float: &Float) -> Self {
25-
float.inner as f32
19+
float.inner
2620
}
2721
}
2822

2923
#[napi]
3024
impl Float {
3125
#[napi(constructor)]
3226
pub fn new_float(inner: f64) -> Float {
33-
Float::from(inner)
27+
Float::from(inner as f32)
28+
}
29+
30+
#[napi]
31+
#[allow(clippy::inherent_to_string)]
32+
pub fn to_string(&self) -> String {
33+
self.inner.to_string()
3434
}
3535
}

src/types/list.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,10 @@ impl List {
3434
let inner = values.into_iter().map(|v| v.to_cql_value()).collect();
3535
List { inner }
3636
}
37+
38+
#[napi]
39+
#[allow(clippy::inherent_to_string)]
40+
pub fn to_string(&self) -> String {
41+
format!("{:?}", self.inner)
42+
}
3743
}

0 commit comments

Comments
 (0)