Skip to content

Commit

Permalink
Support wrapped ids in schema generator
Browse files Browse the repository at this point in the history
  • Loading branch information
kunyavskiy committed Mar 16, 2024
1 parent 6cbca41 commit a320ab2
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 29 deletions.
16 changes: 8 additions & 8 deletions schemas/advanced.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@
"additionalProperties": false,
"required": []
},
"kotlin.collections.LinkedHashMap?<kotlin.String,org.icpclive.cds.tunning.GroupInfoOverride>": {
"kotlin.collections.LinkedHashMap?<org.icpclive.cds.api.GroupId,org.icpclive.cds.tunning.GroupInfoOverride>": {
"oneOf": [
{
"type": "object",
Expand Down Expand Up @@ -432,7 +432,7 @@
"additionalProperties": false,
"required": []
},
"kotlin.collections.LinkedHashMap?<kotlin.String,org.icpclive.cds.tunning.OrganizationInfoOverride>": {
"kotlin.collections.LinkedHashMap?<org.icpclive.cds.api.OrganizationId,org.icpclive.cds.tunning.OrganizationInfoOverride>": {
"oneOf": [
{
"type": "object",
Expand Down Expand Up @@ -497,7 +497,7 @@
"additionalProperties": false,
"required": []
},
"kotlin.collections.LinkedHashMap?<kotlin.String,org.icpclive.cds.tunning.ProblemInfoOverride>": {
"kotlin.collections.LinkedHashMap?<org.icpclive.cds.api.ProblemId,org.icpclive.cds.tunning.ProblemInfoOverride>": {
"oneOf": [
{
"type": "object",
Expand Down Expand Up @@ -552,7 +552,7 @@
}
]
},
"kotlin.collections.LinkedHashMap<kotlin.String,kotlin.String>": {
"kotlin.collections.LinkedHashMap<org.icpclive.cds.api.GroupId,kotlin.String>": {
"type": "object",
"patternProperties": {
".*": {
Expand Down Expand Up @@ -661,7 +661,7 @@
"type": "string"
},
"groupsChampionTitles": {
"$ref": "#/$defs/kotlin.collections.LinkedHashMap<kotlin.String,kotlin.String>"
"$ref": "#/$defs/kotlin.collections.LinkedHashMap<org.icpclive.cds.api.GroupId,kotlin.String>"
},
"rankAwardsMaxRank": {
"type": "number"
Expand Down Expand Up @@ -712,13 +712,13 @@
"$ref": "#/$defs/kotlin.collections.LinkedHashMap?<kotlin.String,org.icpclive.cds.tunning.TeamInfoOverride>"
},
"groupOverrides": {
"$ref": "#/$defs/kotlin.collections.LinkedHashMap?<kotlin.String,org.icpclive.cds.tunning.GroupInfoOverride>"
"$ref": "#/$defs/kotlin.collections.LinkedHashMap?<org.icpclive.cds.api.GroupId,org.icpclive.cds.tunning.GroupInfoOverride>"
},
"organizationOverrides": {
"$ref": "#/$defs/kotlin.collections.LinkedHashMap?<kotlin.String,org.icpclive.cds.tunning.OrganizationInfoOverride>"
"$ref": "#/$defs/kotlin.collections.LinkedHashMap?<org.icpclive.cds.api.OrganizationId,org.icpclive.cds.tunning.OrganizationInfoOverride>"
},
"problemOverrides": {
"$ref": "#/$defs/kotlin.collections.LinkedHashMap?<kotlin.String,org.icpclive.cds.tunning.ProblemInfoOverride>"
"$ref": "#/$defs/kotlin.collections.LinkedHashMap?<org.icpclive.cds.api.ProblemId,org.icpclive.cds.tunning.ProblemInfoOverride>"
},
"scoreboardOverrides": {
"$ref": "#/$defs/org.icpclive.cds.tunning.RankingSettings?<DurationM?,kotlin.Boolean?,org.icpclive.cds.api.PenaltyRoundingMode?>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ public fun Flow<ContestUpdate>.applyAdvancedProperties(advancedPropsFlow: Flow<A
}
}

private fun <T, O> mergeOverrides(
private fun <T, O, ID> mergeOverrides(
infos: List<T>,
overrides: Map<String, O>?,
id: T.() -> String,
unusedMessage: (Set<String>) -> String? = { null },
overrides: Map<ID, O>?,
id: T.() -> ID,
unusedMessage: (Set<ID>) -> String? = { null },
merge: (T, O) -> T,
): List<T> {
return if (overrides == null) {
Expand Down Expand Up @@ -323,11 +323,11 @@ internal fun applyAdvancedProperties(

private fun mergeOrganizations(
organizationInfos: List<OrganizationInfo>,
overrides1: Map<String, OrganizationInfoOverride>?,
overrides1: Map<OrganizationId, OrganizationInfoOverride>?,
) = mergeOverrides(
organizationInfos,
overrides1,
{ id.value },
{ id },
unusedMessage = { "No organization for override: $it" }
) { org, override ->
OrganizationInfo(
Expand All @@ -340,11 +340,11 @@ private fun mergeOrganizations(

private fun mergeGroups(
groups: List<GroupInfo>,
overrides: Map<String, GroupInfoOverride>?,
overrides: Map<GroupId, GroupInfoOverride>?,
) = mergeOverrides(
groups,
overrides,
{ id.value },
{ id },
unusedMessage = { "No group for override: $it" }
) { group, override ->
GroupInfo(
Expand All @@ -357,11 +357,11 @@ private fun mergeGroups(

private fun mergeProblems(
problems: List<ProblemInfo>,
overrides: Map<String, ProblemInfoOverride>?,
overrides: Map<ProblemId, ProblemInfoOverride>?,
) = mergeOverrides(
problems,
overrides,
{ id.value },
{ id },
unusedMessage = { "No problem for override: $it" }
) { problem, override ->
ProblemInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import kotlinx.serialization.Transient
@Serializable
public data class AwardsSettings(
public val championTitle: String? = null,
public val groupsChampionTitles: Map<String, String> = emptyMap(),
public val groupsChampionTitles: Map<GroupId, String> = emptyMap(),
public val rankAwardsMaxRank: Int = 0,
private val medals: List<MedalSettings> = emptyList(),
private val medalGroups: List<List<MedalSettings>> = emptyList(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ internal abstract class AbstractScoreboardCalculator : ScoreboardCalculator {
awardsSettings.championTitle?.let { title ->
add(Award.Winner("winner", title, teamRanks[1]?.toSet() ?: emptySet()))
}
for ((groupId_, title) in awardsSettings.groupsChampionTitles) {
val groupId = GroupId(groupId_)
for ((groupId, title) in awardsSettings.groupsChampionTitles) {
val groupBestRank = firstGroupRank[groupId]
add(
Award.GroupChampion(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ public class AdvancedProperties(
public val teamNameRegexes: TeamRegexOverrides? = null,
public val teamIdRegexes: TeamRegexOverrides? = null,
public val teamOverrides: Map<String, TeamInfoOverride>? = null,
public val groupOverrides: Map<String, GroupInfoOverride>? = null,
public val organizationOverrides: Map<String, OrganizationInfoOverride>? = null,
public val problemOverrides: Map<String, ProblemInfoOverride>? = null,
public val groupOverrides: Map<GroupId, GroupInfoOverride>? = null,
public val organizationOverrides: Map<OrganizationId, OrganizationInfoOverride>? = null,
public val problemOverrides: Map<ProblemId, ProblemInfoOverride>? = null,
public val scoreboardOverrides: RankingSettings? = null,
public val awardsSettings: AwardsSettings? = null,
)
Expand Down Expand Up @@ -229,7 +229,7 @@ public fun ContestInfo.toAdvancedProperties(fields: Set<String>): AdvancedProper
)
},
problemOverrides = problemList.associate {
it.id.value to ProblemInfoOverride(
it.id to ProblemInfoOverride(
displayName = it.displayName.takeIfAsked("problemDisplayName"),
fullName = it.fullName.takeIfAsked("problemFullName"),
color = it.color.takeIfAsked("color"),
Expand All @@ -242,14 +242,14 @@ public fun ContestInfo.toAdvancedProperties(fields: Set<String>): AdvancedProper
)
},
groupOverrides = groupList.associate {
it.id.value to GroupInfoOverride(
it.id to GroupInfoOverride(
displayName = it.displayName.takeIfAsked("groupDisplayName"),
isHidden = it.isHidden.takeIfAsked("groupIsHidden"),
isOutOfContest = it.isOutOfContest.takeIfAsked("isOutOfContest"),
)
},
organizationOverrides = organizationList.associate {
it.id.value to OrganizationInfoOverride(
it.id to OrganizationInfoOverride(
displayName = it.displayName.takeIfAsked("orgDisplayName"),
fullName = it.fullName.takeIfAsked("orgFullName"),
logo = it.logo.takeIfAsked("logo")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ fun PrimitiveKind.toJsonTypeName(): String = when (this) {
PrimitiveKind.STRING -> "string"
}

fun SerialDescriptor.unwrapInlines(): SerialDescriptor = if (!isInline) this else getElementDescriptor(0).unwrapInlines()


@OptIn(ExperimentalSerializationApi::class)
fun SerialDescriptor.toJsonSchemaType(
processed: MutableSet<String>,
Expand All @@ -43,7 +46,7 @@ fun SerialDescriptor.toJsonSchemaType(
return defaultSerializer.descriptor.toJsonSchemaType(processed, serializersModule, definitions, extras, extraTypeProperty)
}
if (isInline) {
return getElementDescriptor(0).toJsonSchemaType(processed, serializersModule, definitions, extras, extraTypeProperty)
return unwrapInlines().toJsonSchemaType(processed, serializersModule, definitions, extras, extraTypeProperty)
}
// Before processing, check for recursion
val paramNamesWithTypes = elementDescriptors.map { it.serialName }
Expand Down Expand Up @@ -164,7 +167,7 @@ fun SerialDescriptor.toJsonSchemaType(
}

StructureKind.MAP -> {
val keysSerializer = getElementDescriptor(0)
val keysSerializer = getElementDescriptor(0).unwrapInlines()
val valuesSerializer = getElementDescriptor(1)
when (keysSerializer.kind) {
PrimitiveKind.STRING -> {
Expand Down

0 comments on commit a320ab2

Please sign in to comment.