Skip to content

Commit

Permalink
fix: fixup endpoint snake_case names
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMrMilchmann committed Oct 22, 2023
1 parent c575ada commit 767e8ac
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
24 changes: 12 additions & 12 deletions src/main/kotlin/com/gw2tb/apigen/model/APIv1Endpoint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ package com.gw2tb.apigen.model
*/
public enum class APIv1Endpoint(
override val endpointName: Name,
override val path: String = endpointName.toCamelCase()
override val path: String = endpointName.toSnakeCase()
) : APIEndpoint {
/**
* The `/v1/build` endpoint.
Expand Down Expand Up @@ -66,7 +66,7 @@ public enum class APIv1Endpoint(
*
* @since 0.7.0
*/
V1_EVENT_DETAILS("/event_details"),
V1_EVENT_DETAILS(Name.derive(snakeCase = "/event_details", titleCase = "/EventDetails")),
/**
* The `/v1/files` endpoint.
*
Expand All @@ -82,15 +82,15 @@ public enum class APIv1Endpoint(
*
* @since 0.7.0
*/
V1_GUILD_DETAILS("/guild_details"),
V1_GUILD_DETAILS(Name.derive(snakeCase = "/guild_details", titleCase = "/GuildDetails")),
/**
* The `/v1/item_details` endpoint.
*
* [Read more on the official Wiki](https://wiki.guildwars2.com/wiki/API:1/item_details)
*
* @since 0.7.0
*/
V1_ITEM_DETAILS("/item_details"),
V1_ITEM_DETAILS(Name.derive(snakeCase = "/item_details", titleCase = "/ItemDetails")),
/**
* The `/v1/items` endpoint.
*
Expand All @@ -106,15 +106,15 @@ public enum class APIv1Endpoint(
*
* @since 0.7.0
*/
V1_MAP_FLOOR("/map_floor"),
V1_MAP_FLOOR(Name.derive(snakeCase = "/map_floor", titleCase = "/MapFloor")),
/**
* The `/v1/map_names` endpoint.
*
* [Read more on the official Wiki](https://wiki.guildwars2.com/wiki/API:1/map_names)
*
* @since 0.7.0
*/
V1_MAP_NAMES("/map_names"),
V1_MAP_NAMES(Name.derive(snakeCase = "/map_names", titleCase = "/MapNames")),
/**
* The `/v1/maps` endpoint.
*
Expand All @@ -130,7 +130,7 @@ public enum class APIv1Endpoint(
*
* @since 0.7.0
*/
V1_RECIPE_DETAILS("/recipe_details"),
V1_RECIPE_DETAILS(Name.derive(snakeCase = "/recipe_details", titleCase = "/RecipeDetails")),
/**
* The `/v1/recipes` endpoint.
*
Expand All @@ -146,7 +146,7 @@ public enum class APIv1Endpoint(
*
* @since 0.7.0
*/
V1_SKIN_DETAILS("/skin_details"),
V1_SKIN_DETAILS(Name.derive(snakeCase = "/skin_details", titleCase = "/SkinDetails")),
/**
* The `/v1/skins` endpoint.
*
Expand All @@ -162,31 +162,31 @@ public enum class APIv1Endpoint(
*
* @since 0.7.0
*/
V1_WORLD_NAMES("/world_names"),
V1_WORLD_NAMES(Name.derive(snakeCase = "/world_names", titleCase = "/WorldNames")),
/**
* The `/v1/wvw/match_details` endpoint.
*
* [Read more on the official Wiki](https://wiki.guildwars2.com/wiki/API:1/wvw/match_details)
*
* @since 0.7.0
*/
V1_WVW_MATCH_DETAILS("/wvw/match_details"),
V1_WVW_MATCH_DETAILS(Name.derive(snakeCase = "/wvw/match_details", titleCase = "/WvW/MatchDetails")),
/**
* The `/v1/wvw/matches` endpoint.
*
* [Read more on the official Wiki](https://wiki.guildwars2.com/wiki/API:1/wvw/matches)
*
* @since 0.7.0
*/
V1_WVW_MATCHES("/wvw/matches"),
V1_WVW_MATCHES("/WvW/Matches"),
/**
* The `/v1/wvw/objective_names` endpoint.
*
* [Read more on the official Wiki](https://wiki.guildwars2.com/wiki/API:1/wvw/objetive_names)
*
* @since 0.7.0
*/
V1_WVW_OBJECTIVE_NAMES("/wvw/objective_names");
V1_WVW_OBJECTIVE_NAMES(Name.derive(snakeCase = "/wvw/objective_names", titleCase = "/WvW/ObjectiveNames"));

constructor(p: String): this(Name.derive(titleCase = p))

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/gw2tb/apigen/model/APIv2Endpoint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ package com.gw2tb.apigen.model
*/
public enum class APIv2Endpoint(
override val endpointName: Name,
override val path: String = endpointName.toCamelCase()
override val path: String = endpointName.toSnakeCase()
) : APIEndpoint {
/**
* The `/v2/account` endpoint.
Expand Down
29 changes: 23 additions & 6 deletions src/main/kotlin/com/gw2tb/apigen/model/Name.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,38 @@ public data class Name private constructor(
var remaining = this@splitCasing

while (remaining.isNotEmpty()) {
var segment = remaining.takeWhile { it == '/' || it.isUpperCase() }

if (segment.length <= (if (segment.startsWith('/')) 2 else 1)) {
segment += remaining.drop(segment.length).takeWhile { it != '/' && !it.isUpperCase() }
val segment = buildString {
if (remaining.first() == '/') {
append("/")
remaining = remaining.drop(1)
}

var buf = remaining.takeWhile { it != '/' && (!it.isLetter() || it.isUpperCase()) }
remaining = remaining.drop(buf.length)
append(buf)

buf = remaining.takeWhile { it != '/' && !it.isUpperCase() }
remaining = remaining.drop(buf.length)
append(buf)
}

if (segment.isNotEmpty()) {
remaining = remaining.substring(segment.length)
add(segment)
}
}
}

fun List<String>.joinToSnakeCase(): String = buildString {
println(this@joinToSnakeCase)
append(this@joinToSnakeCase.first().lowercase())

this@joinToSnakeCase.drop(1).forEach { segment ->
if (!segment.startsWith("/")) append("_")
append(segment.lowercase())
}
}

val snakeCase = snakeCase ?: (camelCase ?: titleCase)?.splitCasing()?.joinToString(separator = "_") { it.lowercase() } ?: error("At least one must be non null")
val snakeCase = snakeCase ?: (camelCase ?: titleCase)?.splitCasing()?.joinToSnakeCase() ?: error("At least one must be non null")
val camelCase = camelCase ?: titleCase?.firstToLowerCase() ?: snakeCase.split("_").joinToString(separator = "") { it.firstToUpperCase() }
val titleCase = titleCase ?: camelCase.firstToUpperCase()

Expand Down

0 comments on commit 767e8ac

Please sign in to comment.