-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Michaël <[email protected]>
- Loading branch information
1 parent
04428d1
commit 8ade5d1
Showing
22 changed files
with
1,312 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}); | ||
}); |
Oops, something went wrong.