-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from a-givertzman/TextFile-read-fix
Text file read fix
- Loading branch information
Showing
14 changed files
with
211 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,39 @@ | ||
import 'dart:async'; | ||
import 'dart:convert'; | ||
|
||
import 'package:hmi_core/src/core/error/failure.dart'; | ||
import 'package:hmi_core/src/core/log/log.dart'; | ||
import 'package:hmi_core/src/core/log/log_level.dart'; | ||
import 'package:hmi_core/src/core/result_new/result.dart'; | ||
import 'package:hmi_core/src/core/text_file.dart'; | ||
/// | ||
class JsonList<T> { | ||
final FutureOr<String> _content; | ||
const JsonList(String content) : _content = content; | ||
JsonList.fromTextFile(TextFile textFile) : _content = textFile.content; | ||
Future<List<T>> get decoded async { | ||
final map = const JsonCodec().decode(await _content) as List<dynamic>; | ||
return map.cast<T>(); | ||
static final _log = const Log('JsonList')..level=LogLevel.info; | ||
final FutureOr<ResultF<String>> _content; | ||
const JsonList._(FutureOr<ResultF<String>> content) : _content = content; | ||
JsonList.fromString(String content) : this._(Ok(content)); | ||
JsonList.fromTextFile(TextFile textFile) : this._(textFile.content); | ||
/// | ||
Future<ResultF<List<T>>> get decoded async { | ||
const failureMessage = 'Failed to parse json list from file.'; | ||
switch(await _content) { | ||
case Ok(:final value): | ||
try { | ||
final decodedJson = const JsonCodec().decode(value) as List<dynamic>; | ||
return Ok( | ||
decodedJson.cast<T>(), | ||
); | ||
} catch(error) { | ||
_log.warning(failureMessage); | ||
return Err( | ||
Failure( | ||
message: error.toString(), | ||
stackTrace: StackTrace.current, | ||
), | ||
); | ||
} | ||
case Err(:final error): | ||
_log.warning(failureMessage); | ||
return Err(error); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,39 @@ | ||
import 'dart:async'; | ||
import 'dart:convert'; | ||
|
||
import 'package:hmi_core/src/core/error/failure.dart'; | ||
import 'package:hmi_core/src/core/log/log.dart'; | ||
import 'package:hmi_core/src/core/log/log_level.dart'; | ||
import 'package:hmi_core/src/core/result_new/result.dart'; | ||
import 'package:hmi_core/src/core/text_file.dart'; | ||
/// | ||
class JsonMap<T> { | ||
final FutureOr<String> _content; | ||
const JsonMap(String content) : _content = content; | ||
JsonMap.fromTextFile(TextFile textFile) : _content = textFile.content; | ||
Future<Map<String, T>> get decoded async { | ||
final map = const JsonCodec().decode(await _content) as Map<String, dynamic>; | ||
return map.cast<String, T>(); | ||
static final _log = const Log('JsonMap')..level=LogLevel.info; | ||
final FutureOr<ResultF<String>> _content; | ||
const JsonMap._(FutureOr<ResultF<String>> content) : _content = content; | ||
JsonMap.fromString(String content) : this._(Ok(content)); | ||
JsonMap.fromTextFile(TextFile textFile) : this._(textFile.content); | ||
/// | ||
Future<ResultF<Map<String, T>>> get decoded async { | ||
const failureMessage = 'Failed to parse json map from file.'; | ||
switch(await _content) { | ||
case Ok(:final value): | ||
try { | ||
final decodedJson = const JsonCodec().decode(value) as Map<String,dynamic>; | ||
return Ok( | ||
decodedJson.cast<String,T>(), | ||
); | ||
} catch(error) { | ||
_log.warning(failureMessage); | ||
return Err( | ||
Failure( | ||
message: error.toString(), | ||
stackTrace: StackTrace.current, | ||
), | ||
); | ||
} | ||
case Err(:final error): | ||
_log.warning(failureMessage); | ||
return Err(error); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.