Skip to content

Commit

Permalink
Add Url accessor to Identifiers (#429)
Browse files Browse the repository at this point in the history
Previously we were relying on UniqueName to hold a Url. UniqueName was
updated to generate filesystem-safe paths for both Linux and Windows, so
it no longer holds a valid Url.  This adds a peer method to also
retrieve the Url of the Model/World/Collection

Signed-off-by: Michael Carroll <michael@openrobotics.org>
  • Loading branch information
mjcarroll authored Jul 24, 2024
1 parent e808b0a commit 9d8c1c2
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 1 deletion.
4 changes: 4 additions & 0 deletions include/gz/fuel_tools/CollectionIdentifier.hh
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ namespace gz::fuel_tools
/// \return Unique collection name.
public: std::string UniqueName() const;

/// \brief Returns a URL for the collection.
/// \remarks this is Server/Owner/Name.
public: gz::common::URI Url() const;

/// \brief Returns all the collection information as a string. Convenient
/// for debugging.
/// \param[in] _prefix Optional prefix for every line of the string.
Expand Down
4 changes: 4 additions & 0 deletions include/gz/fuel_tools/ModelIdentifier.hh
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ namespace gz::fuel_tools
/// \return Unique model name.
public: std::string UniqueName() const;

/// \brief Returns a URL for the model.
/// \remarks this is Server/Owner/Name.
public: gz::common::URI Url() const;

/// \brief set the name of the model.
/// \param[in] _name The name to set. Must be ascii and pass [-_a-z0-9]+.
/// \return true if successful.
Expand Down
4 changes: 4 additions & 0 deletions include/gz/fuel_tools/WorldIdentifier.hh
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ namespace gz::fuel_tools
/// \return Unique world name.
public: std::string UniqueName() const;

/// \brief Returns a URL for the world.
/// \remarks this is Server/Owner/Name.
public: gz::common::URI Url() const;

// /// \brief Sets the SHA 2 256 hash of the world
// /// \param[in] _hash a 256 bit SHA 2 hash
// /// \returns true if successful
Expand Down
10 changes: 10 additions & 0 deletions src/CollectionIdentifier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ std::string CollectionIdentifier::UniqueName() const
this->dataPtr->name);
}

//////////////////////////////////////////////////
gz::common::URI CollectionIdentifier::Url() const
{
return common::URI(
common::joinPaths(this->dataPtr->server.Url().Str(),
this->dataPtr->owner,
"collections",
this->dataPtr->name), true);
}

//////////////////////////////////////////////////
std::string CollectionIdentifier::Name() const
{
Expand Down
27 changes: 26 additions & 1 deletion src/CollectionIdentifier_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ TEST(CollectionIdentifier, SetFields)

/////////////////////////////////////////////////
/// \brief Unique Name
// See https://github.com/gazebosim/gz-fuel-tools/issues/231
TEST(CollectionIdentifier, UniqueName)
{
gz::fuel_tools::ServerConfig srv1;
Expand All @@ -69,6 +68,32 @@ TEST(CollectionIdentifier, UniqueName)
EXPECT_EQ("https://localhost:8003/alice/collections/hello", id.UniqueName());
}

/////////////////////////////////////////////////
/// \brief Unique Name
TEST(CollectionIdentifier, Url)
{
gz::fuel_tools::ServerConfig srv1;
srv1.SetUrl(common::URI("https://localhost:8001", true));

gz::fuel_tools::ServerConfig srv2;
srv2.SetUrl(common::URI("https://localhost:8002", true));

gz::fuel_tools::ServerConfig srv3;
srv3.SetUrl(common::URI("https://localhost:8003", true));

CollectionIdentifier id;
id.SetName("hello");
id.SetOwner("alice");
id.SetServer(srv1);
EXPECT_EQ("https://localhost:8001/alice/collections/hello", id.Url().Str());

id.SetServer(srv2);
EXPECT_EQ("https://localhost:8002/alice/collections/hello", id.Url().Str());

id.SetServer(srv3);
EXPECT_EQ("https://localhost:8003/alice/collections/hello", id.Url().Str());
}

/////////////////////////////////////////////////
/// \brief Copy constructor deep copies
TEST(CollectionIdentifier, CopyConstructorDeepCopy)
Expand Down
10 changes: 10 additions & 0 deletions src/ModelIdentifier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ std::string ModelIdentifier::UniqueName() const
this->dataPtr->name));
}

//////////////////////////////////////////////////
common::URI ModelIdentifier::Url() const
{
return common::URI(
common::joinPaths(this->dataPtr->server.Url().Str(),
this->dataPtr->owner,
"models",
this->dataPtr->name), true);
}

//////////////////////////////////////////////////
std::string ModelIdentifier::Name() const
{
Expand Down
26 changes: 26 additions & 0 deletions src/ModelIdentifier_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,32 @@ TEST(ModelIdentifier, UniqueName)
EXPECT_EQ("localhost%3A8003/alice/models/hello", id.UniqueName());
}

/////////////////////////////////////////////////
/// \brief Url
TEST(ModelIdentifier, Url)
{
gz::fuel_tools::ServerConfig srv1;
srv1.SetUrl(common::URI("https://localhost:8001", true));

gz::fuel_tools::ServerConfig srv2;
srv2.SetUrl(common::URI("https://localhost:8002", true));

gz::fuel_tools::ServerConfig srv3;
srv3.SetUrl(common::URI("https://localhost:8003", true));

ModelIdentifier id;
id.SetName("hello");
id.SetOwner("alice");
id.SetServer(srv1);
EXPECT_EQ("https://localhost:8001/alice/models/hello", id.Url().Str());

id.SetServer(srv2);
EXPECT_EQ("https://localhost:8002/alice/models/hello", id.Url().Str());

id.SetServer(srv3);
EXPECT_EQ("https://localhost:8003/alice/models/hello", id.Url().Str());
}

/////////////////////////////////////////////////
/// \brief Copy constructor deep copies
TEST(ModelIdentifier, CopyConstructorDeepCopy)
Expand Down
10 changes: 10 additions & 0 deletions src/WorldIdentifier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ std::string WorldIdentifier::UniqueName() const
this->dataPtr->name));
}

//////////////////////////////////////////////////
gz::common::URI WorldIdentifier::Url() const
{
return common::URI(
common::joinPaths(this->dataPtr->server.Url().Str(),
this->dataPtr->owner,
"worlds",
this->dataPtr->name), true);
}

//////////////////////////////////////////////////
std::string WorldIdentifier::Name() const
{
Expand Down
27 changes: 27 additions & 0 deletions src/WorldIdentifier_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,33 @@ TEST(WorldIdentifier, UniqueName)
EXPECT_EQ("localhost%3A8003/alice/worlds/hello", id.UniqueName());
}

/////////////////////////////////////////////////
/// \brief Url
TEST(WorldIdentifier, Url)
{
gz::fuel_tools::ServerConfig srv1;
srv1.SetUrl(gz::common::URI("https://localhost:8001/", true));

gz::fuel_tools::ServerConfig srv2;
srv2.SetUrl(gz::common::URI("https://localhost:8002", true));

gz::fuel_tools::ServerConfig srv3;
srv3.SetUrl(gz::common::URI("https://localhost:8003/", true));

WorldIdentifier id;
id.SetName("hello");
id.SetOwner("alice");

id.SetServer(srv1);
EXPECT_EQ("https://localhost:8001/alice/worlds/hello", id.Url().Str());

id.SetServer(srv2);
EXPECT_EQ("https://localhost:8002/alice/worlds/hello", id.Url().Str());

id.SetServer(srv3);
EXPECT_EQ("https://localhost:8003/alice/worlds/hello", id.Url().Str());
}

/////////////////////////////////////////////////
/// \brief Copy constructor deep copies
TEST(WorldIdentifier, CopyConstructorDeepCopy)
Expand Down

0 comments on commit 9d8c1c2

Please sign in to comment.