Skip to content

Commit

Permalink
[Kotlin] Added UInt value to enums (#3276)
Browse files Browse the repository at this point in the history
Co-authored-by: Sztergbaum Roman <rmscastle@gmail.com>
  • Loading branch information
MaximPestryakov and Milerius authored Jun 30, 2023
1 parent c50b3e1 commit 4db51f2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
2 changes: 1 addition & 1 deletion codegen/lib/kotlin_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def self.convert_calling_type_ios(t)
"#{if t.is_nullable then '?' else '' end}.toTwString()"
else
if t.is_enum
"#{if t.is_nullable then '?' else '' end}.value"
"#{if t.is_nullable then '?' else '' end}.nativeValue"
elsif t.is_class
"#{if t.is_nullable then '?' else '' end}.pointer"
else
Expand Down
6 changes: 3 additions & 3 deletions codegen/lib/templates/kotlin/android_enum.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<% has_string = entity.cases.all? { |c| !c.string.nil? } -%>
actual enum class <%= entity.name %>(
@get:JvmName("value")
val value: UInt,
actual val value: UInt,
<% if has_string -%>
actual val stringValue: String,
<% end -%>
Expand Down Expand Up @@ -34,10 +34,10 @@ actual enum class <%= entity.name %>(
<%# Value -%>
<% if entity.cases.any? { |e| !e.value.nil? } -%>

companion object {
actual companion object {
@JvmStatic
@JvmName("createFromValue")
fun fromValue(value: UInt): <%= entity.name %>? =
actual fun fromValue(value: UInt): <%= entity.name %>? =
values().firstOrNull { it.value == value }
}
<% end -%>
Expand Down
5 changes: 5 additions & 0 deletions codegen/lib/templates/kotlin/common_enum.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ expect enum class <%= entity.name %> {
<% end -%>
;

val value: UInt
<% if has_string -%>
val stringValue: String
<% end -%>
Expand All @@ -25,5 +26,9 @@ expect enum class <%= entity.name %> {
<%- end -%>
<%# Value -%>
<% if entity.cases.any? { |e| !e.value.nil? } -%>

companion object {
fun fromValue(value: UInt): <%= entity.name %>?
}
<% end -%>
}
28 changes: 24 additions & 4 deletions codegen/lib/templates/kotlin/ios_enum.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<%= render('kotlin/package.erb') %>

<% has_string = entity.cases.all? { |c| !c.string.nil? } -%>
<% if has_string -%>
import com.trustwallet.core.<%= "TW#{entity.name}" %>.*

<% has_string = entity.cases.all? { |c| !c.string.nil? } -%>
<% end -%>
<% type = ": TW#{entity.name}" -%>
actual enum class <%= entity.name %>(
val value<%= type %>,
<% if has_string -%>
val nativeValue<%= type %>,
actual val stringValue: String,
<% else -%>
val nativeValue<%= type %>,
<% end -%>
) {
<%# Cases -%>
Expand All @@ -19,6 +23,14 @@ actual enum class <%= entity.name %>(
<% end -%>
<% end -%>
;

<% if has_string -%>
actual val value: UInt
get() = nativeValue.value
<% else -%>
actual val value<%= type %>
get() = nativeValue
<% end -%>
<%# Property declarations -%>
<%- entity.properties.each do |property| -%>

Expand All @@ -35,9 +47,17 @@ actual enum class <%= entity.name %>(
<%# Value -%>
<% if entity.cases.any? { |e| !e.value.nil? } -%>

companion object {
fun fromValue(value<%= type %>): <%= entity.name %>? =
actual companion object {
<% if has_string -%>
actual fun fromValue(value: UInt): <%= entity.name %>? =
values().firstOrNull { it.value == value }

fun fromValue(value<%= type %>): <%= entity.name %> =
fromValue(value.value)!!
<% else -%>
actual fun fromValue(value<%= type %>): <%= entity.name %>? =
values().firstOrNull { it.value == value }
<% end -%>
}
<% end -%>
}
8 changes: 7 additions & 1 deletion codegen/lib/templates/kotlin/js_enum.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ actual enum class <%= entity.name %>(
<% end -%>
<% end -%>
;

actual val value: UInt
get() = jsValue.value.toInt().toUInt()
<%# Property declarations -%>
<%- entity.properties.each do |property| -%>

Expand All @@ -32,7 +35,10 @@ actual enum class <%= entity.name %>(
<%# Value -%>
<% if entity.cases.any? { |e| !e.value.nil? } -%>

companion object {
actual companion object {
actual fun fromValue(value: UInt): <%= entity.name %>? =
values().first { it.value == value }

fun fromValue(jsValue: Js<%= entity.name %>): <%= entity.name %> =
values().first { it.jsValue.value == jsValue.value }
}
Expand Down

0 comments on commit 4db51f2

Please sign in to comment.