Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to have 'value' as a parameter identifier in annotated methods #639

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading