• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.options, and a scalar with a forced callback.
92  bytes data = 11;
93  string description = 12;
94  uint32 special_property = 13;
95  int32 bungle = 14;
96}
97
98// Another message.
99message Proto {
100  enum Binary {
101    OFF = 0;
102    ON = 1;
103  }
104
105  message ID {
106    uint32 id = 1;
107  }
108
109  // Circular dependency with Pigweed.
110  Pigweed pigweed = 1;
111
112  // Same name, different namespace.
113  Binary bin = 2;
114  Pigweed.Pigweed.Binary pigweed_pigweed_bin = 3;
115  Pigweed.Protobuf.Binary pigweed_protobuf_bin = 4;
116
117  Pigweed.Protobuf.Compiler meta = 5;
118}
119
120// Yet another message.
121message DeviceInfo {
122  enum DeviceStatus {
123    OK = 0;
124    ASSERT = 1;
125    FAULT = 2;
126    PANIC = 3;
127  }
128
129  string device_name = 1;
130  fixed32 device_id = 2;
131  DeviceStatus status = 3;
132
133  repeated KeyValuePair attributes = 4;
134}
135
136// Ensure recursive submessages work.
137message Crate {
138  string name = 1;
139  repeated Crate smaller_crates = 2;
140}
141
142// Two messages that should properly overlay without collisions.
143message BaseMessage {
144  uint32 length = 1;
145  reserved 2;
146}
147
148message Overlay {
149  reserved 1;
150  uint32 height = 2;
151}
152
153// Ensure that reserved words are suffixed with underscores.
154message IntegerMetadata {
155  int32 bits = 1;
156  bool signed = 2;
157  bool null = 3;
158}
159
160// Enum values are handled differently from normal identifiers because they are
161// automatically case-converted in the generated code. Therefore, we append an
162// underscore if the output format exactly matches a reserved word or a
163// standard-library macro. This enum tests that underscores are appended in
164// cases where they're actually necessary but not when they can be skipped due
165// to case conversion.
166enum ReservedWord {
167  NULL = 0;
168  int = 1;
169  return = 2;
170  break = 3;
171  for  // The linter wants a line break here.
172    = 4;
173  do  // The linter wants a line break here.
174    = 5;
175  // Note: This obviously isn't anywhere near a complete list of C++ keywords
176  //       and standard-library macros.
177}
178
179// Messages and enums named either `Message` or `Fields` should be appended with
180// underscores in generated C++ code so that they don't conflict with the
181// codegen internals. This is only a problem if such types are defined within
182// the scopes of other messages, as generated message structs automatically have
183// nested types `struct Message` and `enum class Fields`. However, we simply
184// apply the transformation in all contexts for consistency and simplicity.
185message Function {
186  message Message {
187    string content = 1;
188  }
189
190  enum Fields {
191    NONE = 0;
192    COMPLEX_NUMBERS = 1;
193    INTEGERS_MOD_5 = 2;
194    MEROMORPHIC_FUNCTIONS_ON_COMPLEX_PLANE = 3;
195    OTHER = 4;
196  }
197
198  Message description = 1;
199  Fields domain_field = 2;
200  Fields codomain_field = 3;
201}
202
203// This might be useful.
204message KeyValuePair {
205  string key = 1;
206  string value = 2;
207}
208
209// Corner cases of code generation.
210message CornerCases {
211  // Generates ReadUint32() and WriteUint32() that call the parent definition.
212  uint32 _uint32 = 1;
213}
214