-
Notifications
You must be signed in to change notification settings - Fork 3
Add support for enumString, literal, and enumValues in Ack.object() #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
753ca39
c6852a8
2a08cbb
39e979c
d3c293d
a85b690
80379c4
58b1366
bf17755
530bb13
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -583,6 +583,16 @@ class SchemaAstAnalyzer { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| null, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case 'enumString': | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case 'literal': | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return (typeProvider.stringType, null); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case 'enumValues': | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final resolvedType = _resolveEnumValuesType(invocation, library); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (resolvedType != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return (resolvedType, null); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Fallback to String if the enum type can't be resolved | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return (typeProvider.stringType, null); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw InvalidGenerationSourceError( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'Unsupported schema method: Ack.$schemaMethod()', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -591,6 +601,59 @@ class SchemaAstAnalyzer { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Extracts the enum type name from an `Ack.enumValues<T>(...)` invocation. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Tries the explicit type argument first, then infers from the argument | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// pattern (e.g. `UserRole.values`). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String? _extractEnumTypeNameFromInvocation(MethodInvocation invocation) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // From type argument: Ack.enumValues<UserRole>(...) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final typeArgs = invocation.typeArguments?.arguments; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (typeArgs != null && typeArgs.isNotEmpty) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final typeAnnotation = typeArgs.first; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (typeAnnotation is NamedType) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return typeAnnotation.name2.lexeme; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return typeArgs.first.toString(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
leoafarias marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // From argument pattern: Ack.enumValues(UserRole.values) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final args = invocation.argumentList.arguments; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (args.isNotEmpty) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final firstArg = args.first; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (firstArg is PrefixedIdentifier && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| firstArg.identifier.name == 'values') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return firstArg.prefix.name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Resolves the enum DartType from an `Ack.enumValues<T>(...)` invocation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// by extracting the type name and looking it up in the library. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| DartType? _resolveEnumValuesType( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| MethodInvocation invocation, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| LibraryElement2 library, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final enumTypeName = _extractEnumTypeNameFromInvocation(invocation); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (enumTypeName == null) return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (final enumElement in library.enums) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (enumElement.name3 == enumTypeName) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return enumElement.thisType; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Also check classes (for class-based enums) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (final classElement in library.classes) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (classElement.name3 == enumTypeName) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return classElement.thisType; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+716
to
+926
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _ListElementRef _resolveListElementRef(Expression firstArg) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (firstArg is MethodInvocation) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| final baseInvocation = _findBaseAckInvocation(firstArg); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1112,6 +1175,10 @@ class SchemaAstAnalyzer { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return 'List<$nestedType>'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (methodName == 'enumValues') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (methodName == 'enumValues') { | |
| if (methodName == 'enumValues') { | |
| // Prefer resolved static types so we correctly handle shapes like | |
| // `alias.UserRole.values` and preserve import prefixes. | |
| final enumArgs = ref.ackBase!.argumentList.arguments; | |
| if (enumArgs.isNotEmpty) { | |
| final argType = enumArgs.first.staticType; | |
| if (argType is InterfaceType) { | |
| InterfaceType? enumType; | |
| // Typical case: `MyEnum.values` has static type `List<MyEnum>`. | |
| if (argType.isDartCoreList && argType.typeArguments.isNotEmpty) { | |
| final elementType = argType.typeArguments.first; | |
| if (elementType is InterfaceType) { | |
| enumType = elementType; | |
| } | |
| } else { | |
| // Fallback: treat the interface type itself as the enum type. | |
| enumType = argType; | |
| } | |
| final enumElement = enumType?.element2; | |
| if (enumElement is EnumElement2) { | |
| // Use the analyzer's display string to keep prefixes and drop nullability. | |
| return enumType!.getDisplayString(withNullability: false); | |
| } | |
| } | |
| } | |
| // Fall back to the existing AST-based extraction to avoid regressions. |
Uh oh!
There was an error while loading. Please reload this page.