- Add mutable configuration provider
- Split tests file - Fixed and nullable functions - Able to set or remove config from system config
This commit is contained in:
@@ -2,8 +2,16 @@ library configurable;
|
||||
|
||||
import 'package:configurable/system_config.dart';
|
||||
|
||||
/// get config or null from system config
|
||||
String? getConfigOrNull(String key, {String? defaultValue}) =>
|
||||
SystemConfig.getOrNull(key, defaultValue: defaultValue);
|
||||
|
||||
/// get config with non-null from system config
|
||||
String getConfig(String key, {String? defaultValue}) =>
|
||||
SystemConfig.get(key, defaultValue: defaultValue);
|
||||
|
||||
/// check has config key or not from system config
|
||||
bool hasConfigkey(String key) => SystemConfig.containsKey(key);
|
||||
|
||||
/// set config into memory
|
||||
void setConfig(String key, String? value) => SystemConfig.set(key, value);
|
||||
|
||||
@@ -1,4 +1,11 @@
|
||||
/// Configuration Provider
|
||||
///
|
||||
/// @author sombochea
|
||||
/// @since 1.0.0
|
||||
abstract class ConfigurationProvider {
|
||||
/// check has key or not in implemented classes
|
||||
bool containsKey(String key);
|
||||
|
||||
/// get or null in implemented classes
|
||||
String? getOrNull(String key, {String? defaultValue});
|
||||
}
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
part of dotenv;
|
||||
|
||||
/// Dotenv Parser
|
||||
///
|
||||
/// @author sombochea
|
||||
/// @since 1.0.0
|
||||
class DotenvParser {
|
||||
static const _singleQuot = "'";
|
||||
static const _keyword = 'export';
|
||||
@@ -8,9 +12,10 @@ class DotenvParser {
|
||||
static final _surroundQuotes = RegExp(r'''^(['"])(.*)\1$''');
|
||||
static final _bashVar = RegExp(r'(?:\\)?(\$)(?:{)?([a-zA-Z_][\w]*)+(?:})?');
|
||||
|
||||
// constructor for parser
|
||||
/// constructor for parser
|
||||
const DotenvParser();
|
||||
|
||||
/// Parse the env lines into map values and return it back
|
||||
Map<String, String> parse(Iterable<String> lines) {
|
||||
var out = <String, String>{};
|
||||
lines.forEach((line) {
|
||||
@@ -73,11 +78,14 @@ class DotenvParser {
|
||||
@visibleForTesting
|
||||
String swallow(String line) => line.replaceAll(_keyword, '').trim();
|
||||
|
||||
/// Check key is valid or not
|
||||
bool _isValid(String s) => s.isNotEmpty && s.contains('=');
|
||||
|
||||
/// Has key in map values
|
||||
bool _has(Map<String, String> map, String key) =>
|
||||
map.containsKey(key) && map[key] != null;
|
||||
|
||||
/// Try to check on platform env
|
||||
String? _tryPlatformEnv(String key) {
|
||||
if (!_has(Platform.environment, key)) {
|
||||
return '';
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
import 'package:configurable/configuration_provider.dart';
|
||||
import 'package:configurable/dotenv/dotenv.dart' show env, load;
|
||||
|
||||
/// Dotenv Configuration Provider
|
||||
///
|
||||
/// @author sombochea
|
||||
/// @since 1.0.0
|
||||
class DotenvConfigurationProvider implements ConfigurationProvider {
|
||||
/// default constructor to load the platform environment
|
||||
DotenvConfigurationProvider() {
|
||||
load();
|
||||
}
|
||||
@@ -15,6 +20,7 @@ class DotenvConfigurationProvider implements ConfigurationProvider {
|
||||
String? getOrNull(String key, {String? defaultValue}) {
|
||||
var value = env[key];
|
||||
|
||||
/// if value is null, then return default value
|
||||
if (value == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
19
lib/mutable_configurable_provider.dart
Normal file
19
lib/mutable_configurable_provider.dart
Normal file
@@ -0,0 +1,19 @@
|
||||
import 'package:configurable/configuration_provider.dart';
|
||||
|
||||
/// Mutable Configuration Provider
|
||||
///
|
||||
/// @author sombochea
|
||||
/// @since 1.0.3
|
||||
abstract class MutableConfigurationProvider implements ConfigurationProvider {
|
||||
/// Allow to set the value into config values
|
||||
void set(String key, String? value);
|
||||
|
||||
/// Allow to set all values into config values
|
||||
void setAll(Map<String, String?> configs);
|
||||
|
||||
/// Allow to remove the key from config values
|
||||
void remove(String key);
|
||||
|
||||
/// Allow to remove all values from config values
|
||||
void removeAll();
|
||||
}
|
||||
@@ -1,12 +1,17 @@
|
||||
import 'package:configurable/configuration_provider.dart';
|
||||
import 'package:configurable/mutable_configurable_provider.dart';
|
||||
|
||||
class SimpleConfigurationProvider implements ConfigurationProvider {
|
||||
/// Simple Configuration Provider
|
||||
///
|
||||
/// @author sombochea
|
||||
/// @since 1.0.0
|
||||
class SimpleConfigurationProvider implements MutableConfigurationProvider {
|
||||
Map<String, String?> configs = {};
|
||||
|
||||
@override
|
||||
String? getOrNull(String key, {String? defaultValue}) {
|
||||
var value = configs[key];
|
||||
|
||||
/// if value is null, then set the default value and return it back
|
||||
if (value == null) {
|
||||
configs[key] = defaultValue;
|
||||
return defaultValue;
|
||||
@@ -19,4 +24,24 @@ class SimpleConfigurationProvider implements ConfigurationProvider {
|
||||
bool containsKey(String key) {
|
||||
return configs.containsKey(key);
|
||||
}
|
||||
|
||||
@override
|
||||
void set(String key, String? value) {
|
||||
configs[key] = value;
|
||||
}
|
||||
|
||||
@override
|
||||
void remove(String key) {
|
||||
configs.remove(key);
|
||||
}
|
||||
|
||||
@override
|
||||
void removeAll() {
|
||||
configs.clear();
|
||||
}
|
||||
|
||||
@override
|
||||
void setAll(Map<String, String?> configs) {
|
||||
configs.addAll(configs);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,25 @@
|
||||
import 'package:configurable/configuration_provider.dart';
|
||||
import 'package:configurable/mutable_configurable_provider.dart';
|
||||
import 'package:configurable/simple_configuration_provider.dart';
|
||||
|
||||
/// System Configuration Static Functions
|
||||
///
|
||||
/// @author sombochea
|
||||
/// @since 1.0.0
|
||||
class SystemConfig {
|
||||
static ConfigurationProvider? provider;
|
||||
static ConfigurationProvider? _provider;
|
||||
|
||||
/// Set configuration provider
|
||||
static void setProvider(ConfigurationProvider provider) {
|
||||
SystemConfig.provider = provider;
|
||||
_provider = provider;
|
||||
}
|
||||
|
||||
static ConfigurationProvider getProvider() {
|
||||
if (provider == null) {
|
||||
if (_provider == null) {
|
||||
setProvider(SimpleConfigurationProvider());
|
||||
}
|
||||
|
||||
return provider!;
|
||||
return _provider!;
|
||||
}
|
||||
|
||||
static String get(String key, {String? defaultValue}) {
|
||||
@@ -25,4 +31,22 @@ class SystemConfig {
|
||||
}
|
||||
|
||||
static bool containsKey(String key) => getProvider().containsKey(key);
|
||||
|
||||
/// Convert to mutable configuration provider
|
||||
static MutableConfigurationProvider _getMutableConfigurationProvider() {
|
||||
if (_provider is MutableConfigurationProvider) {
|
||||
return (_provider as MutableConfigurationProvider);
|
||||
} else {
|
||||
throw Exception(
|
||||
'Error cannot use mutable functions without mutable configuration provider!');
|
||||
}
|
||||
}
|
||||
|
||||
static void set(String key, String? value) {
|
||||
_getMutableConfigurationProvider().set(key, value);
|
||||
}
|
||||
|
||||
static void setAll(Map<String, String?> values) {
|
||||
_getMutableConfigurationProvider().setAll(values);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user