Skip to content

Commit

Permalink
feat: Implement DIFF_CHANGES_CONTENT and IS_DIFF_CHANGES_HAS_FILE
Browse files Browse the repository at this point in the history
… function in GitQL App scope.
  • Loading branch information
AmrDeveloper committed Dec 25, 2024
1 parent 2e77242 commit 9172b72
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
8 changes: 5 additions & 3 deletions docs/gitql_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ those functions are available only in the gitql application.

### GitQL Diffs functions

| Name | Parameters | Return | Description |
| ------------------------ | ----------- | ------- | ----------------------------------------------------- |
| DIFF_CHANGES_FILES_COUNT | DiffChanges | Integer | Return number of unique files changes in this commit. |
| Name | Parameters | Return | Description |
| ------------------------ | ----------------- | ------- | --------------------------------------------------------- |
| DIFF_CHANGES_CONTENT | DiffChanges | Text | Return the full content of all changes appended together. |
| DIFF_CHANGES_FILES_COUNT | DiffChanges | Integer | Return number of unique files changes in this commit. |
| IS_DIFF_CHANGES_HAS_FILE | DiffChanges, Text | Boolean | Return true if this diff changes contains file. |
50 changes: 50 additions & 0 deletions src/gitql/functions/diffs.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,67 @@
use std::collections::HashMap;
use std::collections::HashSet;

use gitql_ast::types::boolean::BoolType;
use gitql_ast::types::integer::IntType;
use gitql_ast::types::text::TextType;
use gitql_core::signature::Signature;
use gitql_core::signature::StandardFunction;
use gitql_core::values::base::Value;
use gitql_core::values::boolean::BoolValue;
use gitql_core::values::integer::IntValue;
use gitql_core::values::text::TextValue;

use crate::gitql::types::diff_changes::DiffChangesType;
use crate::gitql::values::diff_changes::DiffChangesValue;

#[inline(always)]
pub(crate) fn register_diffs_functions(map: &mut HashMap<&'static str, StandardFunction>) {
map.insert("diff_changes_content", diff_changes_full_content);

map.insert("diff_changes_files_count", diff_changes_files_count);

map.insert("is_diff_changes_has_file", diff_changes_contains_file);
}

#[inline(always)]
pub(crate) fn register_diffs_function_signatures(map: &mut HashMap<&'static str, Signature>) {
map.insert(
"diff_changes_content",
Signature {
parameters: vec![Box::new(DiffChangesType)],
return_type: Box::new(TextType),
},
);

map.insert(
"diff_changes_files_count",
Signature {
parameters: vec![Box::new(DiffChangesType)],
return_type: Box::new(IntType),
},
);

map.insert(
"is_diff_changes_has_file",
Signature {
parameters: vec![Box::new(DiffChangesType)],
return_type: Box::new(BoolType),
},
);
}

fn diff_changes_full_content(values: &[Box<dyn Value>]) -> Box<dyn Value> {
if let Some(changes) = values[0].as_any().downcast_ref::<DiffChangesValue>() {
let mut content = String::new();
for change in changes.changes.iter() {
content += &String::from_utf8_lossy(&change.content);
}

return Box::new(TextValue { value: content });
}
Box::new(TextValue {
value: String::default(),
})
}

fn diff_changes_files_count(values: &[Box<dyn Value>]) -> Box<dyn Value> {
Expand All @@ -37,3 +75,15 @@ fn diff_changes_files_count(values: &[Box<dyn Value>]) -> Box<dyn Value> {
}
Box::new(IntValue { value: 0 })
}

fn diff_changes_contains_file(values: &[Box<dyn Value>]) -> Box<dyn Value> {
if let Some(changes) = values[0].as_any().downcast_ref::<DiffChangesValue>() {
let file = values[1].as_text().unwrap();
for change in changes.changes.iter() {
if change.location.eq(&file) {
return Box::new(BoolValue { value: true });
}
}
}
Box::new(BoolValue { value: false })
}
8 changes: 8 additions & 0 deletions src/gitql/types/diff_changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ impl DataType for DiffChangesType {
"DiffChangesType".to_owned()
}

#[allow(clippy::borrowed_box)]
fn equals(&self, other: &Box<dyn DataType>) -> bool {
let self_type: Box<dyn DataType> = Box::new(DiffChangesType);
other.is_any()
|| other.is_variant_contains(&self_type)
|| other.as_any().downcast_ref::<DiffChangesType>().is_some()
}

fn as_any(&self) -> &dyn Any {
self
}
Expand Down

0 comments on commit 9172b72

Please sign in to comment.