Skip to content

Commit 62f8350

Browse files
committed
Convert field name to snake_case in RequestParameterDelegate & remove customParameterName parameter usage
1 parent 110dff3 commit 62f8350

File tree

6 files changed

+45
-36
lines changed

6 files changed

+45
-36
lines changed

library/src/main/java/com/pengrad/telegrambot/request/CreateInvoiceLink.kt

+17-17
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,23 @@ class CreateInvoiceLink(
3535
val currency: String by requestParameter(currency)
3636
val prices: Array<LabeledPrice> by requestParameter(arrayOf(*prices))
3737

38-
var providerToken: String? by optionalRequestParameter(customParameterName = "provider_token")
39-
var subscriptionPeriod: Int? by optionalRequestParameter(customParameterName = "subscription_period")
40-
var businessConnectionId: String? by optionalRequestParameter(customParameterName = "business_connection_id")
41-
var maxTipAmount: Int? by optionalRequestParameter(customParameterName = "max_tip_amount")
42-
var suggestedTipAmounts: Array<Int>? by optionalRequestParameter(customParameterName = "suggested_tip_amounts")
43-
var providerData: String? by optionalRequestParameter(customParameterName = "provider_data")
44-
var photoUrl: String? by optionalRequestParameter(customParameterName = "photo_url")
45-
var photoSize: Int? by optionalRequestParameter(customParameterName = "photo_size")
46-
var photoWidth: Int? by optionalRequestParameter(customParameterName = "photo_width")
47-
var photoHeight: Int? by optionalRequestParameter(customParameterName = "photo_height")
48-
var needName: Boolean? by optionalRequestParameter(customParameterName = "need_name")
49-
var needPhoneNumber: Boolean? by optionalRequestParameter(customParameterName = "need_phone_number")
50-
var needEmail: Boolean? by optionalRequestParameter(customParameterName = "need_email")
51-
var needShippingAddress: Boolean? by optionalRequestParameter(customParameterName = "need_shipping_address")
52-
var sendEmailToProvider: Boolean? by optionalRequestParameter(customParameterName = "send_email_to_provider")
53-
var sendPhoneNumberToProvider: Boolean? by optionalRequestParameter(customParameterName = "send_phone_number_to_provider")
54-
var isFlexible: Boolean? by optionalRequestParameter(customParameterName = "is_flexible")
38+
var providerToken: String? by optionalRequestParameter()
39+
var subscriptionPeriod: Int? by optionalRequestParameter()
40+
var businessConnectionId: String? by optionalRequestParameter()
41+
var maxTipAmount: Int? by optionalRequestParameter()
42+
var suggestedTipAmounts: Array<Int>? by optionalRequestParameter()
43+
var providerData: String? by optionalRequestParameter()
44+
var photoUrl: String? by optionalRequestParameter()
45+
var photoSize: Int? by optionalRequestParameter()
46+
var photoWidth: Int? by optionalRequestParameter()
47+
var photoHeight: Int? by optionalRequestParameter()
48+
var needName: Boolean? by optionalRequestParameter()
49+
var needPhoneNumber: Boolean? by optionalRequestParameter()
50+
var needEmail: Boolean? by optionalRequestParameter()
51+
var needShippingAddress: Boolean? by optionalRequestParameter()
52+
var sendEmailToProvider: Boolean? by optionalRequestParameter()
53+
var sendPhoneNumberToProvider: Boolean? by optionalRequestParameter()
54+
var isFlexible: Boolean? by optionalRequestParameter()
5555

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

library/src/main/java/com/pengrad/telegrambot/request/EditUserStarSubscription.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class EditUserStarSubscription(
99
isCanceled: Boolean
1010
) : KBaseRequest<EditUserStarSubscription, BaseResponse>(BaseResponse::class) {
1111

12-
val userId: Long by requestParameter(userId, customParameterName = "user_id")
13-
val telegramPaymentChargeId: String by requestParameter(telegramPaymentChargeId, customParameterName = "telegram_payment_charge_id")
14-
val isCanceled: Boolean by requestParameter(isCanceled, customParameterName = "is_canceled")
12+
val userId: Long by requestParameter(userId)
13+
val telegramPaymentChargeId: String by requestParameter(telegramPaymentChargeId)
14+
val isCanceled: Boolean by requestParameter(isCanceled)
1515

1616
}

library/src/main/java/com/pengrad/telegrambot/request/SavePreparedInlineMessage.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ class SavePreparedInlineMessage(
1313
SavePreparedInlineMessageResponse::class
1414
) {
1515

16-
val userId: Long by requestParameter(userId, customParameterName = "user_id")
16+
val userId: Long by requestParameter(userId)
1717
val result: InlineQueryResult<*> by requestParameter(result)
1818

19-
var allowUserChats: Boolean? by optionalRequestParameter(customParameterName = "allow_user_chats")
20-
var allowBotChats: Boolean? by optionalRequestParameter(customParameterName = "allow_bot_chats")
21-
var allowGroupChats: Boolean? by optionalRequestParameter(customParameterName = "allow_group_chats")
22-
var allowChannelChats: Boolean? by optionalRequestParameter(customParameterName = "allow_channel_chats")
19+
var allowUserChats: Boolean? by optionalRequestParameter()
20+
var allowBotChats: Boolean? by optionalRequestParameter()
21+
var allowGroupChats: Boolean? by optionalRequestParameter()
22+
var allowChannelChats: Boolean? by optionalRequestParameter()
2323

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

library/src/main/java/com/pengrad/telegrambot/request/SendGift.kt

+4-7
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,12 @@ class SendGift(
1111
giftId: String,
1212
) : KBaseRequest<SendGift, BaseResponse>(BaseResponse::class) {
1313

14-
val userId: Long by requestParameter(userId, customParameterName = "user_id")
15-
val giftId: String by requestParameter(giftId, customParameterName = "gift_id")
14+
val userId: Long by requestParameter(userId)
15+
val giftId: String by requestParameter(giftId)
1616

1717
var text: String? by optionalRequestParameter()
18-
var textParseMode: ParseMode? by optionalRequestParameter(
19-
customParameterName = "text_parse_mode",
20-
valueMapper = { it?.name }
21-
)
22-
var textEntities: Array<MessageEntity>? by optionalRequestParameter(customParameterName = "text_entities")
18+
var textParseMode: ParseMode? by optionalRequestParameter(valueMapper = { it?.name })
19+
var textEntities: Array<MessageEntity>? by optionalRequestParameter()
2320

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

library/src/main/java/com/pengrad/telegrambot/request/SetUserEmojiStatus.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class SetUserEmojiStatus(
99
emojiStatusExpirationDate: Int
1010
) : KBaseRequest<SetUserEmojiStatus, BaseResponse>(BaseResponse::class) {
1111

12-
val userId: Long by requestParameter(userId, customParameterName = "user_id")
13-
val emojiStatusCustomEmojiId: String by requestParameter(emojiStatusCustomEmojiId, customParameterName = "emoji_status_custom_emoji_id")
14-
val emojiStatusExpirationDate: Int by requestParameter(emojiStatusExpirationDate, customParameterName = "emoji_status_expiration_date")
12+
val userId: Long by requestParameter(userId)
13+
val emojiStatusCustomEmojiId: String by requestParameter(emojiStatusCustomEmojiId)
14+
val emojiStatusExpirationDate: Int by requestParameter(emojiStatusExpirationDate)
1515

1616
}

library/src/main/java/com/pengrad/telegrambot/utility/kotlin/RequestParameterDelegate.kt

+13-1
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,21 @@ class RequestParameterDelegate<T : KBaseRequest<*, *>, V>(
4949

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

57+
}
58+
59+
fun convertToSnakeCase(input: String): String {
60+
return input.replace(Regex("([a-z])([A-Z])")) {
61+
"${it.groupValues[1]}_${it.groupValues[2]}"
62+
}.lowercase()
63+
}
64+
65+
fun main() {
66+
println(convertToSnakeCase("userTEST")) // user_id
67+
println(convertToSnakeCase("text")) // text
68+
println(convertToSnakeCase("isCancelled")) // is_cancelled
5769
}

0 commit comments

Comments
 (0)