Skip to content

Commit

Permalink
Merge branch 'v6.2.0-beta.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
pichillilorenzo committed Nov 9, 2024
2 parents 4f550f0 + edb670b commit abf9572
Show file tree
Hide file tree
Showing 378 changed files with 10,248 additions and 1,646 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ class ExchangeableEnum {
final bool fromValueMethod;
final bool toNativeValueMethod;
final bool fromNativeValueMethod;
final bool nameMethod;
final bool toNameMethod;
final bool byNameMethod;
final bool asNameMapMethod;
final bool toStringMethod;
final bool hashCodeMethod;
final bool equalsOperator;
Expand All @@ -15,6 +19,10 @@ class ExchangeableEnum {
this.fromValueMethod = true,
this.toNativeValueMethod = true,
this.fromNativeValueMethod = true,
this.nameMethod = true,
this.toNameMethod = true,
this.byNameMethod = true,
this.asNameMapMethod = true,
this.toStringMethod = true,
this.hashCodeMethod = true,
this.equalsOperator = true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_inappwebview_internal_annotations
description: Internal annotations used by the generator of flutter_inappwebview plugin
version: 1.1.1
version: 1.2.0
homepage: https://github.com/pichillilorenzo/flutter_inappwebview

environment:
Expand Down
92 changes: 78 additions & 14 deletions dev_packages/generators/lib/src/exchangeable_enum_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,13 @@ class ExchangeableEnumGenerator
}

if (annotation.read("fromValueMethod").boolValue && (!visitor.methods.containsKey("fromValue") ||
Util.methodHasIgnore(visitor.methods['fromNativeValue']!))) {
Util.methodHasIgnore(visitor.methods['fromValue']!))) {
final hasBitwiseOrOperator =
annotation.read("bitwiseOrOperator").boolValue;
classBuffer.writeln("""
///Gets a possible [$extClassName] instance from [${enumValue.type}] value.
static $extClassName? fromValue(${enumValue.type}${!Util.typeIsNullable(enumValue.type) ? '?' : ''} value) {
if (value != null) {
if (value != null) {
try {
return $extClassName.values
.firstWhere((element) => element.toValue() == value);
Expand All @@ -230,7 +230,7 @@ class ExchangeableEnumGenerator
classBuffer.writeln("""
///Gets a possible [$extClassName] instance from a native value.
static $extClassName? fromNativeValue(${enumNativeValue.type}${!Util.typeIsNullable(enumNativeValue.type) ? '?' : ''} value) {
if (value != null) {
if (value != null) {
try {
return $extClassName.values
.firstWhere((element) => element.toNativeValue() == value);
Expand All @@ -243,6 +243,44 @@ class ExchangeableEnumGenerator
""");
}

if (annotation.read("nameMethod").boolValue && annotation.read("byNameMethod").boolValue &&
(!visitor.methods.containsKey("byName") || Util.methodHasIgnore(visitor.methods['byName']!))) {
classBuffer.writeln("""
/// Gets a possible [$extClassName] instance value with name [name].
///
/// Goes through [$extClassName.values] looking for a value with
/// name [name], as reported by [$extClassName.name].
/// Returns the first value with the given name, otherwise `null`.
static $extClassName? byName(String? name) {
if (name != null) {
try {
return $extClassName.values
.firstWhere((element) => element.name() == name);
} catch (e) {
return null;
}
}
return null;
}
""");
}

if (annotation.read("nameMethod").boolValue && annotation.read("asNameMapMethod").boolValue &&
(!visitor.methods.containsKey("asNameMap") || Util.methodHasIgnore(visitor.methods['asNameMap']!))) {
classBuffer.writeln("""
/// Creates a map from the names of [$extClassName] values to the values.
///
/// The collection that this method is called on is expected to have
/// values with distinct names, like the `values` list of an enum class.
/// Only one value for each name can occur in the created map,
/// so if two or more values have the same name (either being the
/// same value, or being values of different enum type), at most one of
/// them will be represented in the returned map.
static Map<String, $extClassName> asNameMap() =>
<String, $extClassName>{for (final value in $extClassName.values) value.name(): value};
""");
}

for (final entry in methodEntriesSorted) {
final methodElement = entry.value;
if (Util.methodHasIgnore(methodElement)) {
Expand Down Expand Up @@ -282,6 +320,28 @@ class ExchangeableEnumGenerator
""");
}

if (annotation.read("nameMethod").boolValue && (!visitor.methods.containsKey("name") ||
Util.methodHasIgnore(visitor.methods['name']!))) {
classBuffer.writeln('///Gets the name of the value.');
classBuffer.writeln('String name() {');
classBuffer.writeln('switch(_value) {');
for (final entry in fieldEntriesSorted) {
final fieldName = entry.key;
final fieldElement = entry.value;
if (!fieldElement.isPrivate && fieldElement.isStatic) {
final fieldValue = fieldElement.computeConstantValue()?.getField("_value");
dynamic constantValue = fieldValue?.toIntValue();
if (enumValue.type.isDartCoreString) {
constantValue = "'${fieldValue?.toStringValue()}'";
}
classBuffer.writeln("case $constantValue: return '$fieldName';");
}
}
classBuffer.writeln('}');
classBuffer.writeln('return _value.toString();');
classBuffer.writeln('}');
}

if (annotation.read("hashCodeMethod").boolValue && (!visitor.fields.containsKey("hashCode") ||
Util.methodHasIgnore(visitor.methods['hashCode']!))) {
classBuffer.writeln("""
Expand Down Expand Up @@ -315,19 +375,23 @@ class ExchangeableEnumGenerator
if (enumValue.type.isDartCoreString) {
classBuffer.writeln('return _value;');
} else {
classBuffer.writeln('switch(_value) {');
for (final entry in fieldEntriesSorted) {
final fieldName = entry.key;
final fieldElement = entry.value;
if (!fieldElement.isPrivate && fieldElement.isStatic) {
final fieldValue =
fieldElement.computeConstantValue()?.getField("_value");
final constantValue = fieldValue?.toIntValue();
classBuffer.writeln("case $constantValue: return '$fieldName';");
if (annotation.read("nameMethod").boolValue) {
classBuffer.writeln('return name();');
} else {
classBuffer.writeln('switch(_value) {');
for (final entry in fieldEntriesSorted) {
final fieldName = entry.key;
final fieldElement = entry.value;
if (!fieldElement.isPrivate && fieldElement.isStatic) {
final fieldValue =
fieldElement.computeConstantValue()?.getField("_value");
final constantValue = fieldValue?.toIntValue();
classBuffer.writeln("case $constantValue: return '$fieldName';");
}
}
classBuffer.writeln('}');
classBuffer.writeln('return _value.toString();');
}
classBuffer.writeln('}');
classBuffer.writeln('return _value.toString();');
}
classBuffer.writeln('}');
}
Expand Down
141 changes: 110 additions & 31 deletions dev_packages/generators/lib/src/exchangeable_object_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ class ExchangeableObjectGenerator
final nullable = annotation.read("nullableFromMapFactory").boolValue;
classBuffer
.writeln('static $extClassName${nullable ? '?' : ''} fromMap(');
classBuffer.writeln('Map<String, dynamic>${nullable ? '?' : ''} map');
classBuffer.writeln(
'Map<String, dynamic>${nullable ? '?' : ''} map, {EnumMethod? enumMethod}');
classBuffer.writeln(') {');
if (nullable) {
classBuffer.writeln('if (map == null) { return null; }');
Expand Down Expand Up @@ -368,9 +369,10 @@ class ExchangeableObjectGenerator
customDeserializer.enclosingElement.name;
if (deserializerClassName != null) {
value =
"$deserializerClassName.${customDeserializer.name}($value)";
"$deserializerClassName.${customDeserializer.name}($value, enumMethod: enumMethod)";
} else {
value = "${customDeserializer.name}($value)";
value =
"${customDeserializer.name}($value, enumMethod: enumMethod)";
}
} else {
value = getFromMapValue(value, fieldElement.type);
Expand Down Expand Up @@ -434,7 +436,7 @@ class ExchangeableObjectGenerator
(!visitor.methods.containsKey("toMap") ||
Util.methodHasIgnore(visitor.methods['toMap']!))) {
classBuffer.writeln('///Converts instance to a map.');
classBuffer.writeln('Map<String, dynamic> toMap() {');
classBuffer.writeln('Map<String, dynamic> toMap({EnumMethod? enumMethod}) {');
classBuffer.writeln('return {');
final fieldElements = <FieldElement>[];
if (superClass != null) {
Expand Down Expand Up @@ -474,9 +476,9 @@ class ExchangeableObjectGenerator
final serializerClassName = customSerializer.enclosingElement.name;
if (serializerClassName != null) {
mapValue =
"$serializerClassName.${customSerializer.name}($mapValue)";
"$serializerClassName.${customSerializer.name}($mapValue, enumMethod: enumMethod)";
} else {
mapValue = "${customSerializer.name}($mapValue)";
mapValue = "${customSerializer.name}($mapValue, enumMethod: enumMethod)";
}
} else {
mapValue = getToMapValue(fieldName, fieldElement.type);
Expand All @@ -492,7 +494,7 @@ class ExchangeableObjectGenerator
?.getField("toMapMergeWith")
?.toBoolValue();
if (toMapMergeWith == true) {
classBuffer.writeln('...${methodElement.name}(),');
classBuffer.writeln('...${methodElement.name}(enumMethod: enumMethod),');
}
}
classBuffer.writeln('};');
Expand Down Expand Up @@ -559,6 +561,8 @@ class ExchangeableObjectGenerator

String getFromMapValue(String value, DartType elementType) {
final fieldTypeElement = elementType.element;
// remove class reference terminating with "_"
final classNameReference = fieldTypeElement?.name?.replaceFirst("_", "");
final isNullable = Util.typeIsNullable(elementType);
final displayString = elementType.getDisplayString(withNullability: false);
if (displayString == "Uri") {
Expand Down Expand Up @@ -625,23 +629,36 @@ class ExchangeableObjectGenerator
return "$value${isNullable ? '?' : ''}.cast<${genericTypes.elementAt(0)}, ${genericTypes.elementAt(1)}>()";
} else if (fieldTypeElement != null && hasFromMapMethod(fieldTypeElement)) {
final hasNullableFromMap = hasNullableFromMapFactory(fieldTypeElement);
// remove class reference terminating with "_"
return fieldTypeElement.name!.replaceFirst("_", "") +
".fromMap($value?.cast<String, dynamic>())${!isNullable && hasNullableFromMap ? '!' : ''}";
return classNameReference! +
".fromMap($value?.cast<String, dynamic>(), enumMethod: enumMethod)${!isNullable && hasNullableFromMap ? '!' : ''}";
} else {
final hasFromValue =
fieldTypeElement != null && hasFromValueMethod(fieldTypeElement);
final hasFromNativeValue = fieldTypeElement != null &&
hasFromNativeValueMethod(fieldTypeElement);
if (fieldTypeElement != null && (hasFromValue || hasFromNativeValue)) {
if (hasFromNativeValue) {
// remove class reference terminating with "_"
value = fieldTypeElement.name!.replaceFirst("_", "") +
'.fromNativeValue($value)';
final hasFromValue = fieldTypeElement != null && hasFromValueMethod(fieldTypeElement);
final hasFromNativeValue = fieldTypeElement != null && hasFromNativeValueMethod(fieldTypeElement);
final hasByName = fieldTypeElement != null && hasByNameMethod(fieldTypeElement);
if (fieldTypeElement != null && (hasFromValue || hasFromNativeValue || hasByName)) {
if ([hasFromValue, hasFromNativeValue, hasByName].where((e) => e).length > 1) {
String? defaultEnumMethodValue = null;
if (hasFromNativeValue) {
defaultEnumMethodValue = "EnumMethod.nativeValue";
} else if (hasFromValue) {
defaultEnumMethodValue = "EnumMethod.value";
} else {
defaultEnumMethodValue = "EnumMethod.name";
}
var wrapper = "switch (enumMethod ?? $defaultEnumMethodValue) {";
wrapper += "EnumMethod.nativeValue => " + (hasFromNativeValue ? classNameReference! + '.fromNativeValue($value)' : "null") + ", ";
wrapper += "EnumMethod.value => " + (hasFromValue ? classNameReference! + '.fromValue($value)' : "null") + ", ";
wrapper += "EnumMethod.name => " + (hasByName ? classNameReference! + '.byName($value)' : "null");
wrapper += "}";
value = wrapper;
} else {
// remove class reference terminating with "_"
value = fieldTypeElement.name!.replaceFirst("_", "") +
'.fromValue($value)';
if (hasFromNativeValue) {
value = classNameReference! + '.fromNativeValue($value)';
} else if (hasFromValue) {
value = classNameReference! + '.fromValue($value)';
} else {
value = classNameReference! + '.byName($value)';
}
}
if (!isNullable) {
value += '!';
Expand Down Expand Up @@ -686,17 +703,35 @@ class ExchangeableObjectGenerator
} else if (fieldTypeElement != null && hasToMapMethod(fieldTypeElement)) {
return fieldName +
(Util.typeIsNullable(elementType) ? '?' : '') +
'.toMap()';
'.toMap(enumMethod: enumMethod)';
} else {
final hasToValue =
fieldTypeElement != null && hasToValueMethod(fieldTypeElement);
final hasToNativeValue =
fieldTypeElement != null && hasToNativeValueMethod(fieldTypeElement);
if (fieldTypeElement != null && (hasToValue || hasToNativeValue)) {
if (hasToNativeValue) {
return fieldName + (isNullable ? '?' : '') + '.toNativeValue()';
final hasToValue = fieldTypeElement != null && hasToValueMethod(fieldTypeElement);
final hasToNativeValue = fieldTypeElement != null && hasToNativeValueMethod(fieldTypeElement);
final hasName = fieldTypeElement != null && hasNameMethod(fieldTypeElement);
if (fieldTypeElement != null && (hasToValue || hasToNativeValue || hasName)) {
if ([hasToValue, hasToNativeValue, hasName].where((e) => e).length > 1) {
String? defaultEnumMethodValue = null;
if (hasToNativeValue) {
defaultEnumMethodValue = "EnumMethod.nativeValue";
} else if (hasToValue) {
defaultEnumMethodValue = "EnumMethod.value";
} else {
defaultEnumMethodValue = "EnumMethod.name";
}
var wrapper = "switch (enumMethod ?? $defaultEnumMethodValue) {";
wrapper += "EnumMethod.nativeValue => " + (hasToNativeValue ? (fieldName + (isNullable ? '?' : '') + '.toNativeValue()') : "null") + ", ";
wrapper += "EnumMethod.value => " + (hasToValue ? (fieldName + (isNullable ? '?' : '') + '.toValue()') : "null") + ", ";
wrapper += "EnumMethod.name => " + (hasName ? (fieldName + (isNullable ? '?' : '') + '.name()') : "null");
wrapper += "}";
return wrapper;
} else {
return fieldName + (isNullable ? '?' : '') + '.toValue()';
if (hasToNativeValue) {
return fieldName + (isNullable ? '?' : '') + '.toNativeValue()';
} else if (hasToValue) {
return fieldName + (isNullable ? '?' : '') + '.toValue()';
} else {
return fieldName + (isNullable ? '?' : '') + '.name()';
}
}
}
}
Expand Down Expand Up @@ -820,6 +855,28 @@ class ExchangeableObjectGenerator
return false;
}

bool hasByNameMethod(Element element) {
final hasAnnotation = _coreCheckerEnum.hasAnnotationOf(element);
final byNameMethod = _coreCheckerEnum
.firstAnnotationOfExact(element)
?.getField('byNameMethod')
?.toBoolValue() ??
false;
if (hasAnnotation && byNameMethod) {
return true;
}

final fieldVisitor = ModelVisitor();
element.visitChildren(fieldVisitor);
for (var entry in fieldVisitor.methods.entries) {
if (entry.key == "byName") {
return true;
}
}

return false;
}

bool hasToValueMethod(Element element) {
final hasAnnotation = _coreCheckerEnum.hasAnnotationOf(element);
final hasToValueMethod = _coreCheckerEnum
Expand Down Expand Up @@ -863,4 +920,26 @@ class ExchangeableObjectGenerator

return false;
}

bool hasNameMethod(Element element) {
final hasAnnotation = _coreCheckerEnum.hasAnnotationOf(element);
final hasNameMethod = _coreCheckerEnum
.firstAnnotationOfExact(element)
?.getField('nameMethod')
?.toBoolValue() ??
false;
if (hasAnnotation && hasNameMethod) {
return true;
}

final fieldVisitor = ModelVisitor();
element.visitChildren(fieldVisitor);
for (var entry in fieldVisitor.methods.entries) {
if (entry.key == "name") {
return true;
}
}

return false;
}
}
3 changes: 2 additions & 1 deletion dev_packages/generators/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ dependencies:
build: ^2.4.1
source_gen: ^1.5.0
collection: any
flutter_inappwebview_internal_annotations: ^1.1.1
flutter_inappwebview_internal_annotations: #^1.2.0
path: ../flutter_inappwebview_internal_annotations

dev_dependencies:
build_runner: ^2.4.12
Expand Down
Loading

0 comments on commit abf9572

Please sign in to comment.