Skip to content

Commit 4d101b0

Browse files
committed
Improve documentation and tests, add From impl
1 parent 3605b4f commit 4d101b0

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/ast/value.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,40 @@ use crate::{ast::Ident, tokenizer::Span};
3030
#[cfg(feature = "visitor")]
3131
use sqlparser_derive::{Visit, VisitMut};
3232

33-
/// Primitive SQL values such as number and string
33+
/// Wraps a primitive SQL [`Value`] with its [`Span`] location
34+
///
35+
/// # Example: create a `ValueWithSpan` from a `Value`
36+
/// ```
37+
/// # use sqlparser::ast::{Value, ValueWithSpan};
38+
/// # use sqlparser::tokenizer::{Location, Span};
39+
/// let value = Value::SingleQuotedString(String::from("endpoint"));
40+
/// // from line 1, column 1 to line 1, column 7
41+
/// let span = Span::new(Location::new(1, 1), Location::new(1, 7));
42+
/// let value_with_span = value.with_span(span);
43+
/// ```
44+
///
45+
/// # Example: create a `ValueWithSpan` from a `Value` with an empty span
46+
///
47+
/// You can call [`Value::with_empty_span`] to create a `ValueWithSpan` with an empty span
48+
/// ```
49+
/// # use sqlparser::ast::{Value, ValueWithSpan};
50+
/// # use sqlparser::tokenizer::{Location, Span};
51+
/// let value = Value::SingleQuotedString(String::from("endpoint"));
52+
/// let value_with_span = value.with_empty_span();
53+
/// assert_eq!(value_with_span.span, Span::empty());
54+
/// ```
55+
///
56+
/// You can also use the [`From`] trait to convert `ValueWithSpan` to/from `Value`s
57+
/// ```
58+
/// # use sqlparser::ast::{Value, ValueWithSpan};
59+
/// # use sqlparser::tokenizer::{Location, Span};
60+
/// let value = Value::SingleQuotedString(String::from("endpoint"));
61+
/// // converting `Value` to `ValueWithSpan` results in an empty span
62+
/// let value_with_span: ValueWithSpan = value.into();
63+
/// assert_eq!(value_with_span.span, Span::empty());
64+
/// // convert back to `Value`
65+
/// let value: Value = value_with_span.into();
66+
/// ```
3467
#[derive(Debug, Clone, Eq, Ord)]
3568
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3669
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
@@ -63,6 +96,12 @@ impl From<ValueWithSpan> for Value {
6396
}
6497
}
6598

99+
impl From<Value> for ValueWithSpan {
100+
fn from(value: Value) -> Self {
101+
value.with_empty_span()
102+
}
103+
}
104+
66105
/// Primitive SQL values such as number and string
67106
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
68107
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]

tests/sqlparser_clickhouse.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn parse_map_access_expr() {
5555
"indexOf",
5656
[
5757
Expr::Identifier(Ident::new("string_names")),
58-
Expr::Value(Value::SingleQuotedString("endpoint".to_string()))
58+
Expr::Value(Value::SingleQuotedString("endpoint".to_string()).into())
5959
]
6060
),
6161
})],

0 commit comments

Comments
 (0)