-
Notifications
You must be signed in to change notification settings - Fork 83
feat: Convert custom_attribute_condition_evaluator to TS #561
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
feat: Convert custom_attribute_condition_evaluator to TS #561
Conversation
4856aca
to
408e825
Compare
type Condition = { | ||
[name: string]: 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.
Let's be more specific here. You can check out the structure of condition objects in the datafile by creating audiences with different condition types.
Condition objects have type
, name
, match
, and value
properties. The functions in this module use name
, match
, and value
.
|
||
/** | ||
* Returns true if the value is valid for exact conditions. Valid values include | ||
* strings, booleans, and numbers that aren't NaN, -Infinity, or Infinity. | ||
* @param value | ||
* @returns {Boolean} | ||
* @returns {?boolean} |
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.
Should this just be boolean
, not ?boolean
?
import { isNumber, isSafeInteger } from '../../utils/fns'; | ||
import { | ||
LOG_LEVEL, | ||
LOG_MESSAGES, | ||
} from '../../utils/enums'; | ||
|
||
var MODULE_NAME = 'CUSTOM_ATTRIBUTE_CONDITION_EVALUATOR'; | ||
// TODO: Find place for UserAttributes and Condition types to live in |
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.
We could create a shared types module at lib/shared_types.ts, and put this there.
It would be nice if somehow we could import < 10000 code class="notranslate">UserAttributes from there and use it here, as well as also in lib/index.d.ts. I had some trouble figuring that out, it may not be possible. If so I think we can just duplicate the definition (have one in lib/index.d.ts, and one in lib/shared_types.ts which is imported by out TS modules).
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 also tried to use UserAttributes
in lib/index.d.ts from lib/shared_types.ts i, but could not figure it out how to make it work, so left lib/index.d.ts as is and followed your suggestion.
9bbbd51
to
408e825
Compare
packages/optimizely-sdk/lib/core/custom_attribute_condition_evaluator/index.ts
Outdated
Show resolved
Hide resolved
* TODO: Change to accept and object with named properties | ||
*/ | ||
export var evaluate = function(condition, userAttributes, logger) { | ||
var conditionMatch = condition.match; | ||
export function evaluate(condition: Condition, userAttributes: UserAttributes, logger: LoggerFacade): boolean | null { |
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 a good opportunity to clean up logging in this module.
- Let's change all these functions so that they use a logger defined inside this module (like
const logger = getLogger(MODULE_NAME);
, and then remove thelogger
argument from the list of parameters. - Call the logger method corresponding to the desired level, rather than passing in level (
logger.debug(...)
instead oflogger.log(LOG_LEVEL.DEBUG, ...)
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.
Looks way better, thank you! Just one requested change then this should be ready to merge!
if (!conditionMatch) { | ||
evaluatorForMatch = exactEvaluator; | ||
} else { | ||
evaluatorForMatch = EVALUATORS_BY_MATCH_TYPE[conditionMatch]; | ||
} | ||
if (!evaluatorForMatch) { | ||
return null; | ||
} |
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.
Assuming the whole codebase was TS and passed type checking, I don't think control can ever reach line 88. But the compiler will complain if we remove this if (!evaluatorForMatch)
check. We don't want to return null
without logging a warning message like in the beginning of this function.
Let's do this instead:
if (!conditionMatch) { | |
evaluatorForMatch = exactEvaluator; | |
} else { | |
evaluatorForMatch = EVALUATORS_BY_MATCH_TYPE[conditionMatch]; | |
} | |
if (!evaluatorForMatch) { | |
return null; | |
} | |
if (!conditionMatch) { | |
evaluatorForMatch = exactEvaluator; | |
} else { | |
evaluatorForMatch = EVALUATORS_BY_MATCH_TYPE[conditionMatch] || exactEvaluator; | |
} |
Summary
custom_attribute_condition_evaluator
module from JS to TS.Test plan