Skip to content

Commit

Permalink
Changes as per review
Browse files Browse the repository at this point in the history
  • Loading branch information
mosuem committed Jan 8, 2024
1 parent 23a1c53 commit 592e3ab
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 50 deletions.
2 changes: 1 addition & 1 deletion pkgs/messages/lib/src/deserializer/deserializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import '../message_format.dart';
import '../plural_selector.dart';

abstract class Deserializer<T extends MessageList> {
T deserialize(PluralSelector intl);
T deserialize(PluralSelector selector);
}
17 changes: 9 additions & 8 deletions pkgs/messages/lib/src/deserializer/deserializer_json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class JsonDeserializer extends Deserializer<MessageListJson> {
}

@override
MessageListJson deserialize(PluralSelector intl) {
MessageListJson deserialize(PluralSelector selector) {
if (preamble.version != serializationVersion) {
throw ArgumentError(
'''This message has version ${preamble.version}, while the deserializer has version $serializationVersion''');
Expand All @@ -32,7 +32,7 @@ class JsonDeserializer extends Deserializer<MessageListJson> {
return MessageListJson(
preamble,
_messages,
intl,
selector,
mapping?.map((key, value) => MapEntry(
int.parse(key, radix: serializationRadix),
int.parse(value as String, radix: serializationRadix),
Expand Down Expand Up @@ -89,15 +89,16 @@ class JsonDeserializer extends Deserializer<MessageListJson> {
for (var i = 0; i < submessages.length - 1; i += 2) {
final msg = getMessage(submessages[i + 1]);
final messageMarker = submessages[i];
if (messageMarker case Plural.few) {
if (messageMarker case PluralMarker.few) {
fewMessage = msg;
} else if (messageMarker case Plural.many) {
} else if (messageMarker case PluralMarker.many) {
manyMessage = msg;
} else if (messageMarker case final int d) {
numberCases[d] = msg;
} else if (messageMarker case final int digit) {
numberCases[digit] = msg;
} else if (messageMarker is String &&
messageMarker.startsWith(Plural.wordCase)) {
wordCases[int.parse(messageMarker.substring(1))] = msg;
messageMarker.startsWith(PluralMarker.wordCase)) {
final digit = int.parse(messageMarker.substring(1));
wordCases[digit] = msg;
}
}
return PluralMessage(
Expand Down
20 changes: 2 additions & 18 deletions pkgs/messages/lib/src/message_format.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,15 @@ abstract class Preamble {

abstract class MessageList {
Preamble get preamble;
PluralSelector get intl;
PluralSelector get pluralSelector;

String generateStringAtIndex(int index, List args);

String generateStringAtId(String id, List args);
}

enum PluralEnum {
zeroWord,
zeroNumber,
oneWord,
oneNumber,
twoWord,
twoNumber,
few,
many,
}

class Plural {
sealed class PluralMarker {
static const String wordCase = 'w';
static const String few = 'f';
static const String many = 'm';
}

class Gender {
static const int female = 1;
static const int male = 2;
}
10 changes: 5 additions & 5 deletions pkgs/messages/lib/src/message_list_json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@ class JsonPreamble extends Preamble {

class MessageListJson extends MessageList {
final List<Message> messages;
final PluralSelector _intl;
final PluralSelector _selector;
final JsonPreamble _preamble;
final Map<int, int>? messageIndices;

@override
PluralSelector get intl => _intl;
PluralSelector get pluralSelector => _selector;

@override
Preamble get preamble => _preamble;

MessageListJson(
this._preamble,
this.messages,
this._intl,
this._selector,
this.messageIndices,
);

Expand All @@ -64,11 +64,11 @@ class MessageListJson extends MessageList {
String generateStringAtId(String id, List args) => messages
.where((element) => element.id == id)
.first
.generateString(args, pluralSelector: _intl);
.generateString(args, pluralSelector: _selector);

@override
String generateStringAtIndex(int index, List args) =>
messages[getIndex(index)].generateString(args, pluralSelector: _intl);
messages[getIndex(index)].generateString(args, pluralSelector: _selector);

int getIndex(int index) => messageIndices?[index] ?? index;
}
46 changes: 35 additions & 11 deletions pkgs/messages_builder/lib/generation_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ class GenerationOptions {
/// dart native code.
final DeserializationType deserialization;

///
/// The header to add to all generated files, for example for licensing.
final String header;

/// The origin of the algorithm for determining which plural case to use.
final PluralSelectorType pluralSelector;

GenerationOptions({
Expand All @@ -62,21 +63,31 @@ class GenerationOptions {
deserialization: DeserializationType.web,
messageCalls: (messagesOptions?['generateMethods'] as bool?) ?? true,
findById: (messagesOptions?['generateFindById'] as bool?) ?? false,
indexType: IndexType.values
.where((type) =>
type.name == messagesOptions?['generateFindBy'] as String?)
.firstOrNull ??
IndexType.integer,
indexType: _indexType(messagesOptions),
header: messagesOptions?['header'] as String? ??
'Generated by package:messages_builder.',
pluralSelector: PluralSelectorType.values
.where((type) =>
type.name == messagesOptions?['pluralSelector'] as String?)
.firstOrNull ??
PluralSelectorType.intl,
pluralSelector: _pluralSelector(messagesOptions),
);
return generationOptions;
}

static IndexType _indexType(YamlMap? messagesOptions) {
final generateFindString = messagesOptions?['generateFindBy'] as String?;
return generateFindString != null
? IndexType.values
.where((type) => type.name == generateFindString)
.first
: IndexType.integer;
}

static PluralSelectorType _pluralSelector(YamlMap? messagesOptions) {
final pluralSelectorString = messagesOptions?['pluralSelector'] as String?;
return pluralSelectorString != null
? PluralSelectorType.values
.where((type) => type.name == pluralSelectorString)
.first
: PluralSelectorType.intl;
}
}

enum SerializationType {
Expand All @@ -87,14 +98,27 @@ enum DeserializationType {
web;
}

/// How the indexing of the messages should be implemented.
enum IndexType {
/// No indexing.
none,

/// Indexing via a collection of `static int`s.
integer,

/// Indexing via an enum.
enumerate;
}

/// The origin of the algorithm for determining which plural case to use.
enum PluralSelectorType {
/// From `package:intl`.
intl,

/// From `package:intl4x`.
intl4x,

/// A user-specified algorithm.
custom;
}
13 changes: 7 additions & 6 deletions pkgs/messages_serializer/lib/src/serializer_json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,20 @@ class JsonSerializer extends Serializer<String> {
m.add(encodeMessage(message.other));
final caseIndices = <Object>[];
if (message.few != null) {
caseIndices.add(Plural.few);
caseIndices.add(PluralMarker.few);
caseIndices.add(encodeMessage(message.few!));
}
if (message.many != null) {
caseIndices.add(Plural.many);
caseIndices.add(PluralMarker.many);
caseIndices.add(encodeMessage(message.many!));
}
for (final entry in message.numberCases.entries) {
caseIndices.add(entry.key);
caseIndices.add(encodeMessage(entry.value));
for (final MapEntry(key: caseIndex, value: messageIndex)
in message.numberCases.entries) {
caseIndices.add(caseIndex);
caseIndices.add(encodeMessage(messageIndex));
}
for (final entry in message.wordCases.entries) {
caseIndices.add(Plural.wordCase + entry.key.toString());
caseIndices.add(PluralMarker.wordCase + entry.key.toString());
caseIndices.add(encodeMessage(entry.value));
}
m.add(caseIndices);
Expand Down
3 changes: 2 additions & 1 deletion pkgs/messages_shrinker/lib/messages_shrinker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class MessageShrinker {
final sizeBefore = buffer.length;
final json = JsonDeserializer(buffer).deserialize(
(howMany, {few, locale, many, numberCases, required other, wordCases}) {
throw UnimplementedError();
throw StateError('As the deserialized MessageList is not used, but '
'just immediately reserialized, this selector will not be called.');
},
);
final data = JsonSerializer(json.preamble.hasIds)
Expand Down

0 comments on commit 592e3ab

Please sign in to comment.