Oyelowo / surreal-orm

Powerful & expressive ORM/query-builder/static checker for raw queries/Fully Automated migration tooling , designed to offer an intuitive API, strict type-checking, novel features, & full specification support. It provides a fresh perspective in data management. Currently supports SurrealDB engine. RDMSs(PG, MYSQL etc) and others coming soon
85 stars 2 forks source link

68 impelement pick macro #70

Closed Oyelowo closed 1 month ago

Oyelowo commented 1 month ago

Implement Pick Macro

Example usage

use serde::Serialize;
use surreal_orm::{pick, Pickable};

#[derive(Pickable, Debug, Serialize)]
struct Person<'a, T: 'a, U: 'a> {
    name: String,
    age: u8,
    some: &'a T,
    another: &'a U,
}

pick!(NewPersonWithUnusedTypeGenericsSkipped, Person<'a,_,_> as PersonPickable, [name, age]);
pick!(NewPerson, Person<'a,T,U> as PersonPickable, [name, age]);

pick!{
    #[derive(Serialize)] 
    NewPersonWithAttributes, Person<'a,_,_> as PersonPickable, 
    [
        #[serde(rename = "name2")]
        name, 
        age,
        // #[serde(borrow)]
        // some
    ] 
}

// #[pick(OldPerson, [age, num])]
// #[pick(Book, [title, author])]
// struct NewStructThing {
//    extra: String,
//     #[override]
//     age: u32,
// }

fn main() {
    let person = Person {
        name: "Oyelowo".into(),
        age: 25,
        some: &43,
        another: &"kaka",
    };
    println!("{:?}", person);

    let new2 = NewPersonWithUnusedTypeGenericsSkipped {
        name: "Oye".to_string(),
        age: 154,
    };

    println!("{}", new2.name);
    println!("{}", new2.age);

    let new1 = NewPerson::<'_, u32, &str> {
        name: "Oye".to_string(),
        age: 154,
    };
    println!("{}", new1.name);
    println!("{}", new1.age);

    let new3 = NewPersonWithAttributes {
        name: "Oye".to_string(),
        age: 154,
    };
    println!("{}", new3.name);
}

Future Plans and Contemplation

Support this alternative syntax?

 #[pick(OldPerson, [age, num])]
 #[pick(Book, [title, author])]
 struct NewStructThing {
    extra: String,
     #[override] // This will generate an error by default and prompt you to use the override attribute.
     age: u32,
}