|
| 1 | +import 'package:flutter/services.dart'; |
| 2 | +import 'package:flutter_policy_engine/src/utils/json_handler.dart'; |
| 3 | +import 'package:flutter_policy_engine/src/utils/log_handler.dart'; |
| 4 | + |
| 5 | +/// Handles loading and parsing of external asset files for policy configuration. |
| 6 | +/// |
| 7 | +/// This class provides a convenient way to load JSON policy files from Flutter assets |
| 8 | +/// and parse them into a structured format. It includes error handling and logging |
| 9 | +/// for robust asset loading operations. |
| 10 | +/// |
| 11 | +/// ## Usage |
| 12 | +/// |
| 13 | +/// ```dart |
| 14 | +/// // Initialize with asset path |
| 15 | +/// final assetHandler = ExternalAssetHandler( |
| 16 | +/// assetPath: 'assets/policies/config.json', |
| 17 | +/// ); |
| 18 | +/// |
| 19 | +/// // Load and parse the asset |
| 20 | +/// final policies = await assetHandler.loadAssets(); |
| 21 | +/// ``` |
| 22 | +/// |
| 23 | +/// ## Error Handling |
| 24 | +/// |
| 25 | +/// The class automatically handles asset loading errors and returns an empty map |
| 26 | +/// if the asset cannot be loaded or parsed. All errors are logged for debugging. |
| 27 | +class ExternalAssetHandler { |
| 28 | + /// Creates an [ExternalAssetHandler] instance. |
| 29 | + /// |
| 30 | + /// The [assetPath] parameter specifies the path to the JSON asset file within |
| 31 | + /// the Flutter assets directory. This path should be relative to the assets |
| 32 | + /// folder and must be declared in the `pubspec.yaml` file. |
| 33 | + /// |
| 34 | + /// ## Parameters |
| 35 | + /// |
| 36 | + /// * [assetPath] - The path to the JSON asset file (e.g., 'assets/policies/config.json') |
| 37 | + /// |
| 38 | + /// ## Throws |
| 39 | + /// |
| 40 | + /// * [ArgumentError] - If [assetPath] is empty or null |
| 41 | + /// |
| 42 | + /// ## Example |
| 43 | + /// |
| 44 | + /// ```dart |
| 45 | + /// final handler = ExternalAssetHandler( |
| 46 | + /// assetPath: 'assets/policies/user_roles.json', |
| 47 | + /// ); |
| 48 | + /// ``` |
| 49 | + ExternalAssetHandler({ |
| 50 | + required String assetPath, |
| 51 | + }) : _assetPath = assetPath { |
| 52 | + if (assetPath.isEmpty) { |
| 53 | + throw ArgumentError('Asset path cannot be empty', 'assetPath'); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /// The path to the asset file. |
| 58 | + final String _assetPath; |
| 59 | + |
| 60 | + /// Loads and parses the JSON asset file. |
| 61 | + /// |
| 62 | + /// This method asynchronously loads the JSON file from the specified asset path |
| 63 | + /// and parses it into a [Map<String, dynamic>]. If the asset cannot be loaded |
| 64 | + /// or parsed, an empty map is returned and the error is logged. |
| 65 | + /// |
| 66 | + /// ## Returns |
| 67 | + /// |
| 68 | + /// A [Future<Map<String, dynamic>>] containing the parsed JSON data. |
| 69 | + /// Returns an empty map if loading or parsing fails. |
| 70 | + /// |
| 71 | + /// ## Example |
| 72 | + /// |
| 73 | + /// ```dart |
| 74 | + /// final assetHandler = ExternalAssetHandler( |
| 75 | + /// assetPath: 'assets/policies/config.json', |
| 76 | + /// ); |
| 77 | + /// |
| 78 | + /// final policies = await assetHandler.loadAssets(); |
| 79 | + /// if (policies.isNotEmpty) { |
| 80 | + /// // Process the loaded policies |
| 81 | + /// print('Loaded ${policies.length} policies'); |
| 82 | + /// } else { |
| 83 | + /// print('No policies loaded or asset not found'); |
| 84 | + /// } |
| 85 | + /// ``` |
| 86 | + /// |
| 87 | + /// ## Error Handling |
| 88 | + /// |
| 89 | + /// The method catches and logs the following types of errors: |
| 90 | + /// * Asset not found errors |
| 91 | + /// * JSON parsing errors |
| 92 | + /// * File system access errors |
| 93 | + /// |
| 94 | + /// All errors are logged using [LogHandler.error] for debugging purposes. |
| 95 | + Future<Map<String, dynamic>> loadAssets() async { |
| 96 | + try { |
| 97 | + final jsonString = await rootBundle.loadString(_assetPath); |
| 98 | + return JsonHandler.parseJsonString(jsonString); |
| 99 | + } catch (e) { |
| 100 | + LogHandler.error( |
| 101 | + 'Failed to load policies from asset: $_assetPath', |
| 102 | + error: e, |
| 103 | + ); |
| 104 | + // Return an empty map with the correct type to satisfy the return type |
| 105 | + return <String, dynamic>{}; |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments