From 677627f27742e74118e7b0b563d1023127a0c140 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Sun, 13 Nov 2022 09:21:16 +0100 Subject: [PATCH] dynamite,nextcloud: Fix names colliding with keywords --- .../dynamite/lib/src/openapi_builder.dart | 14 ++--- .../nextcloud/lib/src/nextcloud.openapi.dart | 54 +++++++++---------- packages/nextcloud/test/user_status_test.dart | 22 ++++---- 3 files changed, 46 insertions(+), 44 deletions(-) diff --git a/packages/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/lib/src/openapi_builder.dart index 633589b1..0fef94c6 100644 --- a/packages/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/lib/src/openapi_builder.dart @@ -260,8 +260,7 @@ class OpenAPIBuilder implements Builder { final fields = {}; for (final result in results) { final dartName = _toDartName(result.typeName); - final fieldName = dartName == result.typeName ? '${dartName}_' : dartName; - fields[result.typeName] = fieldName; + fields[result.typeName] = _toFieldName(dartName, result.typeName); } b @@ -1179,11 +1178,12 @@ String _toDartName( result.write(char); } - if (_dartKeywords.contains(result.toString())) { - return '${result.toString()}_'; + final out = result.toString(); + if (_dartKeywords.contains(out) || RegExp(r'^[0-9]+$', multiLine: true).hasMatch(out)) { + return '\$$out'; } - return result.toString(); + return out; } final _dartKeywords = [ @@ -1250,7 +1250,9 @@ final _dartKeywords = [ bool _isNonAlphaNumericString(final String input) => !RegExp(r'^[a-zA-Z0-9]$').hasMatch(input); -String _makeNullable(final String type, final bool nullable) => nullable ? '$type?' : type; +String _makeNullable(final String type, final bool nullable) => nullable && type != 'dynamic' ? '$type?' : type; + +String _toFieldName(final String dartName, final String type) => dartName == type ? '\$$dartName' : dartName; class TypeResolveResult { TypeResolveResult( diff --git a/packages/nextcloud/lib/src/nextcloud.openapi.dart b/packages/nextcloud/lib/src/nextcloud.openapi.dart index 171a72eb..b3ae73ec 100644 --- a/packages/nextcloud/lib/src/nextcloud.openapi.dart +++ b/packages/nextcloud/lib/src/nextcloud.openapi.dart @@ -1324,30 +1324,30 @@ class CoreServerCapabilities { class CoreNavigationApps_Ocs_Data_Order { CoreNavigationApps_Ocs_Data_Order( this._data, { - this.int_, + this.$int, this.string, }); factory CoreNavigationApps_Ocs_Data_Order.fromJson(dynamic data) { - int? int_; + int? $int; try { - int_ = data as int; + $int = data as int; } catch (_) {} String? string; try { string = data as String; } catch (_) {} - assert([int_, string].where((final x) => x != null).length == 1, 'Need oneOf'); + assert([$int, string].where((final x) => x != null).length == 1, 'Need oneOf'); return CoreNavigationApps_Ocs_Data_Order( data, - int_: int_, + $int: $int, string: string, ); } final dynamic _data; - final int? int_; + final int? $int; final String? string; @@ -3239,7 +3239,7 @@ class UserStatusClearAt_Time { UserStatusClearAt_Time( this._data, { this.userStatusClearAtTime0, - this.int_, + this.$int, }); factory UserStatusClearAt_Time.fromJson(dynamic data) { @@ -3247,15 +3247,15 @@ class UserStatusClearAt_Time { try { userStatusClearAtTime0 = UserStatusClearAt_Time0.fromValue(data as String); } catch (_) {} - int? int_; + int? $int; try { - int_ = data as int; + $int = data as int; } catch (_) {} - assert([userStatusClearAtTime0, int_].where((final x) => x != null).length == 1, 'Need oneOf'); + assert([userStatusClearAtTime0, $int].where((final x) => x != null).length == 1, 'Need oneOf'); return UserStatusClearAt_Time( data, userStatusClearAtTime0: userStatusClearAtTime0, - int_: int_, + $int: $int, ); } @@ -3264,7 +3264,7 @@ class UserStatusClearAt_Time { final UserStatusClearAt_Time0? userStatusClearAtTime0; /// Time offset in seconds - final int? int_; + final int? $int; // coverage:ignore-start dynamic toJson() => _data; @@ -3293,7 +3293,7 @@ class UserStatusPublicUserStatus_ClearAt { UserStatusPublicUserStatus_ClearAt( this._data, { this.userStatusClearAt, - this.int_, + this.$int, }); factory UserStatusPublicUserStatus_ClearAt.fromJson(dynamic data) { @@ -3301,14 +3301,14 @@ class UserStatusPublicUserStatus_ClearAt { try { userStatusClearAt = UserStatusClearAt.fromJson(data as Map); } catch (_) {} - int? int_; + int? $int; try { - int_ = data as int; + $int = data as int; } catch (_) {} return UserStatusPublicUserStatus_ClearAt( data, userStatusClearAt: userStatusClearAt, - int_: int_, + $int: $int, ); } @@ -3317,7 +3317,7 @@ class UserStatusPublicUserStatus_ClearAt { final UserStatusClearAt? userStatusClearAt; /// Time as unix timestamp - final int? int_; + final int? $int; // coverage:ignore-start dynamic toJson() => _data; @@ -3473,7 +3473,7 @@ class UserStatus_ClearAt { UserStatus_ClearAt( this._data, { this.userStatusClearAt, - this.int_, + this.$int, }); factory UserStatus_ClearAt.fromJson(dynamic data) { @@ -3481,14 +3481,14 @@ class UserStatus_ClearAt { try { userStatusClearAt = UserStatusClearAt.fromJson(data as Map); } catch (_) {} - int? int_; + int? $int; try { - int_ = data as int; + $int = data as int; } catch (_) {} return UserStatus_ClearAt( data, userStatusClearAt: userStatusClearAt, - int_: int_, + $int: $int, ); } @@ -3497,7 +3497,7 @@ class UserStatus_ClearAt { final UserStatusClearAt? userStatusClearAt; /// Time as unix timestamp - final int? int_; + final int? $int; // coverage:ignore-start dynamic toJson() => _data; @@ -3602,7 +3602,7 @@ class UserStatusPredefinedStatus_ClearAt { UserStatusPredefinedStatus_ClearAt( this._data, { this.userStatusClearAt, - this.int_, + this.$int, }); factory UserStatusPredefinedStatus_ClearAt.fromJson(dynamic data) { @@ -3610,14 +3610,14 @@ class UserStatusPredefinedStatus_ClearAt { try { userStatusClearAt = UserStatusClearAt.fromJson(data as Map); } catch (_) {} - int? int_; + int? $int; try { - int_ = data as int; + $int = data as int; } catch (_) {} return UserStatusPredefinedStatus_ClearAt( data, userStatusClearAt: userStatusClearAt, - int_: int_, + $int: $int, ); } @@ -3626,7 +3626,7 @@ class UserStatusPredefinedStatus_ClearAt { final UserStatusClearAt? userStatusClearAt; /// Time as unix timestamp - final int? int_; + final int? $int; // coverage:ignore-start dynamic toJson() => _data; diff --git a/packages/nextcloud/test/user_status_test.dart b/packages/nextcloud/test/user_status_test.dart index e682939e..ca408931 100644 --- a/packages/nextcloud/test/user_status_test.dart +++ b/packages/nextcloud/test/user_status_test.dart @@ -28,11 +28,11 @@ Future main() async { final meeting = response.ocs.data.singleWhere((final s) => s.id == 'meeting').clearAt.userStatusClearAt!; expect(meeting.type, UserStatusClearAt_Type.period); - expect(meeting.time.int_, 3600); + expect(meeting.time.$int, 3600); final commuting = response.ocs.data.singleWhere((final s) => s.id == 'commuting').clearAt.userStatusClearAt!; expect(commuting.type, UserStatusClearAt_Type.period); - expect(commuting.time.int_, 1800); + expect(commuting.time.$int, 1800); final remoteWork = response.ocs.data.singleWhere((final s) => s.id == 'remote-work').clearAt.userStatusClearAt!; expect(remoteWork.type, UserStatusClearAt_Type.endOf); @@ -44,7 +44,7 @@ Future main() async { final vacationing = response.ocs.data.singleWhere((final s) => s.id == 'vacationing').clearAt; expect(vacationing.userStatusClearAt, null); - expect(vacationing.int_, null); + expect(vacationing.$int, null); }); test('Set status', () async { @@ -56,7 +56,7 @@ Future main() async { expect(response.ocs.data.userStatus!.messageIsPredefined, false); expect(response.ocs.data.userStatus!.icon, null); expect(response.ocs.data.userStatus!.clearAt.userStatusClearAt, null); - expect(response.ocs.data.userStatus!.clearAt.int_, null); + expect(response.ocs.data.userStatus!.clearAt.$int, null); expect(response.ocs.data.userStatus!.status, UserStatusType.online); expect(response.ocs.data.userStatus!.statusIsUserDefined, true); }); @@ -73,7 +73,7 @@ Future main() async { expect(response.ocs.data.userStatus!.messageIsPredefined, false); expect(response.ocs.data.userStatus!.icon, null); expect(response.ocs.data.userStatus!.clearAt.userStatusClearAt, null); - expect(response.ocs.data.userStatus!.clearAt.int_, null); + expect(response.ocs.data.userStatus!.clearAt.$int, null); expect(response.ocs.data.userStatus!.status, UserStatusType.online); expect(response.ocs.data.userStatus!.statusIsUserDefined, true); }); @@ -90,7 +90,7 @@ Future main() async { expect(response.ocs.data[0].message, null); expect(response.ocs.data[0].icon, null); expect(response.ocs.data[0].clearAt.userStatusClearAt, null); - expect(response.ocs.data[0].clearAt.int_, null); + expect(response.ocs.data[0].clearAt.$int, null); expect(response.ocs.data[0].status, UserStatusType.online); }); @@ -103,7 +103,7 @@ Future main() async { expect(response.ocs.data.userStatusPublicUserStatus!.message, null); expect(response.ocs.data.userStatusPublicUserStatus!.icon, null); expect(response.ocs.data.userStatusPublicUserStatus!.clearAt.userStatusClearAt, null); - expect(response.ocs.data.userStatusPublicUserStatus!.clearAt.int_, null); + expect(response.ocs.data.userStatusPublicUserStatus!.clearAt.$int, null); expect(response.ocs.data.userStatusPublicUserStatus!.status, UserStatusType.online); }); @@ -118,7 +118,7 @@ Future main() async { expect(response.ocs.data.userStatus!.messageId, 'meeting'); expect(response.ocs.data.userStatus!.messageIsPredefined, true); expect(response.ocs.data.userStatus!.icon, null); - expect(response.ocs.data.userStatus!.clearAt.int_, clearAt); + expect(response.ocs.data.userStatus!.clearAt.$int, clearAt); expect(response.ocs.data.userStatus!.status, UserStatusType.offline); expect(response.ocs.data.userStatus!.statusIsUserDefined, false); }); @@ -135,7 +135,7 @@ Future main() async { expect(response.ocs.data.userStatus!.messageId, null); expect(response.ocs.data.userStatus!.messageIsPredefined, false); expect(response.ocs.data.userStatus!.icon, '😀'); - expect(response.ocs.data.userStatus!.clearAt.int_, clearAt); + expect(response.ocs.data.userStatus!.clearAt.$int, clearAt); expect(response.ocs.data.userStatus!.status, UserStatusType.offline); expect(response.ocs.data.userStatus!.statusIsUserDefined, false); }); @@ -156,7 +156,7 @@ Future main() async { expect(response.ocs.data.userStatus!.messageIsPredefined, false); expect(response.ocs.data.userStatus!.icon, null); expect(response.ocs.data.userStatus!.clearAt.userStatusClearAt, null); - expect(response.ocs.data.userStatus!.clearAt.int_, null); + expect(response.ocs.data.userStatus!.clearAt.$int, null); expect(response.ocs.data.userStatus!.status, UserStatusType.offline); expect(response.ocs.data.userStatus!.statusIsUserDefined, false); }); @@ -169,7 +169,7 @@ Future main() async { expect(response.messageIsPredefined, false); expect(response.icon, null); expect(response.clearAt.userStatusClearAt, null); - expect(response.clearAt.int_, null); + expect(response.clearAt.$int, null); expect(response.status, UserStatusType.online); expect(response.statusIsUserDefined, false); });