Skip to content

Commit

Permalink
refactor: sdk dx (#470)
Browse files Browse the repository at this point in the history
- [x] rename apply to reduce
- [x] move t.ref to g.ref and remove t.proxy
- [x] rename t.array to t.list
- [x] standard policies should defined in core
- [x] remove python* section in metatype.yml
  • Loading branch information
michael-0acf4 authored Oct 28, 2023
1 parent 8175ad8 commit 3bf54cd
Show file tree
Hide file tree
Showing 166 changed files with 1,721 additions and 1,708 deletions.
14 changes: 7 additions & 7 deletions Cargo.lock

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

4 changes: 0 additions & 4 deletions examples/templates/python/metatype.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,3 @@ typegates:
TG_EXAMPLE_ENV_VAR: "example"
TG_EXAMPLE_POSTGRES: "postgresql://postgres:password@localhost:5432/db"
TG_EXAMPLE_MONGO: "mongodb://root:password@mongo:27017/db"

typegraphs:
python:
include: "api/**/*.py"
12 changes: 6 additions & 6 deletions libs/common/src/typegraph/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub struct ObjectTypeData {
#[skip_serializing_none]
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ArrayTypeData {
pub struct ListTypeData {
pub items: u32,
pub max_items: Option<u32>,
pub min_items: Option<u32>,
Expand Down Expand Up @@ -231,11 +231,11 @@ pub enum TypeNode {
#[serde(flatten)]
data: ObjectTypeData,
},
Array {
List {
#[serde(flatten)]
base: TypeNodeBase,
#[serde(flatten)]
data: ArrayTypeData,
data: ListTypeData,
},
Function {
#[serde(flatten)]
Expand Down Expand Up @@ -273,7 +273,7 @@ impl TypeNode {
| String { base, .. }
| File { base, .. }
| Object { base, .. }
| Array { base, .. }
| List { base, .. }
| Function { base, .. }
| Union { base, .. }
| Either { base, .. }
Expand All @@ -291,7 +291,7 @@ impl TypeNode {
| String { base, .. }
| File { base, .. }
| Object { base, .. }
| Array { base, .. }
| List { base, .. }
| Function { base, .. }
| Union { base, .. }
| Either { base, .. }
Expand All @@ -309,7 +309,7 @@ impl TypeNode {
String { .. } => "string",
File { .. } => "file",
Object { .. } => "object",
Array { .. } => "array",
List { .. } => "list",
Function { .. } => "function",
Union { .. } => "union",
Either { .. } => "either",
Expand Down
8 changes: 4 additions & 4 deletions libs/common/src/typegraph/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serde_json::Value;

use super::{
visitor::{Path, PathSegment, TypeVisitor, VisitResult},
ArrayTypeData, EitherTypeData, FloatTypeData, Injection, IntegerTypeData, ObjectTypeData,
EitherTypeData, FloatTypeData, Injection, IntegerTypeData, ListTypeData, ObjectTypeData,
StringTypeData, UnionTypeData,
};

Expand Down Expand Up @@ -97,7 +97,7 @@ impl TypeVisitor for Validator {
| TypeNode::String { .. } => {
// scalar
}
TypeNode::Array { data, .. } => {
TypeNode::List { data, .. } => {
let item_type = tg.types.get(data.items as usize).unwrap();
if !item_type.is_scalar() {
self.push_error(
Expand Down Expand Up @@ -221,7 +221,7 @@ impl Typegraph {
self.validate_value(data.item, value)
}
}
TypeNode::Array { data, .. } => self.validate_array(data, value),
TypeNode::List { data, .. } => self.validate_array(data, value),
TypeNode::Object { data, .. } => self.validate_object(data, value),
TypeNode::Function { .. } => Err(anyhow!("Unexpected function type")),
TypeNode::Union { data, .. } => self.validate_union(data, value),
Expand Down Expand Up @@ -308,7 +308,7 @@ impl Typegraph {
Ok(())
}

fn validate_array(&self, data: &ArrayTypeData, value: &Value) -> Result<()> {
fn validate_array(&self, data: &ListTypeData, value: &Value) -> Result<()> {
let array = value
.as_array()
.ok_or_else(|| anyhow!("Expected an array got '{}'", to_string(value)))?;
Expand Down
2 changes: 1 addition & 1 deletion libs/common/src/typegraph/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl<'a, V: TypeVisitor + Sized> TypegraphTraversal<'a, V> {
VisitResult::Continue(deeper) if deeper => match type_node {
TypeNode::Optional { data, .. } => self.visit_optional(type_idx, data.item),
TypeNode::Object { data, .. } => self.visit_object(type_idx, &data.properties),
TypeNode::Array { data, .. } => self.visit_array(type_idx, data.items),
TypeNode::List { data, .. } => self.visit_array(type_idx, data.items),
TypeNode::Union { data, .. } => self.visit_union(type_idx, &data.any_of),
TypeNode::Either { data, .. } => self.visit_either(type_idx, &data.one_of),
TypeNode::Function { data, .. } => {
Expand Down
6 changes: 2 additions & 4 deletions meta-cli/src/codegen/deno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ impl<'a> Codegen<'a> {
let tpe = &self.tg.types[idx as usize];
match tpe {
TypeNode::Optional { .. } => Ok("null".to_owned()),
TypeNode::Array { .. } => Ok("[]".to_owned()),
TypeNode::List { .. } => Ok("[]".to_owned()),
TypeNode::Boolean { .. } => Ok("false".to_owned()),
TypeNode::Float { .. } | TypeNode::Integer { .. } => Ok("0".to_owned()),
TypeNode::String { .. } => Ok("\"\"".to_owned()),
Expand Down Expand Up @@ -422,9 +422,7 @@ impl<'a> Codegen<'a> {
TypeNode::Optional { data, .. } => {
Ok(format!("null | {}", self.get_typespec(data.item)?))
}
TypeNode::Array { data, .. } => {
Ok(format!("Array<{}>", self.get_typespec(data.items)?))
}
TypeNode::List { data, .. } => Ok(format!("Array<{}>", self.get_typespec(data.items)?)),
TypeNode::Boolean { .. } => Ok("boolean".to_owned()),
TypeNode::Float { .. } | TypeNode::Integer { .. } => Ok("number".to_owned()),
TypeNode::String { base, .. } => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export function div({ dividend, divisor }: DivInput,
} {
return { quotient: 0, remainder: 0 };
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export function get_rgb({ color }: Get_rgbInput,
} {
return { rgb: [] };
}

Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ export function convert({ color, to }: ConvertInput,
{
return "";
}

2 changes: 1 addition & 1 deletion meta-cli/src/tests/typegraphs/enum_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def enum(g: Graph):

get_rgb = deno.import_(
t.struct({"color": color}),
t.struct({"rgb": t.array(t.integer(), name="RGB")}),
t.struct({"rgb": t.list(t.integer(), name="RGB")}),
module="ts/rgb.ts",
name="get_rgb",
)
Expand Down
2 changes: 1 addition & 1 deletion meta-cli/src/tests/typegraphs/union_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

@typegraph()
def union(g: Graph):
rgb = t.array(t.integer(min=0, max=255), min=3, max=3, name="RGBArray")
rgb = t.list(t.integer(min=0, max=255), min=3, max=3, name="RGBArray")
hex = t.string(pattern="^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$", name="HexColor")
colorName = t.enum(
[
Expand Down
3 changes: 0 additions & 3 deletions meta-cli/tests/metatype.yml
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
typegraphs:
python:
include: "graphs/**/*.py"
12 changes: 6 additions & 6 deletions typegate/src/engine/planner/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import {
} from "../../types.ts";
import { JSONValue } from "../../utils.ts";
import {
ArrayNode,
getVariantTypesIndexes,
ListNode,
ObjectNode,
Type,
TypeNode,
Expand Down Expand Up @@ -164,7 +164,7 @@ interface Dependencies {
* object_arg: { # o -> collectArg() -> collectObjectArg()
* nested1: 12 # -> collectArg()
* },
* array_arg: [ # o -> collectArg() -> collectArrayArg()
* list_arg: [ # o -> collectArg() -> collectListArg()
* 'hello', # -> collectArg()
* 'world', # -> collectArg()
* ],
Expand Down Expand Up @@ -287,8 +287,8 @@ class ArgumentCollector {
return this.collectObjectArg(valueNode, typ);
}

case Type.ARRAY:
return this.collectArrayArg(valueNode, typ);
case Type.LIST:
return this.collectListArg(valueNode, typ);

case Type.INTEGER: {
if (valueNode.kind !== Kind.INT) {
Expand Down Expand Up @@ -457,9 +457,9 @@ class ArgumentCollector {
}

/** Collect the value of a parameter of type 'array'. */
private collectArrayArg(
private collectListArg(
valueNode: ast.ValueNode,
typ: ArrayNode,
typ: ListNode,
): ComputeArg {
if (valueNode.kind !== Kind.LIST) {
throw new TypeMismatchError(
Expand Down
4 changes: 2 additions & 2 deletions typegate/src/engine/query_engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,13 @@ export class QueryEngine {
return { type: "array", items: toJSONSchema(v.type) };
}
const name = v.name.value;
const schema = {
const schema = ({
"Integer": { type: "number" },
"Float": { type: "number" },
"Boolean": { type: "boolean" },
"String": { type: "string" },
"ID": { type: "string" },
}?.[name];
} as any)?.[name];
if (schema) {
return schema;
}
Expand Down
4 changes: 2 additions & 2 deletions typegate/src/engine/typecheck/code_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// SPDX-License-Identifier: Elastic-2.0

import {
ArrayNode,
BooleanNode,
FileNode,
FloatNode,
IntegerNode,
ListNode,
ObjectNode,
OptionalNode,
StringNode,
Expand Down Expand Up @@ -176,7 +176,7 @@ export class CodeGenerator {
}

generateArrayValidator(
typeNode: ArrayNode,
typeNode: ListNode,
itemValidatorName: string,
) {
this.validation(
Expand Down
2 changes: 1 addition & 1 deletion typegate/src/engine/typecheck/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class InputValidationCompiler {
cg.generateOptionalValidator(typeNode, functionName(typeNode.item));
queue.push(typeNode.item);
break;
case "array":
case "list":
cg.generateArrayValidator(typeNode, functionName(typeNode.items));
queue.push(typeNode.items);
break;
Expand Down
2 changes: 1 addition & 1 deletion typegate/src/engine/typecheck/matching_variant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class VariantMatcherCompiler {
cg.generateOptionalValidator(typeNode, functionName(typeNode.item));
queue.push(typeNode.item);
break;
case "array":
case "list":
cg.generateArrayValidator(typeNode, functionName(typeNode.items));
queue.push(typeNode.items);
break;
Expand Down
2 changes: 1 addition & 1 deletion typegate/src/engine/typecheck/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class ResultValidationCompiler {
break;
}

case "array": {
case "list": {
const itemValidatorName = this.validatorName(
typeNode.items,
entry.selectionSet != null,
Expand Down
2 changes: 1 addition & 1 deletion typegate/src/runtimes/prisma/hooks/generate_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ class FieldBuilder {
switch (typeNode.type) {
case Type.OPTIONAL:
return [typeNode.item, "?"];
case Type.ARRAY:
case Type.LIST:
return [typeNode.items, "[]"];
default:
return [typeIdx, ""];
Expand Down
2 changes: 1 addition & 1 deletion typegate/src/runtimes/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export class RandomRuntime extends Runtime {
return this.chance.string();
case "boolean":
return this.chance.bool();
case "array": {
case "list": {
const res = [];
let size = this.chance.integer({ min: 1, max: 10 });
const childNodeName = this.getTgTypeNameByIndex(typ.items);
Expand Down
4 changes: 2 additions & 2 deletions typegate/src/runtimes/typegraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { ensure } from "../utils.ts";
import { Runtime } from "./Runtime.ts";
import { ComputeStage } from "../engine/query_engine.ts";
import {
isArray,
isEither,
isFunction,
isList,
isObject,
isOptional,
isQuantifier,
Expand Down Expand Up @@ -305,7 +305,7 @@ export class TypeGraphRuntime extends Runtime {
};
}

if (isArray(type)) {
if (isList(type)) {
return {
...common,
kind: () => TypeKind.LIST,
Expand Down
2 changes: 1 addition & 1 deletion typegate/src/transports/rest/rest_schema_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class RestSchemaGenerator {
}
break;
}
case "array": {
case "list": {
const itemsIdx = typeNode.items;
const schema = this.#generateHelper(itemsIdx, inProgress);
outputSchema = { type: "array", items: schema };
Expand Down
Loading

0 comments on commit 3bf54cd

Please sign in to comment.