OLD | NEW |
---|---|
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of protoc; | 5 part of protoc; |
6 | 6 |
7 abstract class ProtobufContainer { | 7 abstract class ProtobufContainer { |
8 String get package; | 8 String get package; |
9 String get classname; | 9 String get classname; |
10 String get fqname; | 10 String get fqname; |
11 String get packageImportPrefix => package.replaceAll('.', r'$'); | 11 String get packageImportPrefix => package.replaceAll('.', r'$'); |
12 } | 12 } |
13 | 13 |
14 class CodeGenerator extends ProtobufContainer { | 14 class CodeGenerator extends ProtobufContainer { |
15 final Stream<List<int>> _streamIn; | 15 final Stream<List<int>> _streamIn; |
16 final IOSink _streamOut; | 16 final IOSink _streamOut; |
17 final IOSink _streamErr; | 17 final IOSink _streamErr; |
18 | 18 |
19 CodeGenerator(this._streamIn, this._streamOut, this._streamErr); | 19 CodeGenerator(this._streamIn, this._streamOut, this._streamErr); |
20 | 20 |
21 void generate() { | 21 /// Runs the code generator. The optional [optionsParser] can be used to |
22 /// change how command line options are parsed (see [GenerationOptionsParser] | |
23 /// for details), and [outputConfiguration] can be used to override where | |
24 /// generated files are created and how imports between generated files are | |
25 /// constructed (see [OutputConfiguration] for details). | |
26 void generate({ | |
27 GenerationOptionsParser optionsParser: parseDefaultGenerationOptions, | |
28 OutputConfiguration outputConfiguration}) { | |
22 _streamIn | 29 _streamIn |
23 .fold(new BytesBuilder(), (builder, data) => builder..add(data)) | 30 .fold(new BytesBuilder(), (builder, data) => builder..add(data)) |
24 .then((builder) => builder.takeBytes()) | 31 .then((builder) => builder.takeBytes()) |
25 .then((List<int> bytes) { | 32 .then((List<int> bytes) { |
26 var request = new CodeGeneratorRequest.fromBuffer(bytes); | 33 var request = new CodeGeneratorRequest.fromBuffer(bytes); |
27 var response = new CodeGeneratorResponse(); | 34 var response = new CodeGeneratorResponse(); |
28 | 35 |
29 // Parse the options in the request. Return the errors is any. | 36 // Parse the options in the request. Return the errors is any. |
30 var options = new GenerationOptions(request, response); | 37 var options = optionsParser(request, response); |
31 if (options == null) { | 38 if (options == null) { |
32 _streamOut.add(response.writeToBuffer()); | 39 _streamOut.add(response.writeToBuffer()); |
33 return; | 40 return; |
34 } | 41 } |
35 | 42 |
36 var ctx = new GenerationContext(options); | 43 var ctx = new GenerationContext(options, outputConfiguration == null |
44 ? new DefaultOutputConfiguration() : outputConfiguration); | |
37 List<FileGenerator> generators = <FileGenerator>[]; | 45 List<FileGenerator> generators = <FileGenerator>[]; |
38 for (FileDescriptorProto file in request.protoFile) { | 46 for (FileDescriptorProto file in request.protoFile) { |
39 var generator = new FileGenerator(file, this, ctx); | 47 var generator = new FileGenerator(file, this, ctx); |
40 if (request.fileToGenerate.contains(file.name)) { | 48 if (request.fileToGenerate.contains(file.name)) { |
41 generators.add(generator); | 49 generators.add(generator); |
42 } | 50 } |
43 } | 51 } |
44 | 52 |
45 response.file.addAll( | 53 response.file.addAll( |
46 generators.map((filegen) => filegen.generateResponse())); | 54 generators.map((filegen) => filegen.generateResponse())); |
47 _streamOut.add(response.writeToBuffer()); | 55 _streamOut.add(response.writeToBuffer()); |
48 }); | 56 }); |
49 } | 57 } |
50 | 58 |
51 String get package => ''; | 59 String get package => ''; |
52 String get classname => null; | 60 String get classname => null; |
53 String get fqname => ''; | 61 String get fqname => ''; |
54 } | 62 } |
55 | |
56 | |
57 class GenerationOptions { | |
58 final Map<String, String> fieldNameOptions; | |
59 | |
60 GenerationOptions._(this.fieldNameOptions); | |
61 | |
62 // Parse the options in the request. If there was an error in the | |
63 // options null is returned and the error is set on the response. | |
64 factory GenerationOptions(request, response) { | |
Siggi Cherem (dart-lang)
2014/05/07 23:30:44
I refactored this a bit in options.dart, the main
| |
65 var fieldNameOptions = <String, String>{}; | |
66 var parameter = request.parameter != null ? request.parameter : ''; | |
67 List<String> options = parameter.trim().split(','); | |
68 List<String> errors = []; | |
69 for (var option in options) { | |
70 option = option.trim(); | |
71 if (option.isEmpty) continue; | |
72 List<String> nameValue = option.split('='); | |
73 if (nameValue.length != 1 && nameValue.length != 2) { | |
74 errors.add('Illegal option: $option'); | |
75 continue; | |
76 } | |
77 String name = nameValue[0].trim(); | |
78 String value; | |
79 if (nameValue.length > 1) value = nameValue[1].trim(); | |
80 if (name == 'field_name') { | |
81 if (value == null) { | |
82 errors.add('Illegal option: $option'); | |
83 continue; | |
84 } | |
85 List<String> fromTo = value.split('|'); | |
86 if (fromTo.length != 2) { | |
87 errors.add('Illegal option: $option'); | |
88 continue; | |
89 } | |
90 var fromName = fromTo[0].trim(); | |
91 var toName = fromTo[1].trim(); | |
92 if (fromName.length == 0 || toName.length == 0) { | |
93 errors.add('Illegal option: $option'); | |
94 continue; | |
95 } | |
96 fieldNameOptions['.$fromName'] = toName; | |
97 } else { | |
98 errors.add('Illegal option: $option'); | |
99 } | |
100 } | |
101 if (errors.length > 0) { | |
102 response.error = errors.join('\n'); | |
103 return null; | |
104 } else { | |
105 return new GenerationOptions._(fieldNameOptions); | |
106 } | |
107 } | |
108 | |
109 String fieldNameOption(String fqname) => fieldNameOptions[fqname]; | |
110 } | |
OLD | NEW |