-
Notifications
You must be signed in to change notification settings - Fork 157
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
Regex support in valid key #200
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -10,6 +10,7 @@ case class ValidationMax(max: Ast.expr) extends ValidationSpec | |||
case class ValidationRange(min: Ast.expr, max: Ast.expr) extends ValidationSpec | ||||
case class ValidationAnyOf(values: List[Ast.expr]) extends ValidationSpec | ||||
case class ValidationExpr(checkExpr: Ast.expr) extends ValidationSpec | ||||
case class ValidationRegex(checkExpr: String) extends ValidationSpec | ||||
|
||||
object ValidationEq { | ||||
val LEGAL_KEYS = Set("eq") | ||||
|
@@ -68,16 +69,29 @@ object ValidationExpr { | |||
} | ||||
} | ||||
|
||||
object ValidationRegex { | ||||
val LEGAL_KEYS = Set("regex") | ||||
|
||||
def fromMap(src: Map[String, Any], path: List[String]): Option[ValidationRegex] = | ||||
ParseUtils.getOptValueStr(src, "regex", path).map { case eqExpr => | ||||
ParseUtils.ensureLegalKeys(src, LEGAL_KEYS, path) | ||||
ValidationRegex(eqExpr) | ||||
} | ||||
} | ||||
|
||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: unnecessary double newline.
Suggested change
|
||||
object ValidationSpec { | ||||
val LEGAL_KEYS = | ||||
ValidationEq.LEGAL_KEYS ++ | ||||
ValidationRange.LEGAL_KEYS ++ | ||||
ValidationAnyOf.LEGAL_KEYS ++ | ||||
ValidationExpr.LEGAL_KEYS | ||||
ValidationExpr.LEGAL_KEYS ++ | ||||
ValidationRegex.LEGAL_KEYS | ||||
|
||||
def fromYaml(src: Any, path: List[String]): ValidationSpec = { | ||||
src match { | ||||
case value: String => | ||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: unnecessary newline
Suggested change
|
||||
fromString(value, path) | ||||
case x: Boolean => | ||||
fromString(x.toString, path) | ||||
|
@@ -108,6 +122,9 @@ object ValidationSpec { | |||
val opt4 = ValidationExpr.fromMap(src, path) | ||||
if (opt4.nonEmpty) | ||||
return opt4.get | ||||
val opt5 = ValidationRegex.fromMap(src, path) | ||||
if (opt5.nonEmpty) | ||||
return opt5.get | ||||
|
||||
// No validation templates matched, check for any bogus keys | ||||
ParseUtils.ensureLegalKeys(src, LEGAL_KEYS, path) | ||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -102,6 +102,14 @@ abstract class BaseTranslator(val provider: TypeProvider) | |||||
case (ltype, rtype) => | ||||||
throw new TypeMismatchError(s"can't compare $ltype and $rtype") | ||||||
} | ||||||
case Ast.expr.RegexMatch(str: Ast.expr, regex: String) => { | ||||||
detectType(str) match { | ||||||
case (_: StrType) => | ||||||
doRegexMatchOp(translate(str), doRegexFullLine(regex)) | ||||||
case _ => | ||||||
throw new TypeMismatchError(s"regex match need strings") | ||||||
} | ||||||
} | ||||||
case Ast.expr.BinOp(left: Ast.expr, op: Ast.operator, right: Ast.expr) => | ||||||
(detectType(left), detectType(right), op) match { | ||||||
case (_: NumericType, _: NumericType, _) => | ||||||
|
@@ -198,4 +206,6 @@ abstract class BaseTranslator(val provider: TypeProvider) | |||||
// for the language | ||||||
def anyField(value: Ast.expr, attrName: String): String = | ||||||
s"${translate(value)}.${doName(attrName)}" | ||||||
|
||||||
def doRegexMatchOp(str: String, regex: String): String | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: formatting.
Suggested change
|
||||||
} |
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.
For equality checks (and storing original value in question for C++), we need to know original value data type. But regular expression matches would likely only work for strings. What's the point in passing
_dt: DataType
here?