Skip to content

Commit

Permalink
Updated for null safety
Browse files Browse the repository at this point in the history
  • Loading branch information
parnham committed Mar 23, 2021
1 parent 9ad6814 commit fedbb24
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 64 deletions.
4 changes: 2 additions & 2 deletions example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class Example
// int get Bad;

// @Translate('Test')
// String test;
// String test = "";

// @Translate('NonAbstract')
// String get NonAbstract => "Hi";
Expand Down Expand Up @@ -60,7 +60,7 @@ abstract class Example

class ExampleResources extends Resources
{
@override Future<Map<String, String>> GetModuleEntries(String module, String language) async
@override Future<Map<String, String>> GetModuleEntries(String module, String? language) async
{
return {
"replace": 'Replace "{1}", {0}',
Expand Down
10 changes: 5 additions & 5 deletions example/example.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 36 additions & 37 deletions lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@ String _key(KeyFormat format, String name, String override)
switch (format)
{
case KeyFormat.None: return name;
case KeyFormat.Kebab: return name.replaceAllMapped(_upperCase, (m) => m.start > 0
? '-${m.group(0).toLowerCase()}'
: m.group(0).toLowerCase()
case KeyFormat.Kebab: return name.replaceAllMapped(_upperCase, (m) =>
(m.start > 0 ? '-' : '') + (m[0]?.toLowerCase() ?? '?')
);
}

return name;
}

// Retrieve the key from the annotation. If the
Expand All @@ -40,8 +37,8 @@ String _keyOverride(Element e)
{
return _translateChecker
.firstAnnotationOfExact(e)
.getField('key')
.toStringValue();
?.getField('key')
?.toStringValue() ?? '';
}


Expand All @@ -60,13 +57,12 @@ class TranslatableGenerator extends GeneratorForAnnotation<Translatable>
);
}

final classElement = element as ClassElement;
final className = '_\$${classElement.name}';
final className = '_\$${element.name}';
final name = annotation.read('module').stringValue;
final withMixin = annotation.read('withMixin').boolValue ? "with ${classElement.name}" : "";
final fields = classElement.fields.where((f) => _translateChecker.hasAnnotationOfExact(f.getter) || _translateChecker.hasAnnotationOfExact(f));
final methods = classElement.methods.where((m) => _translateChecker.hasAnnotationOfExact(m));
final lookups = classElement.methods.where((m) => _lookupChecker.hasAnnotationOfExact(m));
final withMixin = annotation.read('withMixin').boolValue ? "with ${element.name}" : "";
final fields = element.fields.where((f) => _translateChecker.hasAnnotationOfExact(f.getter ?? f));// || _translateChecker.hasAnnotationOfExact(f));
final methods = element.methods.where((m) => _translateChecker.hasAnnotationOfExact(m));
final lookups = element.methods.where((m) => _lookupChecker.hasAnnotationOfExact(m));
final format = KeyFormat.values.singleWhere(
(v) => annotation.read('format').objectValue.getField(v.toString().split('.')[1]) != null
);
Expand All @@ -76,19 +72,24 @@ class TranslatableGenerator extends GeneratorForAnnotation<Translatable>

for (var f in fields)
{
final key = _key(format, f.name, _keyOverride(f.getter));
final getter = f.getter;

if (getter != null)
{
final key = _key(format, f.name, _keyOverride(getter));

IsAbstract(f.getter);
ReturnsString(f.type, f);
DoesNotContainKey(f.getter, values, key);
IsAbstract(getter);
ReturnsString(f.type, f);
DoesNotContainKey(getter, values, key);

values[key] = _translateChecker
.firstAnnotationOfExact(f.getter)
.getField('source')
.toStringValue()
.replaceAll('"', '\\"');
values[key] = _translateChecker
.firstAnnotationOfExact(getter)
?.getField('source')
?.toStringValue()
?.replaceAll('"', '\\"') ?? '';

buffer.write('String get ${f.name} => values["${key}"];\n');
buffer.write('String get ${f.name} => values["${key}"] ?? "";\n');
}
}

for (var m in methods)
Expand All @@ -104,11 +105,11 @@ class TranslatableGenerator extends GeneratorForAnnotation<Translatable>
final list = m.parameters.map((p) => p.name).join(', ');
values[key] = _translateChecker
.firstAnnotationOfExact(m)
.getField('source')
.toStringValue()
.replaceAll('"', '\\"');
?.getField('source')
?.toStringValue()
?.replaceAll('"', '\\"') ?? '';

buffer.write('String ${m.name}($parameters) => TranslatableModule.Substitute(values["${key}"], [ $list ]);\n');
buffer.write('String ${m.name}($parameters) => TranslatableModule.Substitute(values["${key}"] ?? "", [ $list ]);\n');
}

for (var l in lookups)
Expand All @@ -119,22 +120,20 @@ class TranslatableGenerator extends GeneratorForAnnotation<Translatable>

final table = _lookupChecker
.firstAnnotationOfExact(l)
.getField('table')
.toMapValue()
.map<String, String>((k, v) => MapEntry(
k.toStringValue(),
v.toStringValue().replaceAll('"', '\\"')
));
?.getField('table')
?.toMapValue()
?.map<String, String>((k, v) => MapEntry(
k?.toStringValue() ?? '',
v?.toStringValue()?.replaceAll('"', '\\"') ?? ''
)) ?? {};

table.forEach((k, v) => DoesNotContainKey(l, values, k));
values.addAll(table);
buffer.write('String ${l.name}(String key) => values[key];\n');
buffer.write('String ${l.name}(String key) => values[key] ?? "";\n');
}



yield '''
class $className extends TranslatableModule $withMixin implements ${classElement.name}
class $className extends TranslatableModule $withMixin implements ${element.name}
{
$className(Resources resources) : super(resources, "$name", {
${values.keys.map((k) => '"${k}": "${values[k]}"').join(',\n')}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ class TranslatableModule

static String Substitute(String text, List<String> values)
{
return text.replaceAllMapped(_matcher, (m) => values[int.parse(m[1])]);
return text.replaceAllMapped(_matcher, (m) => values[int.parse(m[1] ?? '0')]);
}
}
22 changes: 9 additions & 13 deletions lib/src/resources.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ abstract class Resources
}


Future Initialise({ void onError(String) }) async
Future Initialise({ void onError(String)? }) async
{
List<Future<TranslatableModule>> loading = [];

Expand Down Expand Up @@ -43,25 +43,21 @@ abstract class Resources
// is stored in your server-side application. If language is `null` then return
// the values for the appropriate language based on the browser request header. If
// language is a known code then return that language.
Future<Map<String, String>> GetModuleEntries(String module, String language);
Future<Map<String, String>> GetModuleEntries(String module, String? language);


// Update a module with translations for a specific language or based on the browser
// request header if language is `null`.
Future<TranslatableModule> LoadModule(TranslatableModule module, String language) async
Future<TranslatableModule> LoadModule(TranslatableModule module, String? language) async
{
var entries = await GetModuleEntries(module.name, language);
var entries = await GetModuleEntries(module.name, language);
module.orphaned = entries.keys.any((k) => !module.values.containsKey(k));
module.missing = false;

if (entries != null)
for (var k in module.values.keys)
{
module.orphaned = entries.keys.any((k) => !module.values.containsKey(k));
module.missing = false;

for (var k in module.values.keys)
{
if (entries.containsKey(k)) module.values[k] = entries[k];
else module.missing = true;
}
if (entries.containsKey(k)) module.values[k] = entries[k] ?? '';
else module.missing = true;
}

return module;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/type_checks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void IsAbstract(ExecutableElement e)
}

// Deprecate this when type.isDartCoreString becomes available (analyzer 0.36.x)
bool _IsString(DartType type) => type.element != null && type.element.library.isDartCore && type.element.name == "String";
bool _IsString(DartType type) => (type.element?.library?.isDartCore ?? false) && type.element?.name == "String";

void ReturnsString(DartType type, Element e)
{
Expand Down
10 changes: 5 additions & 5 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
name: translatable
version: 1.1.1
version: 1.2.0
author: Dan Parnham <[email protected]>
homepage: https://github.com/emergent-design/translatable
description: >-
Generate translatable getters/functions that are mapped to allow
remote storage and modification of translations.
environment:
sdk: '>=2.0.0 <3.0.0'
sdk: '>=2.12.0 <3.0.0'

dependencies:
build: ^1.2.1
source_gen: ^0.9.4+6
build: ^2.0.0
source_gen: ^1.0.0

dev_dependencies:
build_runner: ^1.9.0
build_runner: ^1.12.2

0 comments on commit fedbb24

Please sign in to comment.