public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pve-devel] [PATCH series/flutter 0/4] fixes related to PVE 7
@ 2021-06-23 10:04 Aaron Lauterer
  2021-06-23 10:04 ` [pve-devel] [PATCH dart_api_client 1/4] Add string serializer to handle int and double values Aaron Lauterer
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Aaron Lauterer @ 2021-06-23 10:04 UTC (permalink / raw)
  To: pve-devel

This series contains a few fixes for problems that I came across when
testing the flutter app against Proxmox VE 7.

The first bigger one, affecting the first 3 patches, is handling of
different types when parsing API responses. The PVE API is not always
returning the expected type. In most situations it is numbers formatted
as strings instead of numbers.

For dynamically types languages this is usually much less of a problem
as it is in statically typed languages as dart.
To handle that, I introduced new custom serializers for Strings,
integers and doubles. If the value is not of the expected type, they
should be able to handle situations where the number is formatted as
string or where the expected string is formatted as number and
cast/parse them accordingly.

The last patch addresses a problem that showed up when trying to open
the power menu for a guest that had been powered off. The handling of
the states template value being null has not been done correctly. I used
the same approach as throughout the other parts where the template
status is checked -> falling back to false.

dart_api_client: Aaron Lauterer (3):
  Add string serializer to handle int and double values
  Add int serializer to handle string values
  Add double serializer to handle String and int values

 lib/src/models/pve_double_serializer.dart | 28 +++++++++++++++++++++++
 lib/src/models/pve_int_serializer.dart    | 25 ++++++++++++++++++++
 lib/src/models/pve_string_serializer.dart | 25 ++++++++++++++++++++
 lib/src/models/serializers.dart           |  6 +++++
 4 files changed, 84 insertions(+)
 create mode 100644 lib/src/models/pve_double_serializer.dart
 create mode 100644 lib/src/models/pve_int_serializer.dart
 create mode 100644 lib/src/models/pve_string_serializer.dart

flutter_frontend: Aaron Lauterer (1):
  power settings: fix handling null for template status

 lib/widgets/pve_qemu_power_settings_widget.dart | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)


-- 
2.30.2





^ permalink raw reply	[flat|nested] 6+ messages in thread

* [pve-devel] [PATCH dart_api_client 1/4] Add string serializer to handle int and double values
  2021-06-23 10:04 [pve-devel] [PATCH series/flutter 0/4] fixes related to PVE 7 Aaron Lauterer
@ 2021-06-23 10:04 ` Aaron Lauterer
  2021-06-23 10:04 ` [pve-devel] [PATCH dart_api_client 2/4] Add int serializer to handle string values Aaron Lauterer
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Aaron Lauterer @ 2021-06-23 10:04 UTC (permalink / raw)
  To: pve-devel

Adding a custom serializer for Strings allows us to catch situations
where the PVE API provides an int or a double instead of the expected
string and convert that to a string.

Signed-off-by: Aaron Lauterer <a.lauterer@proxmox.com>
---
 lib/src/models/pve_string_serializer.dart | 25 +++++++++++++++++++++++
 lib/src/models/serializers.dart           |  2 ++
 2 files changed, 27 insertions(+)
 create mode 100644 lib/src/models/pve_string_serializer.dart

diff --git a/lib/src/models/pve_string_serializer.dart b/lib/src/models/pve_string_serializer.dart
new file mode 100644
index 0000000..0622360
--- /dev/null
+++ b/lib/src/models/pve_string_serializer.dart
@@ -0,0 +1,25 @@
+import 'package:built_collection/built_collection.dart';
+import 'package:built_value/serializer.dart';
+
+class PveStringSerializer implements PrimitiveSerializer<String> {
+  final bool structured = false;
+  @override
+  final Iterable<Type> types = BuiltList<Type>([String]);
+  @override
+  final String wireName = 'String';
+
+  @override
+  Object serialize(Serializers serializers, String string,
+      {FullType specifiedType = FullType.unspecified}) {
+    return string;
+  }
+
+  @override
+  String deserialize(Serializers serializers, Object? serialized,
+      {FullType specifiedType = FullType.unspecified}) {
+    if (serialized is int || serialized is double) {
+      return serialized.toString();
+    }
+    return serialized as String;
+  }
+}
diff --git a/lib/src/models/serializers.dart b/lib/src/models/serializers.dart
index c079ee3..57b06f4 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_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';
 
@@ -41,5 +42,6 @@ part 'serializers.g.dart';
 final Serializers serializers = (_$serializers.toBuilder()
       ..addPlugin(StandardJsonPlugin())
       ..add(PveBoolSerializer())
+      ..add(PveStringSerializer())
       ..add(PveDateTimeFromEpoch()))
     .build();
-- 
2.30.2





^ permalink raw reply	[flat|nested] 6+ messages in thread

* [pve-devel] [PATCH dart_api_client 2/4] Add int serializer to handle string values
  2021-06-23 10:04 [pve-devel] [PATCH series/flutter 0/4] fixes related to PVE 7 Aaron Lauterer
  2021-06-23 10:04 ` [pve-devel] [PATCH dart_api_client 1/4] Add string serializer to handle int and double values Aaron Lauterer
@ 2021-06-23 10:04 ` Aaron Lauterer
  2021-06-23 10:04 ` [pve-devel] [PATCH dart_api_client 3/4] Add double serializer to handle String and int values Aaron Lauterer
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Aaron Lauterer @ 2021-06-23 10:04 UTC (permalink / raw)
  To: pve-devel

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 <a.lauterer@proxmox.com>
---
 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<int> {
+  final bool structured = false;
+  @override
+  final Iterable<Type> types = BuiltList<Type>([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





^ permalink raw reply	[flat|nested] 6+ messages in thread

* [pve-devel] [PATCH dart_api_client 3/4] Add double serializer to handle String and int values
  2021-06-23 10:04 [pve-devel] [PATCH series/flutter 0/4] fixes related to PVE 7 Aaron Lauterer
  2021-06-23 10:04 ` [pve-devel] [PATCH dart_api_client 1/4] Add string serializer to handle int and double values Aaron Lauterer
  2021-06-23 10:04 ` [pve-devel] [PATCH dart_api_client 2/4] Add int serializer to handle string values Aaron Lauterer
@ 2021-06-23 10:04 ` Aaron Lauterer
  2021-06-23 10:04 ` [pve-devel] [PATCH flutter_frontend 4/4] power settings: fix handling null for template status Aaron Lauterer
  2021-06-23 11:36 ` [pve-devel] applied-series: [PATCH series/flutter 0/4] fixes related to PVE 7 Thomas Lamprecht
  4 siblings, 0 replies; 6+ messages in thread
From: Aaron Lauterer @ 2021-06-23 10:04 UTC (permalink / raw)
  To: pve-devel

Adding a custom serializer for double values allows us to handle
situations in which the PVE API provides values in other types. The most
likely possibility is that numbers are formatted as string in the JSON
response which needs to be parsed to double.

We must also handle the situation that the value is an integer which
needs to be cast to double.

Signed-off-by: Aaron Lauterer <a.lauterer@proxmox.com>
---
 lib/src/models/pve_double_serializer.dart | 28 +++++++++++++++++++++++
 lib/src/models/serializers.dart           |  2 ++
 2 files changed, 30 insertions(+)
 create mode 100644 lib/src/models/pve_double_serializer.dart

diff --git a/lib/src/models/pve_double_serializer.dart b/lib/src/models/pve_double_serializer.dart
new file mode 100644
index 0000000..3743a80
--- /dev/null
+++ b/lib/src/models/pve_double_serializer.dart
@@ -0,0 +1,28 @@
+import 'package:built_collection/built_collection.dart';
+import 'package:built_value/serializer.dart';
+
+class PveDoubleSerializer implements PrimitiveSerializer<double> {
+  final bool structured = false;
+  @override
+  final Iterable<Type> types = BuiltList<Type>([double]);
+  @override
+  final String wireName = 'double';
+
+  @override
+  Object serialize(Serializers serializers, double value,
+      {FullType specifiedType = FullType.unspecified}) {
+    return value;
+  }
+
+  @override
+  double deserialize(Serializers serializers, Object? serialized,
+      {FullType specifiedType = FullType.unspecified}) {
+    if (serialized is String) {
+      return double.parse(serialized);
+    }
+    if (serialized is int) {
+      return (serialized).toDouble();
+    }
+    return serialized as double;
+  }
+}
diff --git a/lib/src/models/serializers.dart b/lib/src/models/serializers.dart
index 2a0d501..0be80fc 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_double_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';
@@ -43,6 +44,7 @@ part 'serializers.g.dart';
 final Serializers serializers = (_$serializers.toBuilder()
       ..addPlugin(StandardJsonPlugin())
       ..add(PveBoolSerializer())
+      ..add(PveDoubleSerializer())
       ..add(PveIntSerializer())
       ..add(PveStringSerializer())
       ..add(PveDateTimeFromEpoch()))
-- 
2.30.2





^ permalink raw reply	[flat|nested] 6+ messages in thread

* [pve-devel] [PATCH flutter_frontend 4/4] power settings: fix handling null for template status
  2021-06-23 10:04 [pve-devel] [PATCH series/flutter 0/4] fixes related to PVE 7 Aaron Lauterer
                   ` (2 preceding siblings ...)
  2021-06-23 10:04 ` [pve-devel] [PATCH dart_api_client 3/4] Add double serializer to handle String and int values Aaron Lauterer
@ 2021-06-23 10:04 ` Aaron Lauterer
  2021-06-23 11:36 ` [pve-devel] applied-series: [PATCH series/flutter 0/4] fixes related to PVE 7 Thomas Lamprecht
  4 siblings, 0 replies; 6+ messages in thread
From: Aaron Lauterer @ 2021-06-23 10:04 UTC (permalink / raw)
  To: pve-devel

The template status of a guest can be null and we need to catch it.

This patch aligns that with all the other instances where we do check
the template status.

Signed-off-by: Aaron Lauterer <a.lauterer@proxmox.com>
---
 lib/widgets/pve_qemu_power_settings_widget.dart | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/widgets/pve_qemu_power_settings_widget.dart b/lib/widgets/pve_qemu_power_settings_widget.dart
index 65b3c0c..1c1d2ed 100644
--- a/lib/widgets/pve_qemu_power_settings_widget.dart
+++ b/lib/widgets/pve_qemu_power_settings_widget.dart
@@ -26,7 +26,7 @@ class PveQemuPowerSettings extends StatelessWidget {
                 mainAxisSize: MainAxisSize.min,
                 children: <Widget>[
                   if (qemuStatus == PveResourceStatusType.stopped &&
-                      !state.currentStatus.template)
+                      !(state.currentStatus.template ?? false))
                     ListTile(
                       leading: Icon(Icons.play_arrow),
                       title: Text(
@@ -41,7 +41,7 @@ class PveQemuPowerSettings extends StatelessWidget {
                         PveResourceStatusType.paused,
                         PveResourceStatusType.suspended
                       ].contains(qemuStatus) &&
-                      !state.currentStatus.template)
+                      !(state.currentStatus.template ?? false))
                     ListTile(
                       leading: Icon(Icons.play_arrow),
                       title: Text(
-- 
2.30.2





^ permalink raw reply	[flat|nested] 6+ messages in thread

* [pve-devel] applied-series: [PATCH series/flutter 0/4] fixes related to PVE 7
  2021-06-23 10:04 [pve-devel] [PATCH series/flutter 0/4] fixes related to PVE 7 Aaron Lauterer
                   ` (3 preceding siblings ...)
  2021-06-23 10:04 ` [pve-devel] [PATCH flutter_frontend 4/4] power settings: fix handling null for template status Aaron Lauterer
@ 2021-06-23 11:36 ` Thomas Lamprecht
  4 siblings, 0 replies; 6+ messages in thread
From: Thomas Lamprecht @ 2021-06-23 11:36 UTC (permalink / raw)
  To: Proxmox VE development discussion, Aaron Lauterer

On 23.06.21 12:04, Aaron Lauterer wrote:
> This series contains a few fixes for problems that I came across when
> testing the flutter app against Proxmox VE 7.
> 
> The first bigger one, affecting the first 3 patches, is handling of
> different types when parsing API responses. The PVE API is not always
> returning the expected type. In most situations it is numbers formatted
> as strings instead of numbers.
> 
> For dynamically types languages this is usually much less of a problem
> as it is in statically typed languages as dart.
> To handle that, I introduced new custom serializers for Strings,
> integers and doubles. If the value is not of the expected type, they
> should be able to handle situations where the number is formatted as
> string or where the expected string is formatted as number and
> cast/parse them accordingly.
> 
> The last patch addresses a problem that showed up when trying to open
> the power menu for a guest that had been powered off. The handling of
> the states template value being null has not been done correctly. I used
> the same approach as throughout the other parts where the template
> status is checked -> falling back to false.
> 
> dart_api_client: Aaron Lauterer (3):
>   Add string serializer to handle int and double values
>   Add int serializer to handle string values
>   Add double serializer to handle String and int values
> 
>  lib/src/models/pve_double_serializer.dart | 28 +++++++++++++++++++++++
>  lib/src/models/pve_int_serializer.dart    | 25 ++++++++++++++++++++
>  lib/src/models/pve_string_serializer.dart | 25 ++++++++++++++++++++
>  lib/src/models/serializers.dart           |  6 +++++
>  4 files changed, 84 insertions(+)
>  create mode 100644 lib/src/models/pve_double_serializer.dart
>  create mode 100644 lib/src/models/pve_int_serializer.dart
>  create mode 100644 lib/src/models/pve_string_serializer.dart
> 
> flutter_frontend: Aaron Lauterer (1):
>   power settings: fix handling null for template status
> 
>  lib/widgets/pve_qemu_power_settings_widget.dart | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> 



applied series, thanks!




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-06-23 11:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-23 10:04 [pve-devel] [PATCH series/flutter 0/4] fixes related to PVE 7 Aaron Lauterer
2021-06-23 10:04 ` [pve-devel] [PATCH dart_api_client 1/4] Add string serializer to handle int and double values Aaron Lauterer
2021-06-23 10:04 ` [pve-devel] [PATCH dart_api_client 2/4] Add int serializer to handle string values Aaron Lauterer
2021-06-23 10:04 ` [pve-devel] [PATCH dart_api_client 3/4] Add double serializer to handle String and int values Aaron Lauterer
2021-06-23 10:04 ` [pve-devel] [PATCH flutter_frontend 4/4] power settings: fix handling null for template status Aaron Lauterer
2021-06-23 11:36 ` [pve-devel] applied-series: [PATCH series/flutter 0/4] fixes related to PVE 7 Thomas Lamprecht

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal