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

feat(gate,sdk)!: new policy spec #937

Merged
merged 14 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
4 changes: 3 additions & 1 deletion src/common/src/typegraph/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ pub enum Injection {
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct TypeNodeBase {
pub title: String,
pub policies: Vec<PolicyIndices>,
#[serde(default)]
pub description: Option<String>,
#[serde(default, rename = "enum")]
Expand Down Expand Up @@ -158,6 +157,9 @@ pub struct ObjectTypeData<Id = TypeId> {
pub id: Vec<String>,
#[serde(default)]
pub required: Vec<String>,
#[serde(skip_serializing_if = "IndexMap::is_empty")]
#[serde(default)]
pub policies: IndexMap<String, Vec<PolicyIndices>>,
}

#[skip_serializing_none]
Expand Down
1 change: 1 addition & 0 deletions src/metagen/src/fdk_rust/stubs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ mod test {
TypeNode::Object {
data: ObjectTypeData {
properties: Default::default(),
policies: Default::default(),
id: vec![],
required: vec![],
},
Expand Down
8 changes: 8 additions & 0 deletions src/metagen/src/fdk_rust/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ mod test {
]
.into_iter()
.collect(),
policies: Default::default(),
id: vec![],
// FIXME: remove required
required: vec![],
Expand Down Expand Up @@ -615,6 +616,7 @@ pub enum MyUnion {
properties: [("obj_b".to_string(), 1)].into_iter().collect(),
id: vec![],
required: ["obj_b"].into_iter().map(Into::into).collect(),
policies: Default::default(),
},
base: TypeNodeBase {
title: "ObjA".into(),
Expand All @@ -624,6 +626,7 @@ pub enum MyUnion {
TypeNode::Object {
data: ObjectTypeData {
properties: [("obj_c".to_string(), 2)].into_iter().collect(),
policies: Default::default(),
id: vec![],
required: ["obj_c"].into_iter().map(Into::into).collect(),
},
Expand All @@ -635,6 +638,7 @@ pub enum MyUnion {
TypeNode::Object {
data: ObjectTypeData {
properties: [("obj_a".to_string(), 0)].into_iter().collect(),
policies: Default::default(),
id: vec![],
required: ["obj_a"].into_iter().map(Into::into).collect(),
},
Expand Down Expand Up @@ -665,6 +669,7 @@ pub struct ObjC {
TypeNode::Object {
data: ObjectTypeData {
properties: [("obj_b".to_string(), 1)].into_iter().collect(),
policies: Default::default(),
id: vec![],
required: ["obj_b"].into_iter().map(Into::into).collect(),
},
Expand All @@ -676,6 +681,7 @@ pub struct ObjC {
TypeNode::Object {
data: ObjectTypeData {
properties: [("union_c".to_string(), 2)].into_iter().collect(),
policies: Default::default(),
id: vec![],
required: ["union_c"].into_iter().map(Into::into).collect(),
},
Expand Down Expand Up @@ -714,6 +720,7 @@ pub enum CUnion {
TypeNode::Object {
data: ObjectTypeData {
properties: [("obj_b".to_string(), 1)].into_iter().collect(),
policies: Default::default(),
id: vec![],
required: ["obj_b"].into_iter().map(Into::into).collect(),
},
Expand All @@ -725,6 +732,7 @@ pub enum CUnion {
TypeNode::Object {
data: ObjectTypeData {
properties: [("either_c".to_string(), 2)].into_iter().collect(),
policies: Default::default(),
id: vec![],
required: ["either_c"].into_iter().map(Into::into).collect(),
},
Expand Down
2 changes: 1 addition & 1 deletion src/metagen/src/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub fn test_typegraph_2() -> Typegraph {
TypeNode::Object {
data: ObjectTypeData {
properties: Default::default(),
policies: Default::default(),
id: vec![],
required: vec![],
},
Expand Down Expand Up @@ -99,7 +100,6 @@ pub fn test_typegraph_2() -> Typegraph {
pub fn default_type_node_base() -> TypeNodeBase {
TypeNodeBase {
title: String::new(),
policies: vec![],
description: None,
enumeration: None,
}
Expand Down
42 changes: 29 additions & 13 deletions src/typegate/src/engine/planner/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import type {
FunctionNode,
Injection,
InjectionNode,
PolicyIndices,
} from "../../typegraph/types.ts";

import { getChildTypes, visitTypes } from "../../typegraph/visitor.ts";
Expand Down Expand Up @@ -82,6 +83,7 @@ interface CollectNode {
path: string[];
astNode: ast.ArgumentNode | ast.ObjectFieldNode | undefined;
typeIdx: number;
policies: PolicyIndices[];
}

interface CollectedArgs {
Expand Down Expand Up @@ -120,6 +122,7 @@ export function collectArgs(
path: [argName],
astNode: astNodes[argName],
typeIdx: argTypeIdx,
policies: argTypeNode.policies?.[argName] ?? [],
});
}

Expand Down Expand Up @@ -298,7 +301,7 @@ class ArgumentCollector {

const typ: TypeNode = this.tg.type(typeIdx);

this.addPoliciesFrom(typeIdx);
this.addPoliciesFrom(typeIdx, node.policies);

const injection = this.#getInjection(node.path);
if (injection != null) {
Expand All @@ -319,7 +322,6 @@ class ArgumentCollector {
// try to get a default value for it, else throw an error
if (astNode == null) {
if (typ.type === Type.OPTIONAL) {
this.addPoliciesFrom(typ.item);
const itemType = this.tg.type(typ.item);
const { default_value: defaultValue } = typ;
if (defaultValue != null) {
Expand All @@ -332,6 +334,7 @@ class ArgumentCollector {
this.collectDefaults(
itemType.properties,
node.path,
itemType.policies ?? {},
),
true,
);
Expand All @@ -349,6 +352,7 @@ class ArgumentCollector {
this.collectDefaults(
variantType.properties,
node.path,
variantType.policies ?? {},
),
true,
);
Expand All @@ -372,6 +376,7 @@ class ArgumentCollector {
compute = this.collectDefaults(
variantType.properties,
node.path,
variantType.policies ?? {},
);
break;
} catch (_e) {
Expand All @@ -390,7 +395,7 @@ class ArgumentCollector {

if (typ.type === Type.OBJECT) {
const props = typ.properties;
return this.collectDefaults(props, node.path);
return this.collectDefaults(props, node.path, typ.policies ?? {});
}

throw new MandatoryArgumentError(this.currentNodeDetails);
Expand Down Expand Up @@ -689,12 +694,14 @@ class ArgumentCollector {
private collectDefaults(
props: Record<string, number>,
path: string[],
policies: Record<string, PolicyIndices[]>,
): ComputeArg<Record<string, unknown>> {
const computes: Record<string, ComputeArg> = {};

for (const [name, idx] of Object.entries(props)) {
path.push(name);
computes[name] = this.collectDefault(idx, path);
this.addPoliciesFrom(idx, policies[name] ?? []);
path.pop();
}

Expand Down Expand Up @@ -723,7 +730,6 @@ class ArgumentCollector {
path: string[],
): ComputeArg {
const typ = this.tg.type(typeIdx);
this.addPoliciesFrom(typeIdx);

const injection = this.#getInjection(path);
if (injection != null) {
Expand All @@ -737,13 +743,12 @@ class ArgumentCollector {

switch (typ.type) {
case Type.OPTIONAL: {
this.addPoliciesFrom(typ.item);
const { default_value: defaultValue = null } = typ;
return () => defaultValue;
}

case Type.OBJECT: {
return this.collectDefaults(typ.properties, path);
return this.collectDefaults(typ.properties, path, typ.policies ?? {});
}

default:
Expand All @@ -756,8 +761,12 @@ class ArgumentCollector {
typ: TypeNode,
injection: Injection,
): ComputeArg | null {
visitTypes(this.tg.tg, getChildTypes(typ), (node) => {
this.addPoliciesFrom(node.idx);
visitTypes(this.tg.tg, getChildTypes(typ), ({ type: typeNode }) => {
if (typeNode.type === Type.OBJECT) {
for (const [name, idx] of Object.entries(typeNode.properties)) {
this.addPoliciesFrom(idx, typeNode.policies?.[name] ?? []);
}
}
return true;
});

Expand Down Expand Up @@ -840,8 +849,12 @@ class ArgumentCollector {
}

this.deps.parent.add(key);
visitTypes(this.tg.tg, getChildTypes(typ), (node) => {
this.addPoliciesFrom(node.idx);
visitTypes(this.tg.tg, getChildTypes(typ), ({ type: typeNode }) => {
if (typeNode.type === Type.OBJECT) {
for (const [name, idx] of Object.entries(typeNode.properties)) {
this.addPoliciesFrom(idx, typeNode.policies?.[name] ?? []);
}
}
return true;
});

Expand All @@ -863,11 +876,14 @@ class ArgumentCollector {
};
}

private addPoliciesFrom(typeIdx: TypeIdx) {
const typ = this.tg.type(typeIdx);
private addPoliciesFrom(typeIdx: TypeIdx, policies: PolicyIndices[]) {
if (policies.length === 0) {
return;
}
// TODO we might have to check for duplicate indices
this.policies.set(typeIdx, {
argDetails: this.currentNodeDetails,
policyIndices: typ.policies.map((p) => {
policyIndices: policies.map((p) => {
if (typeof p === "number") {
return p;
}
Expand Down
Loading
Loading