Skip to content

Commit

Permalink
Allow to have 'value' as a parameter identifier in annotated methods (#…
Browse files Browse the repository at this point in the history
…639)

* Init fixing generator to allow for 'value' identifier in method params

WIP

* Add missing replacement of `'value'` to `_valueVar` in generator.dart

* Fix tests and remove potentially wrong edit not covered by tests

* some fixes got overriden during merge of master

---------

Co-authored-by: Robinson Christmann <[email protected]>
  • Loading branch information
IcarusSosie and Robinson Christmann authored Jul 1, 2024
1 parent a785dfb commit b236243
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 120 deletions.
24 changes: 12 additions & 12 deletions generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class RetrofitGenerator extends GeneratorForAnnotation<retrofit.RestApi> {
static const _onSendProgress = 'onSendProgress';
static const _onReceiveProgress = 'onReceiveProgress';
static const _path = 'path';
static const _valueVar = 'value';
static const _valueVar = '_value';
bool hasCustomOptions = false;

/// Global options specified in the `build.yaml`
Expand Down Expand Up @@ -415,7 +415,7 @@ class RetrofitGenerator extends GeneratorForAnnotation<retrofit.RestApi> {
final paths = _getAnnotations(m, retrofit.Path);
var definePath = method.peek('path')?.stringValue;
paths.forEach((k, v) {
final value = v.peek(_valueVar)?.stringValue ?? k.displayName;
final value = v.peek('value')?.stringValue ?? k.displayName;
definePath = definePath?.replaceFirst(
'{$value}',
"\${${k.displayName}${k.type.element?.kind == ElementKind.ENUM ? _hasToJson(k.type) ? '.toJson()' : '.name' : ''}}",
Expand Down Expand Up @@ -840,7 +840,7 @@ You should create a new class to encapsulate the response.
);
}
} else {
blocks.add(const Code('final value = $_resultVar.data!;'));
blocks.add(const Code('final $_valueVar = $_resultVar.data!;'));
}
} else {
if (_isBasicType(returnType)) {
Expand Down Expand Up @@ -868,7 +868,7 @@ You should create a new class to encapsulate the response.
.assign(refer('await $_dioVar.fetch').call([options]))
.statement,
)
..add(const Code('final value = $_resultVar.data;'));
..add(const Code('final $_valueVar = $_resultVar.data;'));
} else if (_typeChecker(GeneratedMessage).isSuperTypeOf(returnType)) {
blocks.add(
declareFinal(_resultVar)
Expand All @@ -877,7 +877,7 @@ You should create a new class to encapsulate the response.
.statement,
);
blocks.add(Code(
"final value = await compute(${_displayString(returnType)}.fromBuffer, $_resultVar.data!);"));
"final $_valueVar = await compute(${_displayString(returnType)}.fromBuffer, $_resultVar.data!);"));
} else {
final fetchType = returnType.isNullable
? 'Map<String,dynamic>?'
Expand Down Expand Up @@ -961,12 +961,12 @@ You should create a new class to encapsulate the response.
if (isWrapped) {
blocks.add(
Code('''
final httpResponse = HttpResponse(value, $_resultVar);
final httpResponse = HttpResponse($_valueVar, $_resultVar);
$returnAsyncWrapper httpResponse;
'''),
);
} else {
blocks.add(Code('$returnAsyncWrapper value;'));
blocks.add(Code('$returnAsyncWrapper $_valueVar;'));
}
}

Expand Down Expand Up @@ -1420,7 +1420,7 @@ if (T != dynamic &&
) {
final queries = _getAnnotations(m, retrofit.Query);
final queryParameters = queries.map((p, r) {
final key = r.peek(_valueVar)?.stringValue ?? p.displayName;
final key = r.peek('value')?.stringValue ?? p.displayName;
final Expression value;
if (_isBasicType(p.type) ||
p.type.isDartCoreList ||
Expand Down Expand Up @@ -1769,7 +1769,7 @@ ${bodyName.displayName} == null
var anyNullable = false;
final fields = _getAnnotations(m, retrofit.Field).map((p, r) {
anyNullable |= p.type.nullabilitySuffix == NullabilitySuffix.question;
final fieldName = r.peek(_valueVar)?.stringValue ?? p.displayName;
final fieldName = r.peek('value')?.stringValue ?? p.displayName;
final isFileField = _typeChecker(File).isAssignableFromType(p.type);
if (isFileField) {
log.severe(
Expand Down Expand Up @@ -1811,7 +1811,7 @@ ${bodyName.displayName} == null

parts.forEach((p, r) {
final fieldName = r.peek('name')?.stringValue ??
r.peek(_valueVar)?.stringValue ??
r.peek('value')?.stringValue ??
p.displayName;
final isFileField = _typeChecker(File).isAssignableFromType(p.type);
final contentType = r.peek('contentType')?.stringValue;
Expand Down Expand Up @@ -2061,7 +2061,7 @@ ${bodyName.displayName} == null

Map<String, Expression> _generateHeaders(MethodElement m) {
final headers = _getMethodAnnotations(m, retrofit.Headers)
.map((e) => e.peek(_valueVar))
.map((e) => e.peek('value'))
.map(
(value) => value?.mapValue.map(
(k, v) {
Expand Down Expand Up @@ -2090,7 +2090,7 @@ ${bodyName.displayName} == null

final annotationsInParam = _getAnnotations(m, retrofit.Header);
final headersInParams = annotationsInParam.map((k, v) {
final value = v.peek(_valueVar)?.stringValue ?? k.displayName;
final value = v.peek('value')?.stringValue ?? k.displayName;
return MapEntry(value, refer(k.displayName));
});
headers.addAll(headersInParams);
Expand Down
Loading

0 comments on commit b236243

Please sign in to comment.