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

feat: Add support @Extras() #648

Merged
merged 3 commits into from
Feb 3, 2024
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
10 changes: 10 additions & 0 deletions generator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## 8.1.0

- Added `@Extras` to pass extra options to dio requests, response, transformer and interceptors.

Example :
```dart
@http.POST('/path/')
Future<String> myMethod(@Extras() Map<String, dynamic> extras);
```

## 8.0.6

- @useResult
Expand Down
47 changes: 46 additions & 1 deletion generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2159,7 +2159,7 @@ ${bodyName.displayName} == null
String localExtraVar,
) {
blocks.add(
declareConst(localExtraVar)
declareFinal(localExtraVar)
.assign(
literalMap(
_getMethodAnnotations(m, retrofit.Extra)
Expand Down Expand Up @@ -2195,6 +2195,51 @@ ${bodyName.displayName} == null
)
.statement,
);

final extraMap = _getAnnotations(m, retrofit.Extras);
for (final p in extraMap.keys) {
final type = p.type;
final displayName = p.displayName;
final Expression value;
if (_isBasicType(type) || type.isDartCoreList || type.isDartCoreMap) {
value = refer(displayName);
} else if (_typeChecker(ProtobufEnum).isSuperTypeOf(type)) {
value = type.nullabilitySuffix == NullabilitySuffix.question
? refer(p.displayName).nullSafeProperty('value')
: refer(p.displayName).property('value');
} else {
switch (clientAnnotation.parser) {
case retrofit.Parser.JsonSerializable:
value = p.type.nullabilitySuffix == NullabilitySuffix.question
? refer(displayName).nullSafeProperty('toJson').call([])
: refer(displayName).property('toJson').call([]);
break;
case retrofit.Parser.MapSerializable:
value = p.type.nullabilitySuffix == NullabilitySuffix.question
? refer(displayName).nullSafeProperty('toMap').call([])
: refer(displayName).property('toMap').call([]);
break;
case retrofit.Parser.DartJsonMapper:
value = refer(displayName);
break;
case retrofit.Parser.FlutterCompute:
value = refer(
'await compute(serialize${_displayString(p.type)}, ${p.displayName})',
);
break;
}
}

final emitter = DartEmitter(useNullSafetySyntax: true);
final buffer = StringBuffer();
value.accept(emitter, buffer);
if (type.nullabilitySuffix == NullabilitySuffix.question) {
refer('?? <String,dynamic>{}').accept(emitter, buffer);
}
final expression = refer(buffer.toString());

blocks.add(refer('$localExtraVar.addAll').call([expression]).statement);
}
}

bool _missingToJson(ClassElement ele) {
Expand Down
4 changes: 2 additions & 2 deletions generator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ topics:
- build-runner
- codegen
- api
version: 8.0.3
version: 8.1.0
environment:
sdk: '>=2.19.0 <4.0.0'

Expand All @@ -17,7 +17,7 @@ dependencies:
code_builder: ^4.4.0
dart_style: ^2.3.0
dio: ^5.0.0
retrofit: ^4.0.2
retrofit: ^4.1.0
source_gen: ^1.3.0
tuple: ^2.0.1
protobuf: ^3.1.0
Expand Down
52 changes: 48 additions & 4 deletions generator/test/src/generator_test_src.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ abstract class BaseUrl {}

@ShouldGenerate(
'''
const _extra = <String, dynamic>{};
final _extra = <String, dynamic>{};
''',
contains: true,
)
Expand All @@ -59,7 +59,7 @@ abstract class EmptyExtras {

@ShouldGenerate(
'''
const _extra = <String, dynamic>{'key': 'value'};
final _extra = <String, dynamic>{'key': 'value'};
''',
contains: true,
)
Expand All @@ -72,7 +72,7 @@ abstract class ExtrasWithPrimitiveValues {

@ShouldGenerate(
'''
const _extra = <String, dynamic>{
final _extra = <String, dynamic>{
'key': 'value',
'key2': 'value2',
};
Expand All @@ -89,7 +89,7 @@ abstract class MultipleExtrasWithPrimitiveValues {

@ShouldGenerate(
'''
const _extra = <String, dynamic>{'key': CustomConstant()};
final _extra = <String, dynamic>{'key': CustomConstant()};
''',
contains: true,
)
Expand All @@ -100,6 +100,50 @@ abstract class ExtrasWithCustomConstant {
Future<void> list();
}

@ShouldGenerate(
'''
final _extra = <String, dynamic>{};
_extra.addAll(extras ?? <String, dynamic>{});
''',
contains: true,
)
@RestApi()
abstract class TestExtrasWithNullable {
@GET('/list/')
Future<void> list(@Extras() Map<String, dynamic>? extras);
}

@ShouldGenerate(
'''
final _extra = <String, dynamic>{'key': 'value'};
_extra.addAll(extras);
''',
contains: true,
)
@RestApi()
abstract class TestExtrasWithMap {
@GET('/list/')
@Extra({'key': 'value'})
Future<void> list(
@Extras() Map<String, dynamic> extras,
);
}

@ShouldGenerate(
'''
final _extra = <String, dynamic>{};
_extra.addAll(u.toJson());
''',
contains: true,
)
@RestApi()
abstract class TestExtrasWithObject {
@GET('/list/')
Future<void> list(
@Extras() User u,
);
}

class CustomConstant {
const CustomConstant();
}
Expand Down
11 changes: 11 additions & 0 deletions retrofit/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## 4.1.0

- Added `@Extras` to pass extra options to dio requests, response, transformer and interceptors.

Example :

```dart
@http.POST('/path/')
Future<String> myMethod(@Extras() Map<String, dynamic> extras);
```

## 4.0.0

- Update dio to ^5.0.0
Expand Down
12 changes: 12 additions & 0 deletions retrofit/lib/dio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ class Extra {
const Extra(this.data);
}

/// Extra data that will be passed to dio's request, response, transformer and interceptors.
/// Simple Example:
///
///```
/// @GET("/get")
/// Future<String> foo(@Extras() Map<String, dynamic> extras)
///```
@immutable
class Extras {
const Extras();
}

@immutable
class CancelRequest {
const CancelRequest();
Expand Down
2 changes: 1 addition & 1 deletion retrofit/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: retrofit
description: retrofit.dart is an dio client generator using source_gen and inspired by Chopper and Retrofit.
homepage: https://mings.in/retrofit.dart/
repository: https://github.com/trevorwang/retrofit.dart/
version: 4.0.2
version: 4.1.0

environment:
sdk: '>=2.17.0 <4.0.0'
Expand Down
Loading