Skip to content

Commit

Permalink
Release 4.0.6 (#341)
Browse files Browse the repository at this point in the history
Co-authored-by: Ivan Terekhin <[email protected]>
Co-authored-by: István Juhos <[email protected]>
Co-authored-by: Meysam Karimi <[email protected]>
Co-authored-by: Youssef Raafat <[email protected]>
Co-authored-by: luis901101 <[email protected]>
Co-authored-by: melvspace <[email protected]>
Co-authored-by: Michal Šrůtek <[email protected]>
Co-authored-by: Andre <[email protected]>
Co-authored-by: John Wimer <[email protected]>
Co-authored-by: Max Röhrl <[email protected]>
Co-authored-by: ipcjs <[email protected]>
Co-authored-by: ibadin <[email protected]>
Co-authored-by: Meysam Karimi <[email protected]>
  • Loading branch information
13 people authored Jun 22, 2022
1 parent 206f845 commit f672b10
Show file tree
Hide file tree
Showing 22 changed files with 653 additions and 195 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ Please refer to the installation guide at [pub.dev](https://pub.dev/packages/cho

* [json serializable Converter](https://github.com/lejard-h/chopper/blob/master/example/bin/main_json_serializable.dart)
* [built value Converter](https://github.com/lejard-h/chopper/blob/master/example/bin/main_built_value.dart)
* [Angular](https://github.com/lejard-h/chopper/blob/master/example/web/main.dart)

## [Issue Tracker](https://github.com/lejard-h/chopper/issues)

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

## 4.0.6

- FieldMap added
- Example migrated to null safety / Angular removed

## 4.0.5

- Add additional param for the authenticator
Expand Down
1 change: 0 additions & 1 deletion chopper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,5 @@ Latest versions:
* [json_serializable Converter](https://github.com/lejard-h/chopper/blob/master/example/bin/main_json_serializable.dart)
* [built_value Converter](https://github.com/lejard-h/chopper/blob/master/example/bin/main_built_value.dart)
* [Angular](https://github.com/lejard-h/chopper/blob/master/example/web/main.dart)
## If you encounter any issues, or need a feature implemented, please visit [Chopper's Issue Tracker on GitHub](https://github.com/lejard-h/chopper/issues).
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();
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: 4.0.5
version: 4.0.6
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

## 4.0.6

- Analyzer dependency upgrade

## 4.0.5

- Analyzer dependency upgrade
Expand Down
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
4 changes: 2 additions & 2 deletions chopper_generator/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
name: chopper_generator
description: Chopper is an http client generator using source_gen, inspired by Retrofit
version: 4.0.5
version: 4.0.6
documentation: https://hadrien-lejard.gitbook.io/chopper
repository: https://github.com/lejard-h/chopper

environment:
sdk: ">=2.12.0 <3.0.0"

dependencies:
analyzer: ^3.0.0
analyzer: ^4.1.0
build: ^2.0.0
built_collection: ^5.0.0
chopper: ^4.0.0
Expand Down
17 changes: 12 additions & 5 deletions example/bin/main_built_value.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:built_collection/built_collection.dart';
import 'package:built_value/serializer.dart';
import 'package:chopper/chopper.dart';
import 'package:chopper_example/built_value_resource.dart';
import 'package:chopper_example/built_value_serializers.dart';
Expand Down Expand Up @@ -52,16 +53,22 @@ main() async {
}

class BuiltValueConverter extends JsonConverter {
T _deserialize<T>(dynamic value) => jsonSerializers.deserializeWith<T>(
jsonSerializers.serializerForType(T),
value,
);
T? _deserialize<T>(dynamic value) {
final serializer = jsonSerializers.serializerForType(T) as Serializer<T>?;
if (serializer == null) {
throw Exception('No serializer for type ${T}');
}
return jsonSerializers.deserializeWith<T>(
serializer,
value,
);
}

BuiltList<T> _deserializeListOf<T>(Iterable value) => BuiltList(
value.map((value) => _deserialize<T>(value)).toList(growable: false),
);

dynamic _decode<T>(entity) {
dynamic _decode<T>(dynamic entity) {
/// handle case when we want to access to Map<String, dynamic> directly
/// getResource or getMapResource
/// Avoid dynamic or unconverted value, this could lead to several issues
Expand Down
12 changes: 6 additions & 6 deletions example/bin/main_json_serializable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ Future<Request> authHeader(Request request) async => applyHeader(
"42",
);

typedef T JsonFactory<T>(Map<String, dynamic> json);
typedef JsonFactory<T> = T Function(Map<String, dynamic> json);

class JsonSerializableConverter extends JsonConverter {
final Map<Type, JsonFactory> factories;

JsonSerializableConverter(this.factories);
const JsonSerializableConverter(this.factories);

T _decodeMap<T>(Map<String, dynamic> values) {
T? _decodeMap<T>(Map<String, dynamic> values) {
/// Get jsonFactory using Type parameters
/// if not found or invalid, throw error or return null
final jsonFactory = factories[T];
Expand All @@ -79,13 +79,13 @@ class JsonSerializableConverter extends JsonConverter {
return jsonFactory(values);
}

List<T> _decodeList<T>(List values) =>
List<T> _decodeList<T>(Iterable values) =>
values.where((v) => v != null).map<T>((v) => _decode<T>(v)).toList();

dynamic _decode<T>(entity) {
if (entity is Iterable) return _decodeList<T>(entity);
if (entity is Iterable) return _decodeList<T>(entity as List);

if (entity is Map) return _decodeMap<T>(entity);
if (entity is Map) return _decodeMap<T>(entity as Map<String, dynamic>);

return entity;
}
Expand Down
22 changes: 0 additions & 22 deletions example/lib/angular_example.dart

This file was deleted.

18 changes: 12 additions & 6 deletions example/lib/built_value_resource.chopper.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions example/lib/built_value_resource.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ abstract class ResourceError

@ChopperApi(baseUrl: "/resources")
abstract class MyService extends ChopperService {
static MyService create([ChopperClient client]) => _$MyService(client);
static MyService create([ChopperClient? client]) => _$MyService(client);

@Get(path: "/{id}/")
Future<Response> getResource(@Path() String id);
Expand All @@ -46,5 +46,5 @@ abstract class MyService extends ChopperService {

@Post()
Future<Response<Resource>> newResource(@Body() Resource resource,
{@Header() String name});
{@Header() String? name});
}
Loading

0 comments on commit f672b10

Please sign in to comment.