- 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:
parent
be0a698d14
commit
649f25b265
2
.env
2
.env
@ -1,2 +1,2 @@
|
||||
app.name = 'CUBETIQ Solution'
|
||||
app.title = 'CUBETIQ Solution'
|
||||
export NAME='Sambo Chea'
|
@ -10,3 +10,9 @@
|
||||
|
||||
## 1.0.2
|
||||
- Add support null-safety
|
||||
|
||||
## 1.0.3
|
||||
- Add mutable configuration provider
|
||||
- Split tests file
|
||||
- Fixed and nullable functions
|
||||
- Able to set or remove config from system config
|
74
README.md
74
README.md
@ -1,12 +1,16 @@
|
||||
# Dart Configurable Environment
|
||||
|
||||
- [x] Allow to get property from env file
|
||||
- [x] Cache property for runtime
|
||||
- [x] Dotenv file support (use DotenvConfigurationProvider)
|
||||
- [x] Support functions (getConfig, getConfigOrNull, hasConfigKey)
|
||||
- [x] Support functions (getConfig, getConfigOrNull, hasConfigKey, setConfig)
|
||||
- [x] Support nullsafety (dart 2.12.2+)
|
||||
- [x] Custom configuration provider
|
||||
|
||||
# Example
|
||||
|
||||
```dart
|
||||
import 'package:configurable/configurable.dart';
|
||||
import 'package:configurable/dotenv_configuration_provider.dart';
|
||||
import 'package:configurable/simple_configuration_provider.dart';
|
||||
import 'package:configurable/system_config.dart';
|
||||
@ -15,22 +19,82 @@ void main() {
|
||||
var key = 'app.name';
|
||||
var value = 'CUBETIQ Solution';
|
||||
|
||||
// in-memory provider (built-in)
|
||||
/// in-memory provider (built-in)
|
||||
var simpleProvider = SimpleConfigurationProvider();
|
||||
SystemConfig.setProvider(simpleProvider);
|
||||
var result1 = SystemConfig.getOrNull(key, defaultValue: value);
|
||||
// output: CUBETIQ Solution
|
||||
|
||||
/// output: CUBETIQ Solution
|
||||
print(result1);
|
||||
|
||||
// dotenv provider (from file .env)
|
||||
/// get config functions
|
||||
getConfig('app.title', defaultValue: 'My App Title!');
|
||||
getConfigOrNull('app.null.ignore');
|
||||
|
||||
/// set config functions
|
||||
/// support only mutable configuration provider
|
||||
setConfig('my.app', 'Hello My App');
|
||||
|
||||
/// dotenv provider (from file .env)
|
||||
var dotenvProvider = DotenvConfigurationProvider();
|
||||
SystemConfig.setProvider(dotenvProvider);
|
||||
var result2 = SystemConfig.getOrNull('HOME');
|
||||
|
||||
// output: user's home directory
|
||||
/// output: user's home directory
|
||||
print(result2);
|
||||
}
|
||||
```
|
||||
|
||||
# Implement custom configuration provider
|
||||
|
||||
```dart
|
||||
class MyCustomProvider implements ConfigurationProvider {
|
||||
Map<String, String?> configDataSet = {};
|
||||
|
||||
@override
|
||||
bool containsKey(String key) {
|
||||
return configDataSet.containsKey(key);
|
||||
}
|
||||
|
||||
@override
|
||||
String? getOrNull(String key, {String? defaultValue}) {
|
||||
var value = configDataSet[key];
|
||||
|
||||
if (value == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
# Contributors
|
||||
|
||||
- Sambo Chea <sombochea@cubetiqs.com>
|
||||
|
||||
# License
|
||||
|
||||
```text
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021 CUBETIQ Solution
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
```
|
||||
|
@ -1,3 +1,4 @@
|
||||
import 'package:configurable/configurable.dart';
|
||||
import 'package:configurable/dotenv_configuration_provider.dart';
|
||||
import 'package:configurable/simple_configuration_provider.dart';
|
||||
import 'package:configurable/system_config.dart';
|
||||
@ -6,18 +7,27 @@ void main() {
|
||||
var key = 'app.name';
|
||||
var value = 'CUBETIQ Solution';
|
||||
|
||||
// in-memory provider (built-in)
|
||||
/// in-memory provider (built-in)
|
||||
var simpleProvider = SimpleConfigurationProvider();
|
||||
SystemConfig.setProvider(simpleProvider);
|
||||
var result1 = SystemConfig.getOrNull(key, defaultValue: value);
|
||||
// output: CUBETIQ Solution
|
||||
|
||||
/// output: CUBETIQ Solution
|
||||
print(result1);
|
||||
|
||||
// dotenv provider (from file .env)
|
||||
/// get config functions
|
||||
getConfig('app.title', defaultValue: 'My App Title!');
|
||||
getConfigOrNull('app.null.ignore');
|
||||
|
||||
/// set config functions
|
||||
/// support only mutable configuration provider
|
||||
setConfig('my.app', 'Hello My App');
|
||||
|
||||
/// dotenv provider (from file .env)
|
||||
var dotenvProvider = DotenvConfigurationProvider();
|
||||
SystemConfig.setProvider(dotenvProvider);
|
||||
var result2 = SystemConfig.getOrNull('HOME');
|
||||
|
||||
// output: user's home directory
|
||||
/// output: user's home directory
|
||||
print(result2);
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
name: configurable
|
||||
description: System Configuration and Dotenv Environment for Dart and Flutter
|
||||
version: 1.0.2
|
||||
version: 1.0.3
|
||||
homepage: https://github.com/CUBETIQ/system-config-dart
|
||||
repository: https://github.com/CUBETIQ/system-config-dart.git
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
import 'package:configurable/configurable.dart' show getConfigOrNull, hasConfigkey;
|
||||
import 'package:configurable/dotenv_configuration_provider.dart';
|
||||
import 'package:configurable/simple_configuration_provider.dart';
|
||||
import 'package:configurable/system_config.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
@ -7,29 +7,23 @@ void main() {
|
||||
test('get system config by key', () {
|
||||
var key = 'app.name';
|
||||
var value = 'CUBETIQ';
|
||||
|
||||
// set simple provider
|
||||
SystemConfig.setProvider(SimpleConfigurationProvider());
|
||||
|
||||
var result = SystemConfig.getOrNull(key, defaultValue: value);
|
||||
|
||||
expect(value, equals(result));
|
||||
expect(value, equals(SystemConfig.getOrNull(key)));
|
||||
});
|
||||
|
||||
test('get system config by key with dotenv provider', () {
|
||||
var key = 'app.name';
|
||||
var value = 'CUBETIQ Solution';
|
||||
|
||||
// set dotenv provider
|
||||
SystemConfig.setProvider(DotenvConfigurationProvider());
|
||||
|
||||
var result = SystemConfig.getOrNull(key);
|
||||
|
||||
expect(value, equals(result));
|
||||
expect(value, equals(SystemConfig.getOrNull(key)));
|
||||
});
|
||||
|
||||
test('get config by key with function', () {
|
||||
var key = 'app.name';
|
||||
var value = 'CUBETIQ Solution';
|
||||
|
||||
// set simple provider
|
||||
SystemConfig.setProvider(SimpleConfigurationProvider());
|
||||
|
||||
var result = getConfigOrNull(key, defaultValue: value);
|
||||
|
||||
expect(value, equals(result));
|
||||
|
@ -5,7 +5,7 @@ void main() {
|
||||
test('just call env', () {
|
||||
load();
|
||||
|
||||
var result = env['app.name'];
|
||||
var result = env['app.title'];
|
||||
|
||||
expect('CUBETIQ Solution', equals(result));
|
||||
});
|
||||
|
18
test/system_dotenv_test.dart
Normal file
18
test/system_dotenv_test.dart
Normal file
@ -0,0 +1,18 @@
|
||||
import 'package:configurable/dotenv_configuration_provider.dart';
|
||||
import 'package:configurable/system_config.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
test('get system config by key with dotenv provider', () {
|
||||
var key = 'app.title';
|
||||
var value = 'CUBETIQ Solution';
|
||||
|
||||
// set dotenv provider
|
||||
SystemConfig.setProvider(DotenvConfigurationProvider());
|
||||
|
||||
var result = SystemConfig.getOrNull(key);
|
||||
|
||||
expect(value, equals(result));
|
||||
expect(value, equals(SystemConfig.getOrNull(key)));
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue
Block a user