Skip to content

Commit

Permalink
Add Support for multiple TypedExtra subclasses. (#714)
Browse files Browse the repository at this point in the history
* Add support for multiple typed args

* updates test case example

* Update CHANGELOG.md

* Adds test case for duplicate keys

* Added case for duplicate keys
  • Loading branch information
devRaphe authored Oct 15, 2024
1 parent 00e090a commit 0631416
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 12 deletions.
19 changes: 19 additions & 0 deletions generator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
# Changelog

## 9.1.3

- Add support for multiple `TypedExtras`.

Example :

```dart
@TypedExtrasSubClass(
id: 'abcd',
fileType: FileType.json,
destinations: [Destination.remote]
)
@AnotherTypedExtrasSubClass(
state: 'Ohio',
destinations: [Destination.remote]
)
@http.POST('/path/')
Future<String> myMethod();
## 9.1.2
- Support passing Enums into `TypedExtras`.
Expand Down
25 changes: 14 additions & 11 deletions generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2302,19 +2302,22 @@ ${bodyName.displayName} == null
}

Map<String, Object> _getMapFromTypedExtras(MethodElement m) {
final annotation =
_getMethodAnnotations(m, retrofit.TypedExtras).firstOrNull;
final fields = annotation?.objectValue.type?.element?.children
.whereType<FieldElement>();
final mapFromTypedExtras = <String, Object>{};
for (final field in fields ?? <FieldElement>[]) {
final value = annotation?.peek(field.name);
final fieldValue = _getFieldValue(value);
if (fieldValue != null) {
mapFromTypedExtras[field.name] = fieldValue;
final annotations = _getMethodAnnotations(m, retrofit.TypedExtras);
final allTypedExtras = <String, Object>{};

for (final annotation in annotations) {
final fields = annotation.objectValue.type?.element?.children
.whereType<FieldElement>();
for (final field in fields ?? <FieldElement>[]) {
final value = annotation.peek(field.name);
final fieldValue = _getFieldValue(value);
if (fieldValue != null) {
allTypedExtras[field.name] = fieldValue;
}
}
}
return mapFromTypedExtras;

return allTypedExtras;
}

void _generateExtra(
Expand Down
71 changes: 70 additions & 1 deletion generator/test/src/generator_test_src.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import 'package:source_gen_test/annotations.dart';

import 'query.pb.dart';

enum FileType {mp4, mp3}
enum FileType { mp4, mp3 }

class DummyTypedExtras extends TypedExtras {
final String id;
final Map<String, dynamic> config;
Expand Down Expand Up @@ -71,6 +72,74 @@ abstract class TypedExtrasTest {
Future<void> list();
}

class AnotherDummyTypedExtras extends TypedExtras {
const AnotherDummyTypedExtras({
required this.peanutButter,
required this.mac,
required this.id,
});

final String peanutButter;
final String mac;
final String id;
}

@ShouldGenerate(
'''
final _extra = <String, dynamic>{
'bacon': 'sausage',
'id': '12345',
'config': {
'date': '24-10-2024',
'type': 'analytics',
'shouldReplace': true,
'subConfig': {'date': '24-11-2025'},
},
'fileTypes': [
'mp3',
'mp4',
],
'sources': {
'internet',
'local',
},
'shouldProceed': true,
'peanutButter': 'Jelly',
'mac': 'Cheese',
};
''',
contains: true,
)
@RestApi()
abstract class MultipleTypedExtrasTest {
@DummyTypedExtras(
id: '1234',
config: {
'date': '24-10-2024',
'type': 'analytics',
'shouldReplace': true,
'subConfig': {'date': '24-11-2025'},
},
fileTypes: [
FileType.mp3,
FileType.mp4,
],
sources: {
'internet',
'local',
},
shouldProceed: true,
)
@AnotherDummyTypedExtras(
peanutButter: 'Jelly',
mac: 'Cheese',
id: '12345',
)
@Extra({'bacon': 'sausage'})
@GET('path')
Future<void> list();
}

@ShouldGenerate(
'''
// ignore_for_file: unnecessary_brace_in_string_interps,no_leading_underscores_for_local_identifiers,unused_element
Expand Down

0 comments on commit 0631416

Please sign in to comment.