• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2009-2021, Google LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Google LLC nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "protos/protos.h"
29 
30 #include "absl/strings/str_format.h"
31 
32 namespace protos {
33 
34 // begin:google_only
35 // absl::Status MessageAllocationError(SourceLocation loc) {
36 //   return absl::Status(absl::StatusCode::kInternal,
37 //                       "Upb message allocation error", loc);
38 // }
39 //
40 // absl::Status ExtensionNotFoundError(int extension_number, SourceLocation loc) {
41 //   return absl::Status(
42 //       absl::StatusCode::kInternal,
43 //       absl::StrFormat("Extension %d not found", extension_number), loc);
44 // }
45 //
46 // absl::Status MessageEncodeError(upb_EncodeStatus status, SourceLocation loc) {
47 //   return absl::Status(absl::StatusCode::kInternal,
48 //                       absl::StrFormat("Upb message encoding error %d", status),
49 //                       loc
50 //
51 //   );
52 // }
53 //
54 // absl::Status MessageDecodeError(upb_DecodeStatus status, SourceLocation loc
55 //
56 // ) {
57 //   return absl::Status(absl::StatusCode::kInternal,
58 //                       absl::StrFormat("Upb message parse error %d", status), loc
59 //
60 //   );
61 // }
62 // end:google_only
63 
64 // begin:github_only
MessageAllocationError(SourceLocation loc)65 absl::Status MessageAllocationError(SourceLocation loc) {
66   return absl::Status(absl::StatusCode::kUnknown,
67                       "Upb message allocation error");
68 }
69 
ExtensionNotFoundError(int ext_number,SourceLocation loc)70 absl::Status ExtensionNotFoundError(int ext_number, SourceLocation loc) {
71   return absl::Status(absl::StatusCode::kUnknown,
72                       absl::StrFormat("Extension %d not found", ext_number));
73 }
74 
MessageEncodeError(upb_EncodeStatus s,SourceLocation loc)75 absl::Status MessageEncodeError(upb_EncodeStatus s, SourceLocation loc) {
76   return absl::Status(absl::StatusCode::kUnknown, "Encoding error");
77 }
78 
MessageDecodeError(upb_DecodeStatus status,SourceLocation loc)79 absl::Status MessageDecodeError(upb_DecodeStatus status, SourceLocation loc
80 
81 ) {
82   return absl::Status(absl::StatusCode::kUnknown, "Upb message parse error");
83 }
84 // end:github_only
85 
86 namespace internal {
87 
GetUpbExtensions(const ExtensionRegistry & extension_registry)88 upb_ExtensionRegistry* GetUpbExtensions(
89     const ExtensionRegistry& extension_registry) {
90   return extension_registry.registry_;
91 }
92 
Serialize(const upb_Message * message,const upb_MiniTable * mini_table,upb_Arena * arena,int options)93 absl::StatusOr<absl::string_view> Serialize(const upb_Message* message,
94                                             const upb_MiniTable* mini_table,
95                                             upb_Arena* arena, int options) {
96   size_t len;
97   char* ptr;
98   upb_EncodeStatus status =
99       upb_Encode(message, mini_table, options, arena, &ptr, &len);
100   if (status == kUpb_EncodeStatus_Ok) {
101     return absl::string_view(ptr, len);
102   }
103   return MessageEncodeError(status);
104 }
105 
106 }  // namespace internal
107 
108 }  // namespace protos
109