Skip to content

Commit ef72675

Browse files
deepsource fix, coderabbitai suggestions incorporated
1 parent 02610d0 commit ef72675

File tree

1 file changed

+76
-53
lines changed

1 file changed

+76
-53
lines changed

src/event/format/json.rs

Lines changed: 76 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -283,63 +283,15 @@ fn valid_type(
283283
match field.data_type() {
284284
DataType::Boolean => value.is_boolean(),
285285
DataType::Int8 | DataType::Int16 | DataType::Int32 | DataType::Int64 => {
286-
if static_schema_flag {
287-
if let Value::String(s) = value {
288-
return s.parse::<i64>().is_ok();
289-
}
290-
}
291-
value.is_i64()
286+
validate_int(value, static_schema_flag)
292287
}
293288
DataType::UInt8 | DataType::UInt16 | DataType::UInt32 | DataType::UInt64 => value.is_u64(),
294289
DataType::Float16 | DataType::Float32 => value.is_f64(),
295-
DataType::Float64 => {
296-
if static_schema_flag {
297-
if let Value::String(s) = value.clone() {
298-
return s.parse::<f64>().is_ok() || s.parse::<i64>().is_ok();
299-
}
300-
return value.is_number();
301-
}
302-
match schema_version {
303-
SchemaVersion::V1 => value.is_number(),
304-
_ => value.is_f64(),
305-
}
306-
}
290+
DataType::Float64 => validate_float(value, schema_version, static_schema_flag),
307291
DataType::Utf8 => value.is_string(),
308-
DataType::List(field) => {
309-
if let Value::Array(arr) = value {
310-
for elem in arr {
311-
if elem.is_null() {
312-
continue;
313-
}
314-
if !valid_type(field, elem, schema_version, static_schema_flag) {
315-
return false;
316-
}
317-
}
318-
}
319-
true
320-
}
292+
DataType::List(field) => validate_list(field, value, schema_version, static_schema_flag),
321293
DataType::Struct(fields) => {
322-
if let Value::Object(val) = value {
323-
for (key, value) in val {
324-
let field = (0..fields.len())
325-
.find(|idx| fields[*idx].name() == key)
326-
.map(|idx| &fields[idx]);
327-
328-
if let Some(field) = field {
329-
if value.is_null() {
330-
continue;
331-
}
332-
if !valid_type(field, value, schema_version, static_schema_flag) {
333-
return false;
334-
}
335-
} else {
336-
return false;
337-
}
338-
}
339-
true
340-
} else {
341-
false
342-
}
294+
validate_struct(fields, value, schema_version, static_schema_flag)
343295
}
344296
DataType::Timestamp(_, _) => value.is_string() || value.is_number(),
345297
_ => {
@@ -348,8 +300,79 @@ fn valid_type(
348300
field.data_type(),
349301
value
350302
);
351-
unreachable!()
303+
false
304+
}
305+
}
306+
}
307+
308+
fn validate_int(value: &Value, static_schema_flag: bool) -> bool {
309+
// allow casting string to int for static schema
310+
if static_schema_flag {
311+
if let Value::String(s) = value {
312+
return s.trim().parse::<i64>().is_ok();
313+
}
314+
}
315+
value.is_i64()
316+
}
317+
318+
fn validate_float(value: &Value, schema_version: SchemaVersion, static_schema_flag: bool) -> bool {
319+
// allow casting string to int for static schema
320+
if static_schema_flag {
321+
if let Value::String(s) = value.clone() {
322+
let trimmed = s.trim();
323+
return trimmed.parse::<f64>().is_ok() || trimmed.parse::<i64>().is_ok();
324+
}
325+
return value.is_number();
326+
}
327+
match schema_version {
328+
SchemaVersion::V1 => value.is_number(),
329+
_ => value.is_f64(),
330+
}
331+
}
332+
333+
fn validate_list(
334+
field: &Field,
335+
value: &Value,
336+
schema_version: SchemaVersion,
337+
static_schema_flag: bool,
338+
) -> bool {
339+
if let Value::Array(arr) = value {
340+
for elem in arr {
341+
if elem.is_null() {
342+
continue;
343+
}
344+
if !valid_type(field, elem, schema_version, static_schema_flag) {
345+
return false;
346+
}
347+
}
348+
}
349+
true
350+
}
351+
352+
fn validate_struct(
353+
fields: &Fields,
354+
value: &Value,
355+
schema_version: SchemaVersion,
356+
static_schema_flag: bool,
357+
) -> bool {
358+
if let Value::Object(val) = value {
359+
for (key, value) in val {
360+
let field = fields.iter().find(|f| f.name() == key);
361+
362+
if let Some(field) = field {
363+
if value.is_null() {
364+
continue;
365+
}
366+
if !valid_type(field, value, schema_version, static_schema_flag) {
367+
return false;
368+
}
369+
} else {
370+
return false;
371+
}
352372
}
373+
true
374+
} else {
375+
false
353376
}
354377
}
355378

0 commit comments

Comments
 (0)