Sanely handle optional fields set to their default values.

Specify when this is allowed and when it's prohibited.
This commit is contained in:
JoelKatz
2012-11-16 15:11:16 -08:00
parent 7b1540c5fd
commit 8bd212e6bc
5 changed files with 16 additions and 9 deletions

View File

@@ -30,7 +30,8 @@ enum SOE_Flags
{
SOE_INVALID = -1,
SOE_REQUIRED = 0, // required
SOE_OPTIONAL = 1, // optional
SOE_OPTIONAL = 1, // optional, may be present with default value
SOE_DEFAULT = 2, // optional, if present, must not have default value
};
class SField

View File

@@ -39,10 +39,10 @@ static bool LEFInit()
<< SOElement(sfOwner, SOE_REQUIRED)
<< SOElement(sfExpiration, SOE_REQUIRED)
<< SOElement(sfBondAmount, SOE_REQUIRED)
<< SOElement(sfCreateCode, SOE_REQUIRED)
<< SOElement(sfFundCode, SOE_REQUIRED)
<< SOElement(sfRemoveCode, SOE_REQUIRED)
<< SOElement(sfExpireCode, SOE_REQUIRED)
<< SOElement(sfCreateCode, SOE_OPTIONAL)
<< SOElement(sfFundCode, SOE_OPTIONAL)
<< SOElement(sfRemoveCode, SOE_OPTIONAL)
<< SOElement(sfExpireCode, SOE_OPTIONAL)
;
DECLARE_LEF(DirectoryNode, ltDIR_NODE)

View File

@@ -138,7 +138,7 @@ void STObject::set(const std::vector<SOElement::ptr>& type)
BOOST_FOREACH(const SOElement::ptr& elem, type)
{
mType.push_back(elem);
if (elem->flags == SOE_OPTIONAL)
if (elem->flags != SOE_REQUIRED)
giveObject(makeNonPresentObject(elem->e_field));
else
giveObject(makeDefaultObject(elem->e_field));
@@ -159,12 +159,18 @@ bool STObject::setType(const std::vector<SOElement::ptr> &type)
{
match = true;
newData.push_back(mData.release(it).release());
if ((elem->flags == SOE_DEFAULT) && it->isDefault())
{
cLog(lsWARNING) << "setType( " << getFName().getName() << ") invalid default "
<< elem->e_field.fieldName;
valid = false;
}
break;
}
if (!match)
{
if (elem->flags != SOE_OPTIONAL)
if (elem->flags == SOE_REQUIRED)
{
cLog(lsWARNING) << "setType( " << getFName().getName() << ") invalid missing "
<< elem->e_field.fieldName;

View File

@@ -688,7 +688,7 @@ public:
void addPath(const STPath& e) { value.push_back(e); }
virtual bool isEquivalent(const SerializedType& t) const;
virtual bool isDefault() const { return value.empty(); }
virtual bool isDefault() const { return value.empty(); }
void printDebug();

View File

@@ -52,7 +52,7 @@ static bool TFInit()
<< SOElement(sfDestination, SOE_REQUIRED)
<< SOElement(sfAmount, SOE_REQUIRED)
<< SOElement(sfSendMax, SOE_OPTIONAL)
<< SOElement(sfPaths, SOE_OPTIONAL)
<< SOElement(sfPaths, SOE_DEFAULT)
<< SOElement(sfInvoiceID, SOE_OPTIONAL)
;