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

Automatically add default values for optional types #45

Open
lyricalsoul opened this issue Jan 27, 2024 · 1 comment
Open

Automatically add default values for optional types #45

lyricalsoul opened this issue Jan 27, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@lyricalsoul
Copy link

HI there!

It would be nice if Papyrus added default values when generating API implementations. Personally, I have a lot of functions that look like this:

    @POST("/sendMessage")
    func sendMessage(
        chatId: TelegramIdentifier,
        messageThreadId: Int?,
        text: String,
        parseMode: ParseMode?,
        entities: [MessageEntity]?,
        linkPreviewOptions: LinkPreviewOptions?,
        disableNotification: Bool?,
        protectContent: Bool?,
        replyParameters: ReplyParameters?,
        replyMarkup: ReplyMarkup?
    ) async throws -> TelegramResponse<Message>

Currently, I have to either:

  1. Pass nil to every single parameter, which doesn't work for the API I'm consuming (and I guess it wouldn't work for a lot of APIs either);
  2. Create several functions with the same path so I wouldn't have to use nil (which is a very horrible approach);
  3. Modify the @API macro so it adds default values for every parameter with an optional type.

I have a fork that implements the third option. However, I also had to modify the request builder so nil values aren't added to the request body, which may not be a very good idea if you have to explicitly set some value as nil.
If you have any other ideas, I'd be happy to implement them and open a PR. Maybe adding another macro, something like @AddDefaultValues?

Thanks!

@joshuawright11
Copy link
Owner

joshuawright11 commented Jan 30, 2024

Hey @lyricalsoul! Thanks for the issue and for looking into this - lack of default values is a major pain point. Is the modified @API macro you made adding default values to the protocol in an extension or to the generated concrete type?

i.e. would the default values apply only to direct usages of the struct MessageSenderAPI from above? Or any use of the protocol MessageSender (assuming your example function is in a protocol called MessageSender).

Ideally it would be the protocol so that mocked interfaces could have the same default value API, perhaps via an extension?

@joshuawright11 joshuawright11 added the enhancement New feature or request label Jan 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants