Skip to content

Commit

Permalink
Fix bugs related to not working setters when objects where empty, imp… (
Browse files Browse the repository at this point in the history
#79)

…lement few convenience methods.
  • Loading branch information
Alik Aslanyan authored Apr 10, 2019
1 parent e88d9d8 commit 0a492f8
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 55 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ script:
before_deploy:
- mkdir -p $TRAVIS_BUILD_DIR/deploy/addons/amxmodx/modules
- mkdir -p $TRAVIS_BUILD_DIR/deploy/addons/amxmodx/configs
- mkdir -p $TRAVIS_BUILD_DIR/deploy/addons/amxmodx/scripting
- mkdir -p $TRAVIS_BUILD_DIR/deploy/addons/amxmodx/scripting/include
- cp $TRAVIS_BUILD_DIR/build/release/*.so $TRAVIS_BUILD_DIR/deploy/addons/amxmodx/modules
- cp $TRAVIS_BUILD_DIR/configs/* $TRAVIS_BUILD_DIR/deploy/addons/amxmodx/configs
- cp $TRAVIS_BUILD_DIR/scripting/* $TRAVIS_BUILD_DIR/deploy/addons/amxmodx/scripting
- cp $TRAVIS_BUILD_DIR/scripting/* $TRAVIS_BUILD_DIR/deploy/addons/amxmodx/scripting/include
- cd $TRAVIS_BUILD_DIR/deploy
- tar czf grip-${TRAVIS_TAG}-${TARGET}.tar.gz addons
deploy:
Expand Down
2 changes: 2 additions & 0 deletions cpp/ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

extern "C" {

cell grip_body_from_json(const void *amx, cell value, bool pretty, cell recursion_limit);

cell grip_body_from_string(const void *amx, const char *str);

cell grip_cancel_request(const void *amx, cell cancellation);
Expand Down
2 changes: 1 addition & 1 deletion cpp/helper/moduleconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* to add multiple entries.
*/
#define MODULE_NAME "gRIP"
#define MODULE_VERSION "0.1.0 Beta"
#define MODULE_VERSION "0.1.1 Beta"
#define MODULE_AUTHOR "Inline"
#define MODULE_URL "https://github.com/in-line/grip"
#define MODULE_LOGTAG "gRIP"
Expand Down
17 changes: 11 additions & 6 deletions cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ cell AMX_NATIVE_CALL grip_json_parse_file_amxx(AMX *amx, cell *params) {

ZERO_INIT_STACK_BUFFER(buffer, params[arg_buffer_size]);

cell ret = grip_json_parse_file(amx, MF_GetAmxString(amx, params[arg_file], 0, &dummy), &buffer[0], params[arg_buffer_size]);
cell ret = grip_json_parse_file(amx,
MF_BuildPathname("%s", MF_GetAmxString(amx, params[arg_file], 0, &dummy)),
&buffer[0], params[arg_buffer_size]);

MF_SetAmxStringSafe(amx, params[arg_buffer], &buffer[0], params[arg_buffer_size]);

Expand Down Expand Up @@ -568,7 +570,7 @@ cell AMX_NATIVE_CALL grip_json_serial_to_string_amxx(AMX *amx, cell *params) {
cell AMX_NATIVE_CALL grip_json_serial_to_file_amxx(AMX *amx, cell *params) {
enum { arg_count, arg_value, arg_file, arg_pretty, arg_recursion_limit };
return grip_json_serial_to_file(amx, params[arg_value],
MF_GetAmxString(amx, params[arg_file], 3, &dummy),
MF_BuildPathname("%s", MF_GetAmxString(amx, params[arg_file], 3, &dummy)),
params[arg_pretty] != 0,
params[arg_recursion_limit]);
}
Expand All @@ -579,6 +581,11 @@ cell AMX_NATIVE_CALL grip_json_validate_amxx(AMX *amx, cell *params) {
return grip_json_validate(amx, params[arg_schema], params[arg_value]);
}

cell AMX_NATIVE_CALL grip_body_from_json_amxx(AMX *amx, cell *params) {
enum { arg_count, arg_value, arg_pretty, arg_recursion_limit};
return grip_body_from_json(amx, params[arg_value], params[arg_pretty] != 0, params[arg_recursion_limit]);
}

AMX_NATIVE_INFO grip_exports[] = {
{"grip_request", grip_request_amxx},
{"grip_destroy_body", grip_destroy_body_amxx},
Expand Down Expand Up @@ -650,19 +657,17 @@ AMX_NATIVE_INFO grip_exports[] = {
{"grip_json_serial_to_string", grip_json_serial_to_string_amxx},
{"grip_json_serial_to_file", grip_json_serial_to_file_amxx},
{"grip_json_validate", grip_json_validate_amxx},
{"grip_body_from_json", grip_body_from_json_amxx},
{nullptr, nullptr}
};


void OnAmxxAttach()
{
MF_AddNatives(grip_exports);
}

void OnPluginsLoaded() {
char configFilePath[MAX_PATH];
MF_BuildPathnameR(configFilePath, sizeof(configFilePath), "%s/grip.ini", MF_GetLocalInfo("amxx_configsdir", "addons/amxmodx/configs"));
grip_init(log_error, configFilePath);
grip_init(log_error, MF_BuildPathname("%s/grip.ini", MF_GetLocalInfo("amxx_configsdir", "addons/amxmodx/configs")));
}

void OnPluginsUnloaded() {
Expand Down
2 changes: 1 addition & 1 deletion rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "grip-rust"
version = "0.1.0-beta"
version = "0.1.1-beta"
authors = ["alik"]
edition = "2018"
build = "src/build.rs"
Expand Down
174 changes: 159 additions & 15 deletions rust/src/ffi/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,28 @@ macro_rules! try_to_get_json_object_value_rc_gc {
};
}

macro_rules! try_to_get_json_object_value_or_insert_rc_gc {
($amx:expr, $object:expr, $name:expr, $dot_notation:expr) => {
try_and_log_ffi!(
$amx,
try_to_get_json_value_gc!($amx, $object).index_selective_safe_or_insert_rc_gc(
try_and_log_ffi!($amx, str_from_ptr($name)),
$dot_notation,
Some(InnerValue::Null)
)
)
};
}

macro_rules! try_to_get_json_object_value_gc {
($amx:expr, $object:expr, $name:expr, $dot_notation:expr) => {{
try_to_get_json_object_value_rc_gc!($amx, $object, $name, $dot_notation).borrow()
}};
}

macro_rules! try_to_get_json_object_value_gc_mut {
macro_rules! try_to_get_json_object_value_gc_or_insert_mut {
($amx:expr, $object:expr, $name:expr, $dot_notation:expr) => {
(&*try_to_get_json_object_value_rc_gc!($amx, $object, $name, $dot_notation)
(&*try_to_get_json_object_value_or_insert_rc_gc!($amx, $object, $name, $dot_notation)
as &RefCell<GCValue>)
.borrow_mut()
};
Expand All @@ -210,17 +223,6 @@ macro_rules! try_to_get_json_object_value {
}};
}

macro_rules! try_to_get_json_object_value_mut {
($amx:expr, $object:expr, $name:expr, $dot_notation:expr) => {{
gc_borrow_inner_mut!(try_to_get_json_object_value_gc_mut!(
$amx,
$object,
$name,
$dot_notation
))
}};
}

macro_rules! try_to_get_json_array {
($amx:expr, $array:expr) => {
match &try_to_get_json_value!($amx, $array) {
Expand Down Expand Up @@ -269,11 +271,23 @@ use std::rc::Rc;

pub trait ValueExt<'a> {
fn dot_index_safe_rc_gc(&'a self, name: &str) -> Result<Rc<RefCell<GCValue>>>;
fn dot_index_safe_or_insert_rc_gc(
&'a self,
name: &str,
default: Option<InnerValue>,
) -> Result<Rc<RefCell<GCValue>>>;
fn index_selective_safe_rc_gc(
&'a self,
name: &str,
dot_notation: bool,
) -> Result<Rc<RefCell<GCValue>>>;

fn index_selective_safe_or_insert_rc_gc(
&'a self,
name: &str,
dot_notation: bool,
default: Option<InnerValue>,
) -> Result<Rc<RefCell<GCValue>>>;
}

impl<'a> ValueExt<'a> for GCValue {
Expand Down Expand Up @@ -301,6 +315,90 @@ impl<'a> ValueExt<'a> for GCValue {
Ok(it.chain_err(|| "Name is invalid")?)
}

fn dot_index_safe_or_insert_rc_gc(
&'a self,
name: &str,
default: Option<InnerValue>,
) -> Result<Rc<RefCell<GCValue>>> {
let mut it: Option<Rc<RefCell<GCValue>>> = None;
let names = name.split('.').collect::<Vec<&str>>();
let len = names.len();
for (default_to_insert, element) in {
names.into_iter().enumerate().map(|(index, string)| {
(
if index == len - 1 {
default.clone()
} else {
None
},
string,
)
})
} {
if element.is_empty() {
bail!("Double/Empty separator in `{}`", name);
}

// Same as bounds checked index.
if let Some(it_raw) = it {
let it_raw: &RefCell<_> = it_raw.borrow();
it = Some(
it_raw
.borrow_mut()
.index_selective_safe_or_insert_rc_gc(element, false, default_to_insert)?
.clone(),
);
} else {
it = Some(
self.index_selective_safe_or_insert_rc_gc(element, false, default_to_insert)?
.clone(),
);
}
}

Ok(it.chain_err(|| "Name is invalid")?)
}

fn index_selective_safe_or_insert_rc_gc(
&'a self,
name: &str,
dot_notation: bool,
default: Option<InnerValue>,
) -> Result<Rc<RefCell<GCValue>>> {
if dot_notation {
self.dot_index_safe_or_insert_rc_gc(name, default)
} else {
match &mut self.borrow_inner_ref_mut() as &mut InnerValue {
InnerValue::Object(m) => match {
match m.get_mut(name) {
None => {
if let Some(default) = default {
m.insert(
name.to_owned(),
Rc::new(RefCell::new(GCValue::new(default))),
);
m.get_mut(name)
} else {
None
}
}
v => v,
}
} {
Some(target) => Ok(target.clone()),
None => bail!(
"Can't index json using `{}`, because json doesn't contain it",
name
),
},
_ => bail!(
"Can't index json using `{}` json stops is not object.",
name
),
}
}
}

fn index_selective_safe_rc_gc(
&'a self,
name: &str,
Expand All @@ -312,7 +410,7 @@ impl<'a> ValueExt<'a> for GCValue {
match &self.borrow_inner_ref() as &InnerValue {
InnerValue::Object(m) => match m.get(name) {
Some(target) => Ok(target.clone()),
_ => bail!(
None => bail!(
"Can't index json using `{}`, because json doesn't contain it",
name
),
Expand Down Expand Up @@ -366,7 +464,9 @@ mod tests {

fn gc_to_json(v: Rc<RefCell<GCValue>>) -> Value {
let value: &RefCell<_> = v.borrow();
(*gc_borrow_inner!(value.borrow())).clone().into_with_recursion_limit(2)
(*gc_borrow_inner!(value.borrow()))
.clone()
.into_with_recursion_limit(2)
}

assert_eq!(
Expand All @@ -391,6 +491,50 @@ mod tests {

assert!(gc_to_json(json.index_selective_safe_rc_gc("a", false).unwrap()).is_object());
assert!(json.index_selective_safe_rc_gc("a.b.c", false).is_err());

assert_eq!(
gc_to_json(
json.index_selective_safe_or_insert_rc_gc("a.b", true, None)
.unwrap()
)
.as_u64()
.unwrap(),
123
);
assert!(json
.index_selective_safe_or_insert_rc_gc("a.b.c", true, None)
.is_err());
assert!(json
.index_selective_safe_or_insert_rc_gc("a..", true, None)
.is_err());
assert!(gc_to_json(
json.index_selective_safe_or_insert_rc_gc("a", true, None)
.unwrap()
)
.is_object());

assert!(gc_to_json(
json.index_selective_safe_or_insert_rc_gc("a", false, None)
.unwrap()
)
.is_object());
assert!(json
.index_selective_safe_or_insert_rc_gc("a.b.c", false, None)
.is_err());

assert!(json
.index_selective_safe_or_insert_rc_gc("a.d.d", false, None)
.is_err());

assert!(gc_to_json(
json.index_selective_safe_or_insert_rc_gc("a.d", true, Some(InnerValue::Null))
.unwrap()
)
.is_null());

assert!(json
.index_selective_safe_or_insert_rc_gc("a.d.d", false, None)
.is_err());
}

}
Loading

0 comments on commit 0a492f8

Please sign in to comment.