papyrus
papyrus copied to clipboard
Automatically add default values for optional types
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:
- Pass
nilto 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); - Create several functions with the same path so I wouldn't have to use
nil(which is a very horrible approach); - Modify the
@APImacro 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!
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?