aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/protobuf/json/filter.h
blob: 9a3ddb54fe317da3027ed9e57e188c073166a939 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#pragma once

#include "config.h"
#include "proto2json_printer.h"
#include "json_output_create.h"

#include <util/generic/yexception.h>
#include <util/generic/utility.h>

#include <functional>

namespace NProtobufJson {
    template <typename TBasePrinter = TProto2JsonPrinter> // TBasePrinter is assumed to be a TProto2JsonPrinter descendant
    class TFilteringPrinter: public TBasePrinter {
    public:
        using TFieldPredicate = std::function<bool(const NProtoBuf::Message&, const NProtoBuf::FieldDescriptor*)>;

        template <typename... TArgs>
        TFilteringPrinter(TFieldPredicate isPrinted, TArgs&&... args)
            : TBasePrinter(std::forward<TArgs>(args)...)
            , IsPrinted(std::move(isPrinted))
        {
        }

        virtual void PrintField(const NProtoBuf::Message& proto,
                                const NProtoBuf::FieldDescriptor& field,
                                IJsonOutput& json,
                                TStringBuf key) override {
            if (key || IsPrinted(proto, &field))
                TBasePrinter::PrintField(proto, field, json, key);
        }

    private:
        TFieldPredicate IsPrinted;
    };

    inline void PrintWithFilter(const NProtoBuf::Message& msg, TFilteringPrinter<>::TFieldPredicate filter, IJsonOutput& output, const TProto2JsonConfig& config = TProto2JsonConfig()) {
        TFilteringPrinter<> printer(std::move(filter), config);
        printer.Print(msg, output);
    }

    inline TString PrintWithFilter(const NProtoBuf::Message& msg, TFilteringPrinter<>::TFieldPredicate filter, const TProto2JsonConfig& config = TProto2JsonConfig()) {
        TString ret;
        PrintWithFilter(msg, std::move(filter), *CreateJsonMapOutput(ret, config), config);
        return ret;
    }

}