aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/protobuf/json/field_option.h
blob: c8a8bfbff5edf45eb75548128ef04ec0a7f3f845 (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
#pragma once

#include <google/protobuf/descriptor.h>
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/message.h>

namespace NProtobufJson {
    // Functor that defines whether given field has some option set to true
    //
    // Example:
    // message T {
    //     optional stroka some_field = 1 [(some_option) = true];
    // }
    //
    template <typename TFieldOptionExtensionId>
    class TFieldOptionFunctor {
    public:
        TFieldOptionFunctor(const TFieldOptionExtensionId& option, bool positive = true)
            : Option(option)
            , Positive(positive)
        {
        }

        bool operator()(const NProtoBuf::Message&, const NProtoBuf::FieldDescriptor* field) const {
            const NProtoBuf::FieldOptions& opt = field->options();
            const bool val = opt.GetExtension(Option);
            return Positive ? val : !val;
        }

    private:
        const TFieldOptionExtensionId& Option;
        bool Positive;
    };

    template <typename TFieldOptionExtensionId>
    TFieldOptionFunctor<TFieldOptionExtensionId> MakeFieldOptionFunctor(const TFieldOptionExtensionId& option, bool positive = true) {
        return TFieldOptionFunctor<TFieldOptionExtensionId>(option, positive);
    }

}