Skip to content

Commit

Permalink
Improve date parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
eandre committed Oct 24, 2024
1 parent 7e625af commit 86eff89
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 21 deletions.
2 changes: 1 addition & 1 deletion runtimes/core/src/api/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Error {
Self {
code: ErrCode::InvalidArgument,
message: public_msg.into(),
internal_message: Some(format!("{:#?}", cause.into())),
internal_message: Some(format!("{:?}", cause.into())),
stack: None,
details: None,
}
Expand Down
10 changes: 10 additions & 0 deletions runtimes/core/src/api/jsonschema/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,16 @@ fn parse_basic_str(basic: &Basic, str: &str) -> APIResult<PValue> {
details: None,
}),

Basic::DateTime => api::DateTime::parse_from_rfc3339(str)
.map(PValue::DateTime)
.map_err(|_err| api::Error {
code: api::ErrCode::InvalidArgument,
message: "invalid datetime".to_string(),
internal_message: Some(format!("invalid datetime string {:?}", str)),
stack: None,
details: None,
}),

_ => Err(api::Error {
code: api::ErrCode::InvalidArgument,
message: "invalid value".to_string(),
Expand Down
1 change: 0 additions & 1 deletion runtimes/core/src/pubsub/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub type MessageId = String;

pub struct MessageData {
pub attrs: HashMap<String, String>,
// pub body: Option<serde_json::Value>,
pub raw_body: Vec<u8>,
}

Expand Down
22 changes: 11 additions & 11 deletions runtimes/js/encore.dev/api/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ export type Method =
| "CONNECT";

export type Header<
TypeOrName extends string | number | boolean = string,
TypeOrName extends string | number | boolean | Date = string,
Name extends string = ""
> = TypeOrName extends string ? string : TypeOrName;

export type Query<
TypeOrName extends string | number | boolean = string,
TypeOrName extends string | number | boolean | Date = string,
Name extends string = ""
> = TypeOrName extends string ? string : TypeOrName;

Expand Down Expand Up @@ -141,21 +141,21 @@ export interface StreamOut<Response> {

export type StreamInOutHandlerFn<HandshakeData, Request, Response> =
HandshakeData extends void
? (stream: StreamInOut<Request, Response>) => Promise<void>
: (
data: HandshakeData,
stream: StreamInOut<Request, Response>
) => Promise<void>;
? (stream: StreamInOut<Request, Response>) => Promise<void>
: (
data: HandshakeData,
stream: StreamInOut<Request, Response>
) => Promise<void>;

export type StreamOutHandlerFn<HandshakeData, Response> =
HandshakeData extends void
? (stream: StreamOut<Response>) => Promise<void>
: (data: HandshakeData, stream: StreamOut<Response>) => Promise<void>;
? (stream: StreamOut<Response>) => Promise<void>
: (data: HandshakeData, stream: StreamOut<Response>) => Promise<void>;

export type StreamInHandlerFn<HandshakeData, Request, Response> =
HandshakeData extends void
? (stream: StreamIn<Request>) => Promise<Response>
: (data: HandshakeData, stream: StreamIn<Request>) => Promise<Response>;
? (stream: StreamIn<Request>) => Promise<Response>
: (data: HandshakeData, stream: StreamIn<Request>) => Promise<Response>;

export type StreamInOut<Request, Response> = StreamIn<Request> &
StreamOut<Response>;
Expand Down
1 change: 1 addition & 0 deletions runtimes/js/src/pvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ impl ToNapiValue for &PVals {

impl FromNapiValue for PVals {
unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result<Self> {
assert_type_of!(env, napi_val, ValueType::Object)?;
let obj = JsObject::from_napi_value(env, napi_val)?;

let mut map = PVals(PValues::new());
Expand Down
23 changes: 15 additions & 8 deletions tsparser/src/parser/resources/apis/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub struct Param {
pub loc: ParamData,
pub typ: Type,
pub optional: bool,
pub range: Range,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -487,6 +488,7 @@ fn extract_path_params(path: &Path, fields: &mut FieldMap) -> anyhow::Result<Vec
loc: ParamData::Path { index },
typ: f.typ.clone(),
optional: f.optional,
range: f.range,
});
}

Expand Down Expand Up @@ -523,6 +525,7 @@ fn extract_loc_params(fields: &FieldMap, default_loc: ParamLocation) -> Vec<Para
loc: param_data,
typ: f.typ.clone(),
optional: f.optional,
range: f.range,
});
}
params
Expand All @@ -536,13 +539,17 @@ fn rewrite_path_types(req: &RequestEncoding, path: Path, raw: bool) -> anyhow::R
.map(|param| (&param.name, param))
.collect::<HashMap<_, _>>();

let typ_to_value_type = |typ: &Type| {
Ok(match typ {
Type::Basic(Basic::String) => ValueType::String,
Type::Basic(Basic::Boolean) => ValueType::Bool,
Type::Basic(Basic::Number | Basic::BigInt) => ValueType::Int,
typ => anyhow::bail!("unsupported path param type: {:?}", typ),
})
let typ_to_value_type = |param: &Param| match &param.typ {
Type::Basic(Basic::String) => ValueType::String,
Type::Basic(Basic::Boolean) => ValueType::Bool,
Type::Basic(Basic::Number | Basic::BigInt) => ValueType::Int,
typ => {
param
.range
.to_span()
.err(&format!("unsupported path parameter type: {:?}", typ));
ValueType::String
}
};

let mut segments = Vec::with_capacity(path.segments.len());
Expand All @@ -551,7 +558,7 @@ fn rewrite_path_types(req: &RequestEncoding, path: Path, raw: bool) -> anyhow::R
Segment::Param { name, .. } => {
// Get the value type of the path parameter.
let value_type = match path_params.get(&name) {
Some(param) => typ_to_value_type(&param.typ)?,
Some(param) => typ_to_value_type(param),
None => {
// Raw endpoints assume path params are strings.
if raw {
Expand Down

0 comments on commit 86eff89

Please sign in to comment.