The Gaudi Framework  v36r1 (3e2fb5a8)
fmt::formatter< nlohmann::json > Class Reference

fmt formatter function for json class able to handle 2 types of formats : {} : in this case the type entry of json is used to deduce what to print, looking into the registry {:name|fmt} : in this case, the entry 'name' of the json will be printed in given format. More...

Collaboration diagram for fmt::formatter< nlohmann::json >:

Public Member Functions

template<typename ParseContext >
constexpr auto parse (ParseContext &ctx)
 
template<typename FormatContext >
auto format (const nlohmann::json &j, FormatContext &ctx)
 

Private Attributes

std::string currentFormat
 
std::string currentName
 

Detailed Description

fmt formatter function for json class able to handle 2 types of formats : {} : in this case the type entry of json is used to deduce what to print, looking into the registry {:name|fmt} : in this case, the entry 'name' of the json will be printed in given format.

See comment on registry for more details

Definition at line 60 of file MessageSvcSink.cpp.

Member Function Documentation

◆ format()

template<typename FormatContext >
auto fmt::formatter< nlohmann::json >::format ( const nlohmann::json &  j,
FormatContext &  ctx 
)
inline

Definition at line 78 of file MessageSvcSink.cpp.

78  {
79  if ( currentFormat.size() == 0 ) {
80  // dealing with {} format, let's find entry for our type in registry
81  const auto type = j.at( "type" ).get<std::string>();
82  // first looking for the entry, then we drop on ":abc" suffix at a time and try again
83  std::string_view type_key{type};
84  // look for the full entry
85  auto entry = registry.find( type_key );
86  // we check if we have type separators before entering the loop
87  auto sep = type_key.rfind( ':' );
88  while ( sep != type_key.npos && entry == registry.end() ) {
89  // not found, remove the trailing ":abc" section
90  type_key.remove_suffix( type_key.size() - sep );
91  // check if we have another chunk to strip
92  sep = type_key.rfind( ':' );
93  // see if the shorter key works
94  entry = registry.find( type_key );
95  }
96  // if still not found, we use the basic "counter"
97  if ( entry == registry.end() ) entry = registry.find( "counter" );
98  assert( entry != registry.end() );
99  // print the json string according to format found
100  // This actually will call this formatter again a number of times
101  return fmt::format_to( ctx.out(), entry->second, j );
102  } else {
103  // dealing with a {:name|fmt} format
104  auto actualFormat = fmt::format( "{{:{}", currentFormat ) + "}";
105  switch ( currentFormat.back() ) {
106  case 'd':
107  return fmt::format_to( ctx.out(), actualFormat, j.at( currentName ).template get<unsigned int>() );
108  case 'g':
109  return fmt::format_to( ctx.out(), actualFormat, j.at( currentName ).template get<double>() );
110  case 'p':
111  actualFormat[actualFormat.size() - 2] = 'g';
112  return fmt::format_to( ctx.out(), actualFormat, j.at( currentName ).template get<double>() * 100 );
113  default:
114  return fmt::format_to( ctx.out(), "Unknown counter format : {}", currentFormat );
115  }
116  }
117  }

◆ parse()

template<typename ParseContext >
constexpr auto fmt::formatter< nlohmann::json >::parse ( ParseContext &  ctx)
inlineconstexpr

Definition at line 63 of file MessageSvcSink.cpp.

63  {
64  auto fmt_begin = ctx.begin();
65  auto fmt_end = std::find( fmt_begin, ctx.end(), '}' );
66  if ( fmt_begin == fmt_end ) {
67  // we are dealing with {}, only make sure currentFormat is empty
68  currentFormat = "";
69  } else {
70  // non empty format, let's split name from format
71  auto fmt_colon = std::find( fmt_begin, fmt_end, '|' );
72  currentName = std::string( fmt_begin, fmt_colon - fmt_begin );
73  currentFormat = std::string( fmt_colon + 1, fmt_end - fmt_colon - 1 );
74  }
75  return fmt_end;
76  }

Member Data Documentation

◆ currentFormat

std::string fmt::formatter< nlohmann::json >::currentFormat
private

Definition at line 120 of file MessageSvcSink.cpp.

◆ currentName

std::string fmt::formatter< nlohmann::json >::currentName
private

Definition at line 121 of file MessageSvcSink.cpp.


The documentation for this class was generated from the following file:
std::string
STL class.
std::find
T find(T... args)
std::string::size
T size(T... args)
std::string::back
T back(T... args)
fmt::formatter< nlohmann::json >::currentFormat
std::string currentFormat
Definition: MessageSvcSink.cpp:120
GaudiPluginService.cpluginsvc.registry
def registry()
Definition: cpluginsvc.py:82
GaudiPython.Pythonizations.ctx
ctx
Definition: Pythonizations.py:566
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
fmt::formatter< nlohmann::json >::currentName
std::string currentName
Definition: MessageSvcSink.cpp:121
gaudirun.type
type
Definition: gaudirun.py:154