Skip to content

Commit

Permalink
Merge pull request #5204 from systeminit/zack/mgmt-fns-create-views
Browse files Browse the repository at this point in the history
feat: mgmt fns can create views, create components in views
  • Loading branch information
zacharyhamm authored Jan 3, 2025
2 parents 3849bf5 + 98142f3 commit b85d66d
Show file tree
Hide file tree
Showing 13 changed files with 740 additions and 381 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

20 changes: 11 additions & 9 deletions bin/lang-js/src/function_kinds/management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,18 @@ export interface ManagmentConnect {
}
}

export interface ManagementCreate {
[key: string]: {
kind: string;
properties?: object;
geometry?: Geometry | { [key: string]: Geometry };
parent?: string;
connect?: ManagmentConnect[],
}
}

export interface ManagementOperations {
create?: {
[key: string]: {
kind: string;
properties?: object;
geometry?: Geometry;
parent?: string;
connect?: ManagmentConnect[],
}
};
create?: ManagementCreate,
update?: {
[key: string]: {
properties?: object;
Expand Down
1 change: 1 addition & 0 deletions lib/dal-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ si-crypto = { path = "../../lib/si-crypto" }
si-data-nats = { path = "../../lib/si-data-nats" }
si-data-pg = { path = "../../lib/si-data-pg" }
si-events = { path = "../../lib/si-events-rs" }
si-id = { path = "../../lib/si-id" }
si-jwt-public-key = { path = "../../lib/si-jwt-public-key" }
si-layer-cache = { path = "../../lib/si-layer-cache" }
si-pkg = { path = "../../lib/si-pkg" }
Expand Down
73 changes: 71 additions & 2 deletions lib/dal-test/src/test_exclusive_schemas/legos/small.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,61 @@ pub(crate) async fn migrate_test_exclusive_schema_small_odd_lego(
let deeply_nested_children =
build_management_func(deeply_nested_children_code, "test:deeplyNestedChildren")?;

let create_component_in_other_views_code = r#"
async function main({ thisComponent, currentView }: Input): Promise<Output> {
const thisView = thisComponent.properties?.si?.resourceId ?? currentView;
const name = `component in ${thisView}`;
return {
status: "ok",
ops: {
create: {
[name]: {
geometry: {
[currentView]: { x: 100, y: 100 },
[thisView]: { x: 15, y: 15 }
}
}
}
}
}
}
"#;

let create_component_in_other_views = build_management_func(
create_component_in_other_views_code,
"test:createComponentsInOtherViews",
)?;

let create_view_and_component_in_view_code = r#"
async function main({ thisComponent, currentView }: Input): Promise<Output> {
const thisView = thisComponent.properties?.si?.resourceId ?? currentView;
const componentName = `component in ${thisView}`;
return {
status: "ok",
ops: {
views: {
create: [thisView],
},
create: {
[componentName]: {
geometry: {
[thisView]: { x: 315, y: 315 }
}
}
}
}
}
}
"#;
let create_view_and_component_in_view = build_management_func(
create_view_and_component_in_view_code,
"test:createViewAndComponentInView",
)?;

let fn_name = "test:deleteActionSmallLego";
let delete_action_func = build_action_func(delete_action_code, fn_name)?;

Expand Down Expand Up @@ -466,11 +521,23 @@ pub(crate) async fn migrate_test_exclusive_schema_small_odd_lego(
.func_unique_id(&create_and_connect_to_self_as_children_func.unique_id)
.build()?,
)
.management_func(
ManagementFuncSpec::builder()
.name("Create in Other Views")
.func_unique_id(&create_component_in_other_views.unique_id)
.build()?,
)
.management_func(
ManagementFuncSpec::builder()
.name("Create View and Component in View")
.func_unique_id(&create_view_and_component_in_view.unique_id)
.build()?,
)
.build()?,
)
.build()?;

let small_lego_spec = small_lego_builder
let small_odd_lego_spec = small_lego_builder
.func(identity_func_spec)
.func(refresh_action_func)
.func(create_action_func)
Expand All @@ -486,10 +553,12 @@ pub(crate) async fn migrate_test_exclusive_schema_small_odd_lego(
.func(create_and_connect_to_self_func)
.func(create_and_connect_to_self_as_children_func)
.func(deeply_nested_children)
.func(create_component_in_other_views)
.func(create_view_and_component_in_view)
.schema(small_lego_schema)
.build()?;

let pkg = SiPkg::load_from_spec(small_lego_spec)?;
let pkg = SiPkg::load_from_spec(small_odd_lego_spec)?;
import_pkg_from_pkg(
ctx,
&pkg,
Expand Down
1 change: 1 addition & 0 deletions lib/dal/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ rust_test(
"//lib/pending-events:pending-events",
"//lib/rebaser-server:rebaser-server",
"//lib/si-events-rs:si-events",
"//lib/si-id:si-id",
"//lib/si-frontend-types-rs:si-frontend-types",
"//lib/si-layer-cache:si-layer-cache",
"//lib/si-pkg:si-pkg",
Expand Down
4 changes: 2 additions & 2 deletions lib/dal/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1487,7 +1487,7 @@ impl Component {
view_id: ViewId,
) -> ComponentResult<Geometry> {
let mut geometry_pre = self.geometry(ctx, view_id).await?;
if geometry_pre.clone().into_raw() != raw_geometry {
if geometry_pre.into_raw() != raw_geometry {
geometry_pre
.update(ctx, raw_geometry)
.await
Expand Down Expand Up @@ -3594,7 +3594,7 @@ impl Component {

Some(GeometryAndView {
view_id,
geometry: geometry.clone().into_raw(),
geometry: geometry.into_raw(),
})
} else {
None
Expand Down
44 changes: 43 additions & 1 deletion lib/dal/src/diagram/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use jwt_simple::prelude::{Deserialize, Serialize};
use si_events::ulid::Ulid;
use si_events::ContentHash;
pub use si_frontend_types::RawGeometry;
use std::collections::HashMap;
use std::sync::Arc;

const DEFAULT_COMPONENT_X_POSITION: &str = "0";
Expand All @@ -41,7 +42,7 @@ pub enum GeometryRepresents {
}

/// Represents spatial data for something to be shown on a view
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, Copy)]
pub struct Geometry {
id: GeometryId,
#[serde(flatten)]
Expand Down Expand Up @@ -264,6 +265,47 @@ impl Geometry {
Ok(geometries)
}

/// Returns all geometries for a component in a map, keyed by the view id
pub async fn by_view_for_component_id(
ctx: &DalContext,
component_id: ComponentId,
) -> DiagramResult<HashMap<ViewId, Self>> {
let mut result = HashMap::new();
let snap = ctx.workspace_snapshot()?;

for geometry_idx in snap
.incoming_sources_for_edge_weight_kind(
component_id,
EdgeWeightKindDiscriminants::Represents,
)
.await?
{
let NodeWeight::Geometry(geo_inner) = snap.get_node_weight(geometry_idx).await? else {
continue;
};

let geo_id = geo_inner.id();

let Some(view_idx) = snap
.incoming_sources_for_edge_weight_kind(geo_id, EdgeWeightKindDiscriminants::Use)
.await?
.pop()
else {
continue;
};

let NodeWeight::View(view_inner) = snap.get_node_weight(view_idx).await? else {
continue;
};

let geo = Self::get_by_id(ctx, geo_id.into()).await?;

result.insert(view_inner.id().into(), geo);
}

Ok(result)
}

pub async fn list_by_view_id(
ctx: &DalContext,
view_id: ViewId,
Expand Down
2 changes: 1 addition & 1 deletion lib/dal/src/diagram/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ impl View {
view_id: ViewId,
) -> DiagramResult<Geometry> {
let mut geometry_pre = self.geometry(ctx, view_id).await?;
if geometry_pre.clone().into_raw() != raw_geometry {
if geometry_pre.into_raw() != raw_geometry {
geometry_pre.update(ctx, raw_geometry).await?;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/dal/src/func/binding/management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl ManagementBinding {
{{
kind: "{name}",
properties?: {sv_type},
geometry?: Geometry,
geometry?: Geometry | {{ [key: string]: Geometry }},
connect?: {{
from: string,
to: {{
Expand Down Expand Up @@ -155,6 +155,7 @@ type Geometry = {{
type Output = {{
status: 'ok' | 'error';
ops?: {{
views?: {{ create: string[]; }},
create?: {{ [key: string]: {component_create_type} }},
update?: {{ [key: string]: {{
properties?: {{ [key: string]: unknown }},
Expand Down
6 changes: 3 additions & 3 deletions lib/dal/src/management/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use si_frontend_types::RawGeometry;
use si_id::{ComponentId, SchemaId, ViewId};

use crate::management::{
ConnectionIdentifier, ManagementConnection, ManagementCreateOperation, ManagementGeometry,
IGNORE_PATHS,
ConnectionIdentifier, ManagementConnection, ManagementCreateGeometry,
ManagementCreateOperation, ManagementGeometry, IGNORE_PATHS,
};
use crate::prop::PropPath;
use crate::{AttributeValue, Component, DalContext, InputSocket, OutputSocket, Prop, PropKind};
Expand Down Expand Up @@ -191,7 +191,7 @@ pub async fn generate_template(
let create = ManagementCreateOperation {
kind: Some(kind),
properties,
geometry,
geometry: geometry.map(ManagementCreateGeometry::CurrentView),
connect,
parent,
};
Expand Down
Loading

0 comments on commit b85d66d

Please sign in to comment.