113#include <library/cpp/yson/node/serialize.h>
114#include <library/cpp/yson/node/node_builder.h>
116#include <library/cpp/yson/consumer.h>
117#include <library/cpp/yson/writer.h>
119#include <util/generic/noncopyable.h>
120#include <util/generic/ptr.h>
121#include <util/stream/str.h>
130 using TUnwrapped = T;
132 static TUnwrapped Unwrap(T t)
146 using TUnwrapped = void;
156 :
private TNonCopyable
160 static void WriteValue(NYT::NYson::IYsonConsumer* consumer,
const T& value)
162 Serialize(value, consumer);
167 template <
class TParent>
class TAny;
170 template <
class TParent>
class TListType;
171 template <
class TParent>
class TMapType;
174 template <
class TParent>
179 operator NYT::NYson::IYsonConsumer* ()
const
186 NYT::NYson::IYsonConsumer* Consumer;
189 TFluentBase(NYT::NYson::IYsonConsumer* consumer, TParent parent)
191 , Parent(std::move(parent))
194 using TUnwrappedParent =
typename TFluentYsonUnwrapper<TParent>::TUnwrapped;
196 TUnwrappedParent GetUnwrappedParent()
198 return TFluentYsonUnwrapper<TParent>::Unwrap(std::move(Parent));
204 template <
template <
class TParent>
class TThis,
class TParent>
205 class TFluentFragmentBase
209 using TDeepThis = TThis<TParent>;
210 using TShallowThis = TThis<TFluentYsonVoid>;
211 using TUnwrappedParent =
typename TFluentYsonUnwrapper<TParent>::TUnwrapped;
213 explicit TFluentFragmentBase(NYT::NYson::IYsonConsumer* consumer, TParent parent = TParent())
218 template <
class TFunc>
219 TDeepThis&
Do(
const TFunc& func)
221 func(TShallowThis(this->Consumer));
222 return *
static_cast<TDeepThis*
>(
this);
226 template <
class TFunc>
227 TDeepThis&
DoIf(
bool condition,
const TFunc& func)
230 func(TShallowThis(this->Consumer));
232 return *
static_cast<TDeepThis*
>(
this);
236 template <
class TFunc,
class TIterator>
237 TDeepThis&
DoFor(
const TIterator&
begin,
const TIterator&
end,
const TFunc& func)
239 for (
auto current =
begin; current !=
end; ++current) {
240 func(TShallowThis(this->Consumer), current);
242 return *
static_cast<TDeepThis*
>(
this);
246 template <
class TFunc,
class TCollection>
247 TDeepThis&
DoFor(
const TCollection& collection,
const TFunc& func)
249 for (
const auto& item : collection) {
250 func(TShallowThis(this->Consumer), item);
252 return *
static_cast<TDeepThis*
>(
this);
258 template <
class TParent>
259 class TAnyWithoutAttributes
263 using TUnwrappedParent =
typename TFluentYsonUnwrapper<TParent>::TUnwrapped;
265 TAnyWithoutAttributes(NYT::NYson::IYsonConsumer* consumer, TParent parent)
271 TUnwrappedParent
Value(
const T& value)
273 WriteValue(this->Consumer, value);
274 return this->GetUnwrappedParent();
280 this->Consumer->OnEntity();
281 return this->GetUnwrappedParent();
285 template <
class TCollection>
286 TUnwrappedParent
List(
const TCollection& collection)
288 this->Consumer->OnBeginList();
289 for (
const auto& item : collection) {
290 this->Consumer->OnListItem();
291 WriteValue(this->Consumer, item);
293 this->Consumer->OnEndList();
294 return this->GetUnwrappedParent();
298 template <
class TCollection>
299 TUnwrappedParent
ListLimited(
const TCollection& collection,
size_t maxSize)
301 this->Consumer->OnBeginAttributes();
302 this->Consumer->OnKeyedItem(
"count");
303 this->Consumer->OnInt64Scalar(collection.size());
304 this->Consumer->OnEndAttributes();
305 this->Consumer->OnBeginList();
306 size_t printedSize = 0;
307 for (
const auto& item : collection) {
308 if (printedSize >= maxSize)
310 this->Consumer->OnListItem();
311 WriteValue(this->Consumer, item);
314 this->Consumer->OnEndList();
315 return this->GetUnwrappedParent();
321 this->Consumer->OnBeginList();
326 template <
class TFunc>
327 TUnwrappedParent
DoList(
const TFunc& func)
329 this->Consumer->OnBeginList();
331 this->Consumer->OnEndList();
332 return this->GetUnwrappedParent();
336 template <
class TFunc,
class TIterator>
339 this->Consumer->OnBeginList();
340 for (
auto current =
begin; current !=
end; ++current) {
343 this->Consumer->OnEndList();
344 return this->GetUnwrappedParent();
348 template <
class TFunc,
class TCollection>
349 TUnwrappedParent
DoListFor(
const TCollection& collection,
const TFunc& func)
351 this->Consumer->OnBeginList();
352 for (
const auto& item : collection) {
355 this->Consumer->OnEndList();
356 return this->GetUnwrappedParent();
362 this->Consumer->OnBeginMap();
367 template <
class TFunc>
368 TUnwrappedParent
DoMap(
const TFunc& func)
370 this->Consumer->OnBeginMap();
372 this->Consumer->OnEndMap();
373 return this->GetUnwrappedParent();
377 template <
class TFunc,
class TIterator>
380 this->Consumer->OnBeginMap();
381 for (
auto current =
begin; current !=
end; ++current) {
384 this->Consumer->OnEndMap();
385 return this->GetUnwrappedParent();
389 template <
class TFunc,
class TCollection>
390 TUnwrappedParent
DoMapFor(
const TCollection& collection,
const TFunc& func)
392 this->Consumer->OnBeginMap();
393 for (
const auto& item : collection) {
396 this->Consumer->OnEndMap();
397 return this->GetUnwrappedParent();
402 template <
class TParent>
404 :
public TAnyWithoutAttributes<TParent>
407 using TBase = TAnyWithoutAttributes<TParent>;
409 explicit TAny(NYT::NYson::IYsonConsumer* consumer, TParent parent)
410 : TBase(consumer, std::move(parent))
416 this->Consumer->OnBeginAttributes();
419 TBase(this->Consumer, this->Parent));
424 template <
class TParent = TFluentYsonVo
id>
426 :
public TFluentFragmentBase<TAttributes, TParent>
429 using TThis = TAttributes<TParent>;
430 using TUnwrappedParent =
typename TFluentYsonUnwrapper<TParent>::TUnwrapped;
432 explicit TAttributes(NYT::NYson::IYsonConsumer* consumer, TParent parent = TParent())
433 : TFluentFragmentBase<TFluentYsonBuilder::TAttributes, TParent>(consumer, std::move(parent))
439 this->Consumer->OnKeyedItem(key);
444 template <
size_t Size>
447 return Item(TStringBuf(key, Size - 1));
455 this->Consumer->OnEndAttributes();
456 return this->GetUnwrappedParent();
461 template <
class TParent = TFluentYsonVo
id>
463 :
public TFluentFragmentBase<TListType, TParent>
466 using TThis = TListType<TParent>;
467 using TUnwrappedParent =
typename TFluentYsonUnwrapper<TParent>::TUnwrapped;
469 explicit TListType(NYT::NYson::IYsonConsumer* consumer, TParent parent = TParent())
470 : TFluentFragmentBase<TFluentYsonBuilder::TListType, TParent>(consumer, std::move(parent))
476 this->Consumer->OnListItem();
485 this->Consumer->OnEndList();
486 return this->GetUnwrappedParent();
491 template <
class TParent = TFluentYsonVo
id>
493 :
public TFluentFragmentBase<TMapType, TParent>
496 using TThis = TMapType<TParent>;
497 using TUnwrappedParent =
typename TFluentYsonUnwrapper<TParent>::TUnwrapped;
499 explicit TMapType(NYT::NYson::IYsonConsumer* consumer, TParent parent = TParent())
500 : TFluentFragmentBase<TFluentYsonBuilder::TMapType, TParent>(consumer, std::move(parent))
504 template <
size_t Size>
507 return Item(TStringBuf(key, Size - 1));
513 this->Consumer->OnKeyedItem(key);
522 this->Consumer->OnEndMap();
523 return this->GetUnwrappedParent();
546static inline TFluentAny BuildYsonFluently(NYT::NYson::IYsonConsumer* consumer)
552static inline TFluentList BuildYsonListFluently(NYT::NYson::IYsonConsumer* consumer)
558static inline TFluentMap BuildYsonMapFluently(NYT::NYson::IYsonConsumer* consumer)
565class TFluentYsonWriterState
569 using TValue = TString;
571 explicit TFluentYsonWriterState(::NYson::EYsonFormat format)
572 : Writer(&Output, format)
580 NYT::NYson::IYsonConsumer* GetConsumer()
586 TStringStream Output;
587 ::NYson::TYsonWriter Writer;
592class TFluentYsonBuilderState
596 using TValue = TNode;
598 explicit TFluentYsonBuilderState()
604 return std::move(Node);
607 NYT::NYson::IYsonConsumer* GetConsumer()
614 TNodeBuilder Builder;
619template <
class TState>
620class TFluentYsonHolder
623 explicit TFluentYsonHolder(::TIntrusivePtr<TState> state)
627 ::TIntrusivePtr<TState> GetState()
const
633 ::TIntrusivePtr<TState> State;
638template <
class TState>
641 using TUnwrapped =
typename TState::TValue;
645 return std::move(holder.GetState()->GetValue());
651template <
class TState>
653BuildYsonFluentlyWithState(::TIntrusivePtr<TState> state)
656 state->GetConsumer(),
661inline TFluentYsonBuilder::TAny<TFluentYsonHolder<TFluentYsonWriterState>>
665 return BuildYsonFluentlyWithState(state);
669inline TFluentYsonBuilder::TAny<TFluentYsonHolder<TFluentYsonBuilderState>>
673 return BuildYsonFluentlyWithState(state);
TUnwrappedParent DoList(const TFunc &func)
Open a list, delegate invocation to func, then close the list.
Definition fluent.h:327
TListType< TParent > BeginList()
Open a list.
Definition fluent.h:319
TUnwrappedParent DoMapFor(const TIterator &begin, const TIterator &end, const TFunc &func)
Open a map, call func(*this, element) for each element of range, then close the map.
Definition fluent.h:378
TUnwrappedParent DoListFor(const TCollection &collection, const TFunc &func)
Open a list, call func(*this, element) for each element of collection, then close the list.
Definition fluent.h:349
TMapType< TParent > BeginMap()
Open a map.
Definition fluent.h:360
TUnwrappedParent Value(const T &value)
Pass value to underlying consumer.
Definition fluent.h:271
TUnwrappedParent DoMapFor(const TCollection &collection, const TFunc &func)
Open a map, call func(*this, element) for each element of collection, then close the map.
Definition fluent.h:390
TUnwrappedParent Entity()
Call OnEntity() of underlying consumer.
Definition fluent.h:278
TUnwrappedParent ListLimited(const TCollection &collection, size_t maxSize)
Serialize maximum maxSize elements of collection to underlying consumer as a list.
Definition fluent.h:299
TUnwrappedParent List(const TCollection &collection)
Serialize collection to underlying consumer as a list.
Definition fluent.h:286
TUnwrappedParent DoListFor(const TIterator &begin, const TIterator &end, const TFunc &func)
Open a list, call func(*this, element) for each element of range, then close the list.
Definition fluent.h:337
TUnwrappedParent DoMap(const TFunc &func)
Open a map, delegate invocation to func, then close the map.
Definition fluent.h:368
Fluent adapter of any value.
Definition fluent.h:405
TAttributes< TBase > BeginAttributes()
Open attributes.
Definition fluent.h:414
Fluent adapter of attributes fragment (the inside part of attributes).
Definition fluent.h:427
TUnwrappedParent EndAttributes()
Close the attributes.
Definition fluent.h:453
TAny< TThis > Item(const TStringBuf &key)
Pass attribute key to underlying consumer.
Definition fluent.h:437
TAny< TThis > Item(const char(&key)[Size])
Pass attribute key to underlying consumer.
Definition fluent.h:445
Base class for all fluent adapters.
Definition fluent.h:176
TDeepThis & DoIf(bool condition, const TFunc &func)
Conditionally delegate invocation to a separate procedure.
Definition fluent.h:227
TDeepThis & DoFor(const TIterator &begin, const TIterator &end, const TFunc &func)
Calls func(*this, element) for each element in range [begin, end).
Definition fluent.h:237
TDeepThis & Do(const TFunc &func)
Delegate invocation to a separate procedure.
Definition fluent.h:219
TDeepThis & DoFor(const TCollection &collection, const TFunc &func)
Calls func(*this, element) for each element in collection.
Definition fluent.h:247
Fluent adapter of list fragment (the inside part of a list).
Definition fluent.h:464
TAny< TThis > Item()
Call OnListItem() of underlying consumer.
Definition fluent.h:474
TUnwrappedParent EndList()
Close the list.
Definition fluent.h:483
Fluent adapter of map fragment (the inside part of a map).
Definition fluent.h:494
TAny< TThis > Item(const char(&key)[Size])
Pass map key to underlying consumer.
Definition fluent.h:505
TUnwrappedParent EndMap()
Close the map.
Definition fluent.h:520
TAny< TThis > Item(const TStringBuf &key)
Pass map key to underlying consumer.
Definition fluent.h:511
This class is actually a namespace for specific fluent adapter classes.
Definition fluent.h:157
Main namespace of YT client.
Definition abortable_stream.h:7
TFluentYsonBuilder::TAny< TFluentYsonHolder< TFluentYsonBuilderState > > BuildYsonNodeFluently()
Create a fluent adapter returning a NYT::TNode when construction is finished.
Definition fluent.h:670
TFluentYsonBuilder::TListType< TFluentYsonVoid > TFluentList
Builder representing the inside of a list (list fragment).
Definition fluent.h:535
TFluentYsonBuilder::TAny< TFluentYsonVoid > TFluentAny
Builder representing any value.
Definition fluent.h:532
TFluentYsonBuilder::TAttributes< TFluentYsonVoid > TFluentAttributes
Builder representing the inside of attributes.
Definition fluent.h:541
TTableReaderIterator< T > end(TTableReader< T > &)
Function to facilitate range-based-for for NYT::TTableReader.
Definition io.h:359
TFluentYsonBuilder::TMapType< TFluentYsonVoid > TFluentMap
Builder representing the inside of a map (map fragment).
Definition fluent.h:538
TFluentYsonBuilder::TAny< TFluentYsonHolder< TFluentYsonWriterState > > BuildYsonStringFluently(::NYson::EYsonFormat format=::NYson::EYsonFormat::Text)
Create a fluent adapter returning a TString with corresponding YSON when construction is finished.
Definition fluent.h:662
TTableReaderIterator< T > begin(TTableReader< T > &reader)
Function to facilitate range-based-for for NYT::TTableReader.
Definition io.h:350