Skip to content

Commit

Permalink
wip: adapt with met-685-policies
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-0acf4 committed Dec 13, 2024
2 parents 478df47 + 68c26a9 commit 89cce3e
Show file tree
Hide file tree
Showing 29 changed files with 715 additions and 627 deletions.
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
47 changes: 26 additions & 21 deletions src/typegate/src/engine/planner/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { mapValues } from "@std/collections/map-values";
import { DependencyResolver } from "./dependency_resolver.ts";
import { Runtime } from "../../runtimes/Runtime.ts";
import { getInjection } from "../../typegraph/utils.ts";
import { Injection } from "../../typegraph/types.ts";
import { Injection, PolicyIndices } from "../../typegraph/types.ts";
import { getInjectionValues } from "./injection_utils.ts";

const logger = getLogger(import.meta);
Expand Down Expand Up @@ -329,7 +329,10 @@ export class Planner {
* @param field {FieldNode} The selection field for node
* @param node
*/
private traverseField(node: Node, field: ast.FieldNode): ComputeStage[] {
private traverseField(
node: Node,
field: ast.FieldNode,
): ComputeStage[] {
const { parent, path, name } = node;

if (parent == null) {
Expand Down Expand Up @@ -395,12 +398,18 @@ export class Planner {
}

const fieldType = this.tg.type(node.typeIdx);
// TODO CHECK does this work with aliases
// TODO array or null??
const policies =
(this.tg.type(parent.typeIdx, Type.OBJECT).policies ?? {})[node.name] ??
[];

const stages = fieldType.type !== Type.FUNCTION
? this.traverseValueField(node)
? this.traverseValueField(node, policies)
: this.traverseFuncField(
node,
this.tg.type(parent.typeIdx, Type.OBJECT).properties,
policies,
);

return stages;
Expand Down Expand Up @@ -429,9 +438,11 @@ export class Planner {
* Create `ComputeStage`s for `node` and its child nodes,
* where `node` corresponds to a selection field for a value (non-function type).
* @param node
* @param policies
*/
private traverseValueField(node: Node): ComputeStage[] {
private traverseValueField(
node: Node,
policies: PolicyIndices[],
): ComputeStage[] {
const outjection = node.scope && this.#getOutjection(node.scope!);
if (outjection) {
return [
Expand Down Expand Up @@ -461,10 +472,6 @@ export class Planner {
rateCalls: true,
rateWeight: 0,
});
const types = this.policiesBuilder.setReferencedTypes(
stage.id(),
node.typeIdx,
);

stages.push(stage);

Expand All @@ -474,7 +481,6 @@ export class Planner {
while (isQuantifier(nestedSchema)) {
nestedTypeIdx = getWrappedType(nestedSchema);
nestedSchema = this.tg.type(nestedTypeIdx);
types.push(nestedTypeIdx);
}

stages.push(...this.traverse({ ...node, typeIdx: nestedTypeIdx }, stage));
Expand All @@ -489,6 +495,7 @@ export class Planner {
private traverseFuncField(
node: Node,
parentProps: Record<string, number>,
policies: PolicyIndices[],
): ComputeStage[] {
const stages: ComputeStage[] = [];
const deps = [];
Expand Down Expand Up @@ -560,33 +567,31 @@ export class Planner {
});
stages.push(stage);

this.policiesBuilder.push(stage.id(), node.typeIdx, collected.policies);
this.policiesBuilder.push(
stage.id(),
node.typeIdx,
collected.policies,
policies,
);

{
const types = this.policiesBuilder.setReferencedTypes(
stage.id(),
node.typeIdx,
outputIdx,
inputIdx,
);

// nested quantifiers
let wrappedTypeIdx = outputIdx;
let wrappedType = this.tg.type(wrappedTypeIdx);
while (isQuantifier(wrappedType)) {
wrappedTypeIdx = getWrappedType(wrappedType);
wrappedType = this.tg.type(wrappedTypeIdx);
types.push(wrappedTypeIdx);
}

stages.push(
...this.traverse(
{ ...node, typeIdx: wrappedTypeIdx, parentStage: stage },
stage,
),
);
}
this.policiesBuilder.pop(stage.id());

this.policiesBuilder.pop(stage.id());
return stages;
}

Expand Down
Loading

0 comments on commit 89cce3e

Please sign in to comment.