Skip to content

Commit

Permalink
fix: Allow some ProductPackaging fields to accept string and JSON map (
Browse files Browse the repository at this point in the history
  • Loading branch information
Rujitsuki committed Sep 27, 2024
1 parent da2ef05 commit 3a47327
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
4 changes: 4 additions & 0 deletions lib/src/model/product_packaging.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:json_annotation/json_annotation.dart';
import 'localized_tag.dart';
import '../utils/json_helper.dart';
import '../interface/json_object.dart';

part 'product_packaging.g.dart';
Expand All @@ -10,20 +11,23 @@ class ProductPackaging extends JsonObject {
/// Shape, canonicalized using [TaxonomyPackagingShape].
@JsonKey(
includeIfNull: false,
fromJson: JsonHelper.localizedTagFromJson,
toJson: LocalizedTag.objToJson,
)
LocalizedTag? shape;

/// Material, canonicalized using [TaxonomyPackagingMaterial].
@JsonKey(
includeIfNull: false,
fromJson: JsonHelper.localizedTagFromJson,
toJson: LocalizedTag.objToJson,
)
LocalizedTag? material;

/// Recycling status, canonicalized using [TaxonomyPackagingRecycling].
@JsonKey(
includeIfNull: false,
fromJson: JsonHelper.localizedTagFromJson,
toJson: LocalizedTag.objToJson,
)
LocalizedTag? recycling;
Expand Down
12 changes: 3 additions & 9 deletions lib/src/model/product_packaging.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions lib/src/utils/json_helper.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import '../interface/json_object.dart';
import '../model/attribute_group.dart';
import '../model/ingredient.dart';
import '../model/localized_tag.dart';
import '../model/product_image.dart';
import '../model/product_packaging.dart';
import '../utils/language_helper.dart';
Expand Down Expand Up @@ -378,6 +379,16 @@ class JsonHelper {
return result;
}

/// Returns a [LocalizedTag] from JSON map or String
static LocalizedTag? localizedTagFromJson(dynamic json) {
if (json is Map<String, dynamic>) {
return LocalizedTag.fromJson(json);
} else if (json is String) {
return LocalizedTag()..name = json;
}
return null;
}

/// Returns a JSON map from [AttributeGroup]s
static List<Map<String, dynamic>>? attributeGroupsToJson(
List<AttributeGroup>? list) {
Expand Down

0 comments on commit 3a47327

Please sign in to comment.