-
Notifications
You must be signed in to change notification settings - Fork 188
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
Added is_type policy function #5691
base: master
Are you sure you want to change the base?
Conversation
Signed-off-by: Victor Moene <[email protected]> Changelog: Added is_type policy function to check type of a variable. Ticket: CFE-3641
f5a9349
to
e80fdd7
Compare
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.
Added some comments that you need to address. Otherwise, great work 🚀 Could you also make an acceptance test? This way we know for certain that it works, and that it continues working in the future. I'll help you get started if you haven't written one before 😉
libpromises/evalfunction.c
Outdated
// parse type | ||
const char* const type_arg_name = TrimWhitespace(RlistScalarValue(type_arg)); | ||
const char* const regex = " "; | ||
const Rlist *const first_arg = RlistFromRegexSplitNoOverflow(type_arg_name, regex, 2); |
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.
Using a regex for splitting a string on a white space character is a bit overkill. See if you can find an easier way to split the string. Also the RlistFromRegexSplitNoOverflow()
function may return error. So make sure that it is handled gracefully. We don't want the program to crash if the user uses the function wrong. Instead we would want to print an error message and return failure.
libpromises/evalfunction.c
Outdated
{ | ||
Log(LOG_LEVEL_ERR, "Function %s : invalid datatype", | ||
fp->name); | ||
return FnFailure(); |
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.
Is this an error? Should it not return FnReturnContext(false)
?
Signed-off-by: Victor Moene <[email protected]>
@@ -5276,38 +5276,21 @@ static FnCallResult FnCallFold(EvalContext *ctx, | |||
|
|||
/*********************************************************************/ | |||
|
|||
static FnCallResult FnCallDatatype(EvalContext *ctx, ARG_UNUSED const Policy *policy, const FnCall *fp, const Rlist *finalargs) | |||
static const char * DataTypeStringFromVarName(EvalContext *ctx, const char *var_name, bool detail) |
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.
const char *
commonly means / implies a string owned by something else, or in other words a string you don't need to free.
{ | ||
detail = BooleanFromString(RlistScalarValue(finalargs->next)); | ||
} | ||
const char * const ouptput_string = DataTypeStringFromVarName(ctx, var_name, detail); |
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.
Never freed.
} | ||
return StringConcatenate(2, "policy ", type_str); |
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.
Never freed.
} | ||
|
||
return FnReturnF("%s %s", type_str, subtype_str); | ||
return StringConcatenate(3, type_str, " ", subtype_str); |
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.
Never freed.
const char *const type_str = | ||
(type == CF_DATA_TYPE_NONE) ? "none" : DataTypeToString(type); | ||
|
||
if (!detail) | ||
{ | ||
return FnReturn(type_str); | ||
return type_str; |
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.
return type_str; | |
return StringDuplicate(type_str); |
@@ -5276,38 +5276,21 @@ static FnCallResult FnCallFold(EvalContext *ctx, | |||
|
|||
/*********************************************************************/ | |||
|
|||
static FnCallResult FnCallDatatype(EvalContext *ctx, ARG_UNUSED const Policy *policy, const FnCall *fp, const Rlist *finalargs) | |||
static const char * DataTypeStringFromVarName(EvalContext *ctx, const char *var_name, bool detail) |
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.
static const char * DataTypeStringFromVarName(EvalContext *ctx, const char *var_name, bool detail) | |
static char * DataTypeStringFromVarName(EvalContext *ctx, const char *var_name, bool detail) |
{ | ||
detail = BooleanFromString(RlistScalarValue(finalargs->next)); | ||
} | ||
const char * const ouptput_string = DataTypeStringFromVarName(ctx, var_name, detail); |
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.
const char * const ouptput_string = DataTypeStringFromVarName(ctx, var_name, detail); | |
char * const ouptput_string = DataTypeStringFromVarName(ctx, var_name, detail); |
return FnFailure(); | ||
} | ||
|
||
return FnReturnF("%s", ouptput_string); |
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.
This is how you can "give" it to the caller, and it's their job to free it.
return FnReturnF("%s", ouptput_string); | |
return FnReturnNoCopy(output_string); |
detail = true; | ||
} | ||
} | ||
const char * const type_string = DataTypeStringFromVarName(ctx, var_name, detail); |
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.
Never freed.
detail = true; | ||
} | ||
} | ||
const char * const type_string = DataTypeStringFromVarName(ctx, var_name, detail); |
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.
const char * const type_string = DataTypeStringFromVarName(ctx, var_name, detail); | |
char * const type_string = DataTypeStringFromVarName(ctx, var_name, detail); |
|
||
return FnReturnContext(StringEqual(type_name, type_string)); |
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.
return FnReturnContext(StringEqual(type_name, type_string)); | |
const bool matching = StringEqual(type_name, type_string); | |
free(type_string); | |
return FnReturnContext(matching); |
Changelog: Added is_type policy function to check type of a variable.
Ticket: CFE-3641