Skip to content

Commit

Permalink
Add @FiledMap @PartMap @PartFileMap (lejard-h#335)
Browse files Browse the repository at this point in the history
Co-authored-by: Meysam Karimi <[email protected]>
  • Loading branch information
meysam1717 and mysmartapply authored May 1, 2022
1 parent 976d457 commit cc2da20
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
38 changes: 38 additions & 0 deletions chopper/lib/src/annotations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,18 @@ class Field {
const Field([this.name]);
}

/// Provides field parameters of a request as [Map<String, dynamic>].
///
/// ```dart
/// @Post(path: '/something')
/// Future<Response> fetch(@FieldMap List<Map<String, dynamic>> query);
/// ```
///
@immutable
class FieldMap {
const FieldMap();
}

/// Defines a multipart request.
///
/// ```dart
Expand All @@ -368,6 +380,19 @@ class Part {
const Part([this.name]);
}

/// Provides part parameters of a request as [PartValue].
///
/// ```dart
/// @Post(path: '/something')
/// @Multipart
/// Future<Response> fetch(@PartMap() List<PartValue> query);
/// ```
///
@immutable
class PartMap {
const PartMap();
}

/// Use [PartFile] to define a file field for a [Multipart] request.
///
/// ```
Expand All @@ -387,5 +412,18 @@ class PartFile {
const PartFile([this.name]);
}

/// Provides partFile parameters of a request as [PartValueFile].
///
/// ```dart
/// @Post(path: '/something')
/// @Multipart
/// Future<Response> fetch(@PartFileMap() List<PartValueFile> query);
/// ```
///
@immutable
class PartFileMap {
const PartFileMap();
}

const multipart = Multipart();
const body = Body();
50 changes: 48 additions & 2 deletions chopper_generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,11 @@ class ChopperGenerator extends GeneratorForAnnotation<chopper.ChopperApi> {
final queries = _getAnnotations(m, chopper.Query);
final queryMap = _getAnnotation(m, chopper.QueryMap);
final fields = _getAnnotations(m, chopper.Field);
final fieldMap = _getAnnotation(m, chopper.FieldMap);
final parts = _getAnnotations(m, chopper.Part);
final partMap = _getAnnotation(m, chopper.PartMap);
final fileFields = _getAnnotations(m, chopper.PartFile);
final fileFieldMap = _getAnnotation(m, chopper.PartFileMap);

final headers = _generateHeaders(m, method!);
final url = _generateUrl(method, paths, baseUrl);
Expand Down Expand Up @@ -190,7 +193,7 @@ class ChopperGenerator extends GeneratorForAnnotation<chopper.ChopperApi> {
final methodOptionalBody = getMethodOptionalBody(method);
final methodName = getMethodName(method);
final methodUrl = getMethodPath(method);
final hasBody = body.isNotEmpty || fields.isNotEmpty;
var hasBody = body.isNotEmpty || fields.isNotEmpty;
if (hasBody) {
if (body.isNotEmpty) {
blocks.add(
Expand All @@ -203,13 +206,56 @@ class ChopperGenerator extends GeneratorForAnnotation<chopper.ChopperApi> {
}
}

final hasParts =
final hasFieldMap = fieldMap.isNotEmpty;
if (hasFieldMap) {
if (hasBody) {
blocks.add(refer('$_bodyVar.addAll').call(
[refer(fieldMap.keys.first)],
).statement);
} else {
blocks.add(
refer(fieldMap.keys.first).assignFinal(_bodyVar).statement,
);
}
}

hasBody = hasBody || hasFieldMap;

var hasParts =
multipart == true && (parts.isNotEmpty || fileFields.isNotEmpty);
if (hasParts) {
blocks.add(
_generateList(parts, fileFields).assignFinal(_partsVar).statement);
}

final hasPartMap = multipart == true && partMap.isNotEmpty;
if (hasPartMap) {
if (hasParts) {
blocks.add(refer('$_partsVar.addAll').call(
[refer(partMap.keys.first)],
).statement);
} else {
blocks.add(
refer(partMap.keys.first).assignFinal(_partsVar).statement,
);
}
}

final hasFileFilesMap = multipart == true && fileFieldMap.isNotEmpty;
if (hasFileFilesMap) {
if (hasParts || hasPartMap) {
blocks.add(refer('$_partsVar.addAll').call(
[refer(fileFieldMap.keys.first)],
).statement);
} else {
blocks.add(
refer(fileFieldMap.keys.first).assignFinal(_partsVar).statement,
);
}
}

hasParts = hasParts || hasPartMap || hasFileFilesMap;

if (!methodOptionalBody && !hasBody && !hasParts) {
_logger.warning(
'$methodName $methodUrl\n'
Expand Down

0 comments on commit cc2da20

Please sign in to comment.