Skip to content

Commit

Permalink
add option class-name custom name retrofit generator (#628)
Browse files Browse the repository at this point in the history
Co-authored-by: hodp ominext <hodp@ominext.com>
  • Loading branch information
ho-doan and HoDoanOmii committed Oct 12, 2023
1 parent d245f4a commit 3378c58
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
3 changes: 3 additions & 0 deletions example/build.yaml
@@ -1,6 +1,9 @@
targets:
$default:
builders:
retrofit_generator:
options:
class-name: RestClientYmlp
reflectable:
generate_for:
- lib/json_mapper_example.dart
5 changes: 3 additions & 2 deletions example/lib/example.dart
Expand Up @@ -11,7 +11,7 @@ part 'example.g.dart';

@RestApi(baseUrl: 'https://5d42a6e2bc64f90014a56ca0.mockapi.io/api/v1/')
abstract class RestClient {
factory RestClient(Dio dio, {String baseUrl}) = _RestClient;
factory RestClient(Dio dio, {String baseUrl}) = RestClientYmlp;

@GET('/tasks/{id}')
Future<ApiResult<Task?>> getNestApiResultGenericsInnerTypeNullable();
Expand Down Expand Up @@ -65,7 +65,8 @@ abstract class RestClient {

@PreventNullToAbsent()
@PATCH('/tasks/{id}')
Future<Task> updateTaskAvatar(@Path() String id, @Field('avatar') String? avatar);
Future<Task> updateTaskAvatar(
@Path() String id, @Field('avatar') String? avatar);

@DELETE('/tasks/{id}')
Future<void> deleteTask(@Path() String id);
Expand Down
33 changes: 21 additions & 12 deletions generator/lib/src/generator.dart
Expand Up @@ -19,18 +19,24 @@ const _analyzerIgnores =
'// ignore_for_file: unnecessary_brace_in_string_interps,no_leading_underscores_for_local_identifiers';

class RetrofitOptions {
RetrofitOptions({this.autoCastResponse, this.emptyRequestBody});
RetrofitOptions({
this.autoCastResponse,
this.emptyRequestBody,
this.className,
});

RetrofitOptions.fromOptions([BuilderOptions? options])
: autoCastResponse =
(options?.config['auto_cast_response']?.toString() ?? 'true') ==
'true',
emptyRequestBody =
(options?.config['empty_request_body']?.toString() ?? 'false') ==
'true';
'true',
className = options?.config['class-name']?.toString();

final bool? autoCastResponse;
final bool? emptyRequestBody;
final String? className;
}

class RetrofitGenerator extends GeneratorForAnnotation<retrofit.RestApi> {
Expand Down Expand Up @@ -78,7 +84,7 @@ class RetrofitGenerator extends GeneratorForAnnotation<retrofit.RestApi> {
}

String _implementClass(ClassElement element, ConstantReader? annotation) {
final className = element.name;
final className = globalOptions.className ?? '_${element.name}';
final enumString = annotation?.peek('parser')?.revive().accessor;
final parser = retrofit.Parser.values
.firstWhereOrNull((e) => e.toString() == enumString);
Expand All @@ -91,7 +97,7 @@ class RetrofitGenerator extends GeneratorForAnnotation<retrofit.RestApi> {
.where((c) => !c.isFactory && !c.isDefaultConstructor);
final classBuilder = Class((c) {
c
..name = '_$className'
..name = className
..types.addAll(element.typeParameters.map((e) => refer(e.name)))
..fields.addAll([_buildDioFiled(), _buildBaseUrlFiled(baseUrl)])
..constructors.addAll(
Expand Down Expand Up @@ -428,8 +434,9 @@ class RetrofitGenerator extends GeneratorForAnnotation<retrofit.RestApi> {
.statement,
);

final preventNullToAbsent = _getMethodAnnotationByType(m, retrofit.PreventNullToAbsent);

final preventNullToAbsent =
_getMethodAnnotationByType(m, retrofit.PreventNullToAbsent);

if (preventNullToAbsent == null && headers.isNotEmpty) {
blocks.add(
const Code('$_localHeadersVar.removeWhere((k, v) => v == null);'),
Expand Down Expand Up @@ -1470,9 +1477,10 @@ if (T != dynamic &&

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

final preventNullToAbsent = _getMethodAnnotationByType(m, retrofit.PreventNullToAbsent);


final preventNullToAbsent =
_getMethodAnnotationByType(m, retrofit.PreventNullToAbsent);

final anyNullable = m.parameters
.any((p) => p.type.nullabilitySuffix == NullabilitySuffix.question);

Expand All @@ -1495,9 +1503,10 @@ if (T != dynamic &&
);
return;
}

final preventNullToAbsent = _getMethodAnnotationByType(m, retrofit.PreventNullToAbsent);


final preventNullToAbsent =
_getMethodAnnotationByType(m, retrofit.PreventNullToAbsent);

final annotation = _getAnnotation(m, retrofit.Body);
final bodyName = annotation?.item1;
if (bodyName != null) {
Expand Down

0 comments on commit 3378c58

Please sign in to comment.