Skip to content

Commit

Permalink
refactor: move injection data to t.func (#871)
Browse files Browse the repository at this point in the history
-
[MET-682](https://linear.app/metatypedev/issue/MET-682/move-all-injection-data-to-tfunc)
  - [x] Move all injection data in `ObjectTypeData` (i.e. `t.func`)
-
[MET-656](https://linear.app/metatypedev/issue/MET-656/sdk-improve-generated-titles-from-applyreduce)
  - [x] Translate reduce to injection specification on `t.func`
-
[MET-94](https://linear.app/metatypedev/issue/MET-94/remove-runtime-field-from-typenode)
  - [x] Remove runtime field from `TypeNode` (#858)
-
[MET-683](https://linear.app/metatypedev/issue/MET-683/move-runtime-related-type-configs-out-of-typenode)
- [x] Move runtime-related configs to `MaterializerData` or
`RuntimeData`
- Misc.
- Enable random ports for the typegate (when `TG_PORT=0`); this will
work with `meta dev` with embedded typegate if you set the `--gate`
option with port `0`.

<!-- 2. Explain WHY the change cannot be made simpler -->



<!-- 3. Explain HOW users should update their code -->

#### Migration notes

...

- [x] The change comes with new or modified tests
- [x] Hard-to-understand functions have explanatory comments
- [ ] End-user documentation is updated to reflect the change
  • Loading branch information
Natoandro authored Oct 22, 2024
1 parent 7d817c8 commit 54558c7
Show file tree
Hide file tree
Showing 215 changed files with 28,228 additions and 22,883 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ src/typegraph/python/typegraph/gen
examples/typegraphs/migrations
examples/typegraphs/**/*.wasm
src/pyrt_wit_wire/wit_wire

/.metatype/
6 changes: 4 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ repos:
- id: deno-fmt
name: Deno format
language: system
entry: bash -c 'cd typegate && deno fmt --ignore=native,src/typegraphs,tmp,tests/e2e/nextjs && cd ../dev && deno fmt && cd ../typegraph/deno && deno fmt --ignore=node_modules,dist && cd ../../libs/metagen/src && deno fmt'
entry: bash -c 'cd typegate && deno fmt --ignore=native,src/typegraphs,tmp,tests/e2e/nextjs,tests/metagen/typegraphs/sample/ts/client.ts && cd ../dev && deno fmt && cd ../typegraph/deno && deno fmt --ignore=node_modules,dist && cd ../../libs/metagen/src && deno fmt'
pass_filenames: false
types:
- ts
Expand Down Expand Up @@ -82,7 +82,7 @@ repos:
- id: check-merge-conflict
- id: end-of-file-fixer
# exclude all generated files
exclude: (?x)( .ghjk/.* |.*deno.lock |.*\.snap$|typegate/src/typegraphs/.*\.json |website/docs/reference/ |libs/pyrt_wit_wire/pyrt | migration_lock.toml)
exclude: (?x)( .ghjk/.* |.*deno.lock |.*\.snap$|typegate/src/typegraphs/.*\.json |website/docs/reference/ |libs/pyrt_wit_wire/pyrt | migration_lock.toml | tests/metagen/typegraphs/sample/[rs|ts|py]/client\.[rs|ts|py])
- repo: https://github.com/Lucas-C/pre-commit-hooks
rev: v1.5.5
hooks:
Expand Down Expand Up @@ -160,6 +160,7 @@ repos:
hooks:
- id: ruff
- id: ruff-format
exclude: "tests/metagen/typegraphs/sample/py/client.py"
- repo: https://github.com/commitizen-tools/commitizen
rev: v3.27.0
hooks:
Expand All @@ -170,6 +171,7 @@ repos:
rev: v1.0
hooks:
- id: fmt
exclude: tests/metagen/typegraphs/sample/rs/client.rs
- id: clippy
args:
- "--locked"
Expand Down
18 changes: 9 additions & 9 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ exclude = [
]

[workspace.package]
version = "0.4.11-rc.0"
version = "0.5.0-rc.1"
edition = "2021"

[workspace.dependencies]
Expand Down
230 changes: 229 additions & 1 deletion deno.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions examples/templates/deno/api/example.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Policy, t, typegraph } from "jsr:@typegraph/sdk@0.4.11-rc.0/index.ts";
import { PythonRuntime } from "jsr:@typegraph/sdk@0.4.11-rc.0/runtimes/python.ts";
import { DenoRuntime } from "jsr:@typegraph/sdk@0.4.11-rc.0/runtimes/deno.ts";
import { Policy, t, typegraph } from "jsr:@typegraph/sdk@0.5.0-rc.1/index.ts";
import { PythonRuntime } from "jsr:@typegraph/sdk@0.5.0-rc.1/runtimes/python.ts";
import { DenoRuntime } from "jsr:@typegraph/sdk@0.5.0-rc.1/runtimes/deno.ts";

await typegraph("example", (g) => {
const pub = Policy.public();
Expand Down
2 changes: 1 addition & 1 deletion examples/templates/deno/compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
typegate:
image: ghcr.io/metatypedev/typegate:v0.4.11-rc.0
image: ghcr.io/metatypedev/typegate:v0.5.0-rc.1
restart: always
ports:
- "7890:7890"
Expand Down
2 changes: 1 addition & 1 deletion examples/templates/node/compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
typegate:
image: ghcr.io/metatypedev/typegate:v0.4.11-rc.0
image: ghcr.io/metatypedev/typegate:v0.5.0-rc.1
restart: always
ports:
- "7890:7890"
Expand Down
2 changes: 1 addition & 1 deletion examples/templates/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"dev": "MCLI_LOADER_CMD='npm x tsx' meta dev"
},
"dependencies": {
"@typegraph/sdk": "^0.4.11-rc.0"
"@typegraph/sdk": "^0.5.0-rc.1"
},
"devDependencies": {
"tsx": "^3.13.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/templates/python/compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
typegate:
image: ghcr.io/metatypedev/typegate:v0.4.11-rc.0
image: ghcr.io/metatypedev/typegate:v0.5.0-rc.1
restart: always
ports:
- "7890:7890"
Expand Down
4 changes: 2 additions & 2 deletions examples/templates/python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[tool.poetry]
name = "example"
version = "0.4.11-rc.0"
version = "0.5.0-rc.1"
description = ""
authors = []

[tool.poetry.dependencies]
python = ">=3.8,<4.0"
typegraph = "0.4.11-rc.0"
typegraph = "0.5.0-rc.1"

[build-system]
requires = ["poetry-core"]
Expand Down
2 changes: 1 addition & 1 deletion examples/typegraphs/metagen/rs/fdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl Router {
}

pub fn init(&self, args: InitArgs) -> Result<InitResponse, InitError> {
static MT_VERSION: &str = "0.4.11-rc.0";
static MT_VERSION: &str = "0.5.0-rc.1";
if args.metatype_version != MT_VERSION {
return Err(InitError::VersionMismatch(MT_VERSION.into()));
}
Expand Down
1 change: 0 additions & 1 deletion ghjk.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// @ts-nocheck: Deno file

// --
import { METATYPE_VERSION, PUBLISHED_VERSION } from "./tools/consts.ts";
import { file, ports, sedLock, semver, stdDeps } from "./tools/deps.ts";
import installs from "./tools/installs.ts";
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

[tool.poetry]
name = "metatype"
version = "0.4.11-rc.0"
version = "0.5.0-rc.1"
description = ""
authors = []

Expand Down
12 changes: 12 additions & 0 deletions src/common/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ pub struct Node {
pub auth: Option<BasicAuth>,
}

impl Node {
pub fn override_port(self, port: u16) -> Self {
let mut base_url = self.base_url;
base_url.set_port(Some(port)).unwrap();
Self {
base_url,
prefix: self.prefix,
auth: self.auth,
}
}
}

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error(transparent)]
Expand Down
25 changes: 15 additions & 10 deletions src/common/src/typegraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,20 @@ impl Typegraph {
_ => bail!("typegraph is invalid: root node is not object"),
}
}
/*
pub fn idx_of(&self, id: &str) -> Option<u32> {
self.name_map.get(id).map(|&idx| idx)
}

pub fn get(&self, id: &str) -> Option<&TypeNode> {
let Some(&idx) = self.name_map.get(id) else {
return None;
};
Some(&self.types[idx as usize])
} */
pub fn resolve_quant(&self, type_idx: TypeId) -> TypeId {
let mut type_idx = type_idx;
loop {
match &self.types[type_idx as usize] {
TypeNode::Optional { data, .. } => {
type_idx = data.item;
}
TypeNode::List { data, .. } => {
type_idx = data.items;
}
_ => break,
}
}
type_idx
}
}
45 changes: 30 additions & 15 deletions src/common/src/typegraph/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use std::collections::BTreeMap;

use anyhow::Result;
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
Expand All @@ -14,27 +15,32 @@ use super::{parameter_transform::FunctionParameterTransform, EffectType, PolicyI
pub type TypeName = String;
pub type TypeId = u32;

type JsonValue = serde_json::Value;

#[derive(Serialize, Deserialize, Clone, Debug, Hash)]
pub struct SingleValue<T: Hash> {
pub value: T,
pub struct SingleValue {
pub value: JsonValue,
}

#[derive(Serialize, Deserialize, Clone, Debug, Hash)]
#[serde(untagged)]
pub enum InjectionData<T: Hash = String> {
SingleValue(SingleValue<T>),
ValueByEffect(BTreeMap<EffectType, T>),
pub enum InjectionData {
SingleValue(SingleValue),
ValueByEffect(BTreeMap<EffectType, JsonValue>),
}

impl<T: Hash> InjectionData<T> {
pub fn values(&self) -> Vec<&T> {
impl InjectionData {
pub fn values<T: serde::de::DeserializeOwned>(&self) -> Result<Vec<T>> {
match self {
InjectionData::SingleValue(v) => vec![&v.value],
InjectionData::ValueByEffect(m) => m.values().collect(),
InjectionData::SingleValue(v) => Ok(vec![serde_json::from_value(v.value.clone())?]),
InjectionData::ValueByEffect(m) => m
.values()
.map(|v| serde_json::from_value(v.clone()).map_err(Into::into))
.collect(),
}
}

pub fn values_mut(&mut self) -> Vec<&mut T> {
pub fn values_mut(&mut self) -> Vec<&mut JsonValue> {
match self {
InjectionData::SingleValue(v) => vec![&mut v.value],
InjectionData::ValueByEffect(m) => m.values_mut().collect(),
Expand All @@ -57,16 +63,11 @@ pub enum Injection {
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct TypeNodeBase {
pub title: String,
pub runtime: u32,
pub policies: Vec<PolicyIndices>,
#[serde(default)]
pub description: Option<String>,
#[serde(default)]
pub injection: Option<Injection>,
#[serde(default, rename = "enum")]
pub enumeration: Option<Vec<String>>, // JSON-serialized values
#[serde(default)]
pub config: IndexMap<String, serde_json::Value>,
}

#[skip_serializing_none]
Expand Down Expand Up @@ -169,13 +170,27 @@ pub struct ListTypeData<Id = TypeId> {
pub unique_items: Option<bool>,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(untagged)]
pub enum InjectionNode {
Parent {
children: IndexMap<String, InjectionNode>,
},
Leaf {
injection: Injection,
},
}

#[skip_serializing_none]
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct FunctionTypeData<Id = TypeId> {
pub input: Id,
#[serde(rename = "parameterTransform")]
pub parameter_transform: Option<FunctionParameterTransform>,
pub output: Id,
pub injections: IndexMap<String, InjectionNode>,
#[serde(rename = "runtimeConfig")]
pub runtime_config: serde_json::Value,
pub materializer: u32,
#[serialize_always]
pub rate_weight: Option<u32>,
Expand Down
Loading

0 comments on commit 54558c7

Please sign in to comment.