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

Add Model.qualifiedNameOfMember, add fast way to find the path to JsonBufferBuilder #87

Merged
merged 3 commits into from
Oct 10, 2024

Conversation

davidmorgan
Copy link
Contributor

@davidmorgan davidmorgan commented Oct 4, 2024

For #72, this seems reasonably convincing that we can get the path to a node cheaply, which means we don't have to add it everywhere explicitly in the schema.

This doesn't add a use case yet, probably there will be some tweaks when there is a first use case; one specific note is that the link to parent could also store the key in the parent, which would avoid the search through parent's keys that currently happens. But that's easy to change later.

///
/// TODO(davidmorgan): this works for any node, but it's not clear yet which types of
/// node we want this functionality exposed for.
/// TODO(davidmorgan): a list of path segments is probably more useful than `String`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Related to this comment, what is the format of the String? Dot separated paths? What are you intended to do with it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As mentioned in the PR opening post, there is no specific use case yet :)

This was prompted by your suggestion that if a Member knows where it is in the tree, you can fully generate code referencing it. So the anticipated use case is for the augmentation builders/helpers.

I guess the first use case will change the format to something actually useful :)

Copy link
Contributor

Choose a reason for hiding this comment

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

I think specifically, we just want to be able to get a qualified name for any given member? Can this just return that instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

/// TODO(davidmorgan): a list of path segments is probably more useful than `String`.
String? pathToMember(Member member) => _pathTo(member.node);

/// Returns the path in the model to [node], or `null` if [node] is not in this `Model`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// Returns the path in the model to [node], or `null` if [node] is not in this `Model`.
/// Returns the path in the model to [node], or `null` if [node] is not in this [Model].

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

pkgs/dart_model/lib/src/dart_model.dart Outdated Show resolved Hide resolved
pkgs/dart_model/lib/src/dart_model.dart Outdated Show resolved Hide resolved
pkgs/dart_model/lib/src/dart_model.dart Outdated Show resolved Hide resolved
pkgs/dart_model/lib/src/dart_model.dart Outdated Show resolved Hide resolved
pkgs/dart_model/lib/src/json_buffer/closed_map.dart Outdated Show resolved Hide resolved
@@ -39,3 +39,13 @@ class _IteratorFunctionIterable<T> extends Iterable<T> {
@override
Iterator<T> get iterator => _function();
}

/// A `Map` in a `JsonBufferBuilder`.
abstract interface class MapInBuffer {
Copy link
Contributor

Choose a reason for hiding this comment

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

Fwiw we could also use Expandos for this, which would also allow us to record parents for other types of maps (and wouldn't need this extra interface and associated type checks).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, thanks.

Another way to tweak would be to lean on the type more, the parent of a map in a buffer is definitely also a map in a buffer. So there should only be one type check to get the whole path. (And as noted elsewhere, we should store the key used as well as the parent, so it really is fast).

pkgs/dart_model/lib/src/json_buffer/typed_map.dart Outdated Show resolved Hide resolved
final member = model.uris['package:dart_model/dart_model.dart']!
.scopes['JsonData']!.members['_root']!;
expect(model.pathToMember(member),
'/uris/package:dart_model/dart_model.dart/scopes/JsonData/members/_root');
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't look like a very useful path, how are you supposed to parse out the library URI? What is the intended use case for these paths?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mentioned elsewhere :) indeed I expect it will change to path segments or maybe something even more specific like a lookup of exactly library URI.

It is intended for augmentation builders which we don't have quite yet :)

@davidmorgan
Copy link
Contributor Author

Per our discussion--will update to QualifiedName as suggested before merging. Thanks :)

@davidmorgan davidmorgan changed the title Add Model.pathToMember, add fast way to find the path to JsonBufferBuilder Add Model.qualifiedNameOfMember, add fast way to find the path to JsonBufferBuilder Oct 10, 2024
@davidmorgan davidmorgan merged commit d306c9b into dart-lang:main Oct 10, 2024
45 checks passed
@davidmorgan davidmorgan deleted the node-path branch October 10, 2024 08:48
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.

2 participants