Skip to content

Commit

Permalink
feat(sdk): apply syntax (#410)
Browse files Browse the repository at this point in the history
Signed-off-by: Michaël <[email protected]>
  • Loading branch information
michael-0acf4 authored Sep 21, 2023
1 parent 04428d1 commit 8ade5d1
Show file tree
Hide file tree
Showing 22 changed files with 1,312 additions and 157 deletions.
81 changes: 81 additions & 0 deletions typegate/tests/typecheck/apply.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from typegraph_next import t, typegraph, Policy, Graph
from typegraph_next.runtimes.deno import DenoRuntime

simple_tpe = t.struct(
{
"one": t.string(),
"two": t.struct(
{
"apply": t.integer(),
"user": t.integer(),
"set": t.integer().optional(),
"context": t.string().optional(),
}
),
}
)


self_ref_tpe = t.struct(
{
"a": t.string(),
"b": t.struct({"nested": t.ref("SelfRef")}).optional(),
"direct": t.ref("SelfRef").optional(),
},
name="SelfRef",
)


@typegraph()
def test_apply_python(g: Graph):
deno = DenoRuntime()
public = Policy.public()
identity_simple = deno.func(
simple_tpe, simple_tpe, code="({ one, two }) => { return { one, two } }"
)

identity_self_ref = deno.func(
self_ref_tpe, self_ref_tpe, code="({ a, b }) => { return { a, b } }"
)

g.expose(
invariantApply=identity_simple.apply(
{
"two": {
"apply": g.inherit(),
"user": g.inherit(),
"set": g.inherit(),
"context": g.inherit(),
}
# "one": g.inherit() # implicit
}
).with_policy(public),
simpleInjection=identity_simple.apply({"one": "ONE!"})
.apply(
{
"two": {
"user": g.inherit(),
"set": g.inherit().set(2),
"context": g.inherit().from_context("someValue"),
},
}
)
.with_policy(public),
selfReferingType=identity_self_ref.apply(
{
"a": g.inherit(), # A1
"b": {
"nested": {
"a": "A2",
"b": {
"nested": {
"a": g.inherit(), # A3
"b": g.inherit().from_context("nestedB"),
"direct": {"a": "direct A3"},
}
},
}
},
}
).with_policy(public),
)
100 changes: 100 additions & 0 deletions typegate/tests/typecheck/apply.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright Metatype OÜ, licensed under the Elastic License 2.0.
// SPDX-License-Identifier: Elastic-2.0

import { Policy, t, typegraph } from "@typegraph/deno/src/mod.ts";
import { DenoRuntime } from "@typegraph/deno/src/runtimes/deno.ts";
import { NONE } from "@typegraph/deno/src/effects.ts";

const student = t.struct({
id: t.integer(),
name: t.string(),
infos: t.struct({
age: t.integer({ min: 10 }),
school: t.string().optional(),
}),
distinctions: t.struct({
awards: t.array(t.struct({
name: t.string(),
points: t.integer(),
})).optional(),
medals: t.integer().optional(),
}).optional(),
}, { name: "Student" });

const grades = t.struct({
year: t.integer({ min: 2000 }),
subjects: t.array(
t.struct({
name: t.string(),
score: t.integer(),
}),
),
});

const tpe = t.struct({ student, grades: grades.optional() });

typegraph("test-apply-deno", (g) => {
const deno = new DenoRuntime();
const pub = Policy.public();
const identityStudent = deno.func(
tpe,
tpe,
{ code: "({ student, grades }) => { return { student, grades } }" },
);

g.expose({
testInvariant: identityStudent.apply({
student: {
id: g.inherit(),
name: g.inherit(),
infos: {
age: g.inherit(),
school: g.inherit(),
},
},
// grades: g.inherit(), // implicit
}).withPolicy(pub),
applyComposition: identityStudent
.apply({
student: {
id: 1234,
name: g.inherit(),
infos: g.inherit(),
distinctions: {
awards: [
{ name: "Chess", points: 1000 },
{ name: "Math Olympiad", points: 100 },
],
medals: g.inherit(),
},
},
// grades: g.inherit(), // implicit
})
.apply({
// student: g.inherit(), // implicit
grades: {
year: g.inherit(),
subjects: g.inherit().set([ // sugar
{ name: "Math", score: 60 },
]),
},
})
.withPolicy(pub),

injectionInherit: identityStudent
.apply({
student: {
id: 1234,
name: g.inherit(),
infos: g.inherit().fromContext("personalInfos"),
},
grades: {
year: g.inherit().set(2000),
subjects: g.inherit().fromContext({
[NONE]: "subjects",
}),
},
})
.withPolicy(pub),
});
});
Loading

0 comments on commit 8ade5d1

Please sign in to comment.