From e69bd0eb754b56a9a6fb5b82da789add0099bd4e Mon Sep 17 00:00:00 2001 From: ashutoshkhadse Date: Fri, 18 Apr 2025 01:29:13 +0530 Subject: [PATCH 1/5] add: flexibility to PriceTotalStats --- lib/src/prices/price_total_stats.dart | 240 +++++++++++++++++++++++--- 1 file changed, 218 insertions(+), 22 deletions(-) diff --git a/lib/src/prices/price_total_stats.dart b/lib/src/prices/price_total_stats.dart index aa8b1f3094..01ef7d79f4 100644 --- a/lib/src/prices/price_total_stats.dart +++ b/lib/src/prices/price_total_stats.dart @@ -8,67 +8,263 @@ part 'price_total_stats.g.dart'; /// Total stats for Prices. /// /// cf. `TotalStats` in https://prices.openfoodfacts.org/api/docs -@JsonSerializable() +@JsonSerializable( + createToJson: true, + createFactory: true, + includeIfNull: true, + explicitToJson: true, + anyMap: true, +) class PriceTotalStats extends JsonObject { - @JsonKey(name: 'price_count') + @JsonKey(includeFromJson: false, includeToJson: false) + late Map _rawData; + + static const String PRICE_COUNT = 'price_count'; + static const String PRICE_TYPE_PRODUCT_CODE_COUNT = + 'price_type_product_code_count'; + static const String PRICE_TYPE_CATEGORY_TAG_COUNT = + 'price_type_category_tag_count'; + static const String PRICE_WITH_DISCOUNT_COUNT = 'price_with_discount_count'; + static const String PRICE_CURRENCY_COUNT = 'price_currency_count'; + static const String PRICE_YEAR_COUNT = 'price_year_count'; + static const String PRICE_LOCATION_COUNTRY_COUNT = + 'price_location_country_count'; + static const String PRICE_KIND_COMMUNITY_COUNT = 'price_kind_community_count'; + static const String PRICE_KIND_CONSUMPTION_COUNT = + 'price_kind_consumption_count'; + static const String PRICE_SOURCE_WEB_COUNT = 'price_source_web_count'; + static const String PRICE_SOURCE_MOBILE_COUNT = 'price_source_mobile_count'; + static const String PRICE_SOURCE_API_COUNT = 'price_source_api_count'; + static const String PRICE_SOURCE_OTHER_COUNT = 'price_source_other_count'; + static const String PRODUCT_COUNT = 'product_count'; + static const String PRODUCT_SOURCE_OFF_COUNT = 'product_source_off_count'; + static const String PRODUCT_SOURCE_OBF_COUNT = 'product_source_obf_count'; + static const String PRODUCT_SOURCE_OPFF_COUNT = 'product_source_opff_count'; + static const String PRODUCT_SOURCE_OPF_COUNT = 'product_source_opf_count'; + static const String PRODUCT_WITH_PRICE_COUNT = 'product_with_price_count'; + static const String PRODUCT_SOURCE_OFF_WITH_PRICE_COUNT = + 'product_source_off_with_price_count'; + static const String PRODUCT_SOURCE_OBF_WITH_PRICE_COUNT = + 'product_source_obf_with_price_count'; + static const String PRODUCT_SOURCE_OPFF_WITH_PRICE_COUNT = + 'product_source_opff_with_price_count'; + static const String PRODUCT_SOURCE_OPF_WITH_PRICE_COUNT = + 'product_source_opf_with_price_count'; + static const String LOCATION_COUNT = 'location_count'; + static const String LOCATION_WITH_PRICE_COUNT = 'location_with_price_count'; + static const String LOCATION_TYPE_OSM_COUNT = 'location_type_osm_count'; + static const String LOCATION_TYPE_ONLINE_COUNT = 'location_type_online_count'; + static const String LOCATION_TYPE_OSM_COUNTRY_COUNT = + 'location_type_osm_country_count'; + static const String PROOF_COUNT = 'proof_count'; + static const String PROOF_WITH_PRICE_COUNT = 'proof_with_price_count'; + static const String PROOF_TYPE_PRICE_TAG_COUNT = 'proof_type_price_tag_count'; + static const String PROOF_TYPE_RECEIPT_COUNT = 'proof_type_receipt_count'; + static const String PROOF_TYPE_GDPR_REQUEST_COUNT = + 'proof_type_gdpr_request_count'; + static const String PROOF_TYPE_SHOP_IMPORT_COUNT = + 'proof_type_shop_import_count'; + static const String PROOF_KIND_COMMUNITY_COUNT = 'proof_kind_community_count'; + static const String PROOF_KIND_CONSUMPTION_COUNT = + 'proof_kind_consumption_count'; + static const String PROOF_SOURCE_WEB_COUNT = 'proof_source_web_count'; + static const String PROOF_SOURCE_MOBILE_COUNT = 'proof_source_mobile_count'; + static const String PROOF_SOURCE_API_COUNT = 'proof_source_api_count'; + static const String PROOF_SOURCE_OTHER_COUNT = 'proof_source_other_count'; + static const String PRICE_TAG_COUNT = 'price_tag_count'; + static const String PRICE_TAG_STATUS_UNKNOWN_COUNT = + 'price_tag_status_unknown_count'; + static const String PRICE_TAG_STATUS_LINKED_TO_PRICE_COUNT = + 'price_tag_status_linked_to_price_count'; + static const String USER_COUNT = 'user_count'; + static const String USER_WITH_PRICE_COUNT = 'user_with_price_count'; + static const String UPDATED = 'updated'; + + @JsonKey(name: PRICE_COUNT) int? priceCount; - @JsonKey(name: 'price_type_product_code_count') + @JsonKey(name: PRICE_TYPE_PRODUCT_CODE_COUNT) int? priceTypeProductCodeCount; - @JsonKey(name: 'price_type_category_tag_count') + @JsonKey(name: PRICE_TYPE_CATEGORY_TAG_COUNT) int? priceTypeCategoryTagCount; - @JsonKey(name: 'product_count') + @JsonKey(name: PRICE_WITH_DISCOUNT_COUNT) + int? priceWithDiscountCount; + + @JsonKey(name: PRICE_CURRENCY_COUNT) + int? priceCurrencyCount; + + @JsonKey(name: PRICE_YEAR_COUNT) + int? priceYearCount; + + @JsonKey(name: PRICE_LOCATION_COUNTRY_COUNT) + int? priceLocationCountryCount; + + @JsonKey(name: PRICE_KIND_COMMUNITY_COUNT) + int? priceKindCommunityCount; + + @JsonKey(name: PRICE_KIND_CONSUMPTION_COUNT) + int? priceKindConsumptionCount; + + @JsonKey(name: PRICE_SOURCE_WEB_COUNT) + int? priceSourceWebCount; + + @JsonKey(name: PRICE_SOURCE_MOBILE_COUNT) + int? priceSourceMobileCount; + + @JsonKey(name: PRICE_SOURCE_API_COUNT) + int? priceSourceApiCount; + + @JsonKey(name: PRICE_SOURCE_OTHER_COUNT) + int? priceSourceOtherCount; + + @JsonKey(name: PRODUCT_COUNT) int? productCount; - @JsonKey(name: 'product_with_price_count') + @JsonKey(name: PRODUCT_SOURCE_OFF_COUNT) + int? productSourceOffCount; + + @JsonKey(name: PRODUCT_SOURCE_OBF_COUNT) + int? productSourceObfCount; + + @JsonKey(name: PRODUCT_SOURCE_OPFF_COUNT) + int? productSourceOpffCount; + + @JsonKey(name: PRODUCT_SOURCE_OPF_COUNT) + int? productSourceOpfCount; + + @JsonKey(name: PRODUCT_WITH_PRICE_COUNT) int? productWithPriceCount; - @JsonKey(name: 'location_count') + @JsonKey(name: PRODUCT_SOURCE_OFF_WITH_PRICE_COUNT) + int? productSourceOffWithPriceCount; + + @JsonKey(name: PRODUCT_SOURCE_OBF_WITH_PRICE_COUNT) + int? productSourceObfWithPriceCount; + + @JsonKey(name: PRODUCT_SOURCE_OPFF_WITH_PRICE_COUNT) + int? productSourceOpffWithPriceCount; + + @JsonKey(name: PRODUCT_SOURCE_OPF_WITH_PRICE_COUNT) + int? productSourceOpfWithPriceCount; + + @JsonKey(name: LOCATION_COUNT) int? locationCount; - @JsonKey(name: 'location_with_price_count') + @JsonKey(name: LOCATION_WITH_PRICE_COUNT) int? locationWithPriceCount; - @JsonKey(name: 'location_type_osm_count') + @JsonKey(name: LOCATION_TYPE_OSM_COUNT) int? locationTypeOsmCount; - @JsonKey(name: 'location_type_online_count') + @JsonKey(name: LOCATION_TYPE_ONLINE_COUNT) int? locationTypeOnlineCount; - @JsonKey(name: 'proof_count') + @JsonKey(name: LOCATION_TYPE_OSM_COUNTRY_COUNT) + int? locationTypeOsmCountryCount; + + @JsonKey(name: PROOF_COUNT) int? proofCount; - @JsonKey(name: 'proof_with_price_count') + @JsonKey(name: PROOF_WITH_PRICE_COUNT) int? proofWithPriceCount; - @JsonKey(name: 'proof_type_price_tag_count') + @JsonKey(name: PROOF_TYPE_PRICE_TAG_COUNT) int? proofTypePriceTagCount; - @JsonKey(name: 'proof_type_receipt_count') + @JsonKey(name: PROOF_TYPE_RECEIPT_COUNT) int? proofTypeReceiptCount; - @JsonKey(name: 'proof_type_gdpr_request_count') + @JsonKey(name: PROOF_TYPE_GDPR_REQUEST_COUNT) int? proofTypeGdprRequestCount; - @JsonKey(name: 'proof_type_shop_import_count') + @JsonKey(name: PROOF_TYPE_SHOP_IMPORT_COUNT) int? proofTypeShopImportCount; - @JsonKey(name: 'user_count') + @JsonKey(name: PROOF_KIND_COMMUNITY_COUNT) + int? proofKindCommunityCount; + + @JsonKey(name: PROOF_KIND_CONSUMPTION_COUNT) + int? proofKindConsumptionCount; + + @JsonKey(name: PROOF_SOURCE_WEB_COUNT) + int? proofSourceWebCount; + + @JsonKey(name: PROOF_SOURCE_MOBILE_COUNT) + int? proofSourceMobileCount; + + @JsonKey(name: PROOF_SOURCE_API_COUNT) + int? proofSourceApiCount; + + @JsonKey(name: PROOF_SOURCE_OTHER_COUNT) + int? proofSourceOtherCount; + + @JsonKey(name: PRICE_TAG_COUNT) + int? priceTagCount; + + @JsonKey(name: PRICE_TAG_STATUS_UNKNOWN_COUNT) + int? priceTagStatusUnknownCount; + + @JsonKey(name: PRICE_TAG_STATUS_LINKED_TO_PRICE_COUNT) + int? priceTagStatusLinkedToPriceCount; + + @JsonKey(name: USER_COUNT) int? userCount; - @JsonKey(name: 'user_with_price_count') + @JsonKey(name: USER_WITH_PRICE_COUNT) int? userWithPriceCount; - @JsonKey(fromJson: JsonHelper.nullableStringTimestampToDate) + @JsonKey(name: UPDATED, fromJson: JsonHelper.nullableStringTimestampToDate) DateTime? updated; + int? getInt(String key) { + if (!_rawData.containsKey(key)) return null; + final value = _rawData[key]; + if (value is int) return value; + return int.tryParse(value?.toString() ?? ''); + } + + String? getString(String key) => + _rawData.containsKey(key) ? _rawData[key]?.toString() : null; + + double? getDouble(String key) { + if (!_rawData.containsKey(key)) return null; + final value = _rawData[key]; + if (value is double) return value; + return double.tryParse(value?.toString() ?? ''); + } + + bool? getBool(String key) => + _rawData.containsKey(key) && _rawData[key] is bool + ? _rawData[key] as bool + : null; + + DateTime? getDateTime(String key) => _rawData.containsKey(key) + ? JsonHelper.nullableStringTimestampToDate(_rawData[key]) + : null; + + bool containsKey(String key) => _rawData.containsKey(key); + + Set get availableKeys => _rawData.keys.toSet(); + PriceTotalStats(); - factory PriceTotalStats.fromJson(Map json) => - _$PriceTotalStatsFromJson(json); + factory PriceTotalStats.fromJson(Map json) { + final result = _$PriceTotalStatsFromJson(json); + result._rawData = Map.from(json); + return result; + } @override - Map toJson() => _$PriceTotalStatsToJson(this); + Map toJson() { + final json = _$PriceTotalStatsToJson(this); + + for (final entry in _rawData.entries) { + if (!json.containsKey(entry.key)) { + json[entry.key] = entry.value; + } + } + return json; + } } From 1e78f3bc2ba0504ba76b4acbf5e816a9f26dd442 Mon Sep 17 00:00:00 2001 From: ashutoshkhadse Date: Fri, 18 Apr 2025 01:29:42 +0530 Subject: [PATCH 2/5] add: flexibility to PriceTotalStats --- lib/src/prices/price_total_stats.g.dart | 133 ++++++++++++++++++------ 1 file changed, 103 insertions(+), 30 deletions(-) diff --git a/lib/src/prices/price_total_stats.g.dart b/lib/src/prices/price_total_stats.g.dart index 200d2dfe30..d9fdd44c92 100644 --- a/lib/src/prices/price_total_stats.g.dart +++ b/lib/src/prices/price_total_stats.g.dart @@ -6,54 +6,127 @@ part of 'price_total_stats.dart'; // JsonSerializableGenerator // ************************************************************************** -PriceTotalStats _$PriceTotalStatsFromJson(Map json) => - PriceTotalStats() - ..priceCount = (json['price_count'] as num?)?.toInt() - ..priceTypeProductCodeCount = - (json['price_type_product_code_count'] as num?)?.toInt() - ..priceTypeCategoryTagCount = - (json['price_type_category_tag_count'] as num?)?.toInt() - ..productCount = (json['product_count'] as num?)?.toInt() - ..productWithPriceCount = - (json['product_with_price_count'] as num?)?.toInt() - ..locationCount = (json['location_count'] as num?)?.toInt() - ..locationWithPriceCount = - (json['location_with_price_count'] as num?)?.toInt() - ..locationTypeOsmCount = - (json['location_type_osm_count'] as num?)?.toInt() - ..locationTypeOnlineCount = - (json['location_type_online_count'] as num?)?.toInt() - ..proofCount = (json['proof_count'] as num?)?.toInt() - ..proofWithPriceCount = (json['proof_with_price_count'] as num?)?.toInt() - ..proofTypePriceTagCount = - (json['proof_type_price_tag_count'] as num?)?.toInt() - ..proofTypeReceiptCount = - (json['proof_type_receipt_count'] as num?)?.toInt() - ..proofTypeGdprRequestCount = - (json['proof_type_gdpr_request_count'] as num?)?.toInt() - ..proofTypeShopImportCount = - (json['proof_type_shop_import_count'] as num?)?.toInt() - ..userCount = (json['user_count'] as num?)?.toInt() - ..userWithPriceCount = (json['user_with_price_count'] as num?)?.toInt() - ..updated = JsonHelper.nullableStringTimestampToDate(json['updated']); +PriceTotalStats _$PriceTotalStatsFromJson(Map json) => PriceTotalStats() + ..priceCount = (json['price_count'] as num?)?.toInt() + ..priceTypeProductCodeCount = + (json['price_type_product_code_count'] as num?)?.toInt() + ..priceTypeCategoryTagCount = + (json['price_type_category_tag_count'] as num?)?.toInt() + ..priceWithDiscountCount = + (json['price_with_discount_count'] as num?)?.toInt() + ..priceCurrencyCount = (json['price_currency_count'] as num?)?.toInt() + ..priceYearCount = (json['price_year_count'] as num?)?.toInt() + ..priceLocationCountryCount = + (json['price_location_country_count'] as num?)?.toInt() + ..priceKindCommunityCount = + (json['price_kind_community_count'] as num?)?.toInt() + ..priceKindConsumptionCount = + (json['price_kind_consumption_count'] as num?)?.toInt() + ..priceSourceWebCount = (json['price_source_web_count'] as num?)?.toInt() + ..priceSourceMobileCount = + (json['price_source_mobile_count'] as num?)?.toInt() + ..priceSourceApiCount = (json['price_source_api_count'] as num?)?.toInt() + ..priceSourceOtherCount = (json['price_source_other_count'] as num?)?.toInt() + ..productCount = (json['product_count'] as num?)?.toInt() + ..productSourceOffCount = (json['product_source_off_count'] as num?)?.toInt() + ..productSourceObfCount = (json['product_source_obf_count'] as num?)?.toInt() + ..productSourceOpffCount = + (json['product_source_opff_count'] as num?)?.toInt() + ..productSourceOpfCount = (json['product_source_opf_count'] as num?)?.toInt() + ..productWithPriceCount = (json['product_with_price_count'] as num?)?.toInt() + ..productSourceOffWithPriceCount = + (json['product_source_off_with_price_count'] as num?)?.toInt() + ..productSourceObfWithPriceCount = + (json['product_source_obf_with_price_count'] as num?)?.toInt() + ..productSourceOpffWithPriceCount = + (json['product_source_opff_with_price_count'] as num?)?.toInt() + ..productSourceOpfWithPriceCount = + (json['product_source_opf_with_price_count'] as num?)?.toInt() + ..locationCount = (json['location_count'] as num?)?.toInt() + ..locationWithPriceCount = + (json['location_with_price_count'] as num?)?.toInt() + ..locationTypeOsmCount = (json['location_type_osm_count'] as num?)?.toInt() + ..locationTypeOnlineCount = + (json['location_type_online_count'] as num?)?.toInt() + ..locationTypeOsmCountryCount = + (json['location_type_osm_country_count'] as num?)?.toInt() + ..proofCount = (json['proof_count'] as num?)?.toInt() + ..proofWithPriceCount = (json['proof_with_price_count'] as num?)?.toInt() + ..proofTypePriceTagCount = + (json['proof_type_price_tag_count'] as num?)?.toInt() + ..proofTypeReceiptCount = (json['proof_type_receipt_count'] as num?)?.toInt() + ..proofTypeGdprRequestCount = + (json['proof_type_gdpr_request_count'] as num?)?.toInt() + ..proofTypeShopImportCount = + (json['proof_type_shop_import_count'] as num?)?.toInt() + ..proofKindCommunityCount = + (json['proof_kind_community_count'] as num?)?.toInt() + ..proofKindConsumptionCount = + (json['proof_kind_consumption_count'] as num?)?.toInt() + ..proofSourceWebCount = (json['proof_source_web_count'] as num?)?.toInt() + ..proofSourceMobileCount = + (json['proof_source_mobile_count'] as num?)?.toInt() + ..proofSourceApiCount = (json['proof_source_api_count'] as num?)?.toInt() + ..proofSourceOtherCount = (json['proof_source_other_count'] as num?)?.toInt() + ..priceTagCount = (json['price_tag_count'] as num?)?.toInt() + ..priceTagStatusUnknownCount = + (json['price_tag_status_unknown_count'] as num?)?.toInt() + ..priceTagStatusLinkedToPriceCount = + (json['price_tag_status_linked_to_price_count'] as num?)?.toInt() + ..userCount = (json['user_count'] as num?)?.toInt() + ..userWithPriceCount = (json['user_with_price_count'] as num?)?.toInt() + ..updated = JsonHelper.nullableStringTimestampToDate(json['updated']); Map _$PriceTotalStatsToJson(PriceTotalStats instance) => { 'price_count': instance.priceCount, 'price_type_product_code_count': instance.priceTypeProductCodeCount, 'price_type_category_tag_count': instance.priceTypeCategoryTagCount, + 'price_with_discount_count': instance.priceWithDiscountCount, + 'price_currency_count': instance.priceCurrencyCount, + 'price_year_count': instance.priceYearCount, + 'price_location_country_count': instance.priceLocationCountryCount, + 'price_kind_community_count': instance.priceKindCommunityCount, + 'price_kind_consumption_count': instance.priceKindConsumptionCount, + 'price_source_web_count': instance.priceSourceWebCount, + 'price_source_mobile_count': instance.priceSourceMobileCount, + 'price_source_api_count': instance.priceSourceApiCount, + 'price_source_other_count': instance.priceSourceOtherCount, 'product_count': instance.productCount, + 'product_source_off_count': instance.productSourceOffCount, + 'product_source_obf_count': instance.productSourceObfCount, + 'product_source_opff_count': instance.productSourceOpffCount, + 'product_source_opf_count': instance.productSourceOpfCount, 'product_with_price_count': instance.productWithPriceCount, + 'product_source_off_with_price_count': + instance.productSourceOffWithPriceCount, + 'product_source_obf_with_price_count': + instance.productSourceObfWithPriceCount, + 'product_source_opff_with_price_count': + instance.productSourceOpffWithPriceCount, + 'product_source_opf_with_price_count': + instance.productSourceOpfWithPriceCount, 'location_count': instance.locationCount, 'location_with_price_count': instance.locationWithPriceCount, 'location_type_osm_count': instance.locationTypeOsmCount, 'location_type_online_count': instance.locationTypeOnlineCount, + 'location_type_osm_country_count': instance.locationTypeOsmCountryCount, 'proof_count': instance.proofCount, 'proof_with_price_count': instance.proofWithPriceCount, 'proof_type_price_tag_count': instance.proofTypePriceTagCount, 'proof_type_receipt_count': instance.proofTypeReceiptCount, 'proof_type_gdpr_request_count': instance.proofTypeGdprRequestCount, 'proof_type_shop_import_count': instance.proofTypeShopImportCount, + 'proof_kind_community_count': instance.proofKindCommunityCount, + 'proof_kind_consumption_count': instance.proofKindConsumptionCount, + 'proof_source_web_count': instance.proofSourceWebCount, + 'proof_source_mobile_count': instance.proofSourceMobileCount, + 'proof_source_api_count': instance.proofSourceApiCount, + 'proof_source_other_count': instance.proofSourceOtherCount, + 'price_tag_count': instance.priceTagCount, + 'price_tag_status_unknown_count': instance.priceTagStatusUnknownCount, + 'price_tag_status_linked_to_price_count': + instance.priceTagStatusLinkedToPriceCount, 'user_count': instance.userCount, 'user_with_price_count': instance.userWithPriceCount, 'updated': instance.updated?.toIso8601String(), From 4e2fc7c12aa438fc025ef32553fd27c539f16be0 Mon Sep 17 00:00:00 2001 From: ashutoshkhadse Date: Sat, 19 Apr 2025 09:46:31 +0530 Subject: [PATCH 3/5] add: flexibility to PriceTotalStats with simpler approach --- lib/src/prices/price_total_stats.dart | 252 +++++++----------------- lib/src/prices/price_total_stats.g.dart | 133 ------------- 2 files changed, 74 insertions(+), 311 deletions(-) delete mode 100644 lib/src/prices/price_total_stats.g.dart diff --git a/lib/src/prices/price_total_stats.dart b/lib/src/prices/price_total_stats.dart index 01ef7d79f4..a510e965b3 100644 --- a/lib/src/prices/price_total_stats.dart +++ b/lib/src/prices/price_total_stats.dart @@ -3,21 +3,11 @@ import 'package:json_annotation/json_annotation.dart'; import '../interface/json_object.dart'; import '../utils/json_helper.dart'; -part 'price_total_stats.g.dart'; - /// Total stats for Prices. /// /// cf. `TotalStats` in https://prices.openfoodfacts.org/api/docs -@JsonSerializable( - createToJson: true, - createFactory: true, - includeIfNull: true, - explicitToJson: true, - anyMap: true, -) class PriceTotalStats extends JsonObject { - @JsonKey(includeFromJson: false, includeToJson: false) - late Map _rawData; + final Map _rawData; static const String PRICE_COUNT = 'price_count'; static const String PRICE_TYPE_PRODUCT_CODE_COUNT = @@ -80,165 +70,80 @@ class PriceTotalStats extends JsonObject { static const String USER_WITH_PRICE_COUNT = 'user_with_price_count'; static const String UPDATED = 'updated'; - @JsonKey(name: PRICE_COUNT) - int? priceCount; - - @JsonKey(name: PRICE_TYPE_PRODUCT_CODE_COUNT) - int? priceTypeProductCodeCount; - - @JsonKey(name: PRICE_TYPE_CATEGORY_TAG_COUNT) - int? priceTypeCategoryTagCount; - - @JsonKey(name: PRICE_WITH_DISCOUNT_COUNT) - int? priceWithDiscountCount; - - @JsonKey(name: PRICE_CURRENCY_COUNT) - int? priceCurrencyCount; - - @JsonKey(name: PRICE_YEAR_COUNT) - int? priceYearCount; - - @JsonKey(name: PRICE_LOCATION_COUNTRY_COUNT) - int? priceLocationCountryCount; - - @JsonKey(name: PRICE_KIND_COMMUNITY_COUNT) - int? priceKindCommunityCount; - - @JsonKey(name: PRICE_KIND_CONSUMPTION_COUNT) - int? priceKindConsumptionCount; - - @JsonKey(name: PRICE_SOURCE_WEB_COUNT) - int? priceSourceWebCount; - - @JsonKey(name: PRICE_SOURCE_MOBILE_COUNT) - int? priceSourceMobileCount; - - @JsonKey(name: PRICE_SOURCE_API_COUNT) - int? priceSourceApiCount; - - @JsonKey(name: PRICE_SOURCE_OTHER_COUNT) - int? priceSourceOtherCount; - - @JsonKey(name: PRODUCT_COUNT) - int? productCount; - - @JsonKey(name: PRODUCT_SOURCE_OFF_COUNT) - int? productSourceOffCount; - - @JsonKey(name: PRODUCT_SOURCE_OBF_COUNT) - int? productSourceObfCount; - - @JsonKey(name: PRODUCT_SOURCE_OPFF_COUNT) - int? productSourceOpffCount; - - @JsonKey(name: PRODUCT_SOURCE_OPF_COUNT) - int? productSourceOpfCount; - - @JsonKey(name: PRODUCT_WITH_PRICE_COUNT) - int? productWithPriceCount; - - @JsonKey(name: PRODUCT_SOURCE_OFF_WITH_PRICE_COUNT) - int? productSourceOffWithPriceCount; - - @JsonKey(name: PRODUCT_SOURCE_OBF_WITH_PRICE_COUNT) - int? productSourceObfWithPriceCount; - - @JsonKey(name: PRODUCT_SOURCE_OPFF_WITH_PRICE_COUNT) - int? productSourceOpffWithPriceCount; - - @JsonKey(name: PRODUCT_SOURCE_OPF_WITH_PRICE_COUNT) - int? productSourceOpfWithPriceCount; - - @JsonKey(name: LOCATION_COUNT) - int? locationCount; - - @JsonKey(name: LOCATION_WITH_PRICE_COUNT) - int? locationWithPriceCount; - - @JsonKey(name: LOCATION_TYPE_OSM_COUNT) - int? locationTypeOsmCount; - - @JsonKey(name: LOCATION_TYPE_ONLINE_COUNT) - int? locationTypeOnlineCount; - - @JsonKey(name: LOCATION_TYPE_OSM_COUNTRY_COUNT) - int? locationTypeOsmCountryCount; - - @JsonKey(name: PROOF_COUNT) - int? proofCount; - - @JsonKey(name: PROOF_WITH_PRICE_COUNT) - int? proofWithPriceCount; - - @JsonKey(name: PROOF_TYPE_PRICE_TAG_COUNT) - int? proofTypePriceTagCount; - - @JsonKey(name: PROOF_TYPE_RECEIPT_COUNT) - int? proofTypeReceiptCount; - - @JsonKey(name: PROOF_TYPE_GDPR_REQUEST_COUNT) - int? proofTypeGdprRequestCount; - - @JsonKey(name: PROOF_TYPE_SHOP_IMPORT_COUNT) - int? proofTypeShopImportCount; - - @JsonKey(name: PROOF_KIND_COMMUNITY_COUNT) - int? proofKindCommunityCount; - - @JsonKey(name: PROOF_KIND_CONSUMPTION_COUNT) - int? proofKindConsumptionCount; - - @JsonKey(name: PROOF_SOURCE_WEB_COUNT) - int? proofSourceWebCount; - - @JsonKey(name: PROOF_SOURCE_MOBILE_COUNT) - int? proofSourceMobileCount; - - @JsonKey(name: PROOF_SOURCE_API_COUNT) - int? proofSourceApiCount; - - @JsonKey(name: PROOF_SOURCE_OTHER_COUNT) - int? proofSourceOtherCount; - - @JsonKey(name: PRICE_TAG_COUNT) - int? priceTagCount; - - @JsonKey(name: PRICE_TAG_STATUS_UNKNOWN_COUNT) - int? priceTagStatusUnknownCount; - - @JsonKey(name: PRICE_TAG_STATUS_LINKED_TO_PRICE_COUNT) - int? priceTagStatusLinkedToPriceCount; - - @JsonKey(name: USER_COUNT) - int? userCount; - - @JsonKey(name: USER_WITH_PRICE_COUNT) - int? userWithPriceCount; - - @JsonKey(name: UPDATED, fromJson: JsonHelper.nullableStringTimestampToDate) - DateTime? updated; + int? get priceCount => getInt(PRICE_COUNT); + int? get priceTypeProductCodeCount => getInt(PRICE_TYPE_PRODUCT_CODE_COUNT); + int? get priceTypeCategoryTagCount => getInt(PRICE_TYPE_CATEGORY_TAG_COUNT); + int? get priceWithDiscountCount => getInt(PRICE_WITH_DISCOUNT_COUNT); + int? get priceCurrencyCount => getInt(PRICE_CURRENCY_COUNT); + int? get priceYearCount => getInt(PRICE_YEAR_COUNT); + int? get priceLocationCountryCount => getInt(PRICE_LOCATION_COUNTRY_COUNT); + int? get priceKindCommunityCount => getInt(PRICE_KIND_COMMUNITY_COUNT); + int? get priceKindConsumptionCount => getInt(PRICE_KIND_CONSUMPTION_COUNT); + int? get priceSourceWebCount => getInt(PRICE_SOURCE_WEB_COUNT); + int? get priceSourceMobileCount => getInt(PRICE_SOURCE_MOBILE_COUNT); + int? get priceSourceApiCount => getInt(PRICE_SOURCE_API_COUNT); + int? get priceSourceOtherCount => getInt(PRICE_SOURCE_OTHER_COUNT); + int? get productCount => getInt(PRODUCT_COUNT); + int? get productSourceOffCount => getInt(PRODUCT_SOURCE_OFF_COUNT); + int? get productSourceObfCount => getInt(PRODUCT_SOURCE_OBF_COUNT); + int? get productSourceOpffCount => getInt(PRODUCT_SOURCE_OPFF_COUNT); + int? get productSourceOpfCount => getInt(PRODUCT_SOURCE_OPF_COUNT); + int? get productWithPriceCount => getInt(PRODUCT_WITH_PRICE_COUNT); + int? get productSourceOffWithPriceCount => + getInt(PRODUCT_SOURCE_OFF_WITH_PRICE_COUNT); + int? get productSourceObfWithPriceCount => + getInt(PRODUCT_SOURCE_OBF_WITH_PRICE_COUNT); + int? get productSourceOpffWithPriceCount => + getInt(PRODUCT_SOURCE_OPFF_WITH_PRICE_COUNT); + int? get productSourceOpfWithPriceCount => + getInt(PRODUCT_SOURCE_OPF_WITH_PRICE_COUNT); + int? get locationCount => getInt(LOCATION_COUNT); + int? get locationWithPriceCount => getInt(LOCATION_WITH_PRICE_COUNT); + int? get locationTypeOsmCount => getInt(LOCATION_TYPE_OSM_COUNT); + int? get locationTypeOnlineCount => getInt(LOCATION_TYPE_ONLINE_COUNT); + int? get locationTypeOsmCountryCount => + getInt(LOCATION_TYPE_OSM_COUNTRY_COUNT); + int? get proofCount => getInt(PROOF_COUNT); + int? get proofWithPriceCount => getInt(PROOF_WITH_PRICE_COUNT); + int? get proofTypePriceTagCount => getInt(PROOF_TYPE_PRICE_TAG_COUNT); + int? get proofTypeReceiptCount => getInt(PROOF_TYPE_RECEIPT_COUNT); + int? get proofTypeGdprRequestCount => getInt(PROOF_TYPE_GDPR_REQUEST_COUNT); + int? get proofTypeShopImportCount => getInt(PROOF_TYPE_SHOP_IMPORT_COUNT); + int? get proofKindCommunityCount => getInt(PROOF_KIND_COMMUNITY_COUNT); + int? get proofKindConsumptionCount => getInt(PROOF_KIND_CONSUMPTION_COUNT); + int? get proofSourceWebCount => getInt(PROOF_SOURCE_WEB_COUNT); + int? get proofSourceMobileCount => getInt(PROOF_SOURCE_MOBILE_COUNT); + int? get proofSourceApiCount => getInt(PROOF_SOURCE_API_COUNT); + int? get proofSourceOtherCount => getInt(PROOF_SOURCE_OTHER_COUNT); + int? get priceTagCount => getInt(PRICE_TAG_COUNT); + int? get priceTagStatusUnknownCount => getInt(PRICE_TAG_STATUS_UNKNOWN_COUNT); + int? get priceTagStatusLinkedToPriceCount => + getInt(PRICE_TAG_STATUS_LINKED_TO_PRICE_COUNT); + int? get userCount => getInt(USER_COUNT); + int? get userWithPriceCount => getInt(USER_WITH_PRICE_COUNT); + + DateTime? get updated => _rawData.containsKey(UPDATED) + ? JsonHelper.nullableStringTimestampToDate(_rawData[UPDATED]) + : null; - int? getInt(String key) { - if (!_rawData.containsKey(key)) return null; - final value = _rawData[key]; - if (value is int) return value; - return int.tryParse(value?.toString() ?? ''); - } + int? getInt(String key) => _rawData.containsKey(key) + ? (_rawData[key] is int + ? _rawData[key] + : int.tryParse(_rawData[key]?.toString() ?? '')) + : null; String? getString(String key) => _rawData.containsKey(key) ? _rawData[key]?.toString() : null; - double? getDouble(String key) { - if (!_rawData.containsKey(key)) return null; - final value = _rawData[key]; - if (value is double) return value; - return double.tryParse(value?.toString() ?? ''); - } + double? getDouble(String key) => _rawData.containsKey(key) + ? (_rawData[key] is double + ? _rawData[key] + : double.tryParse(_rawData[key]?.toString() ?? '')) + : null; - bool? getBool(String key) => - _rawData.containsKey(key) && _rawData[key] is bool - ? _rawData[key] as bool - : null; + bool? getBool(String key) => _rawData.containsKey(key) + ? (_rawData[key] is bool ? _rawData[key] : null) + : null; DateTime? getDateTime(String key) => _rawData.containsKey(key) ? JsonHelper.nullableStringTimestampToDate(_rawData[key]) @@ -248,23 +153,14 @@ class PriceTotalStats extends JsonObject { Set get availableKeys => _rawData.keys.toSet(); - PriceTotalStats(); + PriceTotalStats(this._rawData); + + PriceTotalStats.empty() : _rawData = {}; factory PriceTotalStats.fromJson(Map json) { - final result = _$PriceTotalStatsFromJson(json); - result._rawData = Map.from(json); - return result; + return PriceTotalStats(json); } @override - Map toJson() { - final json = _$PriceTotalStatsToJson(this); - - for (final entry in _rawData.entries) { - if (!json.containsKey(entry.key)) { - json[entry.key] = entry.value; - } - } - return json; - } + Map toJson() => _rawData; } diff --git a/lib/src/prices/price_total_stats.g.dart b/lib/src/prices/price_total_stats.g.dart deleted file mode 100644 index d9fdd44c92..0000000000 --- a/lib/src/prices/price_total_stats.g.dart +++ /dev/null @@ -1,133 +0,0 @@ -// GENERATED CODE - DO NOT MODIFY BY HAND - -part of 'price_total_stats.dart'; - -// ************************************************************************** -// JsonSerializableGenerator -// ************************************************************************** - -PriceTotalStats _$PriceTotalStatsFromJson(Map json) => PriceTotalStats() - ..priceCount = (json['price_count'] as num?)?.toInt() - ..priceTypeProductCodeCount = - (json['price_type_product_code_count'] as num?)?.toInt() - ..priceTypeCategoryTagCount = - (json['price_type_category_tag_count'] as num?)?.toInt() - ..priceWithDiscountCount = - (json['price_with_discount_count'] as num?)?.toInt() - ..priceCurrencyCount = (json['price_currency_count'] as num?)?.toInt() - ..priceYearCount = (json['price_year_count'] as num?)?.toInt() - ..priceLocationCountryCount = - (json['price_location_country_count'] as num?)?.toInt() - ..priceKindCommunityCount = - (json['price_kind_community_count'] as num?)?.toInt() - ..priceKindConsumptionCount = - (json['price_kind_consumption_count'] as num?)?.toInt() - ..priceSourceWebCount = (json['price_source_web_count'] as num?)?.toInt() - ..priceSourceMobileCount = - (json['price_source_mobile_count'] as num?)?.toInt() - ..priceSourceApiCount = (json['price_source_api_count'] as num?)?.toInt() - ..priceSourceOtherCount = (json['price_source_other_count'] as num?)?.toInt() - ..productCount = (json['product_count'] as num?)?.toInt() - ..productSourceOffCount = (json['product_source_off_count'] as num?)?.toInt() - ..productSourceObfCount = (json['product_source_obf_count'] as num?)?.toInt() - ..productSourceOpffCount = - (json['product_source_opff_count'] as num?)?.toInt() - ..productSourceOpfCount = (json['product_source_opf_count'] as num?)?.toInt() - ..productWithPriceCount = (json['product_with_price_count'] as num?)?.toInt() - ..productSourceOffWithPriceCount = - (json['product_source_off_with_price_count'] as num?)?.toInt() - ..productSourceObfWithPriceCount = - (json['product_source_obf_with_price_count'] as num?)?.toInt() - ..productSourceOpffWithPriceCount = - (json['product_source_opff_with_price_count'] as num?)?.toInt() - ..productSourceOpfWithPriceCount = - (json['product_source_opf_with_price_count'] as num?)?.toInt() - ..locationCount = (json['location_count'] as num?)?.toInt() - ..locationWithPriceCount = - (json['location_with_price_count'] as num?)?.toInt() - ..locationTypeOsmCount = (json['location_type_osm_count'] as num?)?.toInt() - ..locationTypeOnlineCount = - (json['location_type_online_count'] as num?)?.toInt() - ..locationTypeOsmCountryCount = - (json['location_type_osm_country_count'] as num?)?.toInt() - ..proofCount = (json['proof_count'] as num?)?.toInt() - ..proofWithPriceCount = (json['proof_with_price_count'] as num?)?.toInt() - ..proofTypePriceTagCount = - (json['proof_type_price_tag_count'] as num?)?.toInt() - ..proofTypeReceiptCount = (json['proof_type_receipt_count'] as num?)?.toInt() - ..proofTypeGdprRequestCount = - (json['proof_type_gdpr_request_count'] as num?)?.toInt() - ..proofTypeShopImportCount = - (json['proof_type_shop_import_count'] as num?)?.toInt() - ..proofKindCommunityCount = - (json['proof_kind_community_count'] as num?)?.toInt() - ..proofKindConsumptionCount = - (json['proof_kind_consumption_count'] as num?)?.toInt() - ..proofSourceWebCount = (json['proof_source_web_count'] as num?)?.toInt() - ..proofSourceMobileCount = - (json['proof_source_mobile_count'] as num?)?.toInt() - ..proofSourceApiCount = (json['proof_source_api_count'] as num?)?.toInt() - ..proofSourceOtherCount = (json['proof_source_other_count'] as num?)?.toInt() - ..priceTagCount = (json['price_tag_count'] as num?)?.toInt() - ..priceTagStatusUnknownCount = - (json['price_tag_status_unknown_count'] as num?)?.toInt() - ..priceTagStatusLinkedToPriceCount = - (json['price_tag_status_linked_to_price_count'] as num?)?.toInt() - ..userCount = (json['user_count'] as num?)?.toInt() - ..userWithPriceCount = (json['user_with_price_count'] as num?)?.toInt() - ..updated = JsonHelper.nullableStringTimestampToDate(json['updated']); - -Map _$PriceTotalStatsToJson(PriceTotalStats instance) => - { - 'price_count': instance.priceCount, - 'price_type_product_code_count': instance.priceTypeProductCodeCount, - 'price_type_category_tag_count': instance.priceTypeCategoryTagCount, - 'price_with_discount_count': instance.priceWithDiscountCount, - 'price_currency_count': instance.priceCurrencyCount, - 'price_year_count': instance.priceYearCount, - 'price_location_country_count': instance.priceLocationCountryCount, - 'price_kind_community_count': instance.priceKindCommunityCount, - 'price_kind_consumption_count': instance.priceKindConsumptionCount, - 'price_source_web_count': instance.priceSourceWebCount, - 'price_source_mobile_count': instance.priceSourceMobileCount, - 'price_source_api_count': instance.priceSourceApiCount, - 'price_source_other_count': instance.priceSourceOtherCount, - 'product_count': instance.productCount, - 'product_source_off_count': instance.productSourceOffCount, - 'product_source_obf_count': instance.productSourceObfCount, - 'product_source_opff_count': instance.productSourceOpffCount, - 'product_source_opf_count': instance.productSourceOpfCount, - 'product_with_price_count': instance.productWithPriceCount, - 'product_source_off_with_price_count': - instance.productSourceOffWithPriceCount, - 'product_source_obf_with_price_count': - instance.productSourceObfWithPriceCount, - 'product_source_opff_with_price_count': - instance.productSourceOpffWithPriceCount, - 'product_source_opf_with_price_count': - instance.productSourceOpfWithPriceCount, - 'location_count': instance.locationCount, - 'location_with_price_count': instance.locationWithPriceCount, - 'location_type_osm_count': instance.locationTypeOsmCount, - 'location_type_online_count': instance.locationTypeOnlineCount, - 'location_type_osm_country_count': instance.locationTypeOsmCountryCount, - 'proof_count': instance.proofCount, - 'proof_with_price_count': instance.proofWithPriceCount, - 'proof_type_price_tag_count': instance.proofTypePriceTagCount, - 'proof_type_receipt_count': instance.proofTypeReceiptCount, - 'proof_type_gdpr_request_count': instance.proofTypeGdprRequestCount, - 'proof_type_shop_import_count': instance.proofTypeShopImportCount, - 'proof_kind_community_count': instance.proofKindCommunityCount, - 'proof_kind_consumption_count': instance.proofKindConsumptionCount, - 'proof_source_web_count': instance.proofSourceWebCount, - 'proof_source_mobile_count': instance.proofSourceMobileCount, - 'proof_source_api_count': instance.proofSourceApiCount, - 'proof_source_other_count': instance.proofSourceOtherCount, - 'price_tag_count': instance.priceTagCount, - 'price_tag_status_unknown_count': instance.priceTagStatusUnknownCount, - 'price_tag_status_linked_to_price_count': - instance.priceTagStatusLinkedToPriceCount, - 'user_count': instance.userCount, - 'user_with_price_count': instance.userWithPriceCount, - 'updated': instance.updated?.toIso8601String(), - }; From 81c5f6e7f559b0d1aec826a37a476f813ea2090b Mon Sep 17 00:00:00 2001 From: ashutoshkhadse Date: Sat, 19 Apr 2025 17:20:04 +0530 Subject: [PATCH 4/5] add: flexibility to PriceTotalStats with the getters and without the constants. --- lib/src/prices/price_total_stats.dart | 204 +++++++------------------- 1 file changed, 56 insertions(+), 148 deletions(-) diff --git a/lib/src/prices/price_total_stats.dart b/lib/src/prices/price_total_stats.dart index a510e965b3..ce4d3249ba 100644 --- a/lib/src/prices/price_total_stats.dart +++ b/lib/src/prices/price_total_stats.dart @@ -1,166 +1,74 @@ import 'package:json_annotation/json_annotation.dart'; -import '../interface/json_object.dart'; import '../utils/json_helper.dart'; /// Total stats for Prices. /// /// cf. `TotalStats` in https://prices.openfoodfacts.org/api/docs -class PriceTotalStats extends JsonObject { - final Map _rawData; - - static const String PRICE_COUNT = 'price_count'; - static const String PRICE_TYPE_PRODUCT_CODE_COUNT = - 'price_type_product_code_count'; - static const String PRICE_TYPE_CATEGORY_TAG_COUNT = - 'price_type_category_tag_count'; - static const String PRICE_WITH_DISCOUNT_COUNT = 'price_with_discount_count'; - static const String PRICE_CURRENCY_COUNT = 'price_currency_count'; - static const String PRICE_YEAR_COUNT = 'price_year_count'; - static const String PRICE_LOCATION_COUNTRY_COUNT = - 'price_location_country_count'; - static const String PRICE_KIND_COMMUNITY_COUNT = 'price_kind_community_count'; - static const String PRICE_KIND_CONSUMPTION_COUNT = - 'price_kind_consumption_count'; - static const String PRICE_SOURCE_WEB_COUNT = 'price_source_web_count'; - static const String PRICE_SOURCE_MOBILE_COUNT = 'price_source_mobile_count'; - static const String PRICE_SOURCE_API_COUNT = 'price_source_api_count'; - static const String PRICE_SOURCE_OTHER_COUNT = 'price_source_other_count'; - static const String PRODUCT_COUNT = 'product_count'; - static const String PRODUCT_SOURCE_OFF_COUNT = 'product_source_off_count'; - static const String PRODUCT_SOURCE_OBF_COUNT = 'product_source_obf_count'; - static const String PRODUCT_SOURCE_OPFF_COUNT = 'product_source_opff_count'; - static const String PRODUCT_SOURCE_OPF_COUNT = 'product_source_opf_count'; - static const String PRODUCT_WITH_PRICE_COUNT = 'product_with_price_count'; - static const String PRODUCT_SOURCE_OFF_WITH_PRICE_COUNT = - 'product_source_off_with_price_count'; - static const String PRODUCT_SOURCE_OBF_WITH_PRICE_COUNT = - 'product_source_obf_with_price_count'; - static const String PRODUCT_SOURCE_OPFF_WITH_PRICE_COUNT = - 'product_source_opff_with_price_count'; - static const String PRODUCT_SOURCE_OPF_WITH_PRICE_COUNT = - 'product_source_opf_with_price_count'; - static const String LOCATION_COUNT = 'location_count'; - static const String LOCATION_WITH_PRICE_COUNT = 'location_with_price_count'; - static const String LOCATION_TYPE_OSM_COUNT = 'location_type_osm_count'; - static const String LOCATION_TYPE_ONLINE_COUNT = 'location_type_online_count'; - static const String LOCATION_TYPE_OSM_COUNTRY_COUNT = - 'location_type_osm_country_count'; - static const String PROOF_COUNT = 'proof_count'; - static const String PROOF_WITH_PRICE_COUNT = 'proof_with_price_count'; - static const String PROOF_TYPE_PRICE_TAG_COUNT = 'proof_type_price_tag_count'; - static const String PROOF_TYPE_RECEIPT_COUNT = 'proof_type_receipt_count'; - static const String PROOF_TYPE_GDPR_REQUEST_COUNT = - 'proof_type_gdpr_request_count'; - static const String PROOF_TYPE_SHOP_IMPORT_COUNT = - 'proof_type_shop_import_count'; - static const String PROOF_KIND_COMMUNITY_COUNT = 'proof_kind_community_count'; - static const String PROOF_KIND_CONSUMPTION_COUNT = - 'proof_kind_consumption_count'; - static const String PROOF_SOURCE_WEB_COUNT = 'proof_source_web_count'; - static const String PROOF_SOURCE_MOBILE_COUNT = 'proof_source_mobile_count'; - static const String PROOF_SOURCE_API_COUNT = 'proof_source_api_count'; - static const String PROOF_SOURCE_OTHER_COUNT = 'proof_source_other_count'; - static const String PRICE_TAG_COUNT = 'price_tag_count'; - static const String PRICE_TAG_STATUS_UNKNOWN_COUNT = - 'price_tag_status_unknown_count'; - static const String PRICE_TAG_STATUS_LINKED_TO_PRICE_COUNT = - 'price_tag_status_linked_to_price_count'; - static const String USER_COUNT = 'user_count'; - static const String USER_WITH_PRICE_COUNT = 'user_with_price_count'; - static const String UPDATED = 'updated'; - - int? get priceCount => getInt(PRICE_COUNT); - int? get priceTypeProductCodeCount => getInt(PRICE_TYPE_PRODUCT_CODE_COUNT); - int? get priceTypeCategoryTagCount => getInt(PRICE_TYPE_CATEGORY_TAG_COUNT); - int? get priceWithDiscountCount => getInt(PRICE_WITH_DISCOUNT_COUNT); - int? get priceCurrencyCount => getInt(PRICE_CURRENCY_COUNT); - int? get priceYearCount => getInt(PRICE_YEAR_COUNT); - int? get priceLocationCountryCount => getInt(PRICE_LOCATION_COUNTRY_COUNT); - int? get priceKindCommunityCount => getInt(PRICE_KIND_COMMUNITY_COUNT); - int? get priceKindConsumptionCount => getInt(PRICE_KIND_CONSUMPTION_COUNT); - int? get priceSourceWebCount => getInt(PRICE_SOURCE_WEB_COUNT); - int? get priceSourceMobileCount => getInt(PRICE_SOURCE_MOBILE_COUNT); - int? get priceSourceApiCount => getInt(PRICE_SOURCE_API_COUNT); - int? get priceSourceOtherCount => getInt(PRICE_SOURCE_OTHER_COUNT); - int? get productCount => getInt(PRODUCT_COUNT); - int? get productSourceOffCount => getInt(PRODUCT_SOURCE_OFF_COUNT); - int? get productSourceObfCount => getInt(PRODUCT_SOURCE_OBF_COUNT); - int? get productSourceOpffCount => getInt(PRODUCT_SOURCE_OPFF_COUNT); - int? get productSourceOpfCount => getInt(PRODUCT_SOURCE_OPF_COUNT); - int? get productWithPriceCount => getInt(PRODUCT_WITH_PRICE_COUNT); +class PriceTotalStats { + final Map json; + + PriceTotalStats(this.json); + + int? get priceCount => getInt('price_count'); + int? get priceTypeProductCodeCount => getInt('price_type_product_code_count'); + int? get priceTypeCategoryTagCount => getInt('price_type_category_tag_count'); + int? get priceWithDiscountCount => getInt('price_with_discount_count'); + int? get priceCurrencyCount => getInt('price_currency_count'); + int? get priceYearCount => getInt('price_year_count'); + int? get priceLocationCountryCount => getInt('price_location_country_count'); + int? get priceKindCommunityCount => getInt('price_kind_community_count'); + int? get priceKindConsumptionCount => getInt('price_kind_consumption_count'); + int? get priceSourceWebCount => getInt('price_source_web_count'); + int? get priceSourceMobileCount => getInt('price_source_mobile_count'); + int? get priceSourceApiCount => getInt('price_source_api_count'); + int? get priceSourceOtherCount => getInt('price_source_other_count'); + int? get productCount => getInt('product_count'); + int? get productSourceOffCount => getInt('product_source_off_count'); + int? get productSourceObfCount => getInt('product_source_obf_count'); + int? get productSourceOpffCount => getInt('product_source_opff_count'); + int? get productSourceOpfCount => getInt('product_source_opf_count'); + int? get productWithPriceCount => getInt('product_with_price_count'); int? get productSourceOffWithPriceCount => - getInt(PRODUCT_SOURCE_OFF_WITH_PRICE_COUNT); + getInt('product_source_off_with_price_count'); int? get productSourceObfWithPriceCount => - getInt(PRODUCT_SOURCE_OBF_WITH_PRICE_COUNT); + getInt('product_source_obf_with_price_count'); int? get productSourceOpffWithPriceCount => - getInt(PRODUCT_SOURCE_OPFF_WITH_PRICE_COUNT); + getInt('product_source_opff_with_price_count'); int? get productSourceOpfWithPriceCount => - getInt(PRODUCT_SOURCE_OPF_WITH_PRICE_COUNT); - int? get locationCount => getInt(LOCATION_COUNT); - int? get locationWithPriceCount => getInt(LOCATION_WITH_PRICE_COUNT); - int? get locationTypeOsmCount => getInt(LOCATION_TYPE_OSM_COUNT); - int? get locationTypeOnlineCount => getInt(LOCATION_TYPE_ONLINE_COUNT); + getInt('product_source_opf_with_price_count'); + int? get locationCount => getInt('location_count'); + int? get locationWithPriceCount => getInt('location_with_price_count'); + int? get locationTypeOsmCount => getInt('location_type_osm_count'); + int? get locationTypeOnlineCount => getInt('location_type_online_count'); int? get locationTypeOsmCountryCount => - getInt(LOCATION_TYPE_OSM_COUNTRY_COUNT); - int? get proofCount => getInt(PROOF_COUNT); - int? get proofWithPriceCount => getInt(PROOF_WITH_PRICE_COUNT); - int? get proofTypePriceTagCount => getInt(PROOF_TYPE_PRICE_TAG_COUNT); - int? get proofTypeReceiptCount => getInt(PROOF_TYPE_RECEIPT_COUNT); - int? get proofTypeGdprRequestCount => getInt(PROOF_TYPE_GDPR_REQUEST_COUNT); - int? get proofTypeShopImportCount => getInt(PROOF_TYPE_SHOP_IMPORT_COUNT); - int? get proofKindCommunityCount => getInt(PROOF_KIND_COMMUNITY_COUNT); - int? get proofKindConsumptionCount => getInt(PROOF_KIND_CONSUMPTION_COUNT); - int? get proofSourceWebCount => getInt(PROOF_SOURCE_WEB_COUNT); - int? get proofSourceMobileCount => getInt(PROOF_SOURCE_MOBILE_COUNT); - int? get proofSourceApiCount => getInt(PROOF_SOURCE_API_COUNT); - int? get proofSourceOtherCount => getInt(PROOF_SOURCE_OTHER_COUNT); - int? get priceTagCount => getInt(PRICE_TAG_COUNT); - int? get priceTagStatusUnknownCount => getInt(PRICE_TAG_STATUS_UNKNOWN_COUNT); + getInt('location_type_osm_country_count'); + int? get proofCount => getInt('proof_count'); + int? get proofWithPriceCount => getInt('proof_with_price_count'); + int? get proofTypePriceTagCount => getInt('proof_type_price_tag_count'); + int? get proofTypeReceiptCount => getInt('proof_type_receipt_count'); + int? get proofTypeGdprRequestCount => getInt('proof_type_gdpr_request_count'); + int? get proofTypeShopImportCount => getInt('proof_type_shop_import_count'); + int? get proofKindCommunityCount => getInt('proof_kind_community_count'); + int? get proofKindConsumptionCount => getInt('proof_kind_consumption_count'); + int? get proofSourceWebCount => getInt('proof_source_web_count'); + int? get proofSourceMobileCount => getInt('proof_source_mobile_count'); + int? get proofSourceApiCount => getInt('proof_source_api_count'); + int? get proofSourceOtherCount => getInt('proof_source_other_count'); + int? get priceTagCount => getInt('price_tag_count'); + int? get priceTagStatusUnknownCount => + getInt('price_tag_status_unknown_count'); int? get priceTagStatusLinkedToPriceCount => - getInt(PRICE_TAG_STATUS_LINKED_TO_PRICE_COUNT); - int? get userCount => getInt(USER_COUNT); - int? get userWithPriceCount => getInt(USER_WITH_PRICE_COUNT); - - DateTime? get updated => _rawData.containsKey(UPDATED) - ? JsonHelper.nullableStringTimestampToDate(_rawData[UPDATED]) - : null; - - int? getInt(String key) => _rawData.containsKey(key) - ? (_rawData[key] is int - ? _rawData[key] - : int.tryParse(_rawData[key]?.toString() ?? '')) - : null; - - String? getString(String key) => - _rawData.containsKey(key) ? _rawData[key]?.toString() : null; - - double? getDouble(String key) => _rawData.containsKey(key) - ? (_rawData[key] is double - ? _rawData[key] - : double.tryParse(_rawData[key]?.toString() ?? '')) - : null; - - bool? getBool(String key) => _rawData.containsKey(key) - ? (_rawData[key] is bool ? _rawData[key] : null) - : null; - - DateTime? getDateTime(String key) => _rawData.containsKey(key) - ? JsonHelper.nullableStringTimestampToDate(_rawData[key]) - : null; - - bool containsKey(String key) => _rawData.containsKey(key); - - Set get availableKeys => _rawData.keys.toSet(); - - PriceTotalStats(this._rawData); + getInt('price_tag_status_linked_to_price_count'); + int? get userCount => getInt('user_count'); + int? get userWithPriceCount => getInt('user_with_price_count'); + DateTime? get updated => getDateTime('updated'); - PriceTotalStats.empty() : _rawData = {}; + int? getInt(String key) => json.containsKey(key) ? json[key] as int? : null; - factory PriceTotalStats.fromJson(Map json) { - return PriceTotalStats(json); - } + DateTime? getDateTime(String key) => + JsonHelper.nullableStringTimestampToDate(json[key]); @override - Map toJson() => _rawData; + Map toJson() => json; } From 601be9510fd74248f96b1c78ac761f50508e4f91 Mon Sep 17 00:00:00 2001 From: ashutoshkhadse Date: Sat, 19 Apr 2025 19:19:30 +0530 Subject: [PATCH 5/5] add: flexibility to PriceTotalStats with the getters and without the constants. --- lib/src/prices/price_total_stats.dart | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/src/prices/price_total_stats.dart b/lib/src/prices/price_total_stats.dart index ce4d3249ba..cbcf35929e 100644 --- a/lib/src/prices/price_total_stats.dart +++ b/lib/src/prices/price_total_stats.dart @@ -1,5 +1,3 @@ -import 'package:json_annotation/json_annotation.dart'; - import '../utils/json_helper.dart'; /// Total stats for Prices. @@ -10,6 +8,10 @@ class PriceTotalStats { PriceTotalStats(this.json); + factory PriceTotalStats.fromJson(Map json) { + return PriceTotalStats(json); + } + int? get priceCount => getInt('price_count'); int? get priceTypeProductCodeCount => getInt('price_type_product_code_count'); int? get priceTypeCategoryTagCount => getInt('price_type_category_tag_count'); @@ -69,6 +71,5 @@ class PriceTotalStats { DateTime? getDateTime(String key) => JsonHelper.nullableStringTimestampToDate(json[key]); - @override Map toJson() => json; }