Skip to content

Commit

Permalink
fix: bad mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
luckasRanarison committed Nov 5, 2024
1 parent 70375bb commit 76a2064
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 23 deletions.
5 changes: 2 additions & 3 deletions examples/typegraphs/execute.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from typegraph import typegraph, Policy, t, Graph
from typegraph import typegraph, Policy, t, Graph, fx
from typegraph.runtimes.deno import DenoRuntime
from typegraph.graph.params import Auth
from typegraph.providers.prisma import PrismaRuntime
from typegraph.gen.exports.runtimes import EffectUpdate
from typegraph.graph.params import Cors


Expand Down Expand Up @@ -82,7 +81,7 @@ def roadmap(g: Graph):
"importance": t.enum(["medium", "important", "critical"]),
}
),
EffectUpdate(True),
fx.update(True),
),
get_context=deno.identity(t.struct({"username": t.string().optional()})).apply(
{
Expand Down
1 change: 0 additions & 1 deletion src/typegraph/deno/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"./deps/mod.ts": "./src/deps/mod.ts",
"./effects.ts": "./src/effects.ts",
"./envs/cli.ts": "./src/envs/cli.ts",
"./host/host.d.ts": "./src/host/host.d.ts",
"./index.ts": "./src/index.ts",
"./io.ts": "./src/io.ts",
"./metagen.ts": "./src/metagen.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/typegraph/deno/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export function getPolicyChain(
}

return {
per_effect: mapValues(
perEffect: mapValues(
p instanceof PolicyPerEffectObject ? p.value : p,
(v: any) => v._id,
) as unknown as PolicyPerEffect,
Expand Down
2 changes: 1 addition & 1 deletion src/typegraph/python/typegraph/runtimes/grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ def __init__(self, proto_file: str, endpoint: str):
def call(self, method: str):
data = GrpcData(method)
func_data = runtimes.call_grpc_method(self.id, data)
return t.func.from_type_func(func_data.value)
return t.func.from_type_func(func_data)
2 changes: 1 addition & 1 deletion src/typegraph/python/typegraph/runtimes/kv.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self, url: str):

def get(self):
mat = self.__operation("get", fx.read())
return t.func(t.struct({"key": t.string()}), t.string(), mat)
return t.func(t.struct({"key": t.string()}), t.string().optional(), mat)

def set(self):
mat = self.__operation("set", fx.update())
Expand Down
2 changes: 1 addition & 1 deletion src/typegraph/python/typegraph/runtimes/substantial.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def _generic_substantial_func(
)
func_data = runtimes.generate_substantial_operation(self.id, data)

return t.func.from_type_func(func_data.value)
return t.func.from_type_func(func_data)

def start(self, kwargs: "t.struct"):
return self._generic_substantial_func("start", kwargs, None)
Expand Down
19 changes: 8 additions & 11 deletions src/typegraph/specs/codegen/rpc/typescript/client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { toCamelCase, toSnakeCase } from "@std/text";

const BUFFER_SIZE = 1024;

const state = { id: 0 };
Expand All @@ -15,20 +17,15 @@ type RpcResponse<R, E = null> = {
id: number | string;
};

function camelToSnakeCase(obj: any): any {
function transformKeys(obj: any, convertKey: (key: string) => string): any {
if (Array.isArray(obj)) {
return obj.map(camelToSnakeCase);
return obj.map((item) => transformKeys(item, convertKey));
} else if (obj && typeof obj === "object") {
const result: Record<string, any> = {};

for (const [key, value] of Object.entries(obj)) {
const snakeKey = key.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();

if (value && typeof value === "object") {
result[snakeKey] = camelToSnakeCase(value);
} else {
result[snakeKey] = value;
}
const newKey = convertKey(key);
result[newKey] = transformKeys(value, convertKey);
}

return result;
Expand All @@ -41,7 +38,7 @@ function rpcRequest<R, P>(method: string, params?: P) {
const request = {
jsonrpc: "2.0",
method,
params: params && camelToSnakeCase(params),
params: params && transformKeys(params, toSnakeCase),
id: state.id,
};

Expand All @@ -68,7 +65,7 @@ function rpcRequest<R, P>(method: string, params?: P) {
throw new Error(response.error.message);
}

return response.result as R;
return transformKeys(response.result, toCamelCase) as R;
}

export { rpcRequest };
2 changes: 1 addition & 1 deletion src/typegraph/specs/codegen/src/lib/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ ${def.props

override formatUnionTypeDef(def: UnionTypeDef): string {
return `export type ${def.ident} =
${def.variants.map((v) => ` | ${v.value ? `{ ${v.tag}: ${v.value} }` : `"${v.tag}"`}`).join("\n")};`;
${def.variants.map((v) => ` | ${v.value ? `{ ${toCamelCase(v.tag)}: ${v.value} }` : `"${v.tag}"`}`).join("\n")};`;
}

override formatFuncDef(def: FuncDef) {
Expand Down
5 changes: 2 additions & 3 deletions tests/runtimes/prisma/full_prisma_mapping.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from typegraph import typegraph, Policy, t, Graph
from typegraph.gen.runtimes import EffectUpdate
from typegraph import typegraph, Policy, t, Graph, fx
from typegraph.providers.prisma import PrismaRuntime


Expand Down Expand Up @@ -100,7 +99,7 @@ def full_prisma_mapping(g: Graph):
"replacement": t.string(),
}
),
EffectUpdate(True),
fx.update(True),
),
# https://www.postgresql.org/docs/10/functions-subquery.html
# expr = ANY(array) equiv. to expr IN (array[0], array[1], ..)
Expand Down

0 comments on commit 76a2064

Please sign in to comment.