Skip to content

Commit

Permalink
feat: add json support
Browse files Browse the repository at this point in the history
  • Loading branch information
groobyming committed May 23, 2024
1 parent 496f0a8 commit 5ee02a9
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/models/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ mod tests {
let ty = RawPrestoTy::Char;
let s = serde_json::to_string(&ty).unwrap();
assert_eq!(s, "\"char\"");

let ty = RawPrestoTy::Json;
let s = serde_json::to_string(&ty).unwrap();
assert_eq!(s, "\"json\"");
}

#[test]
Expand All @@ -162,6 +166,10 @@ mod tests {
let ty = serde_json::from_str::<RawPrestoTy>(data).unwrap();
assert_eq!(ty, RawPrestoTy::Char);

let data = "\"json\"";
let ty = serde_json::from_str::<RawPrestoTy>(data).unwrap();
assert_eq!(ty, RawPrestoTy::Json);

let invalid = "\"xxx\"";
let res = serde_json::from_str::<RawPrestoTy>(invalid);
assert!(res.is_err());
Expand Down
37 changes: 37 additions & 0 deletions src/types/json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::{Context, Presto, PrestoTy};
use serde::de::DeserializeSeed;
use serde::{Deserialize, Deserializer};
use serde_json::Value;

impl Presto for Value {
type ValueType<'a> = &'a Value;
type Seed<'a, 'de> = ValueSeed;

fn value(&self) -> Self::ValueType<'_> {
self
}

fn ty() -> PrestoTy {
PrestoTy::Json
}

fn seed<'a, 'de>(ctx: &'a Context<'a>) -> Self::Seed<'a, 'de> {

Check warning on line 18 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, stable)

unused variable: `ctx`

Check warning on line 18 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, stable)

unused variable: `ctx`

Check warning on line 18 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, stable)

unused variable: `ctx`

Check warning on line 18 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, nightly)

unused variable: `ctx`

Check warning on line 18 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, nightly)

unused variable: `ctx`

Check warning on line 18 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, nightly)

unused variable: `ctx`

Check warning on line 18 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, nightly)

unused variable: `ctx`

Check warning on line 18 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, stable)

unused variable: `ctx`

Check warning on line 18 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, stable)

unused variable: `ctx`

Check warning on line 18 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, stable)

unused variable: `ctx`

Check warning on line 18 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, nightly)

unused variable: `ctx`

Check warning on line 18 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, nightly)

unused variable: `ctx`

Check warning on line 18 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, nightly)

unused variable: `ctx`

Check warning on line 18 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, nightly)

unused variable: `ctx`

Check warning on line 18 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, stable)

unused variable: `ctx`

Check warning on line 18 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, stable)

unused variable: `ctx`

Check warning on line 18 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, stable)

unused variable: `ctx`

Check warning on line 18 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, nightly)

unused variable: `ctx`

Check warning on line 18 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, nightly)

unused variable: `ctx`

Check warning on line 18 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, nightly)

unused variable: `ctx`

Check warning on line 18 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, nightly)

unused variable: `ctx`
ValueSeed
}

fn empty() -> Self {

Check warning on line 22 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, stable)

function cannot return without recursing

Check warning on line 22 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, stable)

function cannot return without recursing

Check warning on line 22 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, stable)

function cannot return without recursing

Check warning on line 22 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, nightly)

function cannot return without recursing

Check warning on line 22 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, nightly)

function cannot return without recursing

Check warning on line 22 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, nightly)

function cannot return without recursing

Check warning on line 22 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, nightly)

function cannot return without recursing

Check warning on line 22 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, stable)

function cannot return without recursing

Check warning on line 22 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, stable)

function cannot return without recursing

Check warning on line 22 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, stable)

function cannot return without recursing

Check warning on line 22 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, nightly)

function cannot return without recursing

Check warning on line 22 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, nightly)

function cannot return without recursing

Check warning on line 22 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, nightly)

function cannot return without recursing

Check warning on line 22 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, nightly)

function cannot return without recursing

Check warning on line 22 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, stable)

function cannot return without recursing

Check warning on line 22 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, stable)

function cannot return without recursing

Check warning on line 22 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, stable)

function cannot return without recursing

Check warning on line 22 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, nightly)

function cannot return without recursing

Check warning on line 22 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, nightly)

function cannot return without recursing

Check warning on line 22 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, nightly)

function cannot return without recursing

Check warning on line 22 in src/types/json.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, nightly)

function cannot return without recursing
Value::empty()
}
}

pub struct ValueSeed;

impl<'de> DeserializeSeed<'de> for ValueSeed {
type Value = Value;
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where
D: Deserializer<'de>,
{
<Value as Deserialize<'de>>::deserialize(deserializer)
}
}
10 changes: 9 additions & 1 deletion src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod integer;
mod interval_day_to_second;
mod interval_year_to_month;
mod ip_address;
pub mod json;
mod map;
mod option;
mod row;
Expand Down Expand Up @@ -43,9 +44,10 @@ use std::sync::Arc;

use derive_more::Display;
use iterable::*;
use serde::de::DeserializeSeed;
use serde::de::{DeserializeSeed, IntoDeserializer};

Check warning on line 47 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, stable)

unused import: `IntoDeserializer`

Check warning on line 47 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, stable)

unused import: `IntoDeserializer`

Check warning on line 47 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, stable)

unused import: `IntoDeserializer`

Check warning on line 47 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, nightly)

unused import: `IntoDeserializer`

Check warning on line 47 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, nightly)

unused import: `IntoDeserializer`

Check warning on line 47 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, nightly)

unused import: `IntoDeserializer`

Check warning on line 47 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, nightly)

unused import: `IntoDeserializer`

Check warning on line 47 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, stable)

unused import: `IntoDeserializer`

Check warning on line 47 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, stable)

unused import: `IntoDeserializer`

Check warning on line 47 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, stable)

unused import: `IntoDeserializer`

Check warning on line 47 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, nightly)

unused import: `IntoDeserializer`

Check warning on line 47 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, nightly)

unused import: `IntoDeserializer`

Check warning on line 47 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, nightly)

unused import: `IntoDeserializer`

Check warning on line 47 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, nightly)

unused import: `IntoDeserializer`

Check warning on line 47 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, stable)

unused import: `IntoDeserializer`

Check warning on line 47 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, stable)

unused import: `IntoDeserializer`

Check warning on line 47 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, stable)

unused import: `IntoDeserializer`

Check warning on line 47 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, nightly)

unused import: `IntoDeserializer`

Check warning on line 47 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, nightly)

unused import: `IntoDeserializer`

Check warning on line 47 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, nightly)

unused import: `IntoDeserializer`

Check warning on line 47 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, nightly)

unused import: `IntoDeserializer`
use serde::Serialize;

use crate::PrestoTy::Uuid;

Check warning on line 50 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, stable)

unused import: `crate::PrestoTy::Uuid`

Check warning on line 50 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, stable)

unused import: `crate::PrestoTy::Uuid`

Check warning on line 50 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, stable)

unused import: `crate::PrestoTy::Uuid`

Check warning on line 50 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, nightly)

unused import: `crate::PrestoTy::Uuid`

Check warning on line 50 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, nightly)

unused import: `crate::PrestoTy::Uuid`

Check warning on line 50 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, nightly)

unused import: `crate::PrestoTy::Uuid`

Check warning on line 50 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (ubuntu-latest, nightly)

unused import: `crate::PrestoTy::Uuid`

Check warning on line 50 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, stable)

unused import: `crate::PrestoTy::Uuid`

Check warning on line 50 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, stable)

unused import: `crate::PrestoTy::Uuid`

Check warning on line 50 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, stable)

unused import: `crate::PrestoTy::Uuid`

Check warning on line 50 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, nightly)

unused import: `crate::PrestoTy::Uuid`

Check warning on line 50 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, nightly)

unused import: `crate::PrestoTy::Uuid`

Check warning on line 50 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, nightly)

unused import: `crate::PrestoTy::Uuid`

Check warning on line 50 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (windows-latest, nightly)

unused import: `crate::PrestoTy::Uuid`

Check warning on line 50 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, stable)

unused import: `crate::PrestoTy::Uuid`

Check warning on line 50 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, stable)

unused import: `crate::PrestoTy::Uuid`

Check warning on line 50 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, stable)

unused import: `crate::PrestoTy::Uuid`

Check warning on line 50 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, nightly)

unused import: `crate::PrestoTy::Uuid`

Check warning on line 50 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, nightly)

unused import: `crate::PrestoTy::Uuid`

Check warning on line 50 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, nightly)

unused import: `crate::PrestoTy::Uuid`

Check warning on line 50 in src/types/mod.rs

View workflow job for this annotation

GitHub Actions / build_and_test (macos-latest, nightly)

unused import: `crate::PrestoTy::Uuid`
use crate::{
ClientTypeSignatureParameter, Column, NamedTypeSignature, RawPrestoTy, RowFieldName,
TypeSignature,
Expand Down Expand Up @@ -165,6 +167,7 @@ fn extract(target: &PrestoTy, provided: &PrestoTy) -> Result<Vec<(usize, Vec<usi
(Map(t1k, t1v), Map(t2k, t2v)) => Ok(extract(t1k, t2k)?.chain(extract(t1v, t2v)?)),
(IpAddress, IpAddress) => Ok(vec![]),
(Uuid, Uuid) => Ok(vec![]),
(Json, Json) => Ok(vec![]),
_ => Err(Error::InvalidPrestoType),
}
}
Expand Down Expand Up @@ -194,6 +197,7 @@ pub enum PrestoTy {
Map(Box<PrestoTy>, Box<PrestoTy>),
Decimal(usize, usize),
IpAddress,
Json,
Unknown,
}

Expand Down Expand Up @@ -303,6 +307,7 @@ impl PrestoTy {
}
RawPrestoTy::IpAddress => PrestoTy::IpAddress,
RawPrestoTy::Uuid => PrestoTy::Uuid,
RawPrestoTy::Json => PrestoTy::Json,
_ => return Err(Error::InvalidTypeSignature),
};

Expand Down Expand Up @@ -367,6 +372,7 @@ impl PrestoTy {
],
IpAddress => vec![],
Uuid => vec![],
Json => vec![],
};

TypeSignature::new(raw_ty, params)
Expand Down Expand Up @@ -412,6 +418,7 @@ impl PrestoTy {
.into(),
IpAddress => RawPrestoTy::IpAddress.to_str().into(),
Uuid => RawPrestoTy::Uuid.to_str().into(),
Json => RawPrestoTy::Json.to_str().into(),
}
}

Expand All @@ -438,6 +445,7 @@ impl PrestoTy {
Map(_, _) => RawPrestoTy::Map,
IpAddress => RawPrestoTy::IpAddress,
Uuid => RawPrestoTy::Uuid,
Json => RawPrestoTy::Json,
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions tests/data/types/json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"columns": [
{
"name": "a",
"type": "json",
"typeSignature": {
"rawType": "json",
"typeArguments": [],
"literalArguments": [],
"arguments": []
}
}
],
"data": [
[
"abc"
]
]
}
13 changes: 13 additions & 0 deletions tests/data_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ fn split(v: Value) -> Option<(Vec<Column>, Value)> {
}
}

#[test]
fn test_json() {
let (s, v) = read("json");
let d = serde_json::from_str::<DataSet<Row>>(&s).unwrap();
assert_ds(d.clone(), v);
let d = d.into_vec();
assert_eq!(d.len(), 1);
assert_eq!(
d[0].clone().into_json(),
vec![Value::String("abc".to_string())]
);
}

#[test]
fn test_char() {
#[derive(Presto, Eq, PartialEq, Debug, Clone)]
Expand Down

0 comments on commit 5ee02a9

Please sign in to comment.