Skip to content

Commit

Permalink
enh: add Schema::child_name(idx)
Browse files Browse the repository at this point in the history
if the Schema represents an object, this method
allows you to get a child's name by index.

if the Schema is not an object, or the index is bad
it returns an empty string.

This commit resolves #13.
  • Loading branch information
cyrush committed Aug 10, 2016
1 parent 28d7a01 commit 54b0ec3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/libs/conduit/Schema.cpp
Expand Up @@ -782,6 +782,24 @@ Schema::child_index(const std::string &path) const
return res;
}

//---------------------------------------------------------------------------//
std::string
Schema::child_name(index_t idx) const
{
std::string res = "";

if(m_dtype.id() == DataType::OBJECT_ID)
{
const std::vector<std::string> &obj_order = object_order();
if( idx < obj_order.size())
{
res = obj_order[idx];
}
}

return res;
}

//---------------------------------------------------------------------------//
Schema &
Schema::fetch(const std::string &path)
Expand Down
5 changes: 5 additions & 0 deletions src/libs/conduit/Schema.hpp
Expand Up @@ -256,6 +256,11 @@ class CONDUIT_API Schema
/// path to index map
index_t child_index(const std::string &path) const;

/// index to path map
/// returns an empty string when passed index is invalid, or
/// this schema does not describe an object.
std::string child_name(index_t idx) const;

/// this uses the fetch method
Schema &operator[](const std::string &path);
/// the const variant uses the "fetch_child" method
Expand Down
23 changes: 23 additions & 0 deletions src/tests/conduit/t_conduit_schema.cpp
Expand Up @@ -182,6 +182,29 @@ TEST(schema_basics, schema_alloc)

}

//-----------------------------------------------------------------------------
TEST(schema_basics, schema_name_by_index)
{
Schema s1;
s1["a"].set(DataType::int64());
s1["b"].set(DataType::float64());
s1["c"].set(DataType::float64());

// standard case
EXPECT_EQ(s1.child_name(0),"a");
EXPECT_EQ(s1.child_name(1),"b");
EXPECT_EQ(s1.child_name(2),"c");

// these are out of bounds, should be empty
EXPECT_EQ(s1.child_name(100),"");
EXPECT_EQ(s1["a"].child_name(100),"");

Schema s2;
// check empty schema
EXPECT_EQ(s2.child_name(100),"");
}


//-----------------------------------------------------------------------------
///
/// commented out b/c spanned_bytes is now private,
Expand Down

0 comments on commit 54b0ec3

Please sign in to comment.