Skip to content

Commit

Permalink
fix(dynamite): handle dart type names
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolas Rimikis <[email protected]>
  • Loading branch information
Leptopoda committed Dec 13, 2023
1 parent c54bce9 commit 9f538f4
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 6 deletions.
15 changes: 13 additions & 2 deletions packages/dynamite/dynamite/lib/src/helpers/dart_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ String toDartName(
}
}

if (_dartKeywords.contains(result) || RegExp(r'^[0-9]+$', multiLine: true).hasMatch(result)) {
if (_reservedNames.contains(result) || RegExp(r'^[0-9]+$', multiLine: true).hasMatch(result)) {
return '\$$result';
}

return result;
}

final _dartKeywords = [
/// A list of dart keywords and type names that need to be escaped.
const _reservedNames = [
'abstract',
'as',
'assert',
'async',
'bool',
'break',
'case',
'catch',
Expand All @@ -37,6 +39,7 @@ final _dartKeywords = [
'default',
'deferred',
'do',
'double',
'dynamic',
'else',
'enum',
Expand All @@ -56,12 +59,17 @@ final _dartKeywords = [
'implements',
'import',
'in',
'int',
'interface',
'is',
'library',
'List',
'Map',
'mixin',
'new',
'null',
'num',
'Object',
'on',
'operator',
'part',
Expand All @@ -70,6 +78,7 @@ final _dartKeywords = [
'set',
'show',
'static',
'String',
'super',
'switch',
'sync',
Expand All @@ -78,6 +87,8 @@ final _dartKeywords = [
'true',
'try',
'typedef',
'Uint8List',
'Uri',
'var',
'void',
'while',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ abstract class TypeResultSomeOf extends TypeResult {
late final String typeName = _typeName;

String get _typeName {
final buffer = StringBuffer(r'$');
final buffer = StringBuffer();
for (final type in optimizedSubTypes) {
buffer.write(toDartName(type.className, uppercaseFirstCharacter: true));
buffer.write(type.className.capitalize());
}

return buffer.toString();
return '\$${toDartName(buffer.toString(), uppercaseFirstCharacter: true)}';
}

BuiltList<TypeResult> get _optimizedSubTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:basics/basics.dart';
import 'package:built_collection/built_collection.dart';
import 'package:dynamite/src/helpers/dart_helpers.dart';
import 'package:meta/meta.dart';
Expand Down
1 change: 1 addition & 0 deletions packages/dynamite/dynamite/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ environment:
sdk: '>=3.1.0 <4.0.0'

dependencies:
basics: ^0.10.0
build: ^2.0.0
built_collection: ^5.0.0
built_value: ^8.0.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ class Client extends DynamiteClient {
/// Parameters:
/// * [contentString]
/// * [contentParameter]
/// * [array]
/// * [$bool]
/// * [string]
/// * [stringBinary]
/// * [$int]
/// * [$double]
/// * [$num]
///
/// Status codes:
/// * 200
Expand All @@ -49,10 +56,24 @@ class Client extends DynamiteClient {
Future<DynamiteResponse<JsonObject, void>> $get({
final ContentString<BuiltMap<String, JsonObject>>? contentString,
final ContentString<BuiltMap<String, JsonObject>>? contentParameter,
final List<JsonObject>? array,
final bool? $bool,
final String? string,
final Uint8List? stringBinary,
final int? $int,
final double? $double,
final num? $num,
}) async {
final rawResponse = $getRaw(
contentString: contentString,
contentParameter: contentParameter,
array: array,
$bool: $bool,
string: string,
stringBinary: stringBinary,
$int: $int,
$double: $double,
$num: $num,
);

return rawResponse.future;
Expand All @@ -66,6 +87,13 @@ class Client extends DynamiteClient {
/// Parameters:
/// * [contentString]
/// * [contentParameter]
/// * [array]
/// * [$bool]
/// * [string]
/// * [stringBinary]
/// * [$int]
/// * [$double]
/// * [$num]
///
/// Status codes:
/// * 200
Expand All @@ -76,6 +104,13 @@ class Client extends DynamiteClient {
DynamiteRawResponse<JsonObject, void> $getRaw({
final ContentString<BuiltMap<String, JsonObject>>? contentString,
final ContentString<BuiltMap<String, JsonObject>>? contentParameter,
final List<JsonObject>? array,
final bool? $bool,
final String? string,
final Uint8List? stringBinary,
final int? $int,
final double? $double,
final num? $num,
}) {
final pathParameters = <String, dynamic>{};
final queryParameters = <String, dynamic>{};
Expand All @@ -100,6 +135,27 @@ class Client extends DynamiteClient {
]),
);
}
if (array != null) {
queryParameters['array'] = array.map((final e) => e.toString());
}
if ($bool != null) {
queryParameters['bool'] = $bool.toString();
}
if (string != null) {
queryParameters['string'] = string;
}
if (stringBinary != null) {
queryParameters['string_binary'] = stringBinary.toString();
}
if ($int != null) {
queryParameters['int'] = $int.toString();
}
if ($double != null) {
queryParameters['double'] = $double.toString();
}
if ($num != null) {
queryParameters['num'] = $num.toString();
}
var uri = Uri.parse(UriTemplate('/').expand(pathParameters));
if (queryParameters.isNotEmpty) {
uri = uri.replace(queryParameters: queryParameters);
Expand Down Expand Up @@ -188,7 +244,8 @@ final Serializers serializers = (Serializers().toBuilder()
]),
ContentStringBuilder<BuiltMap<String, JsonObject>>.new,
)
..add(ContentString.serializer))
..add(ContentString.serializer)
..addBuilderFactory(const FullType(BuiltList, [FullType(JsonObject)]), ListBuilder<JsonObject>.new))
.build();

@visibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,57 @@
}
}
}
},
{
"name": "array",
"in": "query",
"schema": {
"type": "array"
}
},
{
"name": "bool",
"in": "query",
"schema": {
"type": "boolean"
}
},
{
"name": "string",
"in": "query",
"schema": {
"type": "string"
}
},
{
"name": "string_binary",
"in": "query",
"schema": {
"type": "string",
"format": "binary"
}
},
{
"name": "int",
"in": "query",
"schema": {
"type": "integer"
}
},
{
"name": "double",
"in": "query",
"schema": {
"type": "number",
"format": "float"
}
},
{
"name": "num",
"in": "query",
"schema": {
"type": "number"
}
}
],
"responses": {
Expand Down
12 changes: 12 additions & 0 deletions packages/dynamite/dynamite_end_to_end_test/lib/types.openapi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ class Client extends DynamiteClient {
);
}

typedef $Object = dynamic;

typedef $String = dynamic;

typedef $Uri = dynamic;

typedef $Uint8List = dynamic;

typedef $List = dynamic;

typedef $Map = dynamic;

@BuiltValue(instantiable: false)
abstract interface class $BaseInterface {
@BuiltValueField(wireName: 'bool')
Expand Down
18 changes: 18 additions & 0 deletions packages/dynamite/dynamite_end_to_end_test/lib/types.openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@
}
}
}
},
"Object": {
"type": "object"
},
"String": {
"type": "object"
},
"Uri": {
"type": "object"
},
"Uint8List": {
"type": "object"
},
"List": {
"type": "object"
},
"Map": {
"type": "object"
}
}
},
Expand Down

0 comments on commit 9f538f4

Please sign in to comment.