Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-28 10:10:19

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2008 Google Inc.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 #ifndef GOOGLE_PROTOBUF_JSON_INTERNAL_MESSAGE_PATH_H__
0009 #define GOOGLE_PROTOBUF_JSON_INTERNAL_MESSAGE_PATH_H__
0010 
0011 #include <string>
0012 #include <vector>
0013 
0014 #include "absl/cleanup/cleanup.h"
0015 #include "absl/strings/string_view.h"
0016 #include "google/protobuf/descriptor.h"
0017 
0018 namespace google {
0019 namespace protobuf {
0020 namespace json_internal {
0021 // A path in a Protobuf message, annotated specifically for producing nice
0022 // errors.
0023 class MessagePath {
0024  public:
0025   explicit MessagePath(absl::string_view message_root)
0026       : components_(
0027             {Component{FieldDescriptor::TYPE_MESSAGE, message_root, "", -1}}) {}
0028 
0029   // Pushes a new field name, along with an optional type name if it is
0030   // a message or enum.
0031   //
0032   // Returns an RAII object that will pop the field component on scope exit.
0033   auto Push(absl::string_view field_name, FieldDescriptor::Type type,
0034             absl::string_view type_name = "") {
0035     // -1 makes it so the first call to NextRepeated makes the index 0.
0036     components_.push_back(Component{type, type_name, field_name, -1});
0037     return absl::MakeCleanup([this] { components_.pop_back(); });
0038   }
0039 
0040   // Increments the index of this field, indicating it is a repeated field.
0041   //
0042   // The first time this is called, the field will be marked as repeated and
0043   // the index will become 0.
0044   void NextRepeated() { ++components_.back().repeated_index; }
0045 
0046   // Appends a description of the current state of the path to `out`.
0047   void Describe(std::string& out) const;
0048 
0049  private:
0050   struct Component {
0051     FieldDescriptor::Type type;
0052     absl::string_view type_name, field_name;
0053     int32_t repeated_index;
0054   };
0055   std::vector<Component> components_;
0056 };
0057 }  // namespace json_internal
0058 }  // namespace protobuf
0059 }  // namespace google
0060 
0061 #endif  // GOOGLE_PROTOBUF_JSON_INTERNAL_MESSAGE_PATH_H__