Skip to content
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

new arg to filter by subclass ref #2105

Merged
merged 16 commits into from
Feb 19, 2025

Conversation

tintinthong
Copy link
Contributor

@tintinthong tintinthong commented Jan 30, 2025

Purpose of this PR is to enable spec to choose linkedExamples that link by type

Screenshot 2025-02-05 at 16 36 17 Screenshot 2025-02-05 at 16 41 07

API change

  • <@fields @subclassType={{ref}} />. This means users can control the linksTo and linksToMany editor via this arg. It will only work if you pass in a ref which is indeed a subclass of the type. Otherwise, it defaults to original behaviour
  • I named it subclassType as the arg @lukemelia different from here https://discord.com/channels/584043165066199050/900021750367129641/1334556182001483826 bcos, I think its wrong to overwrite the field entirely. Now the api works st, you only pass the ref that way @fields.author is still true to the type of author but you can for example, <@fields.author @subclassType=bigAuthorRef>. The issue with adding another filter to the every = [ ] in the card chooser is that the index never supports 2 types so we always have to commit to one

@tintinthong tintinthong marked this pull request as draft January 30, 2025 08:50
Copy link

github-actions bot commented Jan 30, 2025

Host Test Results

    1 files  ±0      1 suites  ±0   23m 17s ⏱️ -50s
765 tests +1  763 ✔️ +1  2 💤 ±0  0 ±0 
770 runs  +1  768 ✔️ +1  2 💤 ±0  0 ±0 

Results for commit 0ce4775. ± Comparison against base commit 0d0443d.

♻️ This comment has been updated with latest results.

@tintinthong tintinthong changed the base branch from refactor-links-to-many-component to main February 3, 2025 02:37
@tintinthong tintinthong force-pushed the new-arg-to-filter-catalog-entry-type branch from ab3f885 to c8b1f13 Compare February 3, 2025 02:55
@tintinthong tintinthong force-pushed the new-arg-to-filter-catalog-entry-type branch from c8b1f13 to 9f0fe1c Compare February 5, 2025 04:30
@tintinthong tintinthong marked this pull request as ready for review February 5, 2025 08:37
@tintinthong tintinthong requested review from a team and removed request for a team February 5, 2025 08:47
@tintinthong tintinthong changed the title new arg to filter spec type new arg to filter by subclass ref Feb 6, 2025
Copy link
Contributor

@burieberry burieberry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is all good, but I think it would be fine to make an exception and not check isInsideAncestorChain when the type we're comparing to is the base carddef. isInsideAncestorChain will end up loading all the cards in the ancestor chain when we already know that all card subtypes descend from card def. In that case, you can just confirm via isCardDef for the subtype if we're not sure if the type is a card.

@@ -143,6 +147,7 @@ export class LinksToEditor extends GlimmerComponent<Signature> {

private chooseCard = restartableTask(async () => {
let type = identifyCard(this.args.field.card) ?? baseCardRef;
type = await getNarrowestType(this.args.subclassType, type, myLoader());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, you can only call this function if a subclassType arg was provided

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually do you even need to check if the type is the base CardDef? We know that all cards descend from it, so maybe in that case you can skip loading the whole chain and just use the provided subtype.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually do you even need to check if the type is the base CardDef? We know that all cards descend from it, so maybe in that case you can skip loading the whole chain and just use the provided subtype.

I think we do. We need to check if type is base CardDef as a terminating condition to "stop checking".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@burieberry maybe we can talk thru this after standup. Probably some small misunderstanding

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@burieberry I have made the change that you have suggested

@tintinthong tintinthong requested review from burieberry and a team February 11, 2025 10:18
Comment on lines 231 to 240
async function isInsideAncestorChainOfCardDef(
codeRef: CodeRef,
loader: Loader,
): Promise<boolean | undefined> {
let card = await loadCard(codeRef, { loader: loader });
if (isCardDef(card)) {
return true;
}
return false;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about this and I think it would be good to add to this some of your previous logic. This makes sense if the type we're comparing to is base CardDef, or if we only care that the subtype is a card def, regardless of what the type we're comparing to is. If type is not base card def, then we should consider if we want to confirm that subtype is descended from it. We currently only use this subtype argument for BoxelSpec, but making this change would allow us to use it in different cases where the type is not base card def. I'm open to hearing what other people in the team thinks about this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For context, this is the diff change 02e8408. Previously, we use to have more generic handling of the subtype

@burieberry burieberry requested a review from a team February 13, 2025 15:37
@@ -227,3 +227,27 @@ export function humanReadable(ref: CodeRef): string {
function assertNever(value: never) {
return new Error(`should never happen ${value}`);
}

async function isInsideAncestorChainOfCardDef(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

javascript has a similar kind of function called typeof. maybe that is a clearer name for this method: isTypeOf()

Copy link
Contributor

@habdelra habdelra Feb 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, i take that back, this looks like a synonmym for isCardDef. let's just overload that specific method so it takes either a codeRef and loader and an already running card:

export function isCardDef(card: any): card is typeof CardDef;
export function isCardDef(codeRef: CodeRef, loader: Loader): boolean
export function isCardDef(cardOrCodeRef: any, loader?: Loader): boolean { ... }

(not too sure how all the type assertion works out since you can't assert a CodeRef param is a CarDef). if the overload doesn't work, at least try to name this is similar as you can to isCardDef, so that it is clear that we are using the same semantics.

Copy link
Contributor Author

@tintinthong tintinthong Feb 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh ok I think this worked but loadCard is async so I used .then within the isCardDef implementation. I also added a note on using isCardDef in getNarrowestType

Named: {
format?: Format;
displayContainer?: boolean;
subclassType?: ResolvedCodeRef;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps typeConstraint or allowedClass would be a better name?

@tintinthong tintinthong requested review from lukemelia, burieberry, habdelra and a team February 18, 2025 11:55
Copy link
Contributor

@burieberry burieberry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks much better

@tintinthong tintinthong merged commit ff33dc6 into main Feb 19, 2025
44 of 46 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants