@@ -268,7 +268,7 @@ namespace {
268268
269269}
270270
271- int TCommandDescribe::PrintTopicResponsePretty (const NYdb::NTopic::TTopicDescription& description) {
271+ int TCommandDescribe::PrintTopicResponsePretty (const NYdb::NTopic::TTopicDescription& description) const {
272272 Cout << Endl << " RetentionPeriod: " << description.GetRetentionPeriod ().Hours () << " hours" ;
273273 if (description.GetRetentionStorageMb ().Defined ()) {
274274 Cout << Endl << " StorageRetention: " << *description.GetRetentionStorageMb () << " MB" ;
@@ -293,41 +293,43 @@ int TCommandDescribe::PrintTopicResponsePretty(const NYdb::NTopic::TTopicDescrip
293293 return EXIT_SUCCESS;
294294}
295295
296- int TCommandDescribe::PrintTopicResponseProtoJsonBase64 (
297- const NYdb::NTopic::
298- TDescribeTopicResult& result) {
296+ template <typename T>
297+ static int PrintProtoJsonBase64 (const T& msg) {
298+ using namespace google ::protobuf::util;
299+
299300 TString json;
300- google::protobuf::util::JsonPrintOptions jsonOpts;
301- jsonOpts.preserve_proto_field_names = true ;
302- auto convertStatus = google::protobuf::util::MessageToJsonString (
303- TProtoAccessor::GetProto (result.GetTopicDescription ()),
304- &json,
305- jsonOpts
306- );
307- if (convertStatus.ok ()) {
308- Cout << json << Endl;
309- } else {
310- Cerr << " Error occurred while converting result proto to json" << Endl;
301+ JsonPrintOptions opts;
302+ opts.preserve_proto_field_names = true ;
303+ const auto status = MessageToJsonString (msg, &json, opts);
304+
305+ if (!status.ok ()) {
306+ Cerr << " Error occurred while converting proto to json: " << status.message ().ToString () << Endl;
311307 return EXIT_FAILURE;
312308 }
309+
310+ Cout << json << Endl;
313311 return EXIT_SUCCESS;
314312}
315313
316- int TCommandDescribe::PrintTopicResponse (const NYdb::NTopic::TDescribeTopicResult& result) {
317- switch (OutputFormat) {
314+ template <typename T>
315+ using TPrettyPrinter = int (TCommandDescribe::*)(const T&) const ;
316+
317+ template <typename T>
318+ static int PrintDescription (TCommandDescribe* self, EOutputFormat format, const T& value, TPrettyPrinter<T> prettyFunc) {
319+ switch (format) {
318320 case EOutputFormat::Default:
319321 case EOutputFormat::Pretty:
320- PrintTopicResponsePretty (result.GetTopicDescription ());
321- break ;
322+ return std::invoke (prettyFunc, self, value);
322323 case EOutputFormat::Json:
323324 Cerr << " Warning! Option --json is deprecated and will be removed soon. "
324325 << " Use \" --format proto-json-base64\" option instead." << Endl;
325326 [[fallthrough]];
326327 case EOutputFormat::ProtoJsonBase64:
327- return PrintTopicResponseProtoJsonBase64 (result );
328+ return PrintProtoJsonBase64 ( TProtoAccessor::GetProto (value) );
328329 default :
329- throw TMisuseException () << " This command doesn't support " << OutputFormat << " output format" ;
330+ throw TMisuseException () << " This command doesn't support " << format << " output format" ;
330331 }
332+
331333 return EXIT_SUCCESS;
332334}
333335
@@ -336,9 +338,11 @@ int TCommandDescribe::DescribeTopic(TDriver& driver) {
336338 NYdb::NTopic::TDescribeTopicSettings settings;
337339 settings.IncludeStats (ShowStats || ShowPartitionStats);
338340
339- auto describeResult = topicClient.DescribeTopic (Path, settings).GetValueSync ();
340- ThrowOnError (describeResult);
341- return PrintTopicResponse (describeResult);
341+ auto result = topicClient.DescribeTopic (Path, settings).GetValueSync ();
342+ ThrowOnError (result);
343+
344+ const auto & desc = result.GetTopicDescription ();
345+ return PrintDescription (this , OutputFormat, desc, &TCommandDescribe::PrintTopicResponsePretty);
342346}
343347
344348int TCommandDescribe::DescribeTable (TDriver& driver) {
@@ -355,7 +359,9 @@ int TCommandDescribe::DescribeTable(TDriver& driver) {
355359 )
356360 ).GetValueSync ();
357361 ThrowOnError (result);
358- return PrintTableResponse (result);
362+
363+ auto desc = result.GetTableDescription ();
364+ return PrintDescription (this , OutputFormat, desc, &TCommandDescribe::PrintTableResponsePretty);
359365}
360366
361367int TCommandDescribe::DescribeColumnTable (TDriver& driver) {
@@ -370,24 +376,9 @@ int TCommandDescribe::DescribeColumnTable(TDriver& driver) {
370376 )
371377 ).GetValueSync ();
372378 ThrowOnError (result);
373- return PrintTableResponse (result);
374- }
375379
376- int TCommandDescribe::PrintCoordinationNodeResponse (const NYdb::NCoordination::TDescribeNodeResult& result) const {
377- switch (OutputFormat) {
378- case EOutputFormat::Default:
379- case EOutputFormat::Pretty:
380- return PrintCoordinationNodeResponsePretty (result.GetResult ());
381- case EOutputFormat::Json:
382- Cerr << " Warning! Option --json is deprecated and will be removed soon. "
383- << " Use \" --format proto-json-base64\" option instead." << Endl;
384- [[fallthrough]];
385- case EOutputFormat::ProtoJsonBase64:
386- return PrintCoordinationNodeResponseProtoJsonBase64 (result.GetResult ());
387- default :
388- throw TMisuseException () << " This command doesn't support " << OutputFormat << " output format" ;
389- }
390- return EXIT_SUCCESS;
380+ auto desc = result.GetTableDescription ();
381+ return PrintDescription (this , OutputFormat, desc, &TCommandDescribe::PrintTableResponsePretty);
391382}
392383
393384int TCommandDescribe::PrintCoordinationNodeResponsePretty (const NYdb::NCoordination::TNodeDescription& result) const {
@@ -403,28 +394,12 @@ int TCommandDescribe::PrintCoordinationNodeResponsePretty(const NYdb::NCoordinat
403394 return EXIT_SUCCESS;
404395}
405396
406- int TCommandDescribe::PrintCoordinationNodeResponseProtoJsonBase64 (const NYdb::NCoordination::TNodeDescription& result) const {
407- TString json;
408- google::protobuf::util::JsonPrintOptions jsonOpts;
409- jsonOpts.preserve_proto_field_names = true ;
410- auto convertStatus = google::protobuf::util::MessageToJsonString (
411- NYdb::TProtoAccessor::GetProto (result),
412- &json,
413- jsonOpts
414- );
415- if (convertStatus.ok ()) {
416- Cout << json << Endl;
417- return EXIT_SUCCESS;
418- }
419- Cerr << " Error occurred while converting result proto to json: " << TString (convertStatus.message ().ToString ()) << Endl;
420- return EXIT_FAILURE;
421- }
422-
423397int TCommandDescribe::DescribeCoordinationNode (const TDriver& driver) {
424398 NCoordination::TClient client (driver);
425- NCoordination::TDescribeNodeResult description = client.DescribeNode (Path).GetValueSync ();
399+ auto result = client.DescribeNode (Path).GetValueSync ();
426400
427- return PrintCoordinationNodeResponse (description);
401+ const auto & desc = result.GetResult ();
402+ return PrintDescription (this , OutputFormat, desc, &TCommandDescribe::PrintCoordinationNodeResponsePretty);
428403}
429404
430405int TCommandDescribe::PrintReplicationResponsePretty (const NYdb::NReplication::TDescribeReplicationResult& result) const {
@@ -464,47 +439,11 @@ int TCommandDescribe::PrintReplicationResponsePretty(const NYdb::NReplication::T
464439 return EXIT_SUCCESS;
465440}
466441
467- int TCommandDescribe::PrintReplicationResponseProtoJsonBase64 (const NYdb::NReplication::TDescribeReplicationResult& result) const {
468- TString json;
469- google::protobuf::util::JsonPrintOptions jsonOpts;
470- jsonOpts.preserve_proto_field_names = true ;
471- auto convertStatus = google::protobuf::util::MessageToJsonString (
472- NYdb::TProtoAccessor::GetProto (result),
473- &json,
474- jsonOpts
475- );
476- if (convertStatus.ok ()) {
477- Cout << json << Endl;
478- } else {
479- Cerr << " Error occurred while converting result proto to json" << Endl;
480- return EXIT_FAILURE;
481- }
482- return EXIT_SUCCESS;
483- }
484-
485- int TCommandDescribe::PrintReplicationResponse (const NYdb::NReplication::TDescribeReplicationResult& result) const {
486- switch (OutputFormat) {
487- case EOutputFormat::Default:
488- case EOutputFormat::Pretty:
489- PrintReplicationResponsePretty (result);
490- break ;
491- case EOutputFormat::Json:
492- Cerr << " Warning! Option --json is deprecated and will be removed soon. "
493- << " Use \" --format proto-json-base64\" option instead." << Endl;
494- [[fallthrough]];
495- case EOutputFormat::ProtoJsonBase64:
496- return PrintReplicationResponseProtoJsonBase64 (result);
497- default :
498- throw TMisuseException () << " This command doesn't support " << OutputFormat << " output format" ;
499- }
500- return EXIT_SUCCESS;
501- }
502-
503442int TCommandDescribe::DescribeReplication (const TDriver& driver) {
504443 NReplication::TReplicationClient client (driver);
505444 auto result = client.DescribeReplication (Path).ExtractValueSync ();
506445 ThrowOnError (result);
507- return PrintReplicationResponse ( result);
446+ return PrintDescription ( this , OutputFormat, result, &TCommandDescribe::PrintReplicationResponsePretty );
508447}
509448
510449namespace {
@@ -835,26 +774,7 @@ namespace {
835774 }
836775}
837776
838- int TCommandDescribe::PrintTableResponse (NTable::TDescribeTableResult& result) {
839- NTable::TTableDescription tableDescription = result.GetTableDescription ();
840- switch (OutputFormat) {
841- case EOutputFormat::Default:
842- case EOutputFormat::Pretty:
843- PrintResponsePretty (tableDescription);
844- break ;
845- case EOutputFormat::Json:
846- Cerr << " Warning! Option --json is deprecated and will be removed soon. "
847- << " Use \" --format proto-json-base64\" option instead." << Endl;
848- [[fallthrough]];
849- case EOutputFormat::ProtoJsonBase64:
850- return PrintResponseProtoJsonBase64 (tableDescription);
851- default :
852- throw TMisuseException () << " This command doesn't support " << OutputFormat << " output format" ;
853- }
854- return EXIT_SUCCESS;
855- }
856-
857- void TCommandDescribe::PrintResponsePretty (const NTable::TTableDescription& tableDescription) {
777+ int TCommandDescribe::PrintTableResponsePretty (const NTable::TTableDescription& tableDescription) const {
858778 PrintColumns (tableDescription);
859779 PrintIndexes (tableDescription);
860780 PrintChangefeeds (tableDescription);
@@ -875,23 +795,7 @@ void TCommandDescribe::PrintResponsePretty(const NTable::TTableDescription& tabl
875795 if (ShowKeyShardBoundaries || ShowPartitionStats) {
876796 PrintPartitionInfo (tableDescription, ShowKeyShardBoundaries, ShowPartitionStats);
877797 }
878- }
879798
880- int TCommandDescribe::PrintResponseProtoJsonBase64 (const NTable::TTableDescription& tableDescription) {
881- TString json;
882- google::protobuf::util::JsonPrintOptions jsonOpts;
883- jsonOpts.preserve_proto_field_names = true ;
884- auto convertStatus = google::protobuf::util::MessageToJsonString (
885- NYdb::TProtoAccessor::GetProto (tableDescription),
886- &json,
887- jsonOpts
888- );
889- if (convertStatus.ok ()) {
890- Cout << json << Endl;
891- } else {
892- Cerr << " Error occurred while converting result proto to json" << Endl;
893- return EXIT_FAILURE;
894- }
895799 return EXIT_SUCCESS;
896800}
897801
0 commit comments