Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
6c14956
Remote save
SilasMarvin Jul 14, 2023
3b2e3b5
Working remote embeddings
SilasMarvin Jul 17, 2023
f1d6bf7
Compiling
SilasMarvin Jul 19, 2023
22f280e
Commit before moving everything to lazy
SilasMarvin Jul 19, 2023
ec090ca
Working lazy python
SilasMarvin Jul 21, 2023
58c01a3
Commit before moving adjusting Javascript macros
SilasMarvin Jul 21, 2023
9e7b146
Working javascript sdk
SilasMarvin Jul 22, 2023
abb4f5e
Working javascript sdk
SilasMarvin Jul 26, 2023
76ccf3a
The start of working pipelines
SilasMarvin Jul 28, 2023
cfcc66b
Working pipelines in python
SilasMarvin Aug 3, 2023
a9dcbc9
Uncomment
SilasMarvin Aug 3, 2023
8b48750
Added to_dict function
SilasMarvin Aug 3, 2023
6e3f1e6
Small changes and prep for progress bars
SilasMarvin Aug 4, 2023
5365557
Working progress bars and many other small but exciting things
SilasMarvin Aug 4, 2023
66476ff
Prepping to push to test pypi
SilasMarvin Aug 7, 2023
f2613d7
Prepping for javascript
SilasMarvin Aug 8, 2023
92c9623
Improvments to javascript and updates to the python sdk deploy script
SilasMarvin Aug 8, 2023
8a4e3cf
Prepping for real tests
SilasMarvin Aug 8, 2023
12bb3a8
Updated sql
SilasMarvin Aug 9, 2023
2b5b68b
Python examples translated to use pipelines
SilasMarvin Aug 9, 2023
447fc80
Mostly cleaned up and documented crate, and cleaned up python README …
SilasMarvin Aug 10, 2023
333c5e6
Ready for test deployments
SilasMarvin Aug 10, 2023
11bcce2
Updated manual build file for python
SilasMarvin Aug 10, 2023
845bf02
Build fast
SilasMarvin Aug 11, 2023
4904a1a
Small tweaks
SilasMarvin Aug 11, 2023
64dc7e2
Prepping for another test release
SilasMarvin Aug 11, 2023
c3b274c
Prepping to expand query_builder
SilasMarvin Aug 11, 2023
cb143a5
Massive cleanups to macros
SilasMarvin Aug 11, 2023
c66b07b
Massive cleanups to macros
SilasMarvin Aug 11, 2023
b7d4c2d
Ready to release
SilasMarvin Aug 11, 2023
a2c87b1
Formatting
SilasMarvin Aug 13, 2023
dd9c3ab
Renamed files
SilasMarvin Aug 21, 2023
5568608
Added removed file
SilasMarvin Aug 21, 2023
4a2e98d
Removed unnecessary file
SilasMarvin Aug 21, 2023
e673af4
Updated sdk version to 0.9
SilasMarvin Aug 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Massive cleanups to macros
  • Loading branch information
SilasMarvin committed Aug 21, 2023
commit c66b07b45b9d087501b077e64130b575e6c540f7
103 changes: 15 additions & 88 deletions pgml-sdks/rust/pgml-macros/src/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,66 +6,6 @@ use syn::{visit::Visit, DeriveInput, ItemImpl, Type};
use crate::common::{AttributeArgs, GetImplMethod};
use crate::types::{OutputType, SupportedType};

// pub fn generate_custom_into_js_result(parsed: DeriveInput) -> proc_macro::TokenStream {
// let name = parsed.ident;
// let fields_named = match parsed.data {
// syn::Data::Struct(s) => match s.fields {
// syn::Fields::Named(n) => n,
// _ => panic!("custom_into_js proc_macro structs should only have named fields"),
// },
// _ => panic!("custom_into_js proc_macro should only be used on structs"),
// };
//
// let mut sets = Vec::new();
// let mut interface = format!("\ninterface {} {{\n", name);
//
// fields_named.named.into_pairs().for_each(|p| {
// let v = p.into_value();
// let name = v.ident.to_token_stream().to_string();
// let name_ident = v.ident;
// sets.push(quote! {
// let js_item = self.#name_ident.into_js_result(cx)?;
// js_object.set(cx, #name, js_item)?;NODE_OPTIONS=--experimental-vm-modules
// });
// let ty = GetSupportedType::get_type(&v.ty);
// let decleration = match &ty {
// SupportedType::Option(o) => format!("{}?", get_typescript_type(o)),
// _ => get_typescript_type(&ty),
// };
// interface.push_str(&format!("\t{}: {},\n", name, decleration));
// });
//
// interface.push('}');
// let mut file = OpenOptions::new()
// .create(true)
// .write(true)
// .append(true)
// .read(true)
// .open("javascript/index.d.ts")
// .unwrap();
// let mut contents = String::new();
// file.read_to_string(&mut contents)
// .expect("Unable to read typescript decleration file");
// if !contents.contains(&interface) {
// file.write_all(interface.as_bytes())
// .expect("Unable to write typescript decleration file");
// }
//
// let out = quote! {
// #[cfg(feature = "javascript")]
// impl IntoJsResult for #name {
// type Output = neon::types::JsObject;
// fn into_js_result<'a, 'b, 'c: 'b, C: neon::context::Context<'c>>(self, cx: &mut C) -> neon::result::JsResult<'b, Self::Output> {
// use neon::object::Object;
// let js_object = cx.empty_object();
// #(#sets)*
// Ok(js_object)
// }
// }
// };
// proc_macro::TokenStream::from(out)
// }

pub fn generate_javascript_derive(parsed: DeriveInput) -> proc_macro::TokenStream {
let name_ident = format_ident!("{}Javascript", parsed.ident);
let wrapped_type_ident = format_ident!("{}", parsed.ident);
Expand Down Expand Up @@ -94,8 +34,9 @@ pub fn generate_javascript_derive(parsed: DeriveInput) -> proc_macro::TokenStrea

#[cfg(feature = "javascript")]
impl FromJsType for #wrapped_type_ident {
type From = neon::types::JsObject;
fn from_js_type<'a, C: neon::context::Context<'a>>(cx: &mut C, arg: neon::handle::Handle<Self::From>) -> neon::result::NeonResult<Self> {
type From = neon::types::JsValue;
fn from_js_type<'a, C: neon::context::Context<'a>>(cx: &mut C, arg: neon::handle::Handle<Self::From>) -> neon::result::NeonResult<Self> {
let arg: neon::handle::Handle<neon::types::JsObject> = arg.downcast(cx).or_throw(cx)?;
use neon::prelude::*;
use core::ops::Deref;
let s: neon::handle::Handle<neon::types::JsBox<#name_ident>> = arg.get(cx, "s")?;
Expand All @@ -105,10 +46,11 @@ pub fn generate_javascript_derive(parsed: DeriveInput) -> proc_macro::TokenStrea

#[cfg(feature = "javascript")]
impl FromJsType for &mut #wrapped_type_ident {
type From = neon::types::JsObject;
type From = neon::types::JsValue;
fn from_js_type<'a, C: neon::context::Context<'a>>(cx: &mut C, arg: neon::handle::Handle<Self::From>) -> neon::result::NeonResult<Self> {
use neon::prelude::*;
use core::ops::Deref;
let arg: neon::handle::Handle<neon::types::JsObject> = arg.downcast(cx).or_throw(cx)?;
let s: neon::handle::Handle<neon::types::JsBox<#name_ident>> = arg.get(cx, "s")?;
unsafe {
let ptr = &*s.wrapped as *const #wrapped_type_ident;
Expand All @@ -121,10 +63,11 @@ pub fn generate_javascript_derive(parsed: DeriveInput) -> proc_macro::TokenStrea

#[cfg(feature = "javascript")]
impl FromJsType for & #wrapped_type_ident {
type From = neon::types::JsObject;
type From = neon::types::JsValue;
fn from_js_type<'a, C: neon::context::Context<'a>>(cx: &mut C, arg: neon::handle::Handle<Self::From>) -> neon::result::NeonResult<Self> {
use neon::prelude::*;
use core::ops::Deref;
let arg: neon::handle::Handle<neon::types::JsObject> = arg.downcast(cx).or_throw(cx)?;
let s: neon::handle::Handle<neon::types::JsBox<#name_ident>> = arg.get(cx, "s")?;
unsafe {
let ptr = &*s.wrapped as *const #wrapped_type_ident;
Expand All @@ -137,7 +80,7 @@ pub fn generate_javascript_derive(parsed: DeriveInput) -> proc_macro::TokenStrea

#[cfg(feature = "javascript")]
impl IntoJsResult for #wrapped_type_ident {
type Output = neon::types::JsObject;
type Output = neon::types::JsValue;
fn into_js_result<'a, 'b, 'c: 'b, C: neon::context::Context<'c>>(self, cx: &mut C) -> neon::result::JsResult<'b, Self::Output> {
#name_ident::from(self).into_js_result(cx)
}
Expand Down Expand Up @@ -370,14 +313,15 @@ pub fn generate_javascript_methods(

#[cfg(feature = "javascript")]
impl IntoJsResult for #name_ident {
type Output = neon::types::JsObject;
type Output = neon::types::JsValue;
fn into_js_result<'a, 'b, 'c: 'b, C: neon::context::Context<'c>>(self, cx: &mut C) -> neon::result::JsResult<'b, Self::Output> {
use neon::object::Object;
use neon::prelude::Value;
let obj = cx.empty_object();
let s = cx.boxed(self);
obj.set(cx, "s", s)?;
#(#object_sets)*
Ok(obj)
Ok(obj.as_value(cx))
}
}

Expand Down Expand Up @@ -501,21 +445,12 @@ fn get_neon_type(ty: &SupportedType) -> syn::Type {
SupportedType::bool => syn::parse_str("neon::types::JsBoolean").unwrap(),
SupportedType::Vec(_v) => syn::parse_str("neon::types::JsArray").unwrap(),
SupportedType::S => syn::parse_str("neon::types::JsObject").unwrap(),
SupportedType::DateTime => syn::parse_str("neon::types::JsDate").unwrap(),
SupportedType::Tuple(_t) => syn::parse_str("neon::types::JsObject").unwrap(),
SupportedType::HashMap((_k, _v)) => syn::parse_str("neon::types::JsObject").unwrap(),
SupportedType::i64 | SupportedType::f64 | SupportedType::u64 => {
syn::parse_str("neon::types::JsNumber").unwrap()
}
// Our own types
SupportedType::Database
| SupportedType::Collection
| SupportedType::Splitter
| SupportedType::QueryBuilder
| SupportedType::Pipeline
| SupportedType::PipelineSyncData
| SupportedType::QueryRunner
| SupportedType::Model => syn::parse_str("neon::types::JsObject").unwrap(),
SupportedType::CustomType(_t) => syn::parse_str("neon::types::JsValue").unwrap(),
// Add more types as required
_ => syn::parse_str("neon::types::JsValue").unwrap(),
}
Expand All @@ -530,7 +465,7 @@ fn convert_output_type_convert_from_javascript(
) {
let (output_type, convert_from) = match ty {
SupportedType::S => (
Some(quote! {neon::result::JsResult<'a, neon::types::JsObject>}),
Some(quote! {neon::result::JsResult<'a, neon::types::JsValue>}),
Some(format_ident!("Self").into_token_stream()),
),
// t @ SupportedType::Database
Expand Down Expand Up @@ -584,16 +519,8 @@ fn get_typescript_type(ty: &SupportedType) -> String {
}
}
SupportedType::i64 | SupportedType::f64 | SupportedType::u64 => "number".to_string(),
// Our own types
SupportedType::PipelineSyncData => "Json".to_string(),
t @ SupportedType::Database
| t @ SupportedType::Json
| t @ SupportedType::DateTime
| t @ SupportedType::Collection
| t @ SupportedType::Splitter
| t @ SupportedType::QueryBuilder
| t @ SupportedType::QueryRunner
| t @ SupportedType::Model => t.to_string(),

SupportedType::CustomType(t) => t.to_string(),
// Add more types as required
_ => "any".to_string(),
}
Expand Down
19 changes: 8 additions & 11 deletions pgml-sdks/rust/pgml-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,11 @@ pub fn custom_methods(
output
}

// #[proc_macro_derive(custom_into_py)]
// pub fn custom_into_py(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
// let parsed = parse_macro_input!(input as DeriveInput);
// python::generate_into_py(parsed)
// }

// #[proc_macro_derive(custom_into_js_result)]
// pub fn custom_into_js_result(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
// let parsed = parse_macro_input!(input as DeriveInput);
// javascript::generate_custom_into_js_result(parsed)
// }
#[proc_macro_derive(pgml_alias)]
pub fn pgml_alias(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let mut output = proc_macro::TokenStream::new();
let parsed = parse_macro_input!(input as DeriveInput);
output.extend(python::pgml_alias(parsed.clone()));
// output.extend(javascript::pgml_alias(parsed));
output
}
139 changes: 68 additions & 71 deletions pgml-sdks/rust/pgml-macros/src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,54 +12,66 @@ from typing import List, Dict, Optional, Self, Any

"#;

// pub fn generate_into_py(parsed: DeriveInput) -> proc_macro::TokenStream {
// let name = parsed.ident;
// let fields_named = match parsed.data {
// syn::Data::Struct(s) => match s.fields {
// syn::Fields::Named(n) => n,
// _ => panic!("custom_into_py proc_macro structs should only have named fields"),
// },
// _ => panic!("custom_into_py proc_macro should only be used on structs"),
// };
//
// let sets: Vec<proc_macro2::TokenStream> = fields_named.named.into_pairs().map(|p| {
// let v = p.into_value();
// let name = v.ident.to_token_stream().to_string();
// let name_ident = v.ident;
// quote! {
// dict.set_item(#name, self.#name_ident).expect("Error setting python value in custom_into_py proc_macro");
// }
// }).collect();
//
// let stub = format!("\n{} = dict[str, Any]\n", name);
//
// let mut file = OpenOptions::new()
// .create(true)
// .write(true)
// .append(true)
// .read(true)
// .open("python/pgml/pgml.pyi")
// .unwrap();
// let mut contents = String::new();
// file.read_to_string(&mut contents)
// .expect("Unable to read stubs file for python");
// if !contents.contains(&stub) {
// file.write_all(stub.as_bytes())
// .expect("Unable to write stubs file for python");
// }
//
// let expanded = quote! {
// #[cfg(feature = "python")]
// impl pyo3::conversion::IntoPy<pyo3::PyObject> for #name {
// fn into_py(self, py: pyo3::marker::Python<'_>) -> pyo3::PyObject {
// let dict = pyo3::types::PyDict::new(py);
// #(#sets)*
// dict.into()
// }
// }
// };
// proc_macro::TokenStream::from(expanded)
// }
/// This function assumes the user has already impliemented:
/// - `FromPyObject` for the wrapped type
/// - `ToPyObject` for the wrapped type
/// - `IntoPy<PyObject>` for the wrapped type
pub fn pgml_alias(parsed: DeriveInput) -> proc_macro::TokenStream {
let name_ident = format_ident!("{}Python", parsed.ident);
let wrapped_type_ident = parsed.ident;

let expanded = quote! {
#[cfg(feature = "python")]
#[derive(Clone, Debug)]
pub struct #name_ident {
pub wrapped: #wrapped_type_ident
}

#[cfg(feature = "python")]
impl CustomInto<#name_ident> for #wrapped_type_ident {
fn custom_into(self) -> #name_ident {
#name_ident {
wrapped: self,
}
}
}

#[cfg(feature = "python")]
impl CustomInto<#wrapped_type_ident> for #name_ident {
fn custom_into(self) -> #wrapped_type_ident {
self.wrapped
}
}

// From Python to Rust
#[cfg(feature = "python")]
impl pyo3::conversion::FromPyObject<'_> for #name_ident {
fn extract(obj: &pyo3::PyAny) -> pyo3::PyResult<Self> {
Ok(Self {
wrapped: obj.extract()?
})
}
}

// From Rust to Python
#[cfg(feature = "python")]
impl pyo3::conversion::ToPyObject for #name_ident {
fn to_object(&self, py: pyo3::Python) -> pyo3::PyObject {
use pyo3::conversion::ToPyObject;
self.wrapped.to_object(py)
}
}
#[cfg(feature = "python")]
impl pyo3::conversion::IntoPy<pyo3::PyObject> for #name_ident {
fn into_py(self, py: pyo3::Python) -> pyo3::PyObject {
use pyo3::conversion::ToPyObject;
self.wrapped.to_object(py)
}
}
};

proc_macro::TokenStream::from(expanded)
}

pub fn generate_python_derive(parsed: DeriveInput) -> proc_macro::TokenStream {
let name_ident = format_ident!("{}Python", parsed.ident);
Expand All @@ -74,15 +86,6 @@ pub fn generate_python_derive(parsed: DeriveInput) -> proc_macro::TokenStream {
pub wrapped: std::boxed::Box<#wrapped_type_ident>
}

#[cfg(feature = "python")]
impl From<#wrapped_type_ident> for #name_ident {
fn from(w: #wrapped_type_ident) -> Self {
Self {
wrapped: std::boxed::Box::new(w),
}
}
}

#[cfg(feature = "python")]
impl CustomInto<#name_ident> for #wrapped_type_ident {
fn custom_into(self) -> #name_ident {
Expand Down Expand Up @@ -168,7 +171,8 @@ pub fn generate_python_derive(parsed: DeriveInput) -> proc_macro::TokenStream {
impl pyo3::IntoPy<pyo3::PyObject> for #wrapped_type_ident {
fn into_py(self, py: pyo3::Python) -> pyo3::PyObject {
use pyo3::conversion::IntoPy;
#name_ident::from(self).into_py(py)
let x: #name_ident = self.custom_into();
x.into_py(py)
}
}
};
Expand Down Expand Up @@ -301,7 +305,8 @@ pub fn generate_python_methods(
use std::ops::DerefMut;
#prepared_wrapper_arguments
#middle
Ok(#name_ident::from(x))
let x: Self = x.custom_into();
Ok(x)
};
(signature, middle)
} else {
Expand Down Expand Up @@ -524,15 +529,7 @@ fn get_python_type(ty: &SupportedType) -> String {
SupportedType::i64 | SupportedType::u64 => "int".to_string(),
SupportedType::f64 => "float".to_string(),
// Our own types
t @ SupportedType::Database
| t @ SupportedType::Json
| t @ SupportedType::DateTime
| t @ SupportedType::Collection
| t @ SupportedType::Model
| t @ SupportedType::QueryBuilder
| t @ SupportedType::QueryRunner
| t @ SupportedType::Pipeline
| t @ SupportedType::Splitter => t.to_string(),
SupportedType::CustomType(t) => t.to_string(),
// Add more types as required
_ => "Any".to_string(),
}
Expand All @@ -548,9 +545,9 @@ fn get_type_for_optional(ty: &SupportedType) -> String {
SupportedType::Vec(_) => "[]".to_string(),
SupportedType::i64 | SupportedType::u64 => 1.to_string(),
SupportedType::f64 => 1.0.to_string(),
SupportedType::Json => "{}".to_string(),
SupportedType::bool => "True".to_string(),
SupportedType::Model | SupportedType::Splitter => "None".to_string(),
_ => panic!("Type not yet supported for optional python stub: {:?}", ty),

_ => "Any".to_string(),
// _ => panic!("Type not yet supported for optional python stub: {:?}", ty),
}
}
Loading