omnimodel 1.0.4 omnimodel: ^1.0.4 copied to clipboard
Model complex data and access it in an easy and safe way
example/omnimodel_example.dart
import 'dart:collection';
import 'dart:convert';
import 'package:omnimodel/src/common/logger.dart';
import 'package:omnimodel/src/omnimodel.dart';
void main() {
const map = {
"first": 0,
"second": "value",
"third": {
"key1": true,
"key2": 0,
"key3": [0, 1, 2, 3]
},
"fourth": [
"tag1",
"tag2",
"tag3",
]
};
//* Run with
//* dart run omnimodel_examples.dart
// Create the model from the map
var model = OmniModel.fromMap(map);
// Uncomment to disable hints printing. Use this feature to conditionally disable printing in production.
// OmniModelPerferences.enableHints = false;
// Flutter:
// OmniModelPreferences.enableHints = kDebugMode;
logInfo(
"""\n
░█████╗░███╗░░░███╗███╗░░██╗██╗███╗░░░███╗░█████╗░██████╗░███████╗██╗░░░░░
██╔══██╗████╗░████║████╗░██║██║████╗░████║██╔══██╗██╔══██╗██╔════╝██║░░░░░
██║░░██║██╔████╔██║██╔██╗██║██║██╔████╔██║██║░░██║██║░░██║█████╗░░██║░░░░░
██║░░██║██║╚██╔╝██║██║╚████║██║██║╚██╔╝██║██║░░██║██║░░██║██╔══╝░░██║░░░░░
╚█████╔╝██║░╚═╝░██║██║░╚███║██║██║░╚═╝░██║╚█████╔╝██████╔╝███████╗███████╗
░╚════╝░╚═╝░░░░░╚═╝╚═╝░░╚══╝╚═╝╚═╝░░░░░╚═╝░╚════╝░╚═════╝░╚══════╝╚══════╝
█▀▀ ▀▄▀ ▄▀█ █▀▄▀█ █▀█ █░░ █▀▀ █▀
██▄ █░█ █▀█ █░▀░█ █▀▀ █▄▄ ██▄ ▄█
""");
logInfo("map ${JsonEncoder.withIndent(" ").convert(map)}");
logInfo("■" * 100);
logInfo("■ map[first] > ${model.tokenOrNull("first")}\n${"-" * 100}");
logInfo("■ map[first] as String > ${model.tokenOrNull<String>("first")}\n${"-" * 100}");
logInfo("■ map[second] as String > ${model.tokenOr("second", "not found")}\n${"-" * 100}");
logInfo("■ map[second] as List > ${model.tokenOr<List>("second", List.empty())}\n${"-" * 100}");
logInfo("■ map[third] as Map > ${model.tokenOrNull<Map>("third")}\n${"-" * 100}");
logInfo(
"■ map[third][key3] with OmniModel concatenation > ${model.tokenAsModel("third").tokenOrNull("key3")}\n${"-" * 100}");
logInfo("■ map[third][key3] with combined path > ${model.tokenOrNull("third.key3")}\n${"-" * 100}");
logInfo("■ map[fourth] as Map > ${model.tokenOr("fourth", {"new_key": 1})}\n${"-" * 100}");
logInfo("■ map[fourth] as List > ${model.tokenOrNull<List>("fourth")}\n${"-" * 100}");
logInfo("■ map[fifth] (not exists) > ${model.tokenOrNull("fifth")}\n${"-" * 100}");
logInfo(
"■ map[fifth] (not exists) with default value > ${model.tokenOr<String>("fifth", "not found")}\n${"-" * 100}");
logInfo("■ map[fourth1] (mispelled key) > ${model.tokenOrNull("fourth1")}\n${"-" * 100}");
logInfo("■ map[third][key4] (mispelled key) > ${model.tokenOrNull("third.key4")}\n${"-" * 100}");
}