Unverified Commit 728a1933 authored by Jenn Magder's avatar Jenn Magder Committed by GitHub

Migrate gen_l10n to null safety (#80763)

parent 4cceeaa0
......@@ -187,7 +187,7 @@ class OptionalParameter {
// }
//
class Placeholder {
Placeholder(this.resourceId, this.name, Map<String, dynamic> attributes)
Placeholder(this.resourceId, this.name, Map<String, Object?> attributes)
: assert(resourceId != null),
assert(name != null),
example = _stringAttribute(resourceId, name, attributes, 'example'),
......@@ -212,10 +212,10 @@ class Placeholder {
static String? _stringAttribute(
String resourceId,
String name,
Map<String, dynamic> attributes,
Map<String, Object?> attributes,
String attributeName,
) {
final dynamic value = attributes[attributeName];
final Object? value = attributes[attributeName];
if (value == null) {
return null;
}
......@@ -231,9 +231,9 @@ class Placeholder {
static List<OptionalParameter> _optionalParameters(
String resourceId,
String name,
Map<String, dynamic> attributes
Map<String, Object?> attributes
) {
final dynamic value = attributes['optionalParameters'];
final Object? value = attributes['optionalParameters'];
if (value == null) {
return <OptionalParameter>[];
}
......@@ -267,7 +267,7 @@ class Placeholder {
// localized string to be shown for the template ARB file's locale.
// The docs for the Placeholder explain how placeholder entries are defined.
class Message {
Message(Map<String, dynamic> bundle, this.resourceId, bool isResourceAttributeRequired)
Message(Map<String, Object?> bundle, this.resourceId, bool isResourceAttributeRequired)
: assert(bundle != null),
assert(resourceId != null && resourceId.isNotEmpty),
value = _value(bundle, resourceId),
......@@ -298,23 +298,23 @@ class Message {
);
}
static String _value(Map<String, dynamic> bundle, String resourceId) {
final dynamic value = bundle[resourceId];
static String _value(Map<String, Object?> bundle, String resourceId) {
final Object? value = bundle[resourceId];
if (value == null) {
throw L10nException('A value for resource "$resourceId" was not found.');
}
if (value is! String) {
throw L10nException('The value of "$resourceId" is not a string.');
}
return bundle[resourceId] as String;
return value;
}
static Map<String, dynamic> _attributes(
Map<String, dynamic> bundle,
static Map<String, Object?>? _attributes(
Map<String, Object?> bundle,
String resourceId,
bool isResourceAttributeRequired,
) {
final dynamic attributes = bundle['@$resourceId'];
final Object? attributes = bundle['@$resourceId'];
if (isResourceAttributeRequired) {
if (attributes == null) {
throw L10nException(
......@@ -324,7 +324,7 @@ class Message {
}
}
if (attributes != null && attributes is! Map<String, dynamic>) {
if (attributes != null && attributes is! Map<String, Object?>) {
throw L10nException(
'The resource attribute "@$resourceId" is not a properly formatted Map. '
'Ensure that it is a map with keys that are strings.'
......@@ -340,20 +340,20 @@ class Message {
);
}
return attributes as Map<String, dynamic>;
return attributes as Map<String, Object?>?;
}
static String? _description(
Map<String, dynamic> bundle,
Map<String, Object?> bundle,
String resourceId,
bool isResourceAttributeRequired,
) {
final Map<String, dynamic> resourceAttributes = _attributes(bundle, resourceId, isResourceAttributeRequired);
final Map<String, Object?>? resourceAttributes = _attributes(bundle, resourceId, isResourceAttributeRequired);
if (resourceAttributes == null) {
return null;
}
final dynamic value = resourceAttributes['description'];
final Object? value = resourceAttributes['description'];
if (value == null) {
return null;
}
......@@ -366,27 +366,27 @@ class Message {
}
static List<Placeholder> _placeholders(
Map<String, dynamic> bundle,
Map<String, Object?> bundle,
String resourceId,
bool isResourceAttributeRequired,
) {
final Map<String, dynamic> resourceAttributes = _attributes(bundle, resourceId, isResourceAttributeRequired);
final Map<String, Object?>? resourceAttributes = _attributes(bundle, resourceId, isResourceAttributeRequired);
if (resourceAttributes == null) {
return <Placeholder>[];
}
final dynamic allPlaceholdersMap = resourceAttributes['placeholders'];
final Object? allPlaceholdersMap = resourceAttributes['placeholders'];
if (allPlaceholdersMap == null) {
return <Placeholder>[];
}
if (allPlaceholdersMap is! Map<String, dynamic>) {
if (allPlaceholdersMap is! Map<String, Object?>) {
throw L10nException(
'The "placeholders" attribute for message $resourceId, is not '
'properly formatted. Ensure that it is a map with string valued keys.'
);
}
return allPlaceholdersMap.keys.map<Placeholder>((String placeholderName) {
final dynamic value = allPlaceholdersMap[placeholderName];
if (value is! Map<String, dynamic>) {
final Object? value = allPlaceholdersMap[placeholderName];
if (value is! Map<String, Object?>) {
throw L10nException(
'The value of the "$placeholderName" placeholder attribute for message '
'"$resourceId", is not properly formatted. Ensure that it is a map '
......@@ -403,9 +403,9 @@ class AppResourceBundle {
factory AppResourceBundle(File file) {
assert(file != null);
// Assuming that the caller has verified that the file exists and is readable.
Map<String, dynamic> resources;
Map<String, Object?> resources;
try {
resources = json.decode(file.readAsStringSync()) as Map<String, dynamic>;
resources = json.decode(file.readAsStringSync()) as Map<String, Object?>;
} on FormatException catch (e) {
throw L10nException(
'The arb file ${file.path} has the following formatting issue: \n'
......@@ -413,7 +413,7 @@ class AppResourceBundle {
);
}
String localeString = resources['@@locale'] as String;
String? localeString = resources['@@locale'] as String?;
// Look for the first instance of an ISO 639-1 language code, matching exactly.
final String fileName = file.fileSystem.path.basenameWithoutExtension(file.path);
......@@ -470,10 +470,10 @@ class AppResourceBundle {
final File file;
final LocaleInfo locale;
final Map<String, dynamic> resources;
final Map<String, Object?> resources;
final Iterable<String> resourceIds;
String translationFor(Message message) => resources[message.resourceId] as String;
String? translationFor(Message message) => resources[message.resourceId] as String?;
@override
String toString() {
......
......@@ -98,75 +98,12 @@ void main() {
);
});
testWithoutContext('setInputDirectory fails if input string is null', () {
testWithoutContext('setting className fails if input string is empty', () {
_standardFlutterDirectoryL10nSetup(fs);
try {
LocalizationsGenerator.inputDirectoryFromPath(fs, null, fs.directory('bogus'));
LocalizationsGenerator.classNameFromString('');
} on L10nException catch (e) {
expect(e.message, contains('cannot be null'));
return;
}
fail(
'LocalizationsGenerator.setInputDirectory should fail if the '
'input string is null.'
);
});
testWithoutContext(
'setOutputDirectory fails if output string is null while not using the '
'synthetic package option',
() {
_standardFlutterDirectoryL10nSetup(fs);
try {
LocalizationsGenerator.outputDirectoryFromPath(fs, null, false, null);
} on L10nException catch (e) {
expect(e.message, contains('cannot be null'));
return;
}
fail(
'LocalizationsGenerator.setOutputDirectory should fail if the '
'input string is null.'
);
},
);
testWithoutContext('setTemplateArbFile fails if inputDirectory is null', () {
try {
LocalizationsGenerator.templateArbFileFromFileName(defaultTemplateArbFileName, null);
} on L10nException catch (e) {
expect(e.message, contains('cannot be null'));
return;
}
fail(
'LocalizationsGenerator.setTemplateArbFile should fail if the '
'inputDirectory is not specified.'
);
});
testWithoutContext('setTemplateArbFile fails if templateArbFileName is null', () {
_standardFlutterDirectoryL10nSetup(fs);
try {
LocalizationsGenerator.templateArbFileFromFileName(null, fs.directory('bogus'));
} on L10nException catch (e) {
expect(e.message, contains('cannot be null'));
return;
}
fail(
'LocalizationsGenerator.setTemplateArbFile should fail if the '
'templateArbFileName passed in is null.'
);
});
testWithoutContext('setting className fails if input string is null', () {
_standardFlutterDirectoryL10nSetup(fs);
try {
LocalizationsGenerator.classNameFromString(null);
} on L10nException catch (e) {
expect(e.message, contains('cannot be null'));
expect(e.message, contains('cannot be empty'));
return;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment