Skip to content

Commit

Permalink
🔖 release v7.2.0 (#581)
Browse files Browse the repository at this point in the history
* 🔖 release v7.2.0

# chopper

## 7.2.0

- #579

# chopper_generator

## 7.2.0

- #579

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: wenchieh <[email protected]>
  • Loading branch information
techouse and lwj1994 authored Mar 8, 2024
1 parent a8cab3f commit 47084cc
Show file tree
Hide file tree
Showing 10 changed files with 284 additions and 46 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Cache Pub hosted dependencies
uses: actions/cache@88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8
uses: actions/cache@ab5e6d0c87105b4c9c2047343972218f562e4319
with:
path: "~/.pub-cache/hosted"
key: "os:ubuntu-latest;pub-cache-hosted;sdk:stable"
Expand All @@ -45,7 +45,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Cache Pub hosted dependencies
uses: actions/cache@88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8
uses: actions/cache@ab5e6d0c87105b4c9c2047343972218f562e4319
with:
path: "~/.pub-cache/hosted"
key: "os:ubuntu-latest;pub-cache-hosted;sdk:stable;packages:chopper-chopper_built_value-chopper_generator;commands:format-analyze"
Expand Down Expand Up @@ -105,7 +105,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Cache Pub hosted dependencies
uses: actions/cache@88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8
uses: actions/cache@ab5e6d0c87105b4c9c2047343972218f562e4319
with:
path: "~/.pub-cache/hosted"
key: "os:ubuntu-latest;pub-cache-hosted;sdk:stable;packages:chopper-chopper_built_value-chopper_generator;commands:test_with_coverage"
Expand Down Expand Up @@ -176,7 +176,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Cache Pub hosted dependencies
uses: actions/cache@88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8
uses: actions/cache@ab5e6d0c87105b4c9c2047343972218f562e4319
with:
path: "~/.pub-cache/hosted"
key: "os:ubuntu-latest;pub-cache-hosted;sdk:stable;packages:chopper-chopper_built_value;commands:test"
Expand Down
4 changes: 4 additions & 0 deletions chopper/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 7.2.0

- Add support for `@FormUrlEncoded` annotation ([#579](https://github.com/lejard-h/chopper/pull/579))

## 7.1.1+1

- Export `ChopperHttpException` in library exports ([#570](https://github.com/lejard-h/chopper/pull/570))
Expand Down
29 changes: 28 additions & 1 deletion chopper/lib/src/annotations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ final class Field {
///
/// ```dart
/// @Post(path: '/something')
/// Future<Response> fetch(@FieldMap List<Map<String, dynamic>> query);
/// Future<Response> fetch(@FieldMap Map<String, dynamic> query);
/// ```
/// {@endtemplate}
@immutable
Expand Down Expand Up @@ -533,6 +533,30 @@ final class PartFileMap {
const PartFileMap();
}

/// {@template FormUrlEncoded}
///
///
/// Denotes that the request body will use form URL encoding. Fields should be declared as parameters
/// and annotated with [Field]/[FieldMap].
///
/// Requests made with this annotation will have application/x-www-form-urlencoded MIME
/// type. Field names and values will be UTF-8 encoded before being URI-encoded in accordance to <a
/// href="https://datatracker.ietf.org/doc/html/rfc3986">RFC-3986</a>.
///
///
/// ```dart
/// @Post(path: '/something')
/// @FormUrlEncoded
/// Future<Response> fetch(@Field("param") String? param);
/// ```
/// {@endtemplate}
@immutable
@Target({TargetKind.method})
final class FormUrlEncoded {
/// {@macro FormUrlEncoded}
const FormUrlEncoded();
}

/// {@macro ChopperApi}
const chopperApi = ChopperApi();

Expand Down Expand Up @@ -595,3 +619,6 @@ const partFile = PartFile();

/// {@macro PartFileMap}
const partFileMap = PartFileMap();

/// {@macro FormUrlEncoded}
const formUrlEncoded = FormUrlEncoded();
2 changes: 1 addition & 1 deletion chopper/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: chopper
description: Chopper is an http client generator using source_gen, inspired by Retrofit
version: 7.1.1+1
version: 7.2.0
documentation: https://hadrien-lejard.gitbook.io/chopper
repository: https://github.com/lejard-h/chopper

Expand Down
4 changes: 4 additions & 0 deletions chopper_generator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 7.2.0

- Add support for `@FormUrlEncoded` annotation ([#579](https://github.com/lejard-h/chopper/pull/579))

## 7.1.1

- Add option to override build_extension via build.yaml ([#562](https://github.com/lejard-h/chopper/pull/562))
Expand Down
94 changes: 81 additions & 13 deletions chopper_generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ final class ChopperGenerator
) {
final ConstantReader? method = _getMethodAnnotation(m);
final bool multipart = _hasAnnotation(m, chopper.Multipart);
final bool formUrlEncoded = _hasAnnotation(m, chopper.FormUrlEncoded);
final ConstantReader? factoryConverter = _getFactoryConverterAnnotation(m);

final Map<String, ConstantReader> body = _getAnnotation(m, chopper.Body);
Expand All @@ -172,7 +173,7 @@ final class ChopperGenerator
final Map<String, ConstantReader> fileFieldMap =
_getAnnotation(m, chopper.PartFileMap);

final Code? headers = _generateHeaders(m, method!);
final Code? headers = _generateHeaders(m, method!, formUrlEncoded);
final Expression url = _generateUrl(
method,
paths,
Expand Down Expand Up @@ -298,33 +299,45 @@ final class ChopperGenerator
bool hasBody = body.isNotEmpty || fields.isNotEmpty;
if (hasBody) {
if (body.isNotEmpty) {
final DartType bodyType = m.parameters
.firstWhere((p) => _typeChecker(chopper.Body).hasAnnotationOf(p))
.type;
final Expression map = (formUrlEncoded &&
_isMap(bodyType) &&
!_isMapStringString(bodyType))
? _generateMapToStringExpression(refer(body.keys.first))
: refer(body.keys.first);
blocks.add(
declareFinal(Vars.body.toString())
.assign(refer(body.keys.first))
.statement,
declareFinal(Vars.body.toString()).assign(map).statement,
);
} else {
blocks.add(
declareFinal(Vars.body.toString())
.assign(_generateMap(fields))
.assign(_generateMap(fields, enableToString: formUrlEncoded))
.statement,
);
}
}

final bool hasFieldMap = fieldMap.isNotEmpty;
if (hasFieldMap) {
final DartType fieldMapType = m.parameters
.firstWhere(
(p) => _typeChecker(chopper.FieldMap).hasAnnotationOf(p))
.type;
final Expression map =
(formUrlEncoded && !_isMapStringString(fieldMapType))
? _generateMapToStringExpression(refer(fieldMap.keys.first))
: refer(fieldMap.keys.first);
if (hasBody) {
blocks.add(
refer(Vars.body.toString()).property('addAll').call(
[refer(fieldMap.keys.first)],
[map],
).statement,
);
} else {
blocks.add(
declareFinal(Vars.body.toString())
.assign(refer(fieldMap.keys.first))
.statement,
declareFinal(Vars.body.toString()).assign(map).statement,
);
}
}
Expand Down Expand Up @@ -466,6 +479,26 @@ final class ChopperGenerator
});
}

static Expression _generateMapToStringExpression(Reference map) {
return map.property('map<String, String>').call([
Method((b) => b
..requiredParameters.add(
Parameter((b) => b..name = 'key'),
)
..requiredParameters.add(
Parameter((b) => b..name = 'value'),
)
..returns = refer('MapEntry', 'dart.core')
..body = refer('MapEntry', 'dart.core')
.newInstance([
refer('key').property('toString').call([]),
refer('value').property('toString').call([]),
])
.returned
.statement).closure
]);
}

static String _factoryForFunction(FunctionTypedElement function) =>
// ignore: deprecated_member_use
function.enclosingElement is ClassElement
Expand Down Expand Up @@ -546,6 +579,32 @@ final class ChopperGenerator
? type.typeArguments.first
: null;

static bool _isMap(DartType type) {
return _typeChecker(Map).isExactlyType(type) ||
_typeChecker(Map).isAssignableFromType(type);
}

static bool _isMapStringString(DartType type) {
if (!_isMap(type)) {
return false;
}
final firsType = type is InterfaceType && type.typeArguments.isNotEmpty
? type.typeArguments.first
: null;
final secondType = type is InterfaceType && type.typeArguments.length > 1
? type.typeArguments[1]
: null;
if (firsType == null || secondType == null) {
return false;
}
return _isString(firsType) && _isString(secondType);
}

static bool _isString(DartType type) {
return _typeChecker(String).isExactlyType(type) ||
_typeChecker(String).isAssignableFromType(type);
}

static bool _isResponse(DartType type) {
final DartType? responseType = _genericOf(type);
if (responseType == null) return false;
Expand Down Expand Up @@ -664,17 +723,20 @@ final class ChopperGenerator
);

static Expression _generateMap(
Map<ParameterElement, ConstantReader> queries,
) =>
Map<ParameterElement, ConstantReader> queries, {
bool enableToString = false,
}) =>
literalMap(
{
for (final MapEntry<ParameterElement, ConstantReader> query
in queries.entries)
query.value.peek('name')?.stringValue ?? query.key.displayName:
refer(query.key.displayName),
enableToString
? refer(query.key.displayName).property('toString').call([])
: refer(query.key.displayName),
},
refer('String'),
refer('dynamic'),
refer(enableToString ? 'String' : 'dynamic'),
);

static Expression _generateList(
Expand Down Expand Up @@ -718,6 +780,7 @@ final class ChopperGenerator
static Code? _generateHeaders(
MethodElement methodElement,
ConstantReader method,
bool formUrlEncoded,
) {
final StringBuffer codeBuffer = StringBuffer('')..writeln('{');

Expand Down Expand Up @@ -756,6 +819,11 @@ final class ChopperGenerator
}
});

if (formUrlEncoded) {
codeBuffer
.writeln("'content-type': 'application/x-www-form-urlencoded',");
}

codeBuffer.writeln('}');
final String code = codeBuffer.toString();

Expand Down
4 changes: 2 additions & 2 deletions chopper_generator/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: chopper_generator
description: Chopper is an http client generator using source_gen, inspired by Retrofit
version: 7.1.1
version: 7.2.0
documentation: https://hadrien-lejard.gitbook.io/chopper
repository: https://github.com/lejard-h/chopper

Expand All @@ -11,7 +11,7 @@ dependencies:
analyzer: ">=5.13.0 <7.0.0"
build: ^2.4.1
built_collection: ^5.1.1
chopper: ^7.1.0
chopper: ^7.2.0
code_builder: ^4.5.0
dart_style: ^2.3.2
logging: ^1.2.0
Expand Down
Loading

0 comments on commit 47084cc

Please sign in to comment.