GPBDescriptor.h 10.9 KB
Newer Older
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
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#import <Foundation/Foundation.h>

33
#import "GPBRuntimeTypes.h"
34 35 36 37 38 39

@class GPBEnumDescriptor;
@class GPBFieldDescriptor;
@class GPBFileDescriptor;
@class GPBOneofDescriptor;

40 41
NS_ASSUME_NONNULL_BEGIN

42
/** Syntax used in the proto file. */
43
typedef NS_ENUM(uint8_t, GPBFileSyntax) {
44
  /** Unknown syntax. */
45
  GPBFileSyntaxUnknown = 0,
46
  /** Proto2 syntax. */
47
  GPBFileSyntaxProto2 = 2,
48
  /** Proto3 syntax. */
49 50 51
  GPBFileSyntaxProto3 = 3,
};

52
/** Type of proto field. */
53
typedef NS_ENUM(uint8_t, GPBFieldType) {
54 55 56 57 58 59
  /** Optional/required field. Only valid for proto2 fields. */
  GPBFieldTypeSingle,
  /** Repeated field. */
  GPBFieldTypeRepeated,
  /** Map field. */
  GPBFieldTypeMap,
60 61
};

62 63 64
/**
 * Describes a proto message.
 **/
65 66
@interface GPBDescriptor : NSObject<NSCopying>

67
/** Name of the message. */
68
@property(nonatomic, readonly, copy) NSString *name;
69
/** Fields declared in the message. */
70
@property(nonatomic, readonly, strong, nullable) NSArray<GPBFieldDescriptor*> *fields;
71
/** Oneofs declared in the message. */
72
@property(nonatomic, readonly, strong, nullable) NSArray<GPBOneofDescriptor*> *oneofs;
73
/** Extension range declared for the message. */
74
@property(nonatomic, readonly, nullable) const GPBExtensionRange *extensionRanges;
75
/** Number of extension ranges declared for the message. */
76
@property(nonatomic, readonly) uint32_t extensionRangesCount;
77
/** Descriptor for the file where the message was defined. */
78 79
@property(nonatomic, readonly, assign) GPBFileDescriptor *file;

80
/** Whether the message is in wire format or not. */
81
@property(nonatomic, readonly, getter=isWireFormat) BOOL wireFormat;
82
/** The class of this message. */
83
@property(nonatomic, readonly) Class messageClass;
84 85 86 87 88 89 90
/** Containing message descriptor if this message is nested, or nil otherwise. */
@property(readonly, nullable) GPBDescriptor *containingType;
/**
 * Fully qualified name for this message (package.message). Can be nil if the
 * value is unable to be computed.
 */
@property(readonly, nullable) NSString *fullName;
91

92 93 94 95 96 97 98
/**
 * Gets the field for the given number.
 *
 * @param fieldNumber The number for the field to get.
 *
 * @return The field descriptor for the given number, or nil if not found.
 **/
99
- (nullable GPBFieldDescriptor *)fieldWithNumber:(uint32_t)fieldNumber;
100 101 102 103 104 105 106 107

/**
 * Gets the field for the given name.
 *
 * @param name The name for the field to get.
 *
 * @return The field descriptor for the given name, or nil if not found.
 **/
108
- (nullable GPBFieldDescriptor *)fieldWithName:(NSString *)name;
109 110 111 112 113 114 115 116

/**
 * Gets the oneof for the given name.
 *
 * @param name The name for the oneof to get.
 *
 * @return The oneof descriptor for the given name, or nil if not found.
 **/
117
- (nullable GPBOneofDescriptor *)oneofWithName:(NSString *)name;
118 119 120

@end

121 122 123
/**
 * Describes a proto file.
 **/
124 125
@interface GPBFileDescriptor : NSObject

126
/** The package declared in the proto file. */
127
@property(nonatomic, readonly, copy) NSString *package;
128 129
/** The objc prefix declared in the proto file. */
@property(nonatomic, readonly, copy, nullable) NSString *objcPrefix;
130
/** The syntax of the proto file. */
131 132 133 134
@property(nonatomic, readonly) GPBFileSyntax syntax;

@end

135 136 137
/**
 * Describes a oneof field.
 **/
138
@interface GPBOneofDescriptor : NSObject
139
/** Name of the oneof field. */
140
@property(nonatomic, readonly) NSString *name;
141
/** Fields declared in the oneof. */
142
@property(nonatomic, readonly) NSArray<GPBFieldDescriptor*> *fields;
143

144 145 146 147 148 149 150
/**
 * Gets the field for the given number.
 *
 * @param fieldNumber The number for the field to get.
 *
 * @return The field descriptor for the given number, or nil if not found.
 **/
151
- (nullable GPBFieldDescriptor *)fieldWithNumber:(uint32_t)fieldNumber;
152 153 154 155 156 157 158 159

/**
 * Gets the field for the given name.
 *
 * @param name The name for the field to get.
 *
 * @return The field descriptor for the given name, or nil if not found.
 **/
160
- (nullable GPBFieldDescriptor *)fieldWithName:(NSString *)name;
161

162 163
@end

164 165 166
/**
 * Describes a proto field.
 **/
167 168
@interface GPBFieldDescriptor : NSObject

169
/** Name of the field. */
170
@property(nonatomic, readonly, copy) NSString *name;
171
/** Number associated with the field. */
172
@property(nonatomic, readonly) uint32_t number;
173
/** Data type contained in the field. */
174
@property(nonatomic, readonly) GPBDataType dataType;
175
/** Whether it has a default value or not. */
176
@property(nonatomic, readonly) BOOL hasDefaultValue;
177
/** Default value for the field. */
178
@property(nonatomic, readonly) GPBGenericValue defaultValue;
179
/** Whether this field is required. Only valid for proto2 fields. */
180
@property(nonatomic, readonly, getter=isRequired) BOOL required;
181
/** Whether this field is optional. */
182
@property(nonatomic, readonly, getter=isOptional) BOOL optional;
183
/** Type of field (single, repeated, map). */
184
@property(nonatomic, readonly) GPBFieldType fieldType;
185
/** Type of the key if the field is a map. The value's type is -fieldType. */
186
@property(nonatomic, readonly) GPBDataType mapKeyDataType;
187
/** Whether the field is packable. */
188 189
@property(nonatomic, readonly, getter=isPackable) BOOL packable;

190
/** The containing oneof if this field is part of one, nil otherwise. */
191
@property(nonatomic, readonly, assign, nullable) GPBOneofDescriptor *containingOneof;
192

193
/** Class of the message if the field is of message type. */
194
@property(nonatomic, readonly, assign, nullable) Class msgClass;
195

196
/** Descriptor for the enum if this field is an enum. */
197
@property(nonatomic, readonly, strong, nullable) GPBEnumDescriptor *enumDescriptor;
198

199 200 201 202 203 204 205
/**
 * Checks whether the given enum raw value is a valid enum value.
 *
 * @param value The raw enum value to check.
 *
 * @return YES if value is a valid enum raw value.
 **/
206 207
- (BOOL)isValidEnumValue:(int32_t)value;

208
/** @return Name for the text format, or nil if not known. */
209
- (nullable NSString *)textFormatName;
210 211 212

@end

213 214 215
/**
 * Describes a proto enum.
 **/
216 217
@interface GPBEnumDescriptor : NSObject

218
/** Name of the enum. */
219
@property(nonatomic, readonly, copy) NSString *name;
220
/** Function that validates that raw values are valid enum values. */
221 222
@property(nonatomic, readonly) GPBEnumValidationFunc enumVerifier;

223 224 225
/**
 * Returns the enum value name for the given raw enum.
 *
226 227 228
 * Note that there can be more than one name corresponding to a given value
 * if the allow_alias option is used.
 *
229 230
 * @param number The raw enum value.
 *
231
 * @return The first name that matches the enum value passed, or nil if not valid.
232
 **/
233
- (nullable NSString *)enumNameForValue:(int32_t)number;
234 235 236 237 238 239 240 241 242

/**
 * Gets the enum raw value for the given enum name.
 *
 * @param outValue A pointer where the value will be set.
 * @param name     The enum name for which to get the raw value.
 *
 * @return YES if a value was copied into the pointer, NO otherwise.
 **/
243
- (BOOL)getValue:(nullable int32_t *)outValue forEnumName:(NSString *)name;
244

245 246 247 248 249
/**
 * Returns the text format for the given raw enum value.
 *
 * @param number The raw enum value.
 *
250
 * @return The first text format name which matches the enum value, or nil if not valid.
251
 **/
252
- (nullable NSString *)textFormatNameForValue:(int32_t)number;
253 254 255 256 257 258 259 260 261

/**
 * Gets the enum raw value for the given text format name.
 *
 * @param outValue       A pointer where the value will be set.
 * @param textFormatName The text format name for which to get the raw value.
 *
 * @return YES if a value was copied into the pointer, NO otherwise.
 **/
262
- (BOOL)getValue:(nullable int32_t *)outValue forEnumTextFormatName:(NSString *)textFormatName;
263

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
/**
 * Gets the number of defined enum names.
 *
 * @return Count of the number of enum names, including any aliases.
 */
@property(nonatomic, readonly) uint32_t enumNameCount;

/**
 * Gets the enum name corresponding to the given index.
 *
 * @param index Index into the available names.  The defined range is from 0
 *              to self.enumNameCount - 1.
 *
 * @returns The enum name at the given index, or nil if the index is out of range.
 */
- (nullable NSString *)getEnumNameForIndex:(uint32_t)index;

/**
 * Gets the enum text format name corresponding to the given index.
 *
 * @param index Index into the available names.  The defined range is from 0
 *              to self.enumNameCount - 1.
 *
 * @returns The text format name at the given index, or nil if the index is out of range.
 */
- (nullable NSString *)getEnumTextFormatNameForIndex:(uint32_t)index;

291 292
@end

293 294 295
/**
 * Describes a proto extension.
 **/
296
@interface GPBExtensionDescriptor : NSObject<NSCopying>
297
/** Field number under which the extension is stored. */
298
@property(nonatomic, readonly) uint32_t fieldNumber;
299
/** The containing message class, i.e. the class extended by this extension. */
300
@property(nonatomic, readonly) Class containingMessageClass;
301
/** Data type contained in the extension. */
302
@property(nonatomic, readonly) GPBDataType dataType;
303
/** Whether the extension is repeated. */
304
@property(nonatomic, readonly, getter=isRepeated) BOOL repeated;
305
/** Whether the extension is packable. */
306
@property(nonatomic, readonly, getter=isPackable) BOOL packable;
307
/** The class of the message if the extension is of message type. */
308
@property(nonatomic, readonly, assign) Class msgClass;
309
/** The singleton name for the extension. */
310
@property(nonatomic, readonly) NSString *singletonName;
311
/** The enum descriptor if the extension is of enum type. */
312
@property(nonatomic, readonly, strong, nullable) GPBEnumDescriptor *enumDescriptor;
313
/** The default value for the extension. */
314
@property(nonatomic, readonly, nullable) id defaultValue;
315

316
@end
317 318

NS_ASSUME_NONNULL_END