From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 394D675047 for ; Wed, 23 Jun 2021 12:05:18 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 2F99E9417 for ; Wed, 23 Jun 2021 12:04:48 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 18CFA929F for ; Wed, 23 Jun 2021 12:04:46 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id E588444320 for ; Wed, 23 Jun 2021 12:04:45 +0200 (CEST) From: Aaron Lauterer To: pve-devel@lists.proxmox.com Date: Wed, 23 Jun 2021 12:04:42 +0200 Message-Id: <20210623100444.1440650-3-a.lauterer@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210623100444.1440650-1-a.lauterer@proxmox.com> References: <20210623100444.1440650-1-a.lauterer@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.727 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pve-devel] [PATCH dart_api_client 2/4] Add int serializer to handle string values X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 23 Jun 2021 10:05:18 -0000 Adding a custom serializer for integers, allows us to handle situations in which the PVE API provides the values in other types. For now, the most likely situation is that the API formats the JSON value as string instead of a number, therefore we try to catch that and parse it as int. Signed-off-by: Aaron Lauterer --- lib/src/models/pve_int_serializer.dart | 25 +++++++++++++++++++++++++ lib/src/models/serializers.dart | 2 ++ 2 files changed, 27 insertions(+) create mode 100644 lib/src/models/pve_int_serializer.dart diff --git a/lib/src/models/pve_int_serializer.dart b/lib/src/models/pve_int_serializer.dart new file mode 100644 index 0000000..8986463 --- /dev/null +++ b/lib/src/models/pve_int_serializer.dart @@ -0,0 +1,25 @@ +import 'package:built_collection/built_collection.dart'; +import 'package:built_value/serializer.dart'; + +class PveIntSerializer implements PrimitiveSerializer { + final bool structured = false; + @override + final Iterable types = BuiltList([int]); + @override + final String wireName = 'String'; + + @override + Object serialize(Serializers serializers, int value, + {FullType specifiedType = FullType.unspecified}) { + return value; + } + + @override + int deserialize(Serializers serializers, Object? serialized, + {FullType specifiedType = FullType.unspecified}) { + if (serialized is String) { + return int.parse(serialized); + } + return serialized as int; + } +} diff --git a/lib/src/models/serializers.dart b/lib/src/models/serializers.dart index 57b06f4..2a0d501 100644 --- a/lib/src/models/serializers.dart +++ b/lib/src/models/serializers.dart @@ -3,6 +3,7 @@ import 'package:built_value/json_object.dart'; import 'package:built_value/serializer.dart'; import 'package:built_value/standard_json_plugin.dart'; import 'package:proxmox_dart_api_client/src/models/pve_bool_serializer.dart'; +import 'package:proxmox_dart_api_client/src/models/pve_int_serializer.dart'; import 'package:proxmox_dart_api_client/src/models/pve_string_serializer.dart'; import 'package:proxmox_dart_api_client/src/models/pve_datetime_from_epoch_serializer.dart'; import 'package:proxmox_dart_api_client/src/models/pve_models.dart'; @@ -42,6 +43,7 @@ part 'serializers.g.dart'; final Serializers serializers = (_$serializers.toBuilder() ..addPlugin(StandardJsonPlugin()) ..add(PveBoolSerializer()) + ..add(PveIntSerializer()) ..add(PveStringSerializer()) ..add(PveDateTimeFromEpoch())) .build(); -- 2.30.2