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

Allow setting name of FactorSources #305

Merged
merged 7 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ extension FactorSource {
public var name: String {
factorSourceName(factorSource: self)
}

public mutating func setName(_ updated: String) {
self = factorSourceSetName(factorSource: self, updated: updated)
}
}
5 changes: 4 additions & 1 deletion apple/Tests/TestCases/Profile/Factor/FactorSourceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ final class FactorSourceTests: FactorSourceTest<FactorSource> {
}

func test_name() {
XCTAssertEqual(SUT.sample.name, "My Phone")
var sut = SUT.sample
XCTAssertEqual(sut.name, "My Phone")
sut.setName("Updated name")
XCTAssertEqual(sut.name, "Updated name")
}
}
2 changes: 1 addition & 1 deletion crates/sargon-uniffi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "sargon-uniffi"
# Don't forget to update version in crates/sargon/Cargo.toml
version = "1.1.86"
version = "1.1.87"
edition = "2021"
build = "build.rs"

Expand Down
10 changes: 10 additions & 0 deletions crates/sargon-uniffi/src/profile/v100/factors/factor_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ pub fn factor_source_name(factor_source: &FactorSource) -> String {
factor_source.into_internal().name()
}

#[uniffi::export]
pub fn factor_source_set_name(
Copy link
Contributor

Choose a reason for hiding this comment

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

This is nice, but I think we can go a step further and also make the update in the Profile itself, so maybe this API should have the factor source id to be updated.

Or your intention is different?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was thinking of handling it the same way we handle Account name updates, but guess we can directly do altogether. Will update

Copy link
Contributor

Choose a reason for hiding this comment

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

That is more legacy :), we should migrate how we rename accounts sometime to be handled in Sargon.

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 I am not sure if it is better to have a function that updates the name: String of a given factor_source_id: FactorSourceId. Considering Hosts we will already have access to the actual FactorSource and that we already have a function that updates a FactorSource, seems pointless to send the id just to have Sargon fetch it from the Profile.

Maybe instead the function should be like this

pub async fn update_factor_source_name(&self, factor_source: FactorSource, name: String) -> Result<()>

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure that also works, the main idea is to have the Profile mutation in Sargon. You might want to return the update FactorSource as response of this function.

factor_source: FactorSource,
updated: String,
) -> FactorSource {
let mut factor_source = factor_source.into_internal();
factor_source.set_name(updated);
factor_source.into()
}

#[uniffi::export]
pub fn new_factor_source_sample() -> FactorSource {
InternalFactorSource::sample().into()
Expand Down
2 changes: 1 addition & 1 deletion crates/sargon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "sargon"
# Don't forget to update version in crates/sargon-uniffi/Cargo.toml
version = "1.1.86"
version = "1.1.87"
edition = "2021"
build = "build.rs"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ impl BaseBaseIsFactorSource for ArculusCardFactorSource {
fn name(&self) -> String {
self.hint.label.clone()
}

fn set_name(&mut self, updated: String) {
self.hint.label = updated;
}
}

#[cfg(test)]
Expand Down Expand Up @@ -195,6 +199,9 @@ mod tests {

#[test]
fn name() {
assert_eq!(SUT::sample().name(), "Silver");
let mut sut = SUT::sample();
assert_eq!(sut.name(), "Silver");
sut.set_name("Black".to_string());
assert_eq!(sut.name(), "Black");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ impl BaseBaseIsFactorSource for OffDeviceMnemonicFactorSource {
fn name(&self) -> String {
self.hint.label.value.clone()
}

fn set_name(&mut self, updated: String) {
Copy link
Contributor

Choose a reason for hiding this comment

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

minor improvement would be to change String -> impl AsRef<str> making it a bit easier to write unit tests, allowing you to pass string literals (&str) or String :)

Copy link
Contributor

Choose a reason for hiding this comment

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

and inside this function you do updated.as_ref().to_owned()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did remember this suggestion from previous PR review 🙂

I decided not to include it in this case since tests are very simple anyway, and in the end we add one .as_ref().to_owned() in production code, for each .to_owned() removed in tests (being the relation 1-1)

Copy link
Contributor

Choose a reason for hiding this comment

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

it is unfortunate that Rust is not as great as Swift when it comes to method with same name but different types - in Swift we could have written such a method on the protocol - the trait - and allow str/String. here it would be a collision.

self.hint.label.value = updated;
}
}

#[cfg(test)]
Expand Down Expand Up @@ -158,6 +162,9 @@ mod tests {

#[test]
fn name() {
assert_eq!(SUT::sample().name(), "Story about a horse");
let mut sut = SUT::sample();
assert_eq!(sut.name(), "Story about a horse");
sut.set_name("Thrilled with a shark".to_string());
assert_eq!(sut.name(), "Thrilled with a shark");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ impl BaseBaseIsFactorSource for PasswordFactorSource {
fn name(&self) -> String {
self.hint.label.clone()
}

fn set_name(&mut self, updated: String) {
self.hint.label = updated;
}
}

#[cfg(test)]
Expand Down Expand Up @@ -196,6 +200,9 @@ mod tests {

#[test]
fn name() {
assert_eq!(SUT::sample().name(), "Password 1");
let mut sut = SUT::sample();
assert_eq!(sut.name(), "Password 1");
sut.set_name("Password 2".to_string());
assert_eq!(sut.name(), "Password 2");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ impl BaseBaseIsFactorSource
.join(", ");
format!("Questions: {}", ids)
}

fn set_name(&mut self, _updated: String) {
unreachable!("SecurityQuestions cannot be renamed");
}
}

#[cfg(test)]
Expand Down Expand Up @@ -498,4 +502,10 @@ mod tests {
fn name() {
assert_eq!(SUT::sample().name(), "Questions: #0, #1, #2, #3, #4, #5");
}

#[should_panic(expected = "SecurityQuestions cannot be renamed")]
#[test]
fn set_name() {
SUT::sample().set_name("whatever".to_string())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ impl BaseBaseIsFactorSource for TrustedContactFactorSource {
fn name(&self) -> String {
self.contact.name.value.clone()
}

fn set_name(&mut self, updated: String) {
self.contact.name.value = updated;
}
}
impl TrustedContactFactorSource {
fn new_sample(name: &str, email: &str, address: AccountAddress) -> Self {
Expand Down Expand Up @@ -215,6 +219,9 @@ mod tests {

#[test]
fn name() {
assert_eq!(SUT::sample().name(), "Spending Account");
let mut sut = SUT::sample();
assert_eq!(sut.name(), "Spending Account");
sut.set_name("Savings Account".to_string());
assert_eq!(sut.name(), "Savings Account");
}
}
11 changes: 11 additions & 0 deletions crates/sargon/src/profile/v100/factors/factor_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ impl BaseBaseIsFactorSource for FactorSource {
fn name(&self) -> String {
self.map_get(|v| v.name())
}

fn set_name(&mut self, updated: String) {
self.map_set(|v| v.set_name(updated.clone()));
}
}

impl Identifiable for FactorSource {
Expand Down Expand Up @@ -398,6 +402,13 @@ mod tests {
)
}

#[test]
fn set_name() {
let mut sut = SUT::sample();
sut.set_name("new name".to_string());
assert_eq!(sut.name(), "new name");
}

#[test]
fn factor_source_id_device() {
assert_eq!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ impl BaseBaseIsFactorSource for DeviceFactorSource {
fn name(&self) -> String {
self.hint.label.clone()
}

fn set_name(&mut self, updated: String) {
self.hint.label = updated;
}
}

impl DeviceFactorSource {
Expand Down Expand Up @@ -317,6 +321,9 @@ mod tests {

#[test]
fn name() {
assert_eq!(SUT::sample().name(), "My Phone");
let mut sut = SUT::sample();
assert_eq!(sut.name(), "My Phone");
sut.set_name("My Old Phone".to_string());
assert_eq!(sut.name(), "My Old Phone");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ impl BaseBaseIsFactorSource for LedgerHardwareWalletFactorSource {
fn name(&self) -> String {
self.hint.label.clone()
}

fn set_name(&mut self, updated: String) {
self.hint.label = updated
}
}

#[cfg(test)]
Expand Down Expand Up @@ -185,6 +189,9 @@ mod tests {

#[test]
fn name() {
assert_eq!(SUT::sample().name(), "Orange, scratched");
let mut sut = SUT::sample();
assert_eq!(sut.name(), "Orange, scratched");
sut.set_name("Old cracked".to_string());
assert_eq!(sut.name(), "Old cracked");
}
}
1 change: 1 addition & 0 deletions crates/sargon/src/profile/v100/factors/is_factor_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub trait BaseBaseIsFactorSource {
}

fn name(&self) -> String;
fn set_name(&mut self, updated: String);

fn category(&self) -> FactorSourceCategory {
self.factor_source_kind().category()
Expand Down
Loading