-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature/index management #33
Conversation
src/index.rs
Outdated
} | ||
|
||
impl Index { | ||
fn default(r#type: Type) -> Self { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer implementing Default
trait in this case.
impl Default for Index{
fn default()->Self{
Index {
id: None,
is_newly_created: None,
selectivity_estimate: None,
error: None,
code: None,
fields: vec![],
name: None,
unique: None,
r#type,
sparse: None,
min_length: None,
deduplicate: None,
geo_json: None,
expire_after: None,
in_background: Some(false),
}
}
}
And I wonder, why not derive Default
trait?
#[derive(Default)]
struct Index{...}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's because he can't randomy pick the index type, the default is relative to the index type here (r#type
argument)
Thank you so much for your kind contribution. |
thank you very much for this |
Yes, I agree, that sounds reasonable.
I'm not quite sure if this is the same field though. I would have to check.
Well, that's the big question here. I did make an implementation using an Regarding the
which I may use with |
Ok, I switched to the implementation using I also managed to use |
@fMeow when can this be merged and included in a version? |
Sorry for the late response. Sorry for the vague wording that I didn't state my opinion clearly enough. For Here is a quick but incomplete proof of concepts: struct Index{
// common fiedls that all kind of indexes contain
pub fields: Vec<String>,
#[builder(default, setter(into))]
pub name: String,
#[builder(default)]
pub id: String,
...
// fields depends on index type,
#[serde(flatten)]
#[builder(default=xxxx)]
pub index_type: IndexType
}
enum IndexType{
GeoIndex{geo_json:bool},
....
} Though I am hesitate to name the field Thank you!! |
As for the As the HTTP guide on read index suggests, it's used to denote whether the request succeeded. Let me give a quick explanation. All reponse from arangoDB can be divided into two cases: a) successful request b) failed. Almost all responses contain Again, please feel free to remove the Thank you! :) |
Thanks for this explanation, I removed the fields. |
Ok, I see what you mean. But I'm not sure if it's really a more ergonomic solution in the end. Please check the following example and decide for yourself what you think might be cleaner. There's certainly less code in this one, but I think I like the abstraction concept of the current implementation better. But then that's only personal preference: #[macro_use]
extern crate typed_builder;
#[macro_use]
extern crate serde;
#[derive(Debug, Serialize, Deserialize, Default, TypedBuilder)]
struct Index{
pub fields: Vec<String>,
#[builder(default, setter(into))]
pub name: String,
#[builder(default)]
pub id: String,
#[serde(flatten)]
#[builder(default)]
pub index_type: IndexType
}
#[serde(rename_all = "camelCase", tag = "type")]
#[derive(Debug, Serialize, Deserialize)]
enum IndexType{
Persistent{unique:bool, sparse: bool, deduplicate: bool},
Hash{unique:bool, sparse: bool, deduplicate: bool},
Geo{geo_json:bool},
}
impl Default for IndexType {
fn default() -> Self {
IndexType::Persistent { unique: false, sparse: false, deduplicate: false }
}
}
fn main() {
let x = Index::builder().fields(vec![]).name("test").build();
let json = serde_json::to_string(&x).unwrap();
println!("{}", json);
let x = Index::builder().fields(vec![]).name("test").index_type(IndexType::Geo{geo_json:true}).build();
let json = serde_json::to_string(&x).unwrap();
println!("{}", json);
let json = r#"{"fields":[],"name":"test","id":"","type":"persistent","unique":false,"sparse":false,"deduplicate":false}"#;
let x: Index = serde_json::from_str(json).unwrap();
println!("{:#?}", x);
} Please let me know what you think. |
I just wrote that implementation according to what you outlined above, maybe it's easier to think about it that way. I guess both would work out well. If you prefer the current (latest) implementation, please let me finalize the documentation before merging. |
Documentation updated. |
Yes, I agree the current implement that forces to use #[derive(Debug, Serialize, Deserialize, Default, TypedBuilder)]
struct Index{
pub fields: Vec<String>,
#[builder(default, setter(into))]
pub name: String,
#[builder(default)]
pub id: String,
#[serde(flatten)]
#[builder(default="IndexType::Persistent(Persistent{ unique: false, sparse: false, deduplicate: false })")]
pub index_type: IndexType
}
#[serde(rename_all = "camelCase", tag = "type")]
#[derive(Debug, Serialize, Deserialize)]
enum IndexType{
Persistent(PersistentIndex),
Hash(HashIndex),
Geo(GeoIndex),
}
struct PersistentIndex{unique:bool, sparse: bool, deduplicate: bool}
struct HashIndex{unique:bool, sparse: bool, deduplicate: bool}
struct GeoIndex{geo_json:bool} In addition, we can add some handy method to quickly differentiate between each index type. impl IndexType{
pub fn persistent(&self)->Option<&PersistentIndex>{
if let IndexType::Persistent(p)=self{
Some(p)
}
else {
None
}
}
pub fn is_persistent(&self)->bool{
self.persistent().is_some()
}
// ........
} Is this API sounds good to you? Thank you so much for your contribution and quick response!!!! |
Judge from your example, it seem the Index.fileds can be empty. Then maybe we can use a default? #[derive(Debug, Serialize, Deserialize, Default, TypedBuilder)]
struct Index{
#[builder(default="Vec::new()")]
pub fields: Vec<String>,
#[builder(default, setter(into))]
pub name: String,
#[builder(default, setter(into))]
pub id: String,
#[serde(flatten)]
#[builder(default)]
pub index_type: IndexType
} |
I just added the Regarding the implementation with |
This PR is merged now, and will be released in the next several hours. Thank you soooo much! |
Many thanks! |
Initial implementation suggestion for index management on ArangoDB.