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

[hotfix] resource names #302

Merged
merged 5 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>11.0</string>
<string>12.0</string>
</dict>
</plist>
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@
isa = PBXProject;
attributes = {
BuildIndependentTargetsInParallel = YES;
LastUpgradeCheck = 1430;
LastUpgradeCheck = 1510;
ORGANIZATIONNAME = "";
TargetAttributes = {
331C8080294A63A400263BE5 = {
Expand Down Expand Up @@ -453,7 +453,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand Down Expand Up @@ -580,7 +580,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -629,7 +629,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1430"
LastUpgradeVersion = "1510"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion example/viam_example_app/ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import UIKit
import Flutter

@UIApplicationMain
@main
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
Expand Down
4 changes: 3 additions & 1 deletion lib/src/resource/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ class Subtype {
..namespace = namespace
..type = resourceType
..subtype = resourceSubtype
..name = name;
..remotePath.addAll(name.split(':')..removeLast())
..name = name
..localName = name.split(':').last;
}

@override
Expand Down
13 changes: 12 additions & 1 deletion lib/src/resource/manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,18 @@ class ResourceManager {

/// Get a resource with the given [ResourceName]
T getResource<T>(ResourceName name) {
final resource = resources[name];
Resource? resource;
if (resources.containsKey(name)) {
resource = resources[name];
} else {
final resourcesWithoutRemotes = resources.map((rn, res) {
Copy link
Member

Choose a reason for hiding this comment

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

(q) Potentially out of scope for a hotfix but, should resourcesWithoutRemotes live on the ResourceManager such that we don't have to construct this new map potentially every time we call getResource?

Copy link
Member Author

Choose a reason for hiding this comment

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

Did it anyway -- it's a good thought

final rnWithoutRemote = rn
..remotePath.clear()
..name = rn.localName;
return MapEntry(rnWithoutRemote, res);
});
resource = resourcesWithoutRemotes[name];
}
if (resource == null) throw Exception('Resource not found in manager');
return resource as T;
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ environment:
dependencies:
flutter:
sdk: flutter
flutter_webrtc: ^0.11.0
flutter_webrtc: ^0.12.1+hotfix.1
grpc: ^4.0.1
protobuf: ^3.0.0
image: ^4.0.16
Expand Down
30 changes: 30 additions & 0 deletions test/unit_test/resource/base_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:viam_sdk/src/components/sensor/sensor.dart';

void main() {
group('Subtype Tests', () {
test('getResourceName', () {
// Local
final localName = 'my-sensor';
final localRN = Sensor.subtype.getResourceName(localName);
expect(localRN.namespace, Sensor.subtype.namespace);
expect(localRN.type, Sensor.subtype.resourceType);
expect(localRN.subtype, Sensor.subtype.resourceSubtype);
expect(localRN.remotePath, []);
expect(localRN.name, localName);
expect(localRN.localName, localName);

// Remote
final remoteName = 'my-sensor';
final remotePath = 'remote2:remote1';
final fullRemoteName = '$remotePath:$remoteName';
final remoteRN = Sensor.subtype.getResourceName(fullRemoteName);
expect(remoteRN.namespace, Sensor.subtype.namespace);
expect(remoteRN.type, Sensor.subtype.resourceType);
expect(remoteRN.subtype, Sensor.subtype.resourceSubtype);
expect(remoteRN.remotePath, remotePath.split(':'));
expect(remoteRN.name, fullRemoteName);
expect(remoteRN.localName, remoteName);
});
});
}
62 changes: 62 additions & 0 deletions test/unit_test/resource/manager_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:viam_sdk/src/components/sensor/sensor.dart';
import 'package:viam_sdk/src/resource/manager.dart';

import '../components/sensor_test.dart';

void main() {
group('ResourceManager Tests', () {
group('getResource', () {
test('Local', () {
final localName = 'local-sensor';
final localRN = Sensor.getResourceName(localName);
final localResource = FakeSensor(localName);
final manager = ResourceManager();
manager.register(localRN, localResource);

expect(manager.getResource<Sensor>(Sensor.getResourceName(localName)), localResource);
});

test('Remote', () {
final remoteName = 'remote-sensor';
final remotePath = 'remote2:remote1';
final fullRemoteName = '$remotePath:$remoteName';
final remoteRN = Sensor.subtype.getResourceName(fullRemoteName);
final remoteResource = FakeSensor(fullRemoteName);
final manager = ResourceManager();
manager.register(remoteRN, remoteResource);

// Works with full name
expect(manager.getResource<Sensor>(Sensor.getResourceName(fullRemoteName)), remoteResource);

// Works with short name -- no collisions
expect(manager.getResource<Sensor>(Sensor.getResourceName(remoteName)), remoteResource);
});

test('Local and Remote - Same Names', () {
final manager = ResourceManager();

final localName = 'my-sensor';
final localRN = Sensor.getResourceName(localName);
final localResource = FakeSensor(localName);

final remoteName = 'my-sensor';
final remotePath = 'remote2:remote1';
final fullRemoteName = '$remotePath:$remoteName';
final remoteRN = Sensor.subtype.getResourceName(fullRemoteName);
final remoteResource = FakeSensor(fullRemoteName);

manager.register(localRN, localResource);
manager.register(remoteRN, remoteResource);

// When using fully qualified names, it should return appropriately
expect(manager.getResource<Sensor>(Sensor.getResourceName(localName)), localResource);
expect(manager.getResource<Sensor>(Sensor.getResourceName(fullRemoteName)), remoteResource);

// When using just `my-sensor`, it should always return the local
expect(manager.getResource<Sensor>(Sensor.getResourceName(localName)), localResource);
expect(manager.getResource<Sensor>(Sensor.getResourceName(remoteName)), localResource);
});
});
});
}
Loading