Skip to content

Commit

Permalink
Feat: support nested non-primitive types for TypedExtras (#723)
Browse files Browse the repository at this point in the history
* Feat: support nested non-primitive types for TypedExtras

* update changelogs and version
  • Loading branch information
farmery authored Nov 14, 2024
1 parent af5d391 commit a5c49fa
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 15 deletions.
26 changes: 25 additions & 1 deletion generator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
# Changelog

## 9.1.5

- Add support for nested object of non-primitive types in `TypedExtras`.

Example :

```dart
@RestApi()
abstract class TypedExtrasTest {
@DummyTypedExtras(
id: '1234',
config: Config(
date: '24-10-2024',
type: 'analytics',
shouldReplace: true,
subConfig: {'date': '24-11-2025'},
),
)
@GET('path')
Future<void> list();
}
```

## 9.1.3

- Add support for multiple `TypedExtras`.
Expand All @@ -18,7 +41,8 @@
)
@http.POST('/path/')
Future<String> myMethod();
```

## 9.1.2

- Support passing Enums into `TypedExtras`.
Expand Down
13 changes: 13 additions & 0 deletions generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2304,6 +2304,19 @@ ${bodyName.displayName} == null
return _getFieldValue(ConstantReader(item));
}).toSet();
}
if (value?.objectValue.type != null) {
final fields = <String, Object?>{};
final type = value!.objectValue.type;
if (type is InterfaceType) {
for (var field in type.element.fields) {
if (!field.isStatic) {
final fieldValue = value.peek(field.name);
fields[field.name] = _getFieldValue(fieldValue);
}
}
}
return fields;
}
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion generator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ topics:
- rest
- retrofit
- codegen
version: 9.1.2
version: 9.1.5
environment:
sdk: '>=3.3.0 <4.0.0'

Expand Down
40 changes: 27 additions & 13 deletions generator/test/src/generator_test_src.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,23 @@ import 'query.pb.dart';

enum FileType { mp4, mp3 }

class Config {
final String date;
final String type;
final bool shouldReplace;
final Map<String, dynamic> subConfig;

const Config({
required this.date,
required this.type,
required this.shouldReplace,
required this.subConfig,
});
}

class DummyTypedExtras extends TypedExtras {
final String id;
final Map<String, dynamic> config;
final Config config;
final List<FileType> fileTypes;
final Set<String> sources;
final bool shouldProceed;
Expand Down Expand Up @@ -52,12 +66,12 @@ class DummyTypedExtras extends TypedExtras {
abstract class TypedExtrasTest {
@DummyTypedExtras(
id: '1234',
config: {
'date': '24-10-2024',
'type': 'analytics',
'shouldReplace': true,
'subConfig': {'date': '24-11-2025'},
},
config: Config(
date: '24-10-2024',
type: 'analytics',
shouldReplace: true,
subConfig: {'date': '24-11-2025'},
),
fileTypes: [
FileType.mp3,
FileType.mp4,
Expand Down Expand Up @@ -114,12 +128,12 @@ class AnotherDummyTypedExtras extends TypedExtras {
abstract class MultipleTypedExtrasTest {
@DummyTypedExtras(
id: '1234',
config: {
'date': '24-10-2024',
'type': 'analytics',
'shouldReplace': true,
'subConfig': {'date': '24-11-2025'},
},
config: Config(
date: '24-10-2024',
type: 'analytics',
shouldReplace: true,
subConfig: {'date': '24-11-2025'},
),
fileTypes: [
FileType.mp3,
FileType.mp4,
Expand Down

0 comments on commit a5c49fa

Please sign in to comment.