Skip to content

Commit

Permalink
Add a type name to the resource initializers (#4240)
Browse files Browse the repository at this point in the history
to avoid "Overload resolution ambiguity" for different resources with a
same name

#4237
  • Loading branch information
terrakok authored Feb 6, 2024
1 parent 84e5298 commit a6b8b50
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,17 @@ internal fun getResFileSpec(
.forEach { addFunction(it) }
}.build()

private fun getterName(resourceType: ResourceType, resourceName: String): String =
"get_${resourceType.typeName}_$resourceName"

private fun getResourceTypeObject(type: ResourceType, nameToResources: Map<String, List<ResourceItem>>) =
TypeSpec.objectBuilder(type.typeName).apply {
nameToResources.keys
.forEach { name ->
addProperty(
PropertySpec
.builder(name, type.getClassName())
.initializer("get_$name()")
.initializer(getterName(type, name) + "()")
.build()
)
}
Expand All @@ -176,7 +179,7 @@ private fun getResourceTypeObject(type: ResourceType, nameToResources: Map<Strin
private fun getResourceInitializer(name: String, type: ResourceType, items: List<ResourceItem>): FunSpec {
val propertyTypeName = type.getClassName()
val resourceId = "${type}:${name}"
return FunSpec.builder("get_$name")
return FunSpec.builder(getterName(type, name))
.addModifiers(KModifier.PRIVATE)
.returns(propertyTypeName)
.addStatement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,97 +27,101 @@ internal object Res {
public suspend fun readBytes(path: String): ByteArray = readResourceBytes(path)

public object drawable {
public val _3_strange_name: DrawableResource = get__3_strange_name()
public val _3_strange_name: DrawableResource = get_drawable__3_strange_name()

public val vector: DrawableResource = get_vector()
public val vector: DrawableResource = get_drawable_vector()

public val vector_2: DrawableResource = get_vector_2()
public val vector_2: DrawableResource = get_drawable_vector_2()
}

public object string {
public val app_name: StringResource = get_app_name()
public val app_name: StringResource = get_string_app_name()

public val hello: StringResource = get_hello()
public val hello: StringResource = get_string_hello()

public val multi_line: StringResource = get_multi_line()
public val multi_line: StringResource = get_string_multi_line()

public val str_arr: StringResource = get_str_arr()
public val str_arr: StringResource = get_string_str_arr()

public val str_template: StringResource = get_str_template()
public val str_template: StringResource = get_string_str_template()
}

public object font {
public val emptyfont: FontResource = get_emptyfont()
public val emptyfont: FontResource = get_font_emptyfont()
}
}

private fun get__3_strange_name(): DrawableResource =
private fun get_drawable__3_strange_name(): DrawableResource =
org.jetbrains.compose.resources.DrawableResource(
"drawable:_3_strange_name",
setOf(
org.jetbrains.compose.resources.ResourceItem(setOf(), "drawable/3-strange-name.xml"),
)
)

private fun get_vector(): DrawableResource = org.jetbrains.compose.resources.DrawableResource(
"drawable:vector",
setOf(
private fun get_drawable_vector(): DrawableResource =
org.jetbrains.compose.resources.DrawableResource(
"drawable:vector",
setOf(

org.jetbrains.compose.resources.ResourceItem(setOf(org.jetbrains.compose.resources.LanguageQualifier("au"),
org.jetbrains.compose.resources.RegionQualifier("US"), ), "drawable-au-rUS/vector.xml"),
org.jetbrains.compose.resources.ResourceItem(setOf(org.jetbrains.compose.resources.LanguageQualifier("au"),
org.jetbrains.compose.resources.RegionQualifier("US"), ), "drawable-au-rUS/vector.xml"),

org.jetbrains.compose.resources.ResourceItem(setOf(org.jetbrains.compose.resources.ThemeQualifier.DARK,
org.jetbrains.compose.resources.LanguageQualifier("ge"), ), "drawable-dark-ge/vector.xml"),
org.jetbrains.compose.resources.ResourceItem(setOf(org.jetbrains.compose.resources.ThemeQualifier.DARK,
org.jetbrains.compose.resources.LanguageQualifier("ge"), ), "drawable-dark-ge/vector.xml"),

org.jetbrains.compose.resources.ResourceItem(setOf(org.jetbrains.compose.resources.LanguageQualifier("en"),
), "drawable-en/vector.xml"),
org.jetbrains.compose.resources.ResourceItem(setOf(), "drawable/vector.xml"),
org.jetbrains.compose.resources.ResourceItem(setOf(org.jetbrains.compose.resources.LanguageQualifier("en"),
), "drawable-en/vector.xml"),
org.jetbrains.compose.resources.ResourceItem(setOf(), "drawable/vector.xml"),
)
)
)

private fun get_vector_2(): DrawableResource = org.jetbrains.compose.resources.DrawableResource(
"drawable:vector_2",
setOf(
org.jetbrains.compose.resources.ResourceItem(setOf(), "drawable/vector_2.xml"),
private fun get_drawable_vector_2(): DrawableResource =
org.jetbrains.compose.resources.DrawableResource(
"drawable:vector_2",
setOf(
org.jetbrains.compose.resources.ResourceItem(setOf(), "drawable/vector_2.xml"),
)
)
)

private fun get_app_name(): StringResource = org.jetbrains.compose.resources.StringResource(
private fun get_string_app_name(): StringResource = org.jetbrains.compose.resources.StringResource(
"string:app_name", "app_name",
setOf(
org.jetbrains.compose.resources.ResourceItem(setOf(), "values/strings.xml"),
)
)

private fun get_hello(): StringResource = org.jetbrains.compose.resources.StringResource(
private fun get_string_hello(): StringResource = org.jetbrains.compose.resources.StringResource(
"string:hello", "hello",
setOf(
org.jetbrains.compose.resources.ResourceItem(setOf(), "values/strings.xml"),
)
)

private fun get_multi_line(): StringResource = org.jetbrains.compose.resources.StringResource(
"string:multi_line", "multi_line",
setOf(
org.jetbrains.compose.resources.ResourceItem(setOf(), "values/strings.xml"),
private fun get_string_multi_line(): StringResource =
org.jetbrains.compose.resources.StringResource(
"string:multi_line", "multi_line",
setOf(
org.jetbrains.compose.resources.ResourceItem(setOf(), "values/strings.xml"),
)
)
)

private fun get_str_arr(): StringResource = org.jetbrains.compose.resources.StringResource(
private fun get_string_str_arr(): StringResource = org.jetbrains.compose.resources.StringResource(
"string:str_arr", "str_arr",
setOf(
org.jetbrains.compose.resources.ResourceItem(setOf(), "values/strings.xml"),
)
)

private fun get_str_template(): StringResource = org.jetbrains.compose.resources.StringResource(
"string:str_template", "str_template",
setOf(
org.jetbrains.compose.resources.ResourceItem(setOf(), "values/strings.xml"),
private fun get_string_str_template(): StringResource =
org.jetbrains.compose.resources.StringResource(
"string:str_template", "str_template",
setOf(
org.jetbrains.compose.resources.ResourceItem(setOf(), "values/strings.xml"),
)
)
)

private fun get_emptyfont(): FontResource = org.jetbrains.compose.resources.FontResource(
private fun get_font_emptyfont(): FontResource = org.jetbrains.compose.resources.FontResource(
"font:emptyfont",
setOf(
org.jetbrains.compose.resources.ResourceItem(setOf(), "font/emptyFont.otf"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ internal object Res {
public suspend fun readBytes(path: String): ByteArray = readResourceBytes(path)

public object drawable {
public val vector: DrawableResource = get_vector()
public val vector: DrawableResource = get_drawable_vector()
}
}

private fun get_vector(): DrawableResource = org.jetbrains.compose.resources.DrawableResource(
"drawable:vector",
setOf(
org.jetbrains.compose.resources.ResourceItem(setOf(), "drawable/vector.xml"),
)
)
private fun get_drawable_vector(): DrawableResource =
org.jetbrains.compose.resources.DrawableResource(
"drawable:vector",
setOf(
org.jetbrains.compose.resources.ResourceItem(setOf(), "drawable/vector.xml"),
)
)

0 comments on commit a6b8b50

Please sign in to comment.