Skip to content

Commit

Permalink
Address review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmorgan committed Oct 10, 2024
1 parent 9c2a6cf commit ad92034
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
40 changes: 28 additions & 12 deletions pkgs/dart_model/lib/src/dart_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,36 @@ extension ModelExtension on Model {
/// types of node we want this functionality exposed for.
/// TODO(davidmorgan): a list of path segments is probably more useful than
/// `String`.
String? pathToMember(Member member) => _pathTo(member.node);
QualifiedName? qualifiedNameOfMember(Member member) =>
_qualifiedNameOf(member.node);

/// Returns the path in the model to [node], or `null` if [node] is not in this [Model].
String? _pathTo(Map<String, Object?> node) {
if (node == this.node) return '';
final parent = _getParent(node);
if (parent == null) return null;
for (final entry in parent.entries) {
if (entry.value == node) {
final parentPath = _pathTo(parent);
return parentPath == null ? null : '$parentPath/${entry.key}';
}
/// Returns the [QualifiedName] in the model to [node], or `null` if [node] is not in this [Model].
QualifiedName? _qualifiedNameOf(Map<String, Object?> node) {
final members = _getParent(node);
if (members == null) return null;
final interface = _getParent(members);
if (interface == null) return null;
final scopes = _getParent(interface);
if (scopes == null) return null;
final library = _getParent(scopes);
if (library == null) return null;
final libraries = _getParent(library);
if (libraries == null) return null;

final uri = _keyOf(library, libraries);
final name = _keyOf(interface, scopes);

return QualifiedName(uri: uri, name: name);
}

/// Returns the key of [value] in [map].
///
/// Throws if [value] is not in [map].
String _keyOf(Object value, Map<String, Object?> map) {
for (final entry in map.entries) {
if (entry.value == value) return entry.key;
}
return null;
throw ArgumentError('Value not in map: $value, $map');
}

/// Gets the `Map` that contains [node], or `null` if there isn't one.
Expand Down
16 changes: 8 additions & 8 deletions pkgs/dart_model/test/model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,16 @@ void main() {
test('can give the path to Members in buffer backed maps', () {
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');
expect(model.qualifiedNameOfMember(member)!.asString,
'package:dart_model/dart_model.dart#JsonData');
});

test('can give the path to Members in SDK maps', () {
final copiedModel = Model.fromJson(_copyMap(model.node));
final member = copiedModel.uris['package:dart_model/dart_model.dart']!
.scopes['JsonData']!.members['_root']!;
expect(copiedModel.pathToMember(member),
'/uris/package:dart_model/dart_model.dart/scopes/JsonData/members/_root');
expect(copiedModel.qualifiedNameOfMember(member)!.asString,
'package:dart_model/dart_model.dart#JsonData');
});

test('path to Members throws on cycle', () {
Expand All @@ -129,7 +129,7 @@ void main() {
(copiedModel.node['uris'] as Map<String, Object?>)['loop'] = copiedModel;
final member = copiedModel.uris['package:dart_model/dart_model.dart']!
.scopes['JsonData']!.members['_root']!;
expect(() => copiedModel.pathToMember(member), throwsStateError);
expect(() => copiedModel.qualifiedNameOfMember(member), throwsStateError);
});

test('path to Members throws on reused node', () {
Expand All @@ -139,7 +139,7 @@ void main() {
copiedModel.uris['package:dart_model/dart_model.dart']!;
final member = copiedModel.uris['package:dart_model/dart_model.dart']!
.scopes['JsonData']!.members['_root']!;
expect(() => copiedModel.pathToMember(member), throwsStateError);
expect(() => copiedModel.qualifiedNameOfMember(member), throwsStateError);
});

test('path to Member returns null for Member in wrong Map', () {
Expand All @@ -151,8 +151,8 @@ void main() {
.scopes['JsonData']!
.members['_root']!
.node));
expect(copiedModel.pathToMember(member), null);
expect(model.pathToMember(copiedMember), null);
expect(copiedModel.qualifiedNameOfMember(member), null);
expect(model.qualifiedNameOfMember(copiedMember), null);
});
});

Expand Down

0 comments on commit ad92034

Please sign in to comment.