Skip to content

Commit

Permalink
Borrow data for validation
Browse files Browse the repository at this point in the history
Using this KSY:
```
  - id: one_cat
    type: cat
    valid:
      expr: _._sizeof == 1
```

For C++11 changes this incorrect code:
```
m_one_cat = std::unique_ptr<cat_t>(new cat_t(m__io, this, m__root));
m_one_cat->_read();
{
    // Definition: cat_t* one_cat() { return m_one_cat.get(); }
    std::unique_ptr<cat_t> _ = one_cat();
    if (!(1 == 1)) {
        throw kaitai::validation_expr_error<std::unique_ptr<debug_array_user_t::cat_t>>(one_cat(), _io(), std::string("/seq/0"));
    }
}
```

to this:

```
m_one_cat = std::unique_ptr<cat_t>(new cat_t(m__io, this, m__root));
m_one_cat->_read();
{
    // Definition: cat_t* one_cat() { return m_one_cat.get(); }
    cat_t* _ = one_cat();
    if (!(1 == 1)) {
        throw kaitai::validation_expr_error<debug_array_user_t::cat_t*>(one_cat(), _io(), std::string("/seq/0"));
    }
}
```
  • Loading branch information
Mingun committed Oct 4, 2024
1 parent 542b241 commit 6f73836
Showing 1 changed file with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ trait ValidateOps extends ExceptionNames {
val typeProvider: ClassTypeProvider

def attrValidate(attr: AttrLikeSpec, valid: ValidationSpec): Unit = {
val attrTypeRef = attr.dataType.asNonOwning()
val itemValue = Identifier.itemExpr(attr.id, attr.cond.repeat)
valid match {
case ValidationEq(expected) =>
attrValidateExprCompare(attr, Ast.cmpop.Eq, expected, ValidationNotEqualError(attr.dataType))
attrValidateExprCompare(attr, Ast.cmpop.Eq, expected, ValidationNotEqualError(attrTypeRef))
case ValidationMin(min) =>
attrValidateExprCompare(attr, Ast.cmpop.GtE, min, ValidationLessThanError(attr.dataType))
attrValidateExprCompare(attr, Ast.cmpop.GtE, min, ValidationLessThanError(attrTypeRef))
case ValidationMax(max) =>
attrValidateExprCompare(attr, Ast.cmpop.LtE, max, ValidationGreaterThanError(attr.dataType))
attrValidateExprCompare(attr, Ast.cmpop.LtE, max, ValidationGreaterThanError(attrTypeRef))
case ValidationRange(min, max) =>
attrValidateExprCompare(attr, Ast.cmpop.GtE, min, ValidationLessThanError(attr.dataType))
attrValidateExprCompare(attr, Ast.cmpop.LtE, max, ValidationGreaterThanError(attr.dataType))
attrValidateExprCompare(attr, Ast.cmpop.GtE, min, ValidationLessThanError(attrTypeRef))
attrValidateExprCompare(attr, Ast.cmpop.LtE, max, ValidationGreaterThanError(attrTypeRef))
case ValidationAnyOf(values) =>
val bigOrExpr = Ast.expr.BoolOp(
Ast.boolop.Or,
Expand All @@ -41,7 +42,7 @@ trait ValidateOps extends ExceptionNames {
attrValidateExpr(
attr,
checkExpr = bigOrExpr,
err = ValidationNotAnyOfError(attr.dataType),
err = ValidationNotAnyOfError(attrTypeRef),
errArgs = List(
itemValue,
Ast.expr.InternalName(IoIdentifier),
Expand All @@ -62,16 +63,19 @@ trait ValidateOps extends ExceptionNames {
)
case ValidationExpr(expr) =>
blockScopeHeader
typeProvider._currentIteratorType = Some(attr.dataType)
typeProvider._currentIteratorType = Some(attrTypeRef)
// Store value of attribute in the temporary variable with a name that is
// used for `_` variable, because in expression we refer to current value
// using this variable
handleAssignmentTempVar(
attr.dataType,
attrTypeRef,
translator.translate(Ast.expr.Name(Ast.identifier(Identifier.ITERATOR))),
translator.translate(itemValue)
)
attrValidateExpr(
attr,
expr,
ValidationExprError(attr.dataType),
ValidationExprError(attrTypeRef),
List(
itemValue,
Ast.expr.InternalName(IoIdentifier),
Expand Down

0 comments on commit 6f73836

Please sign in to comment.