Skip to content

Commit

Permalink
Feat: Support passing in enums into TypedExtras (#708)
Browse files Browse the repository at this point in the history
* Feat: Allow passing in enums into TypedExtras

* update version and changelog
  • Loading branch information
farmery authored Sep 11, 2024
1 parent 89941db commit 6d61839
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
16 changes: 16 additions & 0 deletions generator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# Changelog

## 9.1.2

- Support passing Enums into `TypedExtras`.

Example :

```dart
@TypedExtrasSubClass(
id: 'abcd',
fileType: FileType.json,
destinations: [Destination.remote]
)
@http.POST('/path/')
Future<String> myMethod();
```

## 9.1.0

- Added `@TypedExtras` to pass extra options to dio requests using custom annotations.
Expand Down
9 changes: 9 additions & 0 deletions generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2276,6 +2276,9 @@ ${bodyName.displayName} == null
if (value?.isDouble ?? false) return value?.doubleValue;
if (value?.isInt ?? false) return value?.intValue;
if (value?.isString ?? false) return value?.stringValue;
if (value?.objectValue.isEnum ?? false) {
return value?.objectValue.variable?.displayName;
}
if (value?.isList ?? false) {
return value?.listValue
.map((item) => _getFieldValue(ConstantReader(item)))
Expand Down Expand Up @@ -2624,6 +2627,12 @@ extension DartTypeExt on DartType {
}
}

extension DartObjectX on DartObject? {
bool get isEnum {
return this?.type?.element?.kind.name == 'ENUM';
}
}

extension ReferenceExt on Reference {
Reference asNoNull() => refer('$symbol!');

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.1
version: 9.1.2
environment:
sdk: '>=3.3.0 <4.0.0'

Expand Down
12 changes: 5 additions & 7 deletions generator/test/src/generator_test_src.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import 'package:source_gen_test/annotations.dart';

import 'query.pb.dart';

enum FileType {mp4, mp3}
class DummyTypedExtras extends TypedExtras {
final String id;
final Map<String, dynamic> config;
final List<String> fileTypes;
final List<FileType> fileTypes;
final Set<String> sources;
final bool shouldProceed;
final bool? canFly;

const DummyTypedExtras({
required this.id,
required this.config,
Expand All @@ -35,9 +35,8 @@ class DummyTypedExtras extends TypedExtras {
'subConfig': {'date': '24-11-2025'},
},
'fileTypes': [
'mp4',
'mp3',
'mkv',
'mp4',
],
'sources': {
'internet',
Expand All @@ -59,9 +58,8 @@ abstract class TypedExtrasTest {
'subConfig': {'date': '24-11-2025'},
},
fileTypes: [
'mp4',
'mp3',
'mkv',
FileType.mp3,
FileType.mp4,
],
sources: {
'internet',
Expand Down

0 comments on commit 6d61839

Please sign in to comment.