Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v1.1.1

- Improved error messages
___________________

## v1.1.0

Implemented SRTE functions: :tada:
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MIT License

Copyright (c) 2012 - 2021 Contentstack
Copyright (c) 2012 - 2026 Contentstack

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
13 changes: 7 additions & 6 deletions lib/contentstack_utils.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
library contentstack_utils;

import 'package:contentstack_utils/src/constants/ErrorMessages.dart';
import 'package:contentstack_utils/src/helper/Automate.dart';
import 'package:contentstack_utils/src/model/Option.dart';
export 'src/embedded/StyleType.dart';
Expand All @@ -8,14 +9,14 @@ export 'src/helper/Metadata.dart';
class Utils {
static void render(jsonObject, List<String> rteKeys, Option option) {
if (!Automate.isValidJson(jsonObject)) {
throw FormatException('Invalid file, Can\'t process the json file');
throw FormatException(ErrorMessages.invalidJsonFile);
}

if (jsonObject is List) {
for (var entry in jsonObject) {
render(entry, rteKeys, option);
}
} else if (jsonObject is Map<String, Object>) {
} else if (jsonObject is Map) {
if (jsonObject.containsKey('_embedded_items')) {
if (rteKeys.isNotEmpty) {
for (var path in rteKeys) {
Expand All @@ -24,8 +25,8 @@ class Utils {
});
}
} else {
Map<String, Object> embeddedKeys = jsonObject['_embedded_items'];
rteKeys = embeddedKeys.keys.toList();
Map embeddedKeys = jsonObject['_embedded_items'] as Map;
rteKeys = embeddedKeys.keys.toList().cast<String>();
embeddedKeys.keys.forEach((keyPath) {
Automate.find_embed_keys(jsonObject, keyPath, (rteContent) {
return renderContent(rteContent, jsonObject, option);
Expand All @@ -34,7 +35,7 @@ class Utils {
}
}
} else {
FormatException('Invalid file for embedded objects');
throw FormatException(ErrorMessages.invalidEmbeddedObjectsInput);
}
}

Expand Down Expand Up @@ -69,7 +70,7 @@ class Utils {

static void jsonToHTML(items, List<String> key_path, Option option) {
if (!Automate.isValidJson(items)) {
throw FormatException('Invalid file, Can\'t process the json file');
throw FormatException(ErrorMessages.invalidJsonFile);
}

if (items is List) {
Expand Down
3 changes: 2 additions & 1 deletion lib/src/GQL.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import 'package:contentstack_utils/src/constants/ErrorMessages.dart';
import 'package:contentstack_utils/src/helper/Automate.dart';
import 'package:contentstack_utils/src/model/Option.dart';

class GQL {
static void jsonToHTML(items, List<String> key_path, Option option) {
if (!Automate.isValidJson(items)) {
throw FormatException('Invalid file, Can\'t process the json file');
throw FormatException(ErrorMessages.invalidJsonFile);
}

if (items is List) {
Expand Down
14 changes: 14 additions & 0 deletions lib/src/constants/ErrorMessages.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// Error message constants for Contentstack Utils
///
/// This file contains all error messages used throughout the library
/// to ensure consistency and ease of maintenance.
class ErrorMessages {
// JSON validation errors
static const String invalidJsonFile =
'Invalid JSON file. Provide a valid JSON file and try again.';

// Embedded objects errors
static const String invalidEmbeddedObjectsInput =
'Invalid input for embedded objects. Expected a Map or List.';
}

14 changes: 8 additions & 6 deletions lib/src/helper/Automate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ class Automate {
}
}

static Map findEmbeddedItems(Map jsonObject, Metadata metadata) {
static Map? findEmbeddedItems(Map jsonObject, Metadata metadata) {
var keys = jsonObject.keys;
for (var item in keys) {
List jsonArray = jsonObject[item];
var filteredContent = jsonArray
.firstWhere((element) => element['uid'] == metadata.getItemUid);
return filteredContent;
.firstWhere((element) => element['uid'] == metadata.getItemUid, orElse: () => null);
if (filteredContent != null) {
return filteredContent;
}
}
return null;
}
Expand Down Expand Up @@ -169,7 +171,7 @@ class Automate {
}
}

static Object findEmbeddedEntry(List jsonList, Metadata metadata) {
static Object? findEmbeddedEntry(List jsonList, Metadata metadata) {
for (var obj in jsonList) {
if (obj is Map) {
if (obj['uid'] == metadata.getItemUid) {
Expand All @@ -189,8 +191,8 @@ class Automate {
return null;
}

static String getStringOption(Option option, Metadata meta, Map content) {
var stringOption = option.renderOption(content, meta);
static String getStringOption(Option option, Metadata meta, Object content) {
var stringOption = option.renderOption(content as Map, meta);
return stringOption;
}

Expand Down
28 changes: 14 additions & 14 deletions lib/src/helper/Metadata.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import 'dart:collection';
import 'package:html/dom.dart';

class Metadata {
String text;
String itemType;
String itemUid;
String contentTypeUid;
String styleType;
String outerHTML;
LinkedHashMap<dynamic, dynamic> attributes;
String? text;
String? itemType;
String? itemUid;
String? contentTypeUid;
String? styleType;
String? outerHTML;
LinkedHashMap<dynamic, dynamic>? attributes;

Metadata(
{this.text,
Expand All @@ -20,19 +20,19 @@ class Metadata {
this.outerHTML,
this.attributes});

String get getText => text;
String? get getText => text;

String get getItemType => itemType;
String? get getItemType => itemType;

String get getItemUid => itemUid;
String? get getItemUid => itemUid;

String get getContentTypeUid => contentTypeUid;
String? get getContentTypeUid => contentTypeUid;

String get getStyleType => styleType;
String? get getStyleType => styleType;

String get getOuterHTML => outerHTML;
String? get getOuterHTML => outerHTML;

LinkedHashMap<dynamic, dynamic> get getAttributes => attributes;
LinkedHashMap<dynamic, dynamic>? get getAttributes => attributes;

Metadata.element(Element element) {
text = element.text;
Expand Down
12 changes: 7 additions & 5 deletions lib/src/model/Option.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ class Option {

static String _findInlineLink(Metadata metadata, Map obj) {
var _title = '';
if (metadata.getText.isNotEmpty) {
_title = metadata.getText;
var metadataText = metadata.getText;
if (metadataText != null && metadataText.isNotEmpty) {
_title = metadataText;
} else if (obj.containsKey('title')) {
_title = obj['title'];
} else if (obj.containsKey('uid')) {
Expand All @@ -42,8 +43,9 @@ class Option {

static String _findDisplayAtrr(Metadata metadata, Map obj) {
var _title = '';
if (metadata.attributes.isNotEmpty) {
_title = metadata.attributes.toString();
var metadataAttrs = metadata.attributes;
if (metadataAttrs != null && metadataAttrs.isNotEmpty) {
_title = metadataAttrs.toString();
} else if (obj.containsKey('title')) {
_title = obj['title'];
} else if (obj.containsKey('filename')) {
Expand All @@ -60,7 +62,7 @@ class Option {
}

String renderOption(Map obj, Metadata metadata) {
var style = metadata.styleType;
var style = metadata.styleType ?? '';
switch (style) {
case 'block':
var titlOrUid = _findTitleOrUid(obj);
Expand Down
16 changes: 8 additions & 8 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
name: contentstack_utils
description: Utils package for Contentstack-dart
version: 1.1.0
version: 1.1.1
homepage: https://www.contentstack.com

environment:
sdk: ">=2.8.0 <3.0.0"
sdk: ">=2.12.0 <4.0.0"

dependencies:
#dartdoc: ^1.0.2
lint: ^1.3.0
path: ^1.7.0
html: ^0.14.0+4
logger: ^1.0.0
lint: ^2.0.0
path: ^1.8.0
html: ^0.15.6
logger: ^2.0.0

dev_dependencies:
pedantic: ^1.11.1
test: ^1.17.5
lints: ^2.0.0
test: ^1.24.0
# test_coverage: ^0.4.1

3 changes: 2 additions & 1 deletion test/utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ void main() {
final _item = _entryArray['entries'][0];
final option = Option.entry(_item);
final result = Utils.renderContent(__stringHtmlEntry, _item, option);
logger.i(result);
//logger.i(result);
expect(result, isNotNull);
});

test('utils.render pass invalid json to cover exception', () {
Expand Down