Skip to content

Commit a187ef4

Browse files
authoredMay 10, 2021
Use encapsulated string constants for non-file sources (#551)
Signed-off-by: Eric Cousineau <eric.cousineau@tri.global>
1 parent 6fa8a19 commit a187ef4

File tree

3 files changed

+22
-28
lines changed

3 files changed

+22
-28
lines changed
 

‎include/sdf/Types.hh

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ namespace sdf
6565

6666
/// \brief The source path replacement if it was parsed from a string,
6767
/// instead of a file.
68-
constexpr char kSdfStringSource[] = "data-string";
68+
constexpr char kSdfStringSource[] = "<data-string>";
6969

7070
/// \brief The source path replacement if the urdf was parsed from a string,
7171
/// instead of a file.
72-
constexpr char kUrdfStringSource[] = "urdf string";
72+
constexpr char kUrdfStringSource[] = "<urdf-string>";
7373

7474
/// \brief Split a string using the delimiter in splitter.
7575
/// \param[in] str The string to split.

‎src/parser.cc

+18-22
Original file line numberDiff line numberDiff line change
@@ -932,10 +932,6 @@ std::string getModelFilePath(const std::string &_modelDirPath)
932932
bool readXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf,
933933
const ParserConfig &_config, const std::string &_source, Errors &_errors)
934934
{
935-
std::string sourcePath = _source;
936-
if (_source == kSdfStringSource || _source == kUrdfStringSource)
937-
sourcePath = "<" + _source + ">";
938-
939935
// Check if the element pointer is deprecated.
940936
if (_sdf->GetRequired() == "-1")
941937
{
@@ -954,7 +950,7 @@ bool readXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf,
954950
_errors.push_back({
955951
ErrorCode::ELEMENT_MISSING,
956952
"SDF Element<" + _sdf->GetName() + "> is missing",
957-
sourcePath});
953+
_source});
958954
return false;
959955
}
960956
else
@@ -1028,7 +1024,7 @@ bool readXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf,
10281024
"'" + std::string(attribute->Value()) +
10291025
"' is reserved; it cannot be used as a value of "
10301026
"attribute [" + p->GetKey() + "]",
1031-
sourcePath,
1027+
_source,
10321028
attribute->GetLineNum()});
10331029
}
10341030
}
@@ -1038,7 +1034,7 @@ bool readXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf,
10381034
_errors.push_back({
10391035
ErrorCode::ATTRIBUTE_INVALID,
10401036
"Unable to read attribute[" + p->GetKey() + "]",
1041-
sourcePath,
1037+
_source,
10421038
attribute->GetLineNum()});
10431039
return false;
10441040
}
@@ -1057,7 +1053,7 @@ bool readXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf,
10571053
Error(
10581054
ErrorCode::ATTRIBUTE_INCORRECT_TYPE,
10591055
ss.str(),
1060-
sourcePath,
1056+
_source,
10611057
_xml->GetLineNum()),
10621058
_errors);
10631059
}
@@ -1075,7 +1071,7 @@ bool readXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf,
10751071
ErrorCode::ATTRIBUTE_MISSING,
10761072
"Required attribute[" + p->GetKey() + "] in element[" + _xml->Value()
10771073
+ "] is not specified in SDF.",
1078-
sourcePath,
1074+
_source,
10791075
_xml->GetLineNum()});
10801076
return false;
10811077
}
@@ -1111,7 +1107,7 @@ bool readXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf,
11111107
_errors.push_back({
11121108
ErrorCode::URI_LOOKUP,
11131109
"Unable to find uri[" + uri + "]",
1114-
sourcePath,
1110+
_source,
11151111
uriElement->GetLineNum()});
11161112
continue;
11171113
}
@@ -1129,7 +1125,7 @@ bool readXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf,
11291125
"Unable to resolve uri[" + uri + "] to model path [" +
11301126
modelPath + "] since it does not contain a model.config " +
11311127
"file.",
1132-
sourcePath,
1128+
_source,
11331129
uriElement->GetLineNum()});
11341130
continue;
11351131
}
@@ -1148,7 +1144,7 @@ bool readXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf,
11481144
_errors.push_back({
11491145
ErrorCode::ATTRIBUTE_MISSING,
11501146
"<include> element missing 'uri' attribute",
1151-
sourcePath,
1147+
_source,
11521148
elemXml->GetLineNum()});
11531149
continue;
11541150
}
@@ -1177,7 +1173,7 @@ bool readXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf,
11771173
_errors.push_back({
11781174
ErrorCode::FILE_READ,
11791175
"Unable to read file[" + filename + "]",
1180-
sourcePath,
1176+
_source,
11811177
uriElement->GetLineNum()});
11821178
return false;
11831179
}
@@ -1219,7 +1215,7 @@ bool readXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf,
12191215
ErrorCode::ELEMENT_MISSING,
12201216
"Failed to find top level <model> / <actor> / <light> for "
12211217
"<include>\n",
1222-
sourcePath,
1218+
_source,
12231219
uriElement->GetLineNum()});
12241220
continue;
12251221
}
@@ -1294,7 +1290,7 @@ bool readXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf,
12941290
ErrorCode::MODEL_PLACEMENT_FRAME_INVALID,
12951291
"<pose> is required when specifying the placement_frame "
12961292
"element",
1297-
sourcePath,
1293+
_source,
12981294
elemXml->GetLineNum()});
12991295
return false;
13001296
}
@@ -1308,7 +1304,7 @@ bool readXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf,
13081304
"'" + placementFrameVal +
13091305
"' is reserved; it cannot be used as a value of "
13101306
"element [placement_frame]",
1311-
sourcePath,
1307+
_source,
13121308
placementFrameElem->GetLineNum()});
13131309
}
13141310
topLevelElem->GetAttribute("placement_frame")
@@ -1327,12 +1323,12 @@ bool readXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf,
13271323
pluginElem = topLevelElem->AddElement("plugin");
13281324

13291325
if (!readXml(
1330-
childElemXml, pluginElem, _config, sourcePath, _errors))
1326+
childElemXml, pluginElem, _config, _source, _errors))
13311327
{
13321328
_errors.push_back({
13331329
ErrorCode::ELEMENT_INVALID,
13341330
"Error reading plugin element",
1335-
sourcePath,
1331+
_source,
13361332
childElemXml->GetLineNum()});
13371333
return false;
13381334
}
@@ -1367,7 +1363,7 @@ bool readXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf,
13671363
{
13681364
ElementPtr element = elemDesc->Clone();
13691365
element->SetParent(_sdf);
1370-
if (readXml(elemXml, element, _config, sourcePath, _errors))
1366+
if (readXml(elemXml, element, _config, _source, _errors))
13711367
{
13721368
_sdf->InsertElement(element);
13731369
}
@@ -1377,7 +1373,7 @@ bool readXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf,
13771373
ErrorCode::ELEMENT_INVALID,
13781374
std::string("Error reading element <") +
13791375
elemXml->Value() + ">",
1380-
sourcePath,
1376+
_source,
13811377
elemXml->GetLineNum()});
13821378
return false;
13831379
}
@@ -1399,7 +1395,7 @@ bool readXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf,
13991395
Error(
14001396
ErrorCode::ELEMENT_INCORRECT_TYPE,
14011397
ss.str(),
1402-
sourcePath,
1398+
_source,
14031399
elemXml->GetLineNum()),
14041400
_errors);
14051401

@@ -1427,7 +1423,7 @@ bool readXml(tinyxml2::XMLElement *_xml, ElementPtr _sdf,
14271423
ErrorCode::ELEMENT_MISSING,
14281424
"XML Missing required element[" + elemDesc->GetName() +
14291425
"], child of element[" + _sdf->GetName() + "]",
1430-
sourcePath,
1426+
_source,
14311427
elemXml->GetLineNum()});
14321428
return false;
14331429
}

‎src/parser_TEST.cc

+2-4
Original file line numberDiff line numberDiff line change
@@ -656,8 +656,7 @@ TEST(Parser, ReadSingleLineStringError)
656656

657657
EXPECT_EQ(errors[0].Code(), sdf::ErrorCode::ATTRIBUTE_MISSING);
658658
ASSERT_TRUE(errors[0].FilePath().has_value());
659-
EXPECT_EQ(errors[0].FilePath().value(),
660-
"<" + std::string(sdf::kSdfStringSource) + ">");
659+
EXPECT_EQ(errors[0].FilePath().value(), sdf::kSdfStringSource);
661660
ASSERT_TRUE(errors[0].LineNumber().has_value());
662661
EXPECT_EQ(errors[0].LineNumber().value(), 1);
663662
}
@@ -695,8 +694,7 @@ TEST(Parser, ReadMultiLineStringError)
695694

696695
EXPECT_EQ(errors[0].Code(), sdf::ErrorCode::ATTRIBUTE_MISSING);
697696
ASSERT_TRUE(errors[0].FilePath().has_value());
698-
EXPECT_EQ(errors[0].FilePath().value(),
699-
"<" + std::string(sdf::kSdfStringSource) + ">");
697+
EXPECT_EQ(errors[0].FilePath().value(), sdf::kSdfStringSource);
700698
ASSERT_TRUE(errors[0].LineNumber().has_value());
701699
EXPECT_EQ(errors[0].LineNumber().value(), 10);
702700
}

0 commit comments

Comments
 (0)
Please sign in to comment.