Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ call .toString() on non-String headers #538

Merged
merged 3 commits into from
Dec 30, 2023
Merged

Conversation

techouse
Copy link
Collaborator

@techouse techouse commented Dec 17, 2023

Enable the user to specify non-String type header values by calling .toString() on any non-String Dart type, i.e.

@Get(path: 'headers')
Future<Response<String>> getHeaders({
  @Header('x-string') String stringHeader,

  /// Using any of the these would have thrown a runtime type error before
  @Header('x-boolean') bool? boolHeader,
  @Header('x-int') int? intHeader,
  @Header('x-double') double? doubleHeader,
  @Header('x-enum') ExampleEnum? enumHeader,
});

This will get generated as

@override
Future<Response<String>> getHeaders({
  required String? stringHeader,
  bool? boolHeader,
  int? intHeader,
  double? doubleHeader,
  ExampleEnum? enumHeader,
}) {
  final Uri $url = Uri.parse('/test/headers');
  final Map<String, String> $headers = {
    if (stringHeader != null) 'x-string': stringHeader, // <-- is a `String`, nothing to do here
    if (boolHeader != null) 'x-boolean': boolHeader.toString(), // <-- calls `.toString()` on a `bool`
    if (intHeader != null) 'x-int': intHeader.toString(), // <-- calls `.toString()` on an `int`
    if (doubleHeader != null) 'x-double': doubleHeader.toString(), // <-- calls `.toString()` on a `double`
    if (enumHeader != null) 'x-enum': enumHeader.toString(), // <-- calls `.toString()` on an `enum`
  };
  final Request $request = Request(
    'GET',
    $url,
    client.baseUrl,
    headers: $headers,
  );
  return client.send<String, String>($request);
}

@techouse techouse changed the title ✨ call .toString() on non-string headers ✨ call .toString() on non-String headers Dec 17, 2023
Copy link

codecov bot commented Dec 17, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (33cb73a) 94.02% compared to head (e93426f) 94.02%.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop     #538   +/-   ##
========================================
  Coverage    94.02%   94.02%           
========================================
  Files           10       10           
  Lines          469      469           
========================================
  Hits           441      441           
  Misses          28       28           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@techouse
Copy link
Collaborator Author

@JEuler @Guldem thoughts?

Copy link
Contributor

@Guldem Guldem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice addition! Got 1 minor feedback thingy.

chopper_generator/lib/src/generator.dart Outdated Show resolved Hide resolved
@techouse
Copy link
Collaborator Author

@JEuler bump 😇

Copy link
Collaborator

@JEuler JEuler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, looks cool little addition! Sorry for missing it firstly.

@techouse techouse merged commit e84d077 into develop Dec 30, 2023
6 checks passed
@techouse techouse deleted the feat/dynamic-headers branch December 30, 2023 08:19
techouse added a commit that referenced this pull request Dec 30, 2023
# chopper

## 7.0.10

- Enable the user to specify non-String type header values by calling `.toString()` on any non-String Dart type. ([#538](#538))

# chopper_generator

## 7.0.7

- Enable the user to specify non-String type header values by calling `.toString()` on any non-String Dart type. ([#538](#538))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants