Skip to content

Commit

Permalink
refactor(dynamite)!: do not double escape names
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolas Rimikis <leptopoda@users.noreply.github.com>
  • Loading branch information
Leptopoda committed Jan 28, 2024
1 parent ea395bc commit c4df466
Show file tree
Hide file tree
Showing 16 changed files with 250 additions and 185 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ Iterable<String> deserializeProperty(
final result = resolveType(
spec,
state,
'${identifier}_${toDartName(propertyName, uppercaseFirstCharacter: true)}',
toDartName(
propertyName,
identifier: identifier,
),
propertySchema,
nullable: isDartParameterNullable(schema.required.contains(propertyName), propertySchema),
);
Expand Down Expand Up @@ -174,7 +177,10 @@ Iterable<String> serializePropertyNullable(
final result = resolveType(
spec,
state,
'${identifier}_${toDartName(propertyName, uppercaseFirstCharacter: true)}',
toDartName(
propertyName,
identifier: identifier,
),
propertySchema,
nullable: isDartParameterNullable(schema.required.contains(propertyName), propertySchema),
);
Expand Down Expand Up @@ -206,7 +212,10 @@ Iterable<String> serializeProperty(
final result = resolveType(
spec,
state,
'${identifier}_${toDartName(propertyName, uppercaseFirstCharacter: true)}',
toDartName(
propertyName,
identifier: identifier,
),
propertySchema,
nullable: isDartParameterNullable(schema.required.contains(propertyName), propertySchema),
);
Expand Down
10 changes: 5 additions & 5 deletions packages/dynamite/dynamite/lib/src/builder/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ Iterable<Method> buildTags(
state,
toDartName(
'$operationName-${parameter.name}',
uppercaseFirstCharacter: true,
className: true,
),
parameter.schema!,
nullable: !parameterRequired,
Expand Down Expand Up @@ -319,10 +319,10 @@ Iterable<Method> buildTags(
if (response.headers != null) {
final identifierBuilder = StringBuffer();
if (tag != null) {
identifierBuilder.write(toDartName(tag, uppercaseFirstCharacter: true));
identifierBuilder.write(toDartName(tag, className: true));
}
identifierBuilder
..write(toDartName(operationName, uppercaseFirstCharacter: true))
..write(toDartName(operationName, className: true))
..write('Headers');
headersType = resolveObject(
spec,
Expand Down Expand Up @@ -356,7 +356,7 @@ Iterable<Method> buildTags(
response,
spec,
state,
toDartName(identifierBuilder.toString(), uppercaseFirstCharacter: true),
toDartName(identifierBuilder.toString(), className: true),
);

if (!hasUriParameters) {
Expand Down Expand Up @@ -581,7 +581,7 @@ final authentication = $client.authentications?.firstWhereOrNull(
final securityScheme = spec.components!.securitySchemes![requirement.keys.single]!;
final dynamiteAuth = toDartName(
'Dynamite-${securityScheme.fullName.join('-')}-Authentication',
uppercaseFirstCharacter: true,
className: true,
);
return refer(dynamiteAuth, 'package:dynamite_runtime/http_client.dart')
.newInstance(const [])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Iterable<Spec> generateSchemas(
) sync* {
if (spec.components?.schemas != null) {
for (final schema in spec.components!.schemas!.entries) {
final identifier = toDartName(schema.key, uppercaseFirstCharacter: true);
final identifier = toDartName(schema.key, className: true);
final result = resolveType(
spec,
state,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ TypeResult? resolveMimeTypeDecode(
final result = resolveType(
spec,
state,
toDartName('$identifier-$mimeType', uppercaseFirstCharacter: true),
toDartName('$identifier-$mimeType', className: true),
mediaType.schema!,
);

Expand Down Expand Up @@ -67,7 +67,7 @@ Iterable<String> resolveMimeTypeEncode(
final result = resolveType(
spec,
state,
toDartName('$identifier-request-$mimeType', uppercaseFirstCharacter: true),
toDartName('$identifier-request-$mimeType', className: true),
mediaType.schema!,
nullable: dartParameterNullable,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ TypeResultObject resolveObject(
var result = resolveType(
spec,
state,
'${identifier}_${toDartName(propertyName, uppercaseFirstCharacter: true)}',
toDartName(
propertyName,
identifier: identifier,
),
propertySchema,
nullable: isDartParameterNullable(
schema.required.contains(propertyName),
Expand Down
5 changes: 4 additions & 1 deletion packages/dynamite/dynamite/lib/src/builder/resolve_ofs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ TypeResult resolveAllOf(
final result = resolveType(
spec,
state,
'${identifier}_${toDartName(propertyName, uppercaseFirstCharacter: true)}',
toDartName(
propertyName,
identifier: identifier,
),
propertySchema,
nullable: isDartParameterNullable(
s.required.contains(propertyName),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ TypeResult resolveType(
final subResult = resolveType(
spec,
state,
toDartName(name, uppercaseFirstCharacter: true),
toDartName(name, className: true),
spec.components!.schemas![name]!,
nullable: nullable,
);
Expand Down
59 changes: 56 additions & 3 deletions packages/dynamite/dynamite/lib/src/helpers/dart_helpers.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,59 @@
/// Converts the provided [name] into a `lowerCamelCase` alphanumerical
/// representation.
///
/// If the resulting name is reserved by dart or only contains numbers it is
/// prefixed with a `$`.
/// Specify [className] to return a `UpperCamelCase` name.
///
/// The optional [identifier] will be prefixed and separated by an `_` if
/// provided. The reserved name check will be made against the resulting
/// string. The identifier itself is not checked to be a valid dart name, nor
/// will `className` have any affect.
///
/// ```dart
/// print(toDartName(
/// 'test-variable-name',
/// )); // testVariableName
///
/// print(toDartName(
/// 'test-variable-name',
/// className: true,
/// )); // TestVariableName
///
/// print(toDartName(
/// '10',
/// )); // $10
///
/// print(toDartName(
/// 'String',
/// )); // string
///
/// print(toDartName(
/// 'String',
/// className: true,
/// )); // $String
///
/// print(toDartName(
/// 'string',
/// identifier: 'Identifier'
/// )); // Identifier_String
///
/// print(toDartName(
/// 'string',
/// identifier: 'Identifier'
/// className: false,
/// )); // Identifier_String
/// ```
String toDartName(
String name, {
bool uppercaseFirstCharacter = false,
String? identifier,
bool className = false,
}) {
final capitalize = identifier != null || className;

var result = '';
var upperCase = uppercaseFirstCharacter;
var firstCharacter = !uppercaseFirstCharacter;
var upperCase = capitalize;
var firstCharacter = !capitalize;
for (final char in name.split('')) {
if (_isNonAlphaNumericString(char)) {
upperCase = true;
Expand All @@ -15,6 +64,10 @@ String toDartName(
}
}

if (identifier != null) {
return '${identifier}_$result';
}

if (_reservedNames.contains(result) || RegExp(r'^[0-9]+$', multiLine: true).hasMatch(result)) {
return '\$$result';
}
Expand Down
2 changes: 1 addition & 1 deletion packages/dynamite/dynamite/lib/src/helpers/dynamite.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ String filterMethodName(String operationId, String tag) {
return output.join('-');
}

String clientName(String tag) => '\$${toDartName(tag, uppercaseFirstCharacter: true)}Client';
String clientName(String tag) => '\$${toDartName(tag, className: true)}Client';

bool isDartParameterNullable(
bool required,
Expand Down
2 changes: 1 addition & 1 deletion packages/dynamite/dynamite/test/dart_helpers_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void main() {

for (final value in values) {
expect(toDartName(value.$1), value.$2);
expect(toDartName(value.$1, uppercaseFirstCharacter: true), value.$3);
expect(toDartName(value.$1, className: true), value.$3);
}
});

Expand Down
44 changes: 22 additions & 22 deletions packages/dynamite/dynamite_end_to_end_test/lib/enum.openapi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -216,59 +216,59 @@ class _$EnumDynamicSerializer implements PrimitiveSerializer<EnumDynamic> {
_fromWire[serialized]!;
}

class WrappedEnum_$String extends EnumClass {
const WrappedEnum_$String._(super.name);
class WrappedEnum_String extends EnumClass {
const WrappedEnum_String._(super.name);

/// `test`
static const WrappedEnum_$String test = _$wrappedEnum$StringTest;
static const WrappedEnum_String test = _$wrappedEnumStringTest;

/// `default`
@BuiltValueEnumConst(wireName: 'default')
static const WrappedEnum_$String $default = _$wrappedEnum$String$default;
static const WrappedEnum_String $default = _$wrappedEnumString$default;

/// Returns a set with all values this enum contains.
static BuiltSet<WrappedEnum_$String> get values => _$wrappedEnum$StringValues;
static BuiltSet<WrappedEnum_String> get values => _$wrappedEnumStringValues;

/// Returns the enum value associated to the [name].
static WrappedEnum_$String valueOf(String name) => _$valueOfWrappedEnum_$String(name);
static WrappedEnum_String valueOf(String name) => _$valueOfWrappedEnum_String(name);

/// Returns the serialized value of this enum value.
String get value => _$jsonSerializers.serializeWith(serializer, this)! as String;

/// Serializer for WrappedEnum_$String.
/// Serializer for WrappedEnum_String.
@BuiltValueSerializer(custom: true)
static Serializer<WrappedEnum_$String> get serializer => const _$WrappedEnum_$StringSerializer();
static Serializer<WrappedEnum_String> get serializer => const _$WrappedEnum_StringSerializer();
}

class _$WrappedEnum_$StringSerializer implements PrimitiveSerializer<WrappedEnum_$String> {
const _$WrappedEnum_$StringSerializer();
class _$WrappedEnum_StringSerializer implements PrimitiveSerializer<WrappedEnum_String> {
const _$WrappedEnum_StringSerializer();

static const Map<WrappedEnum_$String, Object> _toWire = <WrappedEnum_$String, Object>{
WrappedEnum_$String.test: 'test',
WrappedEnum_$String.$default: 'default',
static const Map<WrappedEnum_String, Object> _toWire = <WrappedEnum_String, Object>{
WrappedEnum_String.test: 'test',
WrappedEnum_String.$default: 'default',
};

static const Map<Object, WrappedEnum_$String> _fromWire = <Object, WrappedEnum_$String>{
'test': WrappedEnum_$String.test,
'default': WrappedEnum_$String.$default,
static const Map<Object, WrappedEnum_String> _fromWire = <Object, WrappedEnum_String>{
'test': WrappedEnum_String.test,
'default': WrappedEnum_String.$default,
};

@override
Iterable<Type> get types => const [WrappedEnum_$String];
Iterable<Type> get types => const [WrappedEnum_String];

@override
String get wireName => r'WrappedEnum_$String';
String get wireName => 'WrappedEnum_String';

@override
Object serialize(
Serializers serializers,
WrappedEnum_$String object, {
WrappedEnum_String object, {
FullType specifiedType = FullType.unspecified,
}) =>
_toWire[object]!;

@override
WrappedEnum_$String deserialize(
WrappedEnum_String deserialize(
Serializers serializers,
Object serialized, {
FullType specifiedType = FullType.unspecified,
Expand Down Expand Up @@ -346,7 +346,7 @@ class _$WrappedEnum_IntegerSerializer implements PrimitiveSerializer<WrappedEnum
@BuiltValue(instantiable: false)
abstract interface class $WrappedEnumInterface {
@BuiltValueField(wireName: 'String')
WrappedEnum_$String get string;
WrappedEnum_String get string;
WrappedEnum_Integer get integer;
}

Expand Down Expand Up @@ -408,7 +408,7 @@ final Serializers _$serializers = (Serializers().toBuilder()
..add(EnumDynamic.serializer)
..addBuilderFactory(const FullType(WrappedEnum), WrappedEnumBuilder.new)
..add(WrappedEnum.serializer)
..add(WrappedEnum_$String.serializer)
..add(WrappedEnum_String.serializer)
..add(WrappedEnum_Integer.serializer)
..addBuilderFactory(const FullType(EnumReference), EnumReferenceBuilder.new)
..add(EnumReference.serializer))
Expand Down
Loading

0 comments on commit c4df466

Please sign in to comment.