forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 3
Add TypeMap abstract class #30
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
Merged
FionaBronwen
merged 5 commits into
feature/graphql
from
fionabronwen/type-map-abstraction
Jun 5, 2025
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8736851
Add TypeMap abstract class
FionaBronwen 5361f39
Add type constraints and nominal type for map key
FionaBronwen 69715a9
Fix naming, use MapIterator
FionaBronwen 2cc3738
remove unnecessary null check
FionaBronwen 7a06174
Check if a type is already registered before registering
FionaBronwen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import { UsageFlags } from "@typespec/compiler"; | ||
|
|
||
| /** | ||
| * TypeSpec context for type mapping | ||
| * @template T - The TypeSpec type | ||
| */ | ||
| export interface TSPContext<T = any> { | ||
| type: T; // The TypeSpec type | ||
| usageFlag: UsageFlags; // How the type is being used (input, output, etc.) | ||
| name?: string; // Optional name override | ||
|
FionaBronwen marked this conversation as resolved.
Outdated
|
||
| metadata?: Record<string, any>; // Optional additional metadata | ||
|
FionaBronwen marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| /** | ||
| * Base TypeMap for all GraphQL type mappings | ||
| * @template T - The TypeSpec type | ||
| * @template G - The GraphQL type | ||
| */ | ||
| export abstract class TypeMap<T, G> { | ||
|
FionaBronwen marked this conversation as resolved.
Outdated
|
||
| // Map of materialized GraphQL types | ||
| protected materializedMap = new Map<string, G>(); | ||
|
|
||
| // Map of registration contexts | ||
| protected registrationMap = new Map<string, TSPContext<T>>(); | ||
|
FionaBronwen marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** | ||
| * Register a TypeSpec type with context for later materialization | ||
| * @param context - The TypeSpec context | ||
| * @returns The name used for registration | ||
| */ | ||
| register(context: TSPContext<T>): string { | ||
| const name = this.getNameFromContext(context); | ||
| this.registrationMap.set(name, context); | ||
|
FionaBronwen marked this conversation as resolved.
|
||
| return name; | ||
| } | ||
|
|
||
| /** | ||
| * Get the materialized GraphQL type | ||
| * @param name - The type name | ||
| * @returns The materialized GraphQL type or undefined | ||
| */ | ||
| get(name: string): G | undefined { | ||
| // Return already materialized type if available | ||
| if (this.materializedMap.has(name)) { | ||
| return this.materializedMap.get(name); | ||
| } | ||
|
|
||
| // Attempt to materialize if registered | ||
| const context = this.registrationMap.get(name); | ||
| if (context) { | ||
| const materializedType = this.materialize(context); | ||
| if (materializedType) { | ||
|
FionaBronwen marked this conversation as resolved.
Outdated
|
||
| this.materializedMap.set(name, materializedType); | ||
| return materializedType; | ||
| } | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Check if a type is registered | ||
| */ | ||
| isRegistered(name: string): boolean { | ||
| return this.registrationMap.has(name); | ||
| } | ||
|
|
||
| /** | ||
| * Get all materialized types | ||
| */ | ||
| getAllMaterialized(): G[] { | ||
| return Array.from(this.materializedMap.values()); | ||
| } | ||
|
FionaBronwen marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** | ||
| * Reset the type map | ||
| */ | ||
| reset(): void { | ||
| this.materializedMap.clear(); | ||
| this.registrationMap.clear(); | ||
| } | ||
|
|
||
| /** | ||
| * Get a name from a context | ||
| */ | ||
| protected abstract getNameFromContext(context: TSPContext<T>): string; | ||
|
FionaBronwen marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** | ||
| * Materialize a type from a context | ||
| */ | ||
| protected abstract materialize(context: TSPContext<T>): G | undefined; | ||
|
FionaBronwen marked this conversation as resolved.
Outdated
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.