Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the expand option on the describe route #1755

Merged
merged 4 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions crates/cli/src/subcommands/describe.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::common_args;
use crate::config::Config;
use crate::util::{add_auth_header_opt, database_address, get_auth_header_only};
use clap::{Arg, ArgAction::SetTrue, ArgMatches};
use clap::{Arg, ArgMatches};

pub fn cli() -> clap::Command {
clap::Command::new("describe")
Expand All @@ -21,8 +21,6 @@ pub fn cli() -> clap::Command {
.requires("entity_type")
.help("The name of the entity to describe"),
)
.arg(Arg::new("brief").long("brief").short('b').action(SetTrue)
.help("If this flag is present, a brief description shall be returned"))
.arg(
common_args::identity()
.conflicts_with("anon_identity")
Expand All @@ -41,7 +39,6 @@ pub fn cli() -> clap::Command {

pub async fn exec(mut config: Config, args: &ArgMatches) -> Result<(), anyhow::Error> {
let database = args.get_one::<String>("database").unwrap();
let expand = !args.get_flag("brief");
let entity_name = args.get_one::<String>("entity_name");
let entity_type = args.get_one::<String>("entity_type");
let server = args.get_one::<String>("server").map(|s| s.as_ref());
Expand All @@ -64,13 +61,7 @@ pub async fn exec(mut config: Config, args: &ArgMatches) -> Result<(), anyhow::E
let auth_header = get_auth_header_only(&mut config, anon_identity, identity, server).await?;
let builder = add_auth_header_opt(builder, &auth_header);

let descr = builder
.query(&[("expand", expand)])
.send()
.await?
.error_for_status()?
.text()
.await?;
let descr = builder.send().await?.error_for_status()?.text().await?;
println!("{}", descr);

Ok(())
Expand Down
34 changes: 4 additions & 30 deletions crates/client-api/src/routes/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ impl<'a> EntityDef<'a> {
}
}

fn entity_description_json(description: WithTypespace<EntityDef>, expand: bool) -> Option<Value> {
fn entity_description_json(description: WithTypespace<EntityDef>) -> Option<Value> {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let typ = description.ty().described_entity_ty().as_str();
let len = match description.ty() {
EntityDef::Table(t) => description
Expand All @@ -233,7 +233,6 @@ fn entity_description_json(description: WithTypespace<EntityDef>, expand: bool)
.len(),
EntityDef::Reducer(r) => r.params.elements.len(),
};
if expand {
// TODO(noa): make this less hacky; needs coordination w/ spacetime-web
let schema = match description.ty() {
EntityDef::Table(table) => {
Expand All @@ -253,12 +252,6 @@ fn entity_description_json(description: WithTypespace<EntityDef>, expand: bool)
"arity": len,
"schema": schema
}))
} else {
Some(json!({
"type": typ,
"arity": len,
}))
}
}

#[derive(Deserialize)]
Expand All @@ -268,19 +261,13 @@ pub struct DescribeParams {
entity: String,
}

#[derive(Deserialize)]
pub struct DescribeQueryParams {
expand: Option<bool>,
}

pub async fn describe<S>(
State(worker_ctx): State<S>,
Path(DescribeParams {
name_or_address,
entity_type,
entity,
}): Path<DescribeParams>,
Query(DescribeQueryParams { expand }): Query<DescribeQueryParams>,
Extension(auth): Extension<SpacetimeAuth>,
) -> axum::response::Result<impl IntoResponse>
where
Expand Down Expand Up @@ -311,8 +298,7 @@ where
.ok_or_else(|| (StatusCode::NOT_FOUND, format!("{entity_type} {entity:?} not found")))?;
let description = WithTypespace::new(module.info().module_def.typespace(), &description);

let expand = expand.unwrap_or(true);
let response_json = json!({ entity: entity_description_json(description, expand) });
let response_json = json!({ entity: entity_description_json(description) });

Ok((
StatusCode::OK,
Expand Down Expand Up @@ -343,26 +329,18 @@ pub struct CatalogParams {
}
#[derive(Deserialize)]
pub struct CatalogQueryParams {
expand: Option<bool>,
#[serde(default)]
module_def: bool,
}
pub async fn catalog<S>(
State(worker_ctx): State<S>,
Path(CatalogParams { name_or_address }): Path<CatalogParams>,
Query(CatalogQueryParams { expand, module_def }): Query<CatalogQueryParams>,
Query(CatalogQueryParams { module_def }): Query<CatalogQueryParams>,
Extension(auth): Extension<SpacetimeAuth>,
) -> axum::response::Result<impl IntoResponse>
where
S: ControlStateDelegate + NodeDelegate,
{
if module_def && expand.is_some() {
return Err((
StatusCode::BAD_REQUEST,
"expand and module_def cannot both be specified",
Copy link
Collaborator Author

@bfops bfops Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coolreader18 why was this constraint here? Does it invalidate the returned thing somehow? (eg something about the typespace?)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just because they do opposite things - expand makes the hacky representation more detailed, while module_def uses a non-hacky representation.

)
.into());
}
let address = name_or_address.resolve(&worker_ctx).await?.into();
let database = worker_ctx_find_database(&worker_ctx, &address)
.await?
Expand All @@ -381,15 +359,11 @@ where
let raw = RawModuleDefV9::from(module.info().module_def.clone());
serde_json::to_value(SerializeWrapper::from_ref(&raw)).map_err(log_and_500)?
} else {
let expand = expand.unwrap_or(false);
let response_catalog: HashMap<_, _> = get_catalog(&module)
.map(|entity| {
(
entity.name().to_string().into_boxed_str(),
entity_description_json(
WithTypespace::new(module.info().module_def.typespace(), &entity),
expand,
),
entity_description_json(WithTypespace::new(module.info().module_def.typespace(), &entity)),
)
})
.collect();
Expand Down
Loading