1// Copyright 2019 The Pigweed Authors 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); you may not 4// use this file except in compliance with the License. You may obtain a copy of 5// the License at 6// 7// https://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12// License for the specific language governing permissions and limitations under 13// the License. 14syntax = "proto3"; 15 16// This is a test .proto file for pw_protobuf's codegen implementation. 17 18package pw.protobuf.test; 19 20// Top-level enum definition. 21enum Bool { 22 TRUE = 0; 23 FALSE = 1; 24 FILE_NOT_FOUND = 2; 25} 26 27// Prefixed enum 28enum Error { 29 ERROR_NONE = 0; 30 ERROR_NOT_FOUND = 1; 31 ERROR_UNKNOWN = 2; 32} 33 34// Single-value enum 35enum AlwaysBlue { 36 BLUE = 0; 37} 38 39// A message! 40message Pigweed { 41 // Nested messages and enums. 42 message Pigweed { 43 enum Binary { 44 ZERO = 0; 45 ONE = 1; 46 } 47 48 Bool status = 1; 49 } 50 51 message Protobuf { 52 enum Binary { 53 ONE = 0; 54 ZERO = 1; 55 } 56 57 // We must go deeper. 58 message Compiler { 59 enum Status { 60 OK = 0; 61 ERROR = 1; 62 FUBAR = 2; 63 } 64 65 string file_name = 1; 66 Status status = 2; 67 Binary protobuf_bin = 3; 68 Pigweed.Binary pigweed_bin = 4; 69 } 70 71 Binary binary_value = 1; 72 } 73 74 // Regular types. 75 uint32 magic_number = 1; 76 sint32 ziggy = 2; 77 fixed64 cycles = 3; 78 float ratio = 4; 79 string error_message = 5; 80 81 DeviceInfo device_info = 6; 82 83 // Nested messages and enums as fields. 84 Pigweed pigweed = 7; 85 Protobuf.Binary bin = 8; 86 87 Proto proto = 9; 88 repeated Proto.ID id = 10; 89 90 // Fixed-length bytes field, a string with no maximum size specified in 91 // full_test.pwpb_options, and a scalar with a forced callback. 92 bytes data = 11; 93 string description = 12; 94 uint32 special_property = 13; 95 96 int32 bungle = 14; 97 98 sfixed32 test_sfixed32 = 15; 99 sfixed64 test_sfixed64 = 16; 100 sint64 test_sint64 = 17; 101} 102 103// Another message. 104message Proto { 105 enum Binary { 106 OFF = 0; 107 ON = 1; 108 } 109 110 message ID { 111 uint32 id = 1; 112 } 113 114 // Circular dependency with Pigweed. 115 Pigweed pigweed = 1; 116 117 // Same name, different namespace. 118 Binary bin = 2; 119 Pigweed.Pigweed.Binary pigweed_pigweed_bin = 3; 120 Pigweed.Protobuf.Binary pigweed_protobuf_bin = 4; 121 122 Pigweed.Protobuf.Compiler meta = 5; 123} 124 125// Yet another message. 126message DeviceInfo { 127 enum DeviceStatus { 128 OK = 0; 129 ASSERT = 1; 130 FAULT = 2; 131 PANIC = 3; 132 } 133 134 string device_name = 1; 135 fixed32 device_id = 2; 136 DeviceStatus status = 3; 137 138 repeated KeyValuePair attributes = 4; 139} 140 141// Ensure recursive submessages work. 142message Crate { 143 string name = 1; 144 repeated Crate smaller_crates = 2; 145} 146 147// Two messages that should properly overlay without collisions. 148message BaseMessage { 149 uint32 length = 1; 150 reserved 2; 151} 152 153message Overlay { 154 reserved 1; 155 uint32 height = 2; 156} 157 158// Ensure that reserved words are suffixed with underscores. 159message IntegerMetadata { 160 int32 bits = 1; 161 bool signed = 2; 162 bool null = 3; 163} 164 165// Enum values are handled differently from normal identifiers because they are 166// automatically case-converted in the generated code. Therefore, we append an 167// underscore if the output format exactly matches a reserved word or a 168// standard-library macro. This enum tests that underscores are appended in 169// cases where they're actually necessary but not when they can be skipped due 170// to case conversion. 171enum ReservedWord { 172 NULL = 0; 173 int = 1; 174 return = 2; 175 break = 3; 176 for // The linter wants a line break here. 177 = 4; 178 do // The linter wants a line break here. 179 = 5; 180 // Note: This obviously isn't anywhere near a complete list of C++ keywords 181 // and standard-library macros. 182} 183 184// Messages and enums named either `Message` or `Fields` should be appended with 185// underscores in generated C++ code so that they don't conflict with the 186// codegen internals. This is only a problem if such types are defined within 187// the scopes of other messages, as generated message structs automatically have 188// nested types `struct Message` and `enum class Fields`. However, we simply 189// apply the transformation in all contexts for consistency and simplicity. 190message Function { 191 message Message { 192 string content = 1; 193 } 194 195 enum Fields { 196 NONE = 0; 197 COMPLEX_NUMBERS = 1; 198 INTEGERS_MOD_5 = 2; 199 MEROMORPHIC_FUNCTIONS_ON_COMPLEX_PLANE = 3; 200 OTHER = 4; 201 } 202 203 Message description = 1; 204 Fields domain_field = 2; 205 Fields codomain_field = 3; 206} 207 208// This might be useful. 209message KeyValuePair { 210 string key = 1; 211 string value = 2; 212} 213 214// Corner cases of code generation. 215message CornerCases { 216 // Generates ReadUint32() and WriteUint32() that call the parent definition. 217 uint32 _uint32 = 1; 218} 219 220message OneOfTest { 221 message AMessage { 222 bool a_bool = 1; 223 } 224 225 oneof type { 226 int32 an_int = 1; 227 string a_string = 2; 228 AMessage a_message = 3; 229 } 230} 231 232message LargeNestedTest { 233 message LargeNested { 234 bytes data = 1; 235 } 236 237 LargeNested large_nested = 1; 238} 239