Skip to content

Commit 9dff914

Browse files
mateoguzmanameta-codesync[bot]
authored andcommitted
Generate Kotlin files with enums.py (#1845)
Summary: X-link: facebook/react-native#55631 This PR extends enums.py to also write Kotlin files. To introduce this in the least breaking manner, we can gradually do one by one and at the end clean up the Java script. Migrate YogaDirection to Kotlin. Changelog: [Internal] Pull Request resolved: #1845 Test Plan: RN ```sh yarn android yarn test-android ``` Yoga ```sh ./gradlew :yoga:assembleDebug ``` Reviewed By: cortinico Differential Revision: D93555645 Pulled By: NickGerleman fbshipit-source-id: cdddc9850f05cf0782cc4fdb782f22776ab12d31
1 parent 77fecbf commit 9dff914

3 files changed

Lines changed: 82 additions & 36 deletions

File tree

enums.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@
8484
],
8585
}
8686

87+
# Temporary filter enums to not upgrade all enums at once
88+
KOTLIN_ENUM_NAMES = {"Direction"}
89+
90+
ENUMS_KOTLIN = {name: ENUMS[name] for name in KOTLIN_ENUM_NAMES}
91+
ENUMS_JAVA = {
92+
name: values for name, values in ENUMS.items() if name not in KOTLIN_ENUM_NAMES
93+
}
94+
8795
DO_NOT_STRIP = ["LogLevel"]
8896

8997
BITSET_ENUMS = ["Errata"]
@@ -120,6 +128,10 @@ def to_java_upper(symbol):
120128
return _format_name(symbol, "_", "upper")
121129

122130

131+
def to_kotlin_upper(symbol):
132+
return _format_name(symbol, "_", "upper")
133+
134+
123135
def to_hyphenated_lower(symbol):
124136
return _format_name(symbol, "-", "lower")
125137

@@ -215,7 +227,7 @@ def to_hyphenated_lower(symbol):
215227
f.write("\n")
216228

217229
# write out java files
218-
for name, values in sorted(ENUMS.items()):
230+
for name, values in sorted(ENUMS_JAVA.items()):
219231
with open(root + "/java/com/facebook/yoga/Yoga%s.java" % name, "w") as f:
220232
f.write(get_license("java"))
221233
f.write("package com.facebook.yoga;\n\n")
@@ -267,6 +279,46 @@ def to_hyphenated_lower(symbol):
267279
f.write(" }\n")
268280
f.write("}\n")
269281

282+
# write out Kotlin files
283+
for name, values in sorted(ENUMS_KOTLIN.items()):
284+
with open(root + "/java/com/facebook/yoga/Yoga%s.kt" % name, "w") as f:
285+
f.write(get_license("kotlin"))
286+
f.write("package com.facebook.yoga\n\n")
287+
f.write("public enum class Yoga%s(public val intValue: Int) {\n" % name)
288+
if len(values) > 0:
289+
for value in values:
290+
if isinstance(value, tuple):
291+
f.write(" %s(%d)" % (to_kotlin_upper(value[0]), value[1]))
292+
else:
293+
f.write(" %s(%d)" % (to_kotlin_upper(value), values.index(value)))
294+
if values.index(value) is len(values) - 1:
295+
f.write(";\n")
296+
else:
297+
f.write(",\n")
298+
else:
299+
f.write("__EMPTY(-1);")
300+
f.write("\n")
301+
f.write(" public fun intValue(): Int = intValue\n")
302+
f.write("\n")
303+
f.write(" public companion object {\n")
304+
f.write(" @JvmStatic\n")
305+
f.write(" public fun fromInt(value: Int): Yoga%s =\n" % name)
306+
f.write(" when (value) {\n")
307+
for value in values:
308+
if isinstance(value, tuple):
309+
f.write(" %d -> %s\n" % (value[1], to_kotlin_upper(value[0])))
310+
else:
311+
f.write(
312+
" %d -> %s\n"
313+
% (values.index(value), to_kotlin_upper(value))
314+
)
315+
f.write(
316+
' else -> throw IllegalArgumentException("Unknown enum value: $value")\n'
317+
)
318+
f.write(" }\n")
319+
f.write(" }\n")
320+
f.write("}\n")
321+
270322
# write out TypeScript file
271323
with open(root + "/javascript/src/generated/YGEnums.ts", "w") as f:
272324
f.write(get_license("js"))

java/com/facebook/yoga/YogaDirection.java

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
// @generated by enums.py
9+
10+
package com.facebook.yoga
11+
12+
public enum class YogaDirection(public val intValue: Int) {
13+
INHERIT(0),
14+
LTR(1),
15+
RTL(2);
16+
17+
public fun intValue(): Int = intValue
18+
19+
public companion object {
20+
@JvmStatic
21+
public fun fromInt(value: Int): YogaDirection =
22+
when (value) {
23+
0 -> INHERIT
24+
1 -> LTR
25+
2 -> RTL
26+
else -> throw IllegalArgumentException("Unknown enum value: $value")
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)