From be2c56e9f02b209777d1cac10dda25d236a6dcb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Iv=C3=A1n=20L=C3=B3pez=20Gonz=C3=A1lez?= Date: Wed, 18 Sep 2024 09:54:24 +0100 Subject: [PATCH] storage: read size current from JSON config --- .../storage/config_conversions/from_json.rb | 6 +-- .../from_json_conversions/size.rb | 40 ++++++++++++++----- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/service/lib/agama/storage/config_conversions/from_json.rb b/service/lib/agama/storage/config_conversions/from_json.rb index 14ba24df1..846a8b715 100644 --- a/service/lib/agama/storage/config_conversions/from_json.rb +++ b/service/lib/agama/storage/config_conversions/from_json.rb @@ -28,21 +28,19 @@ module Storage module ConfigConversions # Config conversion from JSON hash according to schema. class FromJSON - # @todo Replace product_config param by a ProductDefinition. - # # @param config_json [Hash] # @param product_config [Agama::Config] def initialize(config_json, product_config:) + # TODO: Replace product_config param by a ProductDefinition. @config_json = config_json @product_config = product_config end # Performs the conversion from Hash according to the JSON schema. # - # @todo Raise error if config_json does not match the JSON schema. - # # @return [Storage::Config] def convert + # TODO: Raise error if config_json does not match the JSON schema. config = FromJSONConversions::Config .new(config_json, config_builder: config_builder) .convert diff --git a/service/lib/agama/storage/config_conversions/from_json_conversions/size.rb b/service/lib/agama/storage/config_conversions/from_json_conversions/size.rb index 19e4a1850..cb02433aa 100644 --- a/service/lib/agama/storage/config_conversions/from_json_conversions/size.rb +++ b/service/lib/agama/storage/config_conversions/from_json_conversions/size.rb @@ -55,30 +55,48 @@ def convert(default = nil) def conversions(_default) { default: false, - min: convert_size(:min), - max: convert_size(:max) || Y2Storage::DiskSize.unlimited + min: convert_min_size, + max: convert_max_size } end # @return [Y2Storage::DiskSize, nil] - def convert_size(field) + def convert_min_size value = case size_json when Hash - size_json[field] + size_json[:min] when Array - field == :max ? size_json[1] : size_json[0] + size_json[0] else size_json end - return unless value + disk_size(value) + end - begin - # This parses without legacy_units, ie. "1 GiB" != "1 GB" - Y2Storage::DiskSize.new(value) - rescue TypeError - # JSON schema validations should prevent this from happening + # @return [Y2Storage::DiskSize, nil] + def convert_max_size + value = case size_json + when Hash + size_json[:max] + when Array + size_json[1] + else + size_json end + + return Y2Storage::DiskSize.unlimited unless value + + disk_size(value) + end + + # @param value [String, Integer] e.g., "2 GiB". + # @return [Y2Storage::DiskSize, nil] nil if value is "current". + def disk_size(value) + return if value == "current" + + # This parses without legacy_units, ie. "1 GiB" != "1 GB". + Y2Storage::DiskSize.new(value) end end end