Skip to content

Commit

Permalink
Support null in JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoh committed Mar 15, 2018
1 parent 7560b88 commit 31d852d
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ag"
version = "0.1.0"
version = "0.3.2"
authors = ["Russell Cohen <[email protected]>"]

[dependencies]
Expand Down
7 changes: 3 additions & 4 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub enum Value {
// Consider big int
Int(i64),
Float(f64),
None,
}

impl OrdSubset for Value {
Expand All @@ -49,6 +50,7 @@ impl Display for Value {
&Value::Str(ref s) => write!(f, "{}", s),
&Value::Int(ref s) => write!(f, "{}", s),
&Value::Float(ref s) => write!(f, "{}", s),
&Value::None => write!(f, "$None$"),
}
}
}
Expand All @@ -58,14 +60,11 @@ impl Value {
match self {
&Value::Str(ref s) => format!("{}", s),
&Value::Int(ref s) => format!("{}", s),
&Value::None => format!("$None$"),
&Value::Float(ref s) => format!("{:.*}", render_config.floating_points, s),
}
}

pub fn no_value() -> Value {
Value::Str("$None$".to_string())
}

pub fn from_string(s: &str) -> Value {
let int_value = s.parse::<i64>();
let float_value = s.parse::<f64>();
Expand Down
8 changes: 5 additions & 3 deletions src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl AggregateFunction for Percentile {
let pct_opt = self.ckms.query(self.percentile);
pct_opt
.map(|(_usize, pct_float)| data::Value::Float(pct_float))
.unwrap_or(data::Value::no_value())
.unwrap_or(data::Value::None)
}
}

Expand Down Expand Up @@ -298,6 +298,7 @@ impl UnaryPreAggOperator for ParseJson {
&&Value::String(ref s) => {
Some(record.put(k, data::Value::Str(s.to_string())))
}
&&Value::Null => Some(record.put(k, data::Value::None)),
_other => None,
})
})
Expand All @@ -320,15 +321,16 @@ mod tests {

#[test]
fn test_json() {
let rec = Record::new(r#"{"k1": 5, "k2": 5.5, "k3": "str"}"#);
let rec = Record::new(r#"{"k1": 5, "k2": 5.5, "k3": "str", "k4": null}"#);
let parser = ParseJson {};
let rec = parser.process(&rec).unwrap();
assert_eq!(
rec.data,
hashmap!{
"k1".to_string() => Value::Int(5),
"k2".to_string() => Value::Float(5.5),
"k3".to_string() => Value::Str("str".to_string())
"k3".to_string() => Value::Str("str".to_string()),
"k4".to_string() => Value::None
}
);
}
Expand Down
10 changes: 7 additions & 3 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,13 @@ impl PrettyPrinter {
.iter()
.map(|column_name| {
let value = record.data.get(column_name);
let or_none = data::Value::no_value();
let value = value.unwrap_or(&or_none);
let unpadded = format!("[{}={}]", column_name, value.render(&self.render_config));

let unpadded = match value {
Some(value) => {
format!("[{}={}]", column_name, value.render(&self.render_config))
}
None => "".to_string(),
};
if no_padding {
unpadded
} else {
Expand Down
1 change: 1 addition & 0 deletions test_files/test_json.log
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
{"level": "error", "message": "Oh now an error!"}
{"level": "info", "message": "A thing happened", "num_things": 12}
{"level": "info", "message": "A different event", "event_duration": 1002.5}
{"level": null}

0 comments on commit 31d852d

Please sign in to comment.