Skip to content

Commit

Permalink
changed to store parent IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
nestal committed Apr 29, 2013
1 parent 87d9697 commit ee6408d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
5 changes: 5 additions & 0 deletions libgrive/src/drive2/CommonUri.hh
Expand Up @@ -33,4 +33,9 @@ namespace mime_types
const std::string folder = "application/vnd.google-apps.folder" ;
}

namespace kinds
{
const std::string parent = "drive#parentReference" ;
}

} } // end of namespace gr::v2
14 changes: 13 additions & 1 deletion libgrive/src/drive2/Drive.cc
Expand Up @@ -25,6 +25,7 @@
#include "protocol/Json.hh"

#include <iostream>
#include <iterator>

namespace gr { namespace v2 {

Expand All @@ -46,14 +47,25 @@ void Drive::Refresh( http::Agent *agent )
for ( std::vector<Json>::iterator i = items.begin() ; i != items.end() ; ++i )
{
const Resource *r = Add( *i ) ;
std::cout << r->Title() << " " << r->Mime() << std::endl ;
}
}
}

const Resource* Drive::Add( const Json& item )
{
Resource *r = new Resource( item["id"].Str(), item["mimeType"].Str(), item["title"].Str() ) ;

// initialize parent IDs
Json parents ;
if ( item.Get( "parents", parents ) )
{
std::vector<std::string> parent_ids ;
parents.Select<std::string>( "id", std::back_inserter(parent_ids) ) ;

r->SetParent( parent_ids.begin(), parent_ids.end() ) ;
std::cout << r->Title() << " " << r->ID() << " " << parent_ids.size() << std::endl ;
}

return *m_db.insert(r).first ;
}

Expand Down
5 changes: 0 additions & 5 deletions libgrive/src/drive2/Resource.cc
Expand Up @@ -60,9 +60,4 @@ bool Resource::IsFolder() const
return m_mime == "application/vnd.google-apps.folder" ;
}

void Resource::Add( const std::string& child_id )
{
m_children.push_back( child_id ) ;
}

} } // end of namespace gr::v2
12 changes: 8 additions & 4 deletions libgrive/src/drive2/Resource.hh
Expand Up @@ -30,21 +30,25 @@ class Resource
public :
Resource() ;
Resource( const std::string& id, const std::string& mime, const std::string& title ) ;


template <typename InputIt>
void SetParent( InputIt first, InputIt last )
{
m_parent.assign( first, last ) ;
}

std::string ID() const ;
std::string Mime() const ;
std::string Title() const ;

bool IsFolder() const ;

void Add( const std::string& child_id ) ;

private :
std::string m_id ;
std::string m_mime ;
std::string m_title ;

std::vector<std::string> m_children ;
std::vector<std::string> m_parent ;
} ;

} } // end of namespace gr::v2
12 changes: 12 additions & 0 deletions libgrive/src/protocol/Json.hh
Expand Up @@ -100,6 +100,18 @@ public :
Json FindInArray( const std::string& key, const std::string& value ) const ;
bool FindInArray( const std::string& key, const std::string& value, Json& result ) const ;

/** Expect *this is a JSON array of objects. Select all "key" values inside each
objects in the array and copies them in the output iterator \a out.
*/
template <typename T, typename Out>
Out Select( const std::string& key, Out out )
{
Array a = AsArray() ;
for ( Array::iterator i = a.begin() ; i != a.end() ; ++i )
*out++ = i->As<T>() ;
return out ;
}

friend std::ostream& operator<<( std::ostream& os, const Json& json ) ;
void Write( DataStream *out ) const ;

Expand Down

0 comments on commit ee6408d

Please sign in to comment.