Skip to content

Commit f03a16f

Browse files
committed
Refactor Line Proto Serializer with References to Line Proto Docs
1 parent 5eecd0b commit f03a16f

File tree

1 file changed

+55
-25
lines changed

1 file changed

+55
-25
lines changed

influxdb/src/query/line_proto_term.rs

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,41 +37,26 @@ impl LineProtoTerm<'_> {
3737
}
3838
}
3939

40+
/// Serializes Field Values.
4041
fn escape_field_value(v: &Type, use_v2: bool) -> String {
4142
use Type::*;
4243
match v {
43-
Boolean(v) => {
44-
if *v {
45-
"true"
46-
} else {
47-
"false"
48-
}
49-
}
50-
.to_string(),
51-
Float(v) => v.to_string(),
52-
SignedInteger(v) => format!("{}i", v),
53-
UnsignedInteger(v) => {
54-
if use_v2 {
55-
format!("{}u", v)
56-
} else {
57-
format!("{}i", v)
58-
}
59-
}
44+
Boolean(v) => Self::escape_boolean(v),
45+
Float(v) => Self::escape_float(v),
46+
SignedInteger(v) => Self::escape_signed_integer(v),
47+
UnsignedInteger(v) => Self::escape_unsigned_integer(v, use_v2),
6048
Text(v) => format!(r#""{}""#, Self::escape_any(v, &QUOTES_SLASHES)),
6149
}
6250
}
6351

52+
/// Serializes Tag Values. InfluxDB stores tag values as strings, so we format everything to string.
53+
///
54+
/// V2: https://docs.influxdata.com/influxdb/cloud/reference/syntax/line-protocol/#tag-set
55+
/// V1: https://docs.influxdata.com/influxdb/v1/write_protocols/line_protocol_tutorial/#data-types
6456
fn escape_tag_value(v: &Type) -> String {
6557
use Type::*;
6658
match v {
67-
Boolean(v) => {
68-
if *v {
69-
"true"
70-
} else {
71-
"false"
72-
}
73-
}
74-
.to_string(),
59+
Boolean(v) => Self::escape_boolean(v),
7560
Float(v) => format!(r#"{}"#, v),
7661
SignedInteger(v) => format!(r#"{}"#, v),
7762
UnsignedInteger(v) => format!(r#"{}"#, v),
@@ -82,6 +67,51 @@ impl LineProtoTerm<'_> {
8267
fn escape_any(s: &str, re: &Regex) -> String {
8368
re.replace_all(s, r"\$0").to_string()
8469
}
70+
71+
/// Escapes a Rust f64 to InfluxDB Line Protocol
72+
///
73+
/// https://docs.influxdata.com/influxdb/cloud/reference/syntax/line-protocol/#float
74+
/// IEEE-754 64-bit floating-point numbers. Default numerical type.
75+
/// InfluxDB supports scientific notation in float field values, but this crate does not serialize them.
76+
fn escape_float(v: &f64) -> String {
77+
v.to_string()
78+
}
79+
80+
/// Escapes a Rust bool to InfluxDB Line Protocol
81+
///
82+
/// https://docs.influxdata.com/influxdb/cloud/reference/syntax/line-protocol/#boolean
83+
/// Stores true or false values.
84+
fn escape_boolean(v: &bool) -> String {
85+
if *v {
86+
"true"
87+
} else {
88+
"false"
89+
}
90+
.to_string()
91+
}
92+
93+
/// Escapes a Rust i64 to InfluxDB Line Protocol
94+
///
95+
/// https://docs.influxdata.com/influxdb/cloud/reference/syntax/line-protocol/#integer
96+
/// Signed 64-bit integers. Trailing i on the number specifies an integer.
97+
fn escape_signed_integer(v: &i64) -> String {
98+
format!("{}i", v)
99+
}
100+
101+
/// Escapes a Rust u64 to InfluxDB Line Protocol
102+
///
103+
/// https://docs.influxdata.com/influxdb/cloud/reference/syntax/line-protocol/#uinteger
104+
/// Unsigned 64-bit integers. Trailing u on the number specifies an unsigned integer.
105+
///
106+
/// InfluxDB version 1 does not know unsigned, we fallback to (signed) integer:
107+
/// https://docs.influxdata.com/influxdb/v1/write_protocols/line_protocol_tutorial/#data-types
108+
fn escape_unsigned_integer(v: &u64, use_v2: bool) -> String {
109+
if use_v2 {
110+
format!("{}u", v)
111+
} else {
112+
format!("{}i", v)
113+
}
114+
}
85115
}
86116

87117
#[cfg(test)]

0 commit comments

Comments
 (0)