Skip to content

Commit

Permalink
Convert field name to snake_case in RequestParameterDelegate & remove…
Browse files Browse the repository at this point in the history
… customParameterName parameter usage
  • Loading branch information
anfanik committed Jan 24, 2025
1 parent 110dff3 commit 2b05eee
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@ class CreateInvoiceLink(
val currency: String by requestParameter(currency)
val prices: Array<LabeledPrice> by requestParameter(arrayOf(*prices))

var providerToken: String? by optionalRequestParameter(customParameterName = "provider_token")
var subscriptionPeriod: Int? by optionalRequestParameter(customParameterName = "subscription_period")
var businessConnectionId: String? by optionalRequestParameter(customParameterName = "business_connection_id")
var maxTipAmount: Int? by optionalRequestParameter(customParameterName = "max_tip_amount")
var suggestedTipAmounts: Array<Int>? by optionalRequestParameter(customParameterName = "suggested_tip_amounts")
var providerData: String? by optionalRequestParameter(customParameterName = "provider_data")
var photoUrl: String? by optionalRequestParameter(customParameterName = "photo_url")
var photoSize: Int? by optionalRequestParameter(customParameterName = "photo_size")
var photoWidth: Int? by optionalRequestParameter(customParameterName = "photo_width")
var photoHeight: Int? by optionalRequestParameter(customParameterName = "photo_height")
var needName: Boolean? by optionalRequestParameter(customParameterName = "need_name")
var needPhoneNumber: Boolean? by optionalRequestParameter(customParameterName = "need_phone_number")
var needEmail: Boolean? by optionalRequestParameter(customParameterName = "need_email")
var needShippingAddress: Boolean? by optionalRequestParameter(customParameterName = "need_shipping_address")
var sendEmailToProvider: Boolean? by optionalRequestParameter(customParameterName = "send_email_to_provider")
var sendPhoneNumberToProvider: Boolean? by optionalRequestParameter(customParameterName = "send_phone_number_to_provider")
var isFlexible: Boolean? by optionalRequestParameter(customParameterName = "is_flexible")
var providerToken: String? by optionalRequestParameter()
var subscriptionPeriod: Int? by optionalRequestParameter()
var businessConnectionId: String? by optionalRequestParameter()
var maxTipAmount: Int? by optionalRequestParameter()
var suggestedTipAmounts: Array<Int>? by optionalRequestParameter()
var providerData: String? by optionalRequestParameter()
var photoUrl: String? by optionalRequestParameter()
var photoSize: Int? by optionalRequestParameter()
var photoWidth: Int? by optionalRequestParameter()
var photoHeight: Int? by optionalRequestParameter()
var needName: Boolean? by optionalRequestParameter()
var needPhoneNumber: Boolean? by optionalRequestParameter()
var needEmail: Boolean? by optionalRequestParameter()
var needShippingAddress: Boolean? by optionalRequestParameter()
var sendEmailToProvider: Boolean? by optionalRequestParameter()
var sendPhoneNumberToProvider: Boolean? by optionalRequestParameter()
var isFlexible: Boolean? by optionalRequestParameter()

fun providerToken(providerToken: String) = apply { this.providerToken = providerToken }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class EditUserStarSubscription(
isCanceled: Boolean
) : KBaseRequest<EditUserStarSubscription, BaseResponse>(BaseResponse::class) {

val userId: Long by requestParameter(userId, customParameterName = "user_id")
val telegramPaymentChargeId: String by requestParameter(telegramPaymentChargeId, customParameterName = "telegram_payment_charge_id")
val isCanceled: Boolean by requestParameter(isCanceled, customParameterName = "is_canceled")
val userId: Long by requestParameter(userId)
val telegramPaymentChargeId: String by requestParameter(telegramPaymentChargeId)
val isCanceled: Boolean by requestParameter(isCanceled)

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class SavePreparedInlineMessage(
SavePreparedInlineMessageResponse::class
) {

val userId: Long by requestParameter(userId, customParameterName = "user_id")
val userId: Long by requestParameter(userId)
val result: InlineQueryResult<*> by requestParameter(result)

var allowUserChats: Boolean? by optionalRequestParameter(customParameterName = "allow_user_chats")
var allowBotChats: Boolean? by optionalRequestParameter(customParameterName = "allow_bot_chats")
var allowGroupChats: Boolean? by optionalRequestParameter(customParameterName = "allow_group_chats")
var allowChannelChats: Boolean? by optionalRequestParameter(customParameterName = "allow_channel_chats")
var allowUserChats: Boolean? by optionalRequestParameter()
var allowBotChats: Boolean? by optionalRequestParameter()
var allowGroupChats: Boolean? by optionalRequestParameter()
var allowChannelChats: Boolean? by optionalRequestParameter()

fun allowUserChats(allowUserChats: Boolean) = apply { this.allowUserChats = allowUserChats }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@ class SendGift(
giftId: String,
) : KBaseRequest<SendGift, BaseResponse>(BaseResponse::class) {

val userId: Long by requestParameter(userId, customParameterName = "user_id")
val giftId: String by requestParameter(giftId, customParameterName = "gift_id")
val userId: Long by requestParameter(userId)
val giftId: String by requestParameter(giftId)

var text: String? by optionalRequestParameter()
var textParseMode: ParseMode? by optionalRequestParameter(
customParameterName = "text_parse_mode",
valueMapper = { it?.name }
)
var textEntities: Array<MessageEntity>? by optionalRequestParameter(customParameterName = "text_entities")
var textParseMode: ParseMode? by optionalRequestParameter(valueMapper = { it?.name })
var textEntities: Array<MessageEntity>? by optionalRequestParameter()

fun text(text: String) = apply { this.text = text }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class SetUserEmojiStatus(
emojiStatusExpirationDate: Int
) : KBaseRequest<SetUserEmojiStatus, BaseResponse>(BaseResponse::class) {

val userId: Long by requestParameter(userId, customParameterName = "user_id")
val emojiStatusCustomEmojiId: String by requestParameter(emojiStatusCustomEmojiId, customParameterName = "emoji_status_custom_emoji_id")
val emojiStatusExpirationDate: Int by requestParameter(emojiStatusExpirationDate, customParameterName = "emoji_status_expiration_date")
val userId: Long by requestParameter(userId)
val emojiStatusCustomEmojiId: String by requestParameter(emojiStatusCustomEmojiId)
val emojiStatusExpirationDate: Int by requestParameter(emojiStatusExpirationDate)

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,15 @@ class RequestParameterDelegate<T : KBaseRequest<*, *>, V>(

private fun updateRequestValue(thisRef: T, property: KProperty<*>, value: V) {
thisRef.addParameter(
name = customParameterName ?: property.name, // todo: format field name - camelCase to snake_case
name = customParameterName ?: convertToSnakeCase(property.name),
value = valueMapper?.invoke(value) ?: value
)
}

}

fun convertToSnakeCase(input: String): String {
return input.replace(Regex("([a-z])([A-Z])")) {
"${it.groupValues[1]}_${it.groupValues[2]}"
}.lowercase()
}

0 comments on commit 2b05eee

Please sign in to comment.