Skip to content

Commit

Permalink
fix: compilation error on web with complex interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Tienisto committed Oct 20, 2024
1 parent 7b7bcbd commit fb24c20
Show file tree
Hide file tree
Showing 7 changed files with 363 additions and 61 deletions.
46 changes: 30 additions & 16 deletions slang/lib/src/builder/generator/generate_header.dart
Original file line number Diff line number Diff line change
Expand Up @@ -588,28 +588,42 @@ void _generateInterfaces({
// equals override
buffer.writeln();
buffer.writeln('\t@override');
buffer.write(
'\tbool operator ==(Object other) => other is ${interface.name}');
for (final attribute in interface.attributes) {
buffer.write(
' && ${attribute.attributeName} == other.${attribute.attributeName}');
}
buffer.writeln(';');
buffer.writeln('\tbool operator ==(Object other) {');
buffer.writeln('\t\tif (identical(this, other)) return true;');
buffer.writeln('\t\tif (other is! ${interface.name}) return false;');

buffer.writeln();
buffer.writeln('\t\tfinal fields = _fields;');
buffer.writeln('\t\tfinal otherFields = other._fields;');
buffer.writeln('\t\tfor (int i = 0; i < fields.length; i++) {');
buffer.writeln('\t\t\tif (fields[i] != otherFields[i]) return false;');
buffer.writeln('\t\t}');

buffer.writeln();
buffer.writeln('\t\treturn true;');
buffer.writeln('\t}');

// hashCode override
buffer.writeln();
buffer.writeln('\t@override');
buffer.write('\tint get hashCode => ');
bool multiply = false;
buffer.writeln('\tint get hashCode {');
buffer.writeln('\t\tfinal fields = _fields;');
buffer.writeln('\t\tint result = fields.first.hashCode;');
buffer.writeln('\t\tfor (final element in fields.skip(1)) {');
buffer.writeln('\t\t\tresult *= element.hashCode;');
buffer.writeln('\t\t}');

buffer.writeln();
buffer.writeln('\t\treturn result;');
buffer.writeln('\t}');

// fields
buffer.writeln();
buffer.writeln('\tList<Object> get _fields => [');
for (final attribute in interface.attributes) {
if (multiply) {
buffer.write(' * ');
}
buffer.write(attribute.attributeName);
buffer.write('.hashCode');
multiply = true;
buffer.writeln('\t\t${attribute.attributeName},');
}
buffer.writeln(';');
buffer.writeln('\t];');

buffer.writeln('}');
}
Expand Down
2 changes: 1 addition & 1 deletion slang/lib/src/builder/generator/helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const String characteristicLinkPrefix = '_root.';
String getImportName({
required I18nLocale locale,
}) {
return '_\$${locale.languageTag.replaceAll('-', '_')}';
return 'l_${locale.languageTag.replaceAll('-', '_')}';
}

/// Returns the class name of the root translation class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import 'package:slang/node.dart';
import 'package:slang_flutter/slang_flutter.dart';
export 'package:slang_flutter/slang_flutter.dart';

import 'translations_de.g.dart' deferred as _$de;
import 'translations_de.g.dart' deferred as l_de;
part 'translations_en.g.dart';

/// Supported locales.
Expand Down Expand Up @@ -52,8 +52,8 @@ enum AppLocale with BaseAppLocale<AppLocale, Translations> {
ordinalResolver: ordinalResolver,
);
case AppLocale.de:
await _$de.loadLibrary();
return _$de.TranslationsDe(
await l_de.loadLibrary();
return l_de.TranslationsDe(
overrides: overrides,
cardinalResolver: cardinalResolver,
ordinalResolver: ordinalResolver,
Expand All @@ -75,7 +75,7 @@ enum AppLocale with BaseAppLocale<AppLocale, Translations> {
ordinalResolver: ordinalResolver,
);
case AppLocale.de:
return _$de.TranslationsDe(
return l_de.TranslationsDe(
overrides: overrides,
cardinalResolver: cardinalResolver,
ordinalResolver: ordinalResolver,
Expand Down Expand Up @@ -193,30 +193,102 @@ mixin PageData {
String? get content => null;

@override
bool operator ==(Object other) => other is PageData && title == other.title && content == other.content;
bool operator ==(Object other) {
if (identical(this, other)) return true;
if (other is! PageData) return false;

final fields = _fields;
final otherFields = other._fields;
for (int i = 0; i < fields.length; i++) {
if (fields[i] != otherFields[i]) return false;
}

return true;
}

@override
int get hashCode => title.hashCode * content.hashCode;
int get hashCode {
final fields = _fields;
int result = fields.first.hashCode;
for (final element in fields.skip(1)) {
result *= element.hashCode;
}

return result;
}

List<Object> get _fields => [
title,
content,
];
}

mixin MPage {
String get title;
String? get content => null;

@override
bool operator ==(Object other) => other is MPage && title == other.title && content == other.content;
bool operator ==(Object other) {
if (identical(this, other)) return true;
if (other is! MPage) return false;

final fields = _fields;
final otherFields = other._fields;
for (int i = 0; i < fields.length; i++) {
if (fields[i] != otherFields[i]) return false;
}

return true;
}

@override
int get hashCode => title.hashCode * content.hashCode;
int get hashCode {
final fields = _fields;
int result = fields.first.hashCode;
for (final element in fields.skip(1)) {
result *= element.hashCode;
}

return result;
}

List<Object> get _fields => [
title,
content,
];
}

mixin EndData {
List<String> get stringPages;
List<Map<String, String>> get pages;

@override
bool operator ==(Object other) => other is EndData && stringPages == other.stringPages && pages == other.pages;
bool operator ==(Object other) {
if (identical(this, other)) return true;
if (other is! EndData) return false;

final fields = _fields;
final otherFields = other._fields;
for (int i = 0; i < fields.length; i++) {
if (fields[i] != otherFields[i]) return false;
}

return true;
}

@override
int get hashCode => stringPages.hashCode * pages.hashCode;
int get hashCode {
final fields = _fields;
int result = fields.first.hashCode;
for (final element in fields.skip(1)) {
result *= element.hashCode;
}

return result;
}

List<Object> get _fields => [
stringPages,
pages,
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import 'package:slang/node.dart';
import 'package:slang_flutter/slang_flutter.dart';
export 'package:slang_flutter/slang_flutter.dart';

import 'translations_de.g.dart' deferred as _$de;
import 'translations_de.g.dart' deferred as l_de;
part 'translations_en.g.dart';

/// Supported locales.
Expand Down Expand Up @@ -52,8 +52,8 @@ enum AppLocale with BaseAppLocale<AppLocale, Translations> {
ordinalResolver: ordinalResolver,
);
case AppLocale.de:
await _$de.loadLibrary();
return _$de.TranslationsDe(
await l_de.loadLibrary();
return l_de.TranslationsDe(
overrides: overrides,
cardinalResolver: cardinalResolver,
ordinalResolver: ordinalResolver,
Expand All @@ -75,7 +75,7 @@ enum AppLocale with BaseAppLocale<AppLocale, Translations> {
ordinalResolver: ordinalResolver,
);
case AppLocale.de:
return _$de.TranslationsDe(
return l_de.TranslationsDe(
overrides: overrides,
cardinalResolver: cardinalResolver,
ordinalResolver: ordinalResolver,
Expand Down
92 changes: 82 additions & 10 deletions slang/test/integration/resources/main/_expected_main.output
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import 'package:slang/node.dart';
import 'package:slang_flutter/slang_flutter.dart';
export 'package:slang_flutter/slang_flutter.dart';

import 'translations_de.g.dart' deferred as _$de;
import 'translations_de.g.dart' deferred as l_de;
part 'translations_en.g.dart';

/// Supported locales.
Expand Down Expand Up @@ -52,8 +52,8 @@ enum AppLocale with BaseAppLocale<AppLocale, Translations> {
ordinalResolver: ordinalResolver,
);
case AppLocale.de:
await _$de.loadLibrary();
return _$de.TranslationsDe(
await l_de.loadLibrary();
return l_de.TranslationsDe(
overrides: overrides,
cardinalResolver: cardinalResolver,
ordinalResolver: ordinalResolver,
Expand All @@ -75,7 +75,7 @@ enum AppLocale with BaseAppLocale<AppLocale, Translations> {
ordinalResolver: ordinalResolver,
);
case AppLocale.de:
return _$de.TranslationsDe(
return l_de.TranslationsDe(
overrides: overrides,
cardinalResolver: cardinalResolver,
ordinalResolver: ordinalResolver,
Expand Down Expand Up @@ -193,30 +193,102 @@ mixin PageData {
String? get content => null;

@override
bool operator ==(Object other) => other is PageData && title == other.title && content == other.content;
bool operator ==(Object other) {
if (identical(this, other)) return true;
if (other is! PageData) return false;

final fields = _fields;
final otherFields = other._fields;
for (int i = 0; i < fields.length; i++) {
if (fields[i] != otherFields[i]) return false;
}

return true;
}

@override
int get hashCode => title.hashCode * content.hashCode;
int get hashCode {
final fields = _fields;
int result = fields.first.hashCode;
for (final element in fields.skip(1)) {
result *= element.hashCode;
}

return result;
}

List<Object> get _fields => [
title,
content,
];
}

mixin MPage {
String get title;
String? get content => null;

@override
bool operator ==(Object other) => other is MPage && title == other.title && content == other.content;
bool operator ==(Object other) {
if (identical(this, other)) return true;
if (other is! MPage) return false;

final fields = _fields;
final otherFields = other._fields;
for (int i = 0; i < fields.length; i++) {
if (fields[i] != otherFields[i]) return false;
}

return true;
}

@override
int get hashCode => title.hashCode * content.hashCode;
int get hashCode {
final fields = _fields;
int result = fields.first.hashCode;
for (final element in fields.skip(1)) {
result *= element.hashCode;
}

return result;
}

List<Object> get _fields => [
title,
content,
];
}

mixin EndData {
List<String> get stringPages;
List<Map<String, String>> get pages;

@override
bool operator ==(Object other) => other is EndData && stringPages == other.stringPages && pages == other.pages;
bool operator ==(Object other) {
if (identical(this, other)) return true;
if (other is! EndData) return false;

final fields = _fields;
final otherFields = other._fields;
for (int i = 0; i < fields.length; i++) {
if (fields[i] != otherFields[i]) return false;
}

return true;
}

@override
int get hashCode => stringPages.hashCode * pages.hashCode;
int get hashCode {
final fields = _fields;
int result = fields.first.hashCode;
for (final element in fields.skip(1)) {
result *= element.hashCode;
}

return result;
}

List<Object> get _fields => [
stringPages,
pages,
];
}
Loading

0 comments on commit fb24c20

Please sign in to comment.