-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(definitions): add initial implementation of data driven entity g…
…eneration (#25)
- Loading branch information
eexsty
committed
Apr 24, 2022
1 parent
46c3687
commit e4fbc3c
Showing
6 changed files
with
111 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
entities: | ||
- name: Player | ||
package: org.hexalite.generation.entity | ||
feature_flag: example | ||
fields: | ||
a: | ||
kind: | ||
kotlin: java.util.UUID | ||
rust: String | ||
sql: 'uuid().not_null().primary_key()' | ||
test_c: | ||
kind: | ||
kotlin: Int | ||
rust: u32 | ||
sql: 'integer().not_null().default(0)' | ||
hello_d: | ||
kind: | ||
kotlin: String | ||
rust: String | ||
sql: 'varchar(255).not_null().default("")' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ syn = "^1" | |
quote = "^1" | ||
serde = { version = "^1", features = ["derive"] } | ||
serde_yaml = "^0.8" | ||
home = "^0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
|
||
#[cfg(feature = "example")] | ||
definitions_generator::generate_entity_definitions!("definitions/example.yml"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,69 @@ | ||
mod specification; | ||
use std::fs; | ||
|
||
use proc_macro::TokenStream; | ||
use quote::format_ident; | ||
use specification::SpecificationRoot; | ||
|
||
mod specification; | ||
|
||
#[proc_macro] | ||
pub fn generate_entity_definitions(item: TokenStream) -> TokenStream { | ||
let mut path = item.to_string(); | ||
path.pop(); | ||
path.remove(0); | ||
|
||
let code = path | ||
.split("|") | ||
.map(|path| { | ||
let home = home::home_dir().expect("Could not find home directory"); | ||
|
||
let root = home | ||
.join(".hexalite") | ||
.join("dev") | ||
.canonicalize() | ||
.expect("Could not find ~/.hexalite/dev directory"); | ||
|
||
let file = root.join(path); | ||
let file = fs::read_to_string(&file) | ||
.expect(format!("Nao achou em {}", file.display()).as_str()); | ||
|
||
let specification: SpecificationRoot = | ||
serde_yaml::from_str(file.as_str()).expect("Could not parse specification"); | ||
|
||
let quotes = specification | ||
.entities | ||
.iter() | ||
.map(|specification| { | ||
let struct_fields_name = specification | ||
.fields | ||
.iter() | ||
.map(|(name, _)| format_ident!("{}", name.clone().trim().to_string())) | ||
.collect::<Vec<_>>(); | ||
let struct_fields_kind = specification | ||
.fields | ||
.iter() | ||
.map(|(_, field)| format_ident!("{}", field.kind.rust.trim().to_string())) | ||
.collect::<Vec<_>>(); | ||
let name = format_ident!("{}", specification.name.trim()); | ||
|
||
quote::quote! { | ||
#[derive(Debug, Clone)] | ||
pub struct #name { | ||
#(pub #struct_fields_name: #struct_fields_kind),* | ||
} | ||
} | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
quote::quote! { | ||
#(#quotes) | ||
* | ||
} | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
TokenStream::from(quote::quote! { | ||
#(#code) | ||
* | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters