adding helper to get a list of variants from a string

This commit is contained in:
Daniel Larimer 2015-03-31 18:46:05 -04:00
parent 633ab3f32d
commit 8b5e2e7613
3 changed files with 50 additions and 24 deletions

View file

@ -30,6 +30,7 @@ namespace fc
static variant from_stream( buffered_istream& in, parse_type ptype = legacy_parser );
static variant from_string( const string& utf8_str, parse_type ptype = legacy_parser );
static variants variants_from_string( const string& utf8_str, parse_type ptype = legacy_parser );
static string to_string( const variant& v );
static string to_pretty_string( const variant& v );

View file

@ -13,6 +13,13 @@ namespace fc { namespace rpc {
class cli : public api_connection
{
public:
~cli()
{
if( _run_complete.valid() )
{
stop();
}
}
virtual variant send_call( api_id_type api_id, string method_name, variants args = variants() )
{
FC_ASSERT(false);
@ -35,6 +42,7 @@ namespace fc { namespace rpc {
_run_complete.cancel();
_run_complete.wait();
}
void wait(){ _run_complete.wait(); }
void format_result( const string& method, std::function<string(variant,const variants&)> formatter)
{
_result_formatters[method] = formatter;
@ -42,32 +50,34 @@ namespace fc { namespace rpc {
private:
void run()
{
while( !_run_complete.canceled() )
{
std::cout << ">>> ";
std::string line;
fc::getline( fc::cin, line );
auto line_stream = std::make_shared<fc::stringstream>( line );
buffered_istream bstream(line_stream);
fc::variant method;
fc::variants args;
method = fc::json::from_stream( bstream );
while( true )
while( !fc::cin.eof() && !_run_complete.canceled() )
{
try {
args.push_back( fc::json::from_stream( bstream ) );
} catch ( const fc::eof_exception& eof ){}
}
auto result = receive_call( 0, method.get_string(), args );
auto itr = _result_formatters.find( method.get_string() );
if( itr == _result_formatters.end() )
{
std::cout << fc::json::to_pretty_string( result ) << "\n";
}
else
std::cout << itr->second( result, args ) << "\n";
}
std::cout << ">>> ";
std::cout.flush();
std::string line;
fc::getline( fc::cin, line );
std::cout << line << "\n";
line += char(EOF);
fc::variants args = fc::json::variants_from_string(line);;
if( args.size() == 0 ) continue;
const string& method = args[0].get_string();
auto result = receive_call( 0, method, variants( args.begin()+1,args.end() ) );
auto itr = _result_formatters.find( method );
if( itr == _result_formatters.end() )
{
std::cout << fc::json::to_pretty_string( result ) << "\n";
}
else
std::cout << itr->second( result, args ) << "\n";
}
catch ( const fc::exception& e )
{
edump((e.to_detail_string()));
}
}
}
std::map<string,std::function<string(variant,const variants&)> > _result_formatters;
fc::future<void> _run_complete;

View file

@ -427,6 +427,7 @@ namespace fc
return token_from_stream( in );
case 0x04: // ^D end of transmission
case EOF:
case 0:
FC_THROW_EXCEPTION( eof_exception, "unexpected end of file" );
default:
FC_THROW_EXCEPTION( parse_error_exception, "Unexpected char '${c}' in \"${s}\"",
@ -453,6 +454,20 @@ namespace fc
}
} FC_RETHROW_EXCEPTIONS( warn, "", ("str",utf8_str) ) }
variants json::variants_from_string( const std::string& utf8_str, parse_type ptype )
{ try {
variants result;
fc::stringstream in( utf8_str );
//in.exceptions( std::ifstream::eofbit );
try {
while( true )
{
// result.push_back( variant_from_stream( in ));
result.push_back(json_relaxed::variant_from_stream<fc::stringstream, false>( in ));
}
} catch ( const fc::eof_exception& ){}
return result;
} FC_RETHROW_EXCEPTIONS( warn, "", ("str",utf8_str) ) }
/*
void toUTF8( const char str, ostream& os )
{