You've already forked pgbackrest
mirror of
https://github.com/pgbackrest/pgbackrest.git
synced 2025-09-16 09:06:18 +02:00
Add varStrForce() to Variant object.
This commit is contained in:
@@ -465,6 +465,67 @@ varStr(const Variant *this)
|
||||
return result;
|
||||
}
|
||||
|
||||
/***********************************************************************************************************************************
|
||||
Return string regardless of variant type
|
||||
***********************************************************************************************************************************/
|
||||
String *
|
||||
varStrForce(const Variant *this)
|
||||
{
|
||||
String *result = NULL;
|
||||
|
||||
switch (varType(this))
|
||||
{
|
||||
case varTypeBool:
|
||||
{
|
||||
if (varBool(this))
|
||||
result = strNew("true");
|
||||
else
|
||||
result = strNew("false");
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case varTypeDouble:
|
||||
{
|
||||
String *working = strNewFmt("%f", varDbl(this));
|
||||
|
||||
// Strip off any final 0s and the decimal point if there are no non-zero digits after it
|
||||
const char *begin = strPtr(working);
|
||||
const char *end = begin + strSize(working) - 1;
|
||||
|
||||
while (end > strPtr(working) && (*end == '0' || *end == '.'))
|
||||
{
|
||||
end--;
|
||||
|
||||
if (*(end + 1) == '.')
|
||||
break;
|
||||
}
|
||||
|
||||
result = strNewN(begin, end - begin + 1);
|
||||
strFree(working);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case varTypeInt:
|
||||
{
|
||||
result = strNewFmt("%d", varInt(this));
|
||||
break;
|
||||
}
|
||||
|
||||
case varTypeString:
|
||||
{
|
||||
result = strDup(varStr(this));
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
THROW(FormatError, "unable to force %s to %s", variantTypeName[this->type], variantTypeName[varTypeString]);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/***********************************************************************************************************************************
|
||||
New variant list variant
|
||||
***********************************************************************************************************************************/
|
||||
|
@@ -49,6 +49,7 @@ KeyValue *varKv(const Variant *this);
|
||||
Variant *varNewStr(const String *data);
|
||||
Variant *varNewStrZ(const char *data);
|
||||
String *varStr(const Variant *this);
|
||||
String *varStrForce(const Variant *this);
|
||||
|
||||
Variant *varNewVarLst(const VariantList *data);
|
||||
Variant *varNewVarLstEmpty();
|
||||
|
@@ -58,9 +58,10 @@ StringList *perlCommand()
|
||||
}
|
||||
|
||||
case cfgDefOptTypeFloat:
|
||||
case cfgDefOptTypeInteger:
|
||||
case cfgDefOptTypeString:
|
||||
{
|
||||
strCatFmt(mainCallParam, ", '--%s'", cfgOptionName(optionId));
|
||||
strCatFmt(mainCallParam, ", '%lg'", cfgOptionDbl(optionId));
|
||||
strCatFmt(mainCallParam, ", '--%s', '%s'", cfgOptionName(optionId), strPtr(varStrForce(cfgOption(optionId))));
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -80,13 +81,6 @@ StringList *perlCommand()
|
||||
break;
|
||||
}
|
||||
|
||||
case cfgDefOptTypeInteger:
|
||||
{
|
||||
strCatFmt(mainCallParam, ", '--%s'", cfgOptionName(optionId));
|
||||
strCatFmt(mainCallParam, ", '%d'", cfgOptionInt(optionId));
|
||||
break;
|
||||
}
|
||||
|
||||
case cfgDefOptTypeList:
|
||||
{
|
||||
StringList *valueList = strLstNewVarLst(cfgOptionLst(optionId));
|
||||
@@ -99,13 +93,6 @@ StringList *perlCommand()
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case cfgDefOptTypeString:
|
||||
{
|
||||
strCatFmt(mainCallParam, ", '--%s'", cfgOptionName(optionId));
|
||||
strCatFmt(mainCallParam, ", '%s'", strPtr(cfgOptionStr(optionId)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -122,24 +109,15 @@ StringList *perlCommand()
|
||||
for (unsigned int paramIdx = 0; paramIdx < strLstSize(cfgCommandParam()); paramIdx++)
|
||||
strCatFmt(mainCallParam, ", '%s'", strPtr(strLstGet(cfgCommandParam(), paramIdx)));
|
||||
|
||||
// Construct perl option list to add to bin
|
||||
// Add Perl options
|
||||
StringList *perlOptionList = strLstNewVarLst(cfgOptionLst(cfgOptPerlOption));
|
||||
String *binPerlOption = strNew("");
|
||||
|
||||
if (perlOptionList != NULL)
|
||||
{
|
||||
for (unsigned int argIdx = 0; argIdx < strLstSize(perlOptionList); argIdx++)
|
||||
{
|
||||
// // Add to bin option list
|
||||
// strCatFmt(binPerlOption, " --" PARAM_PERL_OPTION "=\"%s\"", strPtr(strLstGet(perlOptionList, argIdx)));
|
||||
|
||||
// Add to list that will be passed to exec
|
||||
strLstAdd(perlArgList, strLstGet(perlOptionList, argIdx));
|
||||
}
|
||||
}
|
||||
|
||||
// Construct Perl main call
|
||||
String *mainCall = strNewFmt(PGBACKREST_MAIN "('%s%s'%s)", strPtr(cfgExe()), strPtr(binPerlOption), strPtr(mainCallParam));
|
||||
String *mainCall = strNewFmt(PGBACKREST_MAIN "('%s'%s)", strPtr(cfgExe()), strPtr(mainCallParam));
|
||||
|
||||
// End arg list for perl exec
|
||||
strLstAdd(perlArgList, strNew("-M" PGBACKREST_MODULE));
|
||||
|
@@ -163,6 +163,18 @@ void testRun()
|
||||
|
||||
TEST_ERROR(varBoolForce(varNewStr(strNew(BOGUS_STR))), FormatError, "unable to convert str 'BOGUS' to bool");
|
||||
|
||||
// -------------------------------------------------------------------------------------------------------------------------
|
||||
TEST_RESULT_STR(strPtr(varStrForce(varNewStr(strNew("teststring")))), "teststring", "force string to string");
|
||||
TEST_RESULT_STR(strPtr(varStrForce(varNewInt(999))), "999", "force int to string");
|
||||
TEST_RESULT_STR(strPtr(varStrForce(varNewDbl(999.1234))), "999.1234", "force double to string");
|
||||
TEST_RESULT_STR(strPtr(varStrForce(varNewDbl((double)999999999.123456))), "999999999.123456", "force double to string");
|
||||
TEST_RESULT_STR(strPtr(varStrForce(varNewDbl(999.0))), "999", "force double to string");
|
||||
TEST_RESULT_STR(strPtr(varStrForce(varNewDbl(9990.0))), "9990", "force double to string");
|
||||
TEST_RESULT_STR(strPtr(varStrForce(varNewBool(true))), "true", "force bool to string");
|
||||
TEST_RESULT_STR(strPtr(varStrForce(varNewBool(false))), "false", "force bool to string");
|
||||
|
||||
TEST_ERROR(varStrForce(varNewKv()), FormatError, "unable to force KeyValue to String");
|
||||
|
||||
// -------------------------------------------------------------------------------------------------------------------------
|
||||
string = varNewStr(strNew("not-an-int"));
|
||||
TEST_ERROR(varIntForce(string), FormatError, "unable to convert str 'not-an-int' to int");
|
||||
|
Reference in New Issue
Block a user