From 4cd65a0d0161b8f55262d28a613273fda17cf697 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Sun, 24 Sep 2023 19:54:41 +0200 Subject: [PATCH] feat(dynamite,nextcloud): use serializer for serializing base types Signed-off-by: Nikolas Rimikis --- .../lib/src/models/type_result/base.dart | 42 ++++++++----------- .../src/models/type_result/type_result.dart | 2 +- .../nextcloud/lib/src/api/core.openapi.dart | 29 ++++++++----- .../lib/src/api/files_sharing.openapi.dart | 12 +++--- .../lib/src/api/provisioning_api.openapi.dart | 14 +++---- .../lib/src/api/user_status.openapi.dart | 4 +- 6 files changed, 52 insertions(+), 51 deletions(-) diff --git a/packages/dynamite/dynamite/lib/src/models/type_result/base.dart b/packages/dynamite/dynamite/lib/src/models/type_result/base.dart index fe9df7f2..aa58e38c 100644 --- a/packages/dynamite/dynamite/lib/src/models/type_result/base.dart +++ b/packages/dynamite/dynamite/lib/src/models/type_result/base.dart @@ -21,37 +21,29 @@ class TypeResultBase extends TypeResult { final String object, { final bool onlyChildren = false, final String? mimeType, - }) => - name == 'String' ? object : '$object.toString()'; - - @override - String deserialize(final String object, {final bool toBuilder = false}) => '($object as $nullableName)'; - - @override - String decode(final String object) { - switch (name) { - case 'String': - return '($object as String)'; - case 'int': - return 'int.parse($object as String)'; - case 'bool': - return "($object as String == 'true')"; - case 'JsonObject': - return 'JsonObject($object)'; + }) { + switch (mimeType) { + case null: + case 'application/json': + case 'application/x-www-form-urlencoded': + if (className == 'String') { + return object; + } else { + return '$object.toString()'; + } + case 'application/octet-stream': + return 'utf8.encode($object) as Uint8List'; default: - throw Exception('Can not decode "$name" from String'); + throw Exception('Can not encode mime type "$mimeType"'); } } @override TypeResultBase get dartType { - final String dartName; - switch (name) { - case 'JsonObject': - dartName = 'dynamic'; - default: - dartName = name; - } + final dartName = switch (name) { + 'JsonObject' => 'dynamic', + _ => name, + }; return TypeResultBase(dartName, nullable: nullable); } diff --git a/packages/dynamite/dynamite/lib/src/models/type_result/type_result.dart b/packages/dynamite/dynamite/lib/src/models/type_result/type_result.dart index 7067ddfc..db2c66ef 100644 --- a/packages/dynamite/dynamite/lib/src/models/type_result/type_result.dart +++ b/packages/dynamite/dynamite/lib/src/models/type_result/type_result.dart @@ -8,7 +8,7 @@ part 'map.dart'; part 'object.dart'; @immutable -abstract class TypeResult { +sealed class TypeResult { TypeResult( this.className, { this.generics = const [], diff --git a/packages/nextcloud/lib/src/api/core.openapi.dart b/packages/nextcloud/lib/src/api/core.openapi.dart index 8b2086fd..85b655c8 100644 --- a/packages/nextcloud/lib/src/api/core.openapi.dart +++ b/packages/nextcloud/lib/src/api/core.openapi.dart @@ -1,5 +1,6 @@ // ignore_for_file: camel_case_types // ignore_for_file: public_member_api_docs +import 'dart:convert'; import 'dart:typed_data'; import 'package:built_collection/built_collection.dart'; @@ -2111,7 +2112,10 @@ class CoreWipeClient { body, ); if (response.statusCode == 200 || response.statusCode == 404) { - return JsonObject(await response.body); + return _jsonSerializers.deserialize( + json.decode(await response.body), + specifiedType: const FullType(JsonObject), + )! as JsonObject; } throw await CoreApiException.fromResponse(response); // coverage:ignore-line } @@ -2630,7 +2634,8 @@ class _$CoreAvatarAvatarGetAvatarDarkHeadersSerializer final value = iterator.current! as String; switch (key) { case 'x-nc-iscustomavatar': - result.xNcIscustomavatar = int.parse(value); + result.xNcIscustomavatar = + _jsonSerializers.deserialize(json.decode(value), specifiedType: const FullType(int))! as int; } } @@ -2703,7 +2708,8 @@ class _$CoreAvatarAvatarGetAvatarHeadersSerializer implements StructuredSerializ final value = iterator.current! as String; switch (key) { case 'x-nc-iscustomavatar': - result.xNcIscustomavatar = int.parse(value); + result.xNcIscustomavatar = + _jsonSerializers.deserialize(json.decode(value), specifiedType: const FullType(int))! as int; } } @@ -3587,10 +3593,10 @@ class _$CoreNavigationEntry_OrderSerializer implements PrimitiveSerializer x != null).isNotEmpty, 'Need oneOf for ${result._data}'); return result.build(); @@ -3837,7 +3843,10 @@ class _$CoreOcmOcmDiscoveryHeadersSerializer implements StructuredSerializer x != null).isNotEmpty, 'Need oneOf for ${result._data}'); return result.build(); @@ -7569,10 +7578,10 @@ class _$CoreUnifiedSearchResult_CursorSerializer implements PrimitiveSerializer< }) { final result = CoreUnifiedSearchResult_CursorBuilder()..data = JsonObject(data); try { - result._$int = data as int?; + result._$int = _jsonSerializers.deserialize(data, specifiedType: const FullType(int))! as int; } catch (_) {} try { - result._string = data as String?; + result._string = _jsonSerializers.deserialize(data, specifiedType: const FullType(String))! as String; } catch (_) {} assert([result._$int, result._string].where((final x) => x != null).isNotEmpty, 'Need oneOf for ${result._data}'); return result.build(); diff --git a/packages/nextcloud/lib/src/api/files_sharing.openapi.dart b/packages/nextcloud/lib/src/api/files_sharing.openapi.dart index e1c07dfc..9368a709 100644 --- a/packages/nextcloud/lib/src/api/files_sharing.openapi.dart +++ b/packages/nextcloud/lib/src/api/files_sharing.openapi.dart @@ -1876,10 +1876,10 @@ class _$FilesSharingShareInfo_SizeSerializer implements PrimitiveSerializer x != null).isNotEmpty, 'Need oneOf for ${result._data}'); return result.build(); @@ -1970,10 +1970,10 @@ class _$FilesSharingShare_ItemSizeSerializer implements PrimitiveSerializer x != null).isNotEmpty, 'Need oneOf for ${result._data}'); return result.build(); @@ -2739,7 +2739,7 @@ class _$FilesSharingShareesapiSearchShareTypeSerializer }) { final result = FilesSharingShareesapiSearchShareTypeBuilder()..data = JsonObject(data); try { - result._$int = data as int?; + result._$int = _jsonSerializers.deserialize(data, specifiedType: const FullType(int))! as int; } catch (_) {} try { result._builtListInt = (_jsonSerializers.deserialize( @@ -3540,7 +3540,7 @@ class _$FilesSharingShareesapiFindRecommendedShareTypeSerializer }) { final result = FilesSharingShareesapiFindRecommendedShareTypeBuilder()..data = JsonObject(data); try { - result._$int = data as int?; + result._$int = _jsonSerializers.deserialize(data, specifiedType: const FullType(int))! as int; } catch (_) {} try { result._builtListInt = (_jsonSerializers.deserialize( diff --git a/packages/nextcloud/lib/src/api/provisioning_api.openapi.dart b/packages/nextcloud/lib/src/api/provisioning_api.openapi.dart index bb60b0d7..04ac33f2 100644 --- a/packages/nextcloud/lib/src/api/provisioning_api.openapi.dart +++ b/packages/nextcloud/lib/src/api/provisioning_api.openapi.dart @@ -3190,10 +3190,10 @@ class _$ProvisioningApiGroupDetails_UsercountSerializer }) { final result = ProvisioningApiGroupDetails_UsercountBuilder()..data = JsonObject(data); try { - result._$bool = data as bool?; + result._$bool = _jsonSerializers.deserialize(data, specifiedType: const FullType(bool))! as bool; } catch (_) {} try { - result._$int = data as int?; + result._$int = _jsonSerializers.deserialize(data, specifiedType: const FullType(int))! as int; } catch (_) {} assert([result._$bool, result._$int].where((final x) => x != null).isNotEmpty, 'Need oneOf for ${result._data}'); return result.build(); @@ -3250,10 +3250,10 @@ class _$ProvisioningApiGroupDetails_DisabledSerializer }) { final result = ProvisioningApiGroupDetails_DisabledBuilder()..data = JsonObject(data); try { - result._$bool = data as bool?; + result._$bool = _jsonSerializers.deserialize(data, specifiedType: const FullType(bool))! as bool; } catch (_) {} try { - result._$int = data as int?; + result._$int = _jsonSerializers.deserialize(data, specifiedType: const FullType(int))! as int; } catch (_) {} assert([result._$bool, result._$int].where((final x) => x != null).isNotEmpty, 'Need oneOf for ${result._data}'); return result.build(); @@ -3600,13 +3600,13 @@ class _$ProvisioningApiUserDetailsQuota_QuotaSerializer }) { final result = ProvisioningApiUserDetailsQuota_QuotaBuilder()..data = JsonObject(data); try { - result._$num = data as num?; + result._$num = _jsonSerializers.deserialize(data, specifiedType: const FullType(num))! as num; } catch (_) {} try { - result._$int = data as int?; + result._$int = _jsonSerializers.deserialize(data, specifiedType: const FullType(int))! as int; } catch (_) {} try { - result._string = data as String?; + result._string = _jsonSerializers.deserialize(data, specifiedType: const FullType(String))! as String; } catch (_) {} assert( [result._$num, result._$int, result._string].where((final x) => x != null).isNotEmpty, diff --git a/packages/nextcloud/lib/src/api/user_status.openapi.dart b/packages/nextcloud/lib/src/api/user_status.openapi.dart index 96563011..904e89c5 100644 --- a/packages/nextcloud/lib/src/api/user_status.openapi.dart +++ b/packages/nextcloud/lib/src/api/user_status.openapi.dart @@ -766,7 +766,7 @@ class _$UserStatusClearAt_TimeSerializer implements PrimitiveSerializer x != null).isNotEmpty,