Skip to content

Commit 02eed82

Browse files
authored
Merge pull request #37 from TatsuUkraine/release-3.0.0-nullsafe.0
Release 3.0.0-nullsafety.0 -> master
2 parents b831b74 + e9654de commit 02eed82

File tree

10 files changed

+95
-92
lines changed

10 files changed

+95
-92
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [3.0.0] - 26.04.2021
2+
3+
- migrate to null-safe
4+
15
## [2.3.0] - 19.04.2021
26

37
- update deps versions

example/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: environment_config_example
22
version: 0.0.1
33

44
environment:
5-
sdk: ">=2.7.0 <3.0.0"
5+
sdk: ">=2.12.0 <3.0.0"
66

77
dependencies:
88
environment_config:

lib/argument_parser.dart

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ArgumentParser {
1414

1515
/// Defines if `config` key was specified
1616
/// during command run
17-
String parseConfigPath() {
17+
String? parseConfigPath() {
1818
ArgParser parser = ArgParser()..addOption(ConfigFieldType.CONFIG);
1919

2020
final ArgResults argResults = parser.parse(
@@ -63,12 +63,10 @@ class ArgumentParser {
6363
);
6464
});
6565

66-
final parsedArguments = parser.parse(arguments);
66+
final ArgResults parsedArguments = parser.parse(arguments);
6767

68-
return Map.fromIterable(
69-
parsedArguments.options,
70-
key: (dynamic key) => key,
71-
value: (dynamic key) => parsedArguments[key],
72-
);
68+
return {
69+
for (String key in parsedArguments.options) key: parsedArguments[key],
70+
};
7371
}
7472
}

lib/config.dart

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,19 @@ class Config {
1717

1818
final Iterable<FieldConfig> _fields;
1919

20-
Config(this.config, this.arguments, Iterable<FieldConfig> fields, this.extConfig): _fields = fields;
21-
22-
factory Config.fromMap(
23-
PlatformValueProvider valueProvider,
24-
Map<dynamic, dynamic> config,
25-
Map<dynamic, dynamic> args
26-
) {
27-
final String devExtension = config[ConfigFieldType.DEV_EXTENSION];
20+
Config(
21+
this.config, this.arguments, Iterable<FieldConfig> fields, this.extConfig)
22+
: _fields = fields;
23+
24+
factory Config.fromMap(PlatformValueProvider valueProvider,
25+
Map<dynamic, dynamic> config, Map<String, dynamic> args) {
26+
final String? devExtension = config[ConfigFieldType.DEV_EXTENSION];
2827
final Map<dynamic, dynamic> configFields = config[ConfigFieldType.FIELDS];
29-
final Map<dynamic, dynamic> extensions = config[ConfigFieldType.EXTENSIONS] ?? {};
28+
final Map<dynamic, dynamic> extensions =
29+
config[ConfigFieldType.EXTENSIONS] ?? {};
30+
3031
Map<dynamic, dynamic> extension = {};
31-
String extensionName;
32+
String? extensionName;
3233

3334
if (devExtension != null && args[devExtension]) {
3435
extensionName = devExtension;
@@ -44,40 +45,38 @@ class Config {
4445
extension = extensions[extensionName] ?? {};
4546
}
4647

47-
Map<dynamic, dynamic> extensionFields = extension[ConfigFieldType.FIELDS] ?? {};
48+
Map<dynamic, dynamic> extensionFields =
49+
extension[ConfigFieldType.FIELDS] ?? {};
4850

49-
final Iterable<FieldConfig> fields = configFields.keys
50-
.map((key) => FieldConfig(
51-
valueProvider,
52-
key,
53-
config[ConfigFieldType.FIELDS][key] ?? {},
54-
extensionFields[key] ?? {},
55-
args[key]
56-
));
51+
final Iterable<FieldConfig> fields = configFields.keys.map((key) =>
52+
FieldConfig(
53+
valueProvider,
54+
key,
55+
config[ConfigFieldType.FIELDS][key] ?? {},
56+
extensionFields[key] ?? {},
57+
args[key]));
5758

5859
return Config(config, args, fields, extension);
5960
}
6061

6162
/// Target file for generated config class
62-
String get filePath {
63-
return 'lib/${_getConfigValue(ConfigFieldType.PATH, 'environment_config.dart')}';
64-
}
63+
String get filePath =>
64+
'lib/${_getConfigValue(ConfigFieldType.PATH, 'environment_config.dart')}';
6565

6666
/// Target file for `.env` params
67-
String get dotEnvFilePath {
68-
return _getConfigValue(ConfigFieldType.DOTENV_PATH, '.env');
69-
}
67+
String get dotEnvFilePath =>
68+
_getConfigValue(ConfigFieldType.DOTENV_PATH, '.env')!;
7069

7170
/// Provides config class name
7271
String get className {
73-
String className = _getConfigValue(ConfigFieldType.CLASS);
72+
String? className = _getConfigValue(ConfigFieldType.CLASS);
7473

7574
if (className != null) {
7675
return className;
7776
}
7877

7978
final String fileName =
80-
RegExp(r'\/([\w_-]+)\.dart$').firstMatch(filePath).group(1);
79+
RegExp(r'\/([\w_-]+)\.dart$').firstMatch(filePath)!.group(1)!;
8180

8281
return fileName
8382
.split('_')
@@ -86,20 +85,18 @@ class Config {
8685
}
8786

8887
/// Fields, that should be exported to `.env` file
89-
Iterable<FieldConfig> get dotEnvFields {
90-
return _fields.where((field) => field.isDotEnv);
91-
}
88+
Iterable<FieldConfig> get dotEnvFields =>
89+
_fields.where((field) => field.isDotEnv);
9290

9391
/// Fields, that should be exported to Dart config file
94-
Iterable<FieldConfig> get classConfigFields {
95-
return _fields.where((field) => field.isConfigField);
96-
}
92+
Iterable<FieldConfig> get classConfigFields =>
93+
_fields.where((field) => field.isConfigField);
9794

9895
/// Collection if imports, that should be added to config class
9996
Iterable<String> get imports => [
100-
...(config[ConfigFieldType.IMPORTS]?.toList() ?? []),
101-
...(extConfig[ConfigFieldType.IMPORTS]?.toList() ?? []),
102-
];
97+
...(config[ConfigFieldType.IMPORTS]?.toList() ?? []),
98+
...(extConfig[ConfigFieldType.IMPORTS]?.toList() ?? []),
99+
];
103100

104101
/// If class should contain `const` constructor
105102
bool get isClassConst => config[ConfigFieldType.CONST] ?? false;
@@ -110,7 +107,7 @@ class Config {
110107
/// Defines if generator should try to create Dart config file
111108
bool get createConfigClass => classConfigFields.isNotEmpty;
112109

113-
String _getConfigValue(key, [String defaultValue]) {
110+
String? _getConfigValue(key, [String? defaultValue]) {
114111
if (arguments.containsKey(key) && !arguments[key].isEmpty) {
115112
return arguments[key];
116113
}

lib/config_generator.dart

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,14 @@ class ConfigGenerator {
2020
if (config.createConfigClass) {
2121
futures.add(_generateClass());
2222
}
23-
23+
2424
if (config.createDotEnv) {
2525
futures.add(_generateDotEnv());
2626
}
2727

2828
if (futures.isEmpty) {
29-
throw ValidationError(
30-
ConfigFieldType.FIELDS,
31-
'At least one field should be defined for `.env` or Dart config class'
32-
);
29+
throw ValidationError(ConfigFieldType.FIELDS,
30+
'At least one field should be defined for `.env` or Dart config class');
3331
}
3432

3533
return Future.wait(futures);
@@ -50,14 +48,15 @@ class ConfigGenerator {
5048
Class((ClassBuilder builder) => builder
5149
..constructors.addAll(constructors)
5250
..name = config.className
53-
..fields.addAll(config.classConfigFields.map((FieldConfig field) => Field(
54-
(FieldBuilder builder) => builder
55-
..name = field.name
56-
..static = field.isStatic
57-
..modifier = field.modifier
58-
..type = Reference(field.type)
59-
..assignment = Code(field.value),
60-
)))),
51+
..fields.addAll(
52+
config.classConfigFields.map((FieldConfig field) => Field(
53+
(FieldBuilder builder) => builder
54+
..name = field.name
55+
..static = field.isStatic
56+
..modifier = field.modifier
57+
..type = Reference(field.type)
58+
..assignment = Code(field.value),
59+
)))),
6160
]));
6261

6362
final classDefinition =

lib/config_loader.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const String _CONFIG_KEY = 'environment_config';
2222
/// - pubspec.yaml
2323
///
2424
/// If no config file found will throw exception
25-
Future<YamlMap> loadConfig(String path) async {
25+
Future<YamlMap> loadConfig(String? path) async {
2626
List<String> files = [..._CONFIG_FILES];
2727

2828
if (path != null) {
@@ -41,7 +41,7 @@ Future<YamlMap> loadConfig(String path) async {
4141
continue;
4242
}
4343

44-
final YamlMap config = loadYaml(yamlString);
44+
final YamlMap? config = loadYaml(yamlString);
4545

4646
if (config == null) {
4747
continue;

lib/errors/validation_error.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'config_error.dart';
22

33
class ValidationError extends ConfigError {
44
final String field;
5-
final String message;
5+
final String? message;
66

77
ValidationError(this.field, [this.message]);
88

lib/field_config.dart

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,15 @@ class FieldConfig {
1717
final Map<dynamic, dynamic> extField;
1818

1919
/// Value provided from command params
20-
final String _value;
20+
final String? _value;
2121

2222
final PlatformValueProvider _valueProvider;
2323

2424
FieldConfig(
25-
PlatformValueProvider valueProvider,
26-
this.name,
27-
this.field,
28-
this.extField,
29-
[String value]
30-
) : _value = value, _valueProvider = valueProvider {
25+
PlatformValueProvider valueProvider, this.name, this.field, this.extField,
26+
[String? value])
27+
: _value = value,
28+
_valueProvider = valueProvider {
3129
if (_fieldValue == null) {
3230
throw ValidationError(name, '"$name" is required');
3331
}
@@ -57,7 +55,9 @@ class FieldConfig {
5755
return false;
5856
}
5957

60-
return extField[ConfigFieldType.CONST] ?? field[ConfigFieldType.CONST] ?? true;
58+
return extField[ConfigFieldType.CONST] ??
59+
field[ConfigFieldType.CONST] ??
60+
true;
6161
}
6262

6363
/// Is Field should be defined as STATIC
@@ -73,37 +73,42 @@ class FieldConfig {
7373
///
7474
/// If `pattern` is specified, value will injected into it
7575
String get value {
76-
String pattern = _pattern;
76+
String? pattern = _pattern;
7777

78-
if (_pattern == null && type == 'String') {
78+
if (pattern == null && _isStringType) {
7979
pattern = '\'__VALUE__\'';
8080
}
8181

8282
if (pattern == null) {
83-
return _fieldValue;
83+
return _fieldValue!;
8484
}
8585

86-
return pattern.replaceAll(_PATTERN_REGEXP, _fieldValue);
86+
return pattern.replaceAll(_PATTERN_REGEXP, _fieldValue!);
8787
}
8888

8989
/// Value for key in `.env` file
90-
String get dotEnvValue {
91-
return _pattern?.replaceAll(_PATTERN_REGEXP, _fieldValue) ?? _fieldValue;
92-
}
90+
String get dotEnvValue =>
91+
_pattern?.replaceAll(_PATTERN_REGEXP, _fieldValue!) ?? _fieldValue!;
9392

94-
String get _pattern => extField[ConfigFieldType.PATTERN] ?? field[ConfigFieldType.PATTERN];
93+
String? get _pattern =>
94+
extField[ConfigFieldType.PATTERN] ?? field[ConfigFieldType.PATTERN];
9595

96-
String get _globalValue {
97-
final String globalKey = extField[ConfigFieldType.ENV_VAR] ?? field[ConfigFieldType.ENV_VAR];
96+
String? get _globalValue {
97+
final String? globalKey =
98+
extField[ConfigFieldType.ENV_VAR] ?? field[ConfigFieldType.ENV_VAR];
9899

99100
if ((globalKey ?? '').isNotEmpty) {
100-
return _valueProvider.getValue(globalKey);
101+
return _valueProvider.getValue(globalKey!);
101102
}
102103

103104
return null;
104105
}
105106

106-
String get _fieldValue {
107-
return (_value ?? _globalValue ?? extField[ConfigFieldType.DEFAULT] ?? field[ConfigFieldType.DEFAULT])?.toString();
108-
}
109-
}
107+
String? get _fieldValue => (_value ??
108+
_globalValue ??
109+
extField[ConfigFieldType.DEFAULT] ??
110+
field[ConfigFieldType.DEFAULT])
111+
?.toString();
112+
113+
bool get _isStringType => const ['String', 'String?'].contains(type);
114+
}

lib/platform_value_provider.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ class PlatformValueProvider {
55
final Map<String, String> data = Platform.environment;
66

77
/// Returns value from platform env variable
8-
String getValue(String key) {
8+
String? getValue(String key) {
99
if (data.containsKey(key)) {
1010
return data[key];
1111
}
1212

1313
return null;
1414
}
15-
}
15+
}

pubspec.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
name: environment_config
22
description: Environment specific config generator. Allows to specify env configuration during CI/CD build. Primarily created to simplify Flutter build configuration
3-
version: 2.3.0
3+
version: 3.0.0
44
homepage: https://github.com/TatsuUkraine/dart_environment_config
55
repository: https://github.com/TatsuUkraine/dart_environment_config
66
issue_tracker: https://github.com/TatsuUkraine/dart_environment_config/issues
77

88
environment:
9-
sdk: ">=2.2.2 <3.0.0"
9+
sdk: '>=2.12.0 <3.0.0'
1010

1111
dependencies:
12-
dart_style: ">=2.0.0 <3.0.0"
13-
code_builder: ">=3.0.0 <4.0.0"
14-
args: ">=2.0.0 <3.0.0"
15-
yaml: ">=3.0.0 <4.0.0"
12+
dart_style: '>=2.0.0 <3.0.0'
13+
code_builder: '>=4.0.0 <5.0.0'
14+
args: '>=2.0.0 <3.0.0'
15+
yaml: '>=3.0.0 <4.0.0'

0 commit comments

Comments
 (0)