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

AgentConfig API is error prone #106

Open
roman-kashitsyn opened this issue Feb 1, 2021 · 3 comments
Open

AgentConfig API is error prone #106

roman-kashitsyn opened this issue Feb 1, 2021 · 3 comments

Comments

@roman-kashitsyn
Copy link
Contributor

Currently, AgentConfig is a bare struct with public fields implementing Default. This has 2 issues:

  1. Any changes in the Config will break existing user programs.
  2. The URL field is initialized with an invalid value to force user to specify it. If user makes a mistake, they will only discover it at runtime. We can easily prevent this mistake at compile time by making the fields pub(crate), removing Default and introducing a Builder initialized with required arguments, e.g.
pub struct AgentConfig { pub(crate) url: Url, ... }

impl AgentConfig {
    pub fn url(&self) -> &Url { ... }
    // ...
}

pub struct ConfigBuilder { ... }
impl ConfigBuilder {
    // Arguments of the [new] function are required.
    pub fn new(url: Url) -> Self { ... }
    // ...
    pub fn build(self) -> AgentConfig { ... }
}
@hansl
Copy link
Contributor

hansl commented Feb 1, 2021

Thanks for the feedback. The builder itself already exists; https://github.com/dfinity/agent-rs/blob/next/ic-agent/src/agent/builder.rs#L4 (and is used in icx and dfx).

We could have the url field in the Config type be Option and on build() verify it's not None. Then limit the Config visibility. This would be a breaking change but it's a very good one.

This is fairly simple work, if you want to go ahead please open a PR. I'll probably be able to find some time later this week to fix this.

@roman-kashitsyn
Copy link
Contributor Author

The builder itself already exists

Indeed, thanks for the pointer! Then I don't quite understand why Agent::new and AgentConfig are public at all.

We could have the url field in the Config type be Option and on build() verify it's not None.

I'm not sure it's much different from the current state of affairs, the error will be still a runtime one, not compile time.

@hansl
Copy link
Contributor

hansl commented Feb 2, 2021

why Agent::new and AgentConfig are public at all.

Some historical reason, maybe a change in dfx or something. There was initially no builder, but now that there is a good one they shouldn't be public.

I'm not sure it's much different from the current state of affairs, the error will be still a runtime one, not compile time.

Right you are. Your way is better then. Thanks!

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

No branches or pull requests

2 participants