Skip to content

Generate Kotlin files with enums.py #1845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@
],
}

# Temporary filter enums to not upgrade all enums at once
KOTLIN_ENUM_NAMES = {"Direction"}

ENUMS_KOTLIN = {name: ENUMS[name] for name in KOTLIN_ENUM_NAMES}
ENUMS_JAVA = {name: values for name, values in ENUMS.items() if name not in KOTLIN_ENUM_NAMES}

DO_NOT_STRIP = ["LogLevel"]

BITSET_ENUMS = ["Errata"]
Expand Down Expand Up @@ -120,6 +126,9 @@ def to_java_upper(symbol):
return _format_name(symbol, "_", "upper")


def to_kotlin_upper(symbol):
return _format_name(symbol, "_", "upper")

def to_hyphenated_lower(symbol):
return _format_name(symbol, "-", "lower")

Expand Down Expand Up @@ -215,7 +224,7 @@ def to_hyphenated_lower(symbol):
f.write("\n")

# write out java files
for name, values in sorted(ENUMS.items()):
for name, values in sorted(ENUMS_JAVA.items()):
with open(root + "/java/com/facebook/yoga/Yoga%s.java" % name, "w") as f:
f.write(get_license("java"))
f.write("package com.facebook.yoga;\n\n")
Expand Down Expand Up @@ -267,6 +276,48 @@ def to_hyphenated_lower(symbol):
f.write(" }\n")
f.write("}\n")

# write out Kotlin files
for name, values in sorted(ENUMS_KOTLIN.items()):
with open(root + "/java/com/facebook/yoga/Yoga%s.kt" % name, "w") as f:
f.write(get_license("kotlin"))
f.write("package com.facebook.yoga\n\n")
f.write("public enum class Yoga%s(public val intValue: Int) {\n" % name)
if len(values) > 0:
for value in values:
if isinstance(value, tuple):
f.write(" %s(%d)" % (to_kotlin_upper(value[0]), value[1]))
else:
f.write(" %s(%d)" % (to_kotlin_upper(value), values.index(value)))
if values.index(value) is len(values) - 1:
f.write(";\n")
else:
f.write(",\n")
else:
f.write("__EMPTY(-1);")
f.write("\n")
f.write(" public fun intValue(): Int = intValue\n")
f.write("\n")
f.write(" public companion object {\n")
f.write(" @JvmStatic\n")
f.write(" public fun fromInt(value: Int): Yoga%s =\n" % name)
f.write(" when (value) {\n")
for value in values:
if isinstance(value, tuple):
f.write(
" %d -> %s\n" % (value[1], to_kotlin_upper(value[0]))
)
else:
f.write(
" %d -> %s\n"
% (values.index(value), to_kotlin_upper(value))
)
f.write(
' else -> throw IllegalArgumentException("Unknown enum value: $value")\n'
)
f.write(" }\n")
f.write(" }\n")
f.write("}\n")

# write out TypeScript file
with open(root + "/javascript/src/generated/YGEnums.ts", "w") as f:
f.write(get_license("js"))
Expand Down
35 changes: 0 additions & 35 deletions java/com/facebook/yoga/YogaDirection.java

This file was deleted.

29 changes: 29 additions & 0 deletions java/com/facebook/yoga/YogaDirection.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// @generated by enums.py

package com.facebook.yoga

public enum class YogaDirection(public val intValue: Int) {
INHERIT(0),
LTR(1),
RTL(2);

public fun intValue(): Int = intValue

public companion object {
@JvmStatic
public fun fromInt(value: Int): YogaDirection =
when (value) {
0 -> INHERIT
1 -> LTR
2 -> RTL
else -> throw IllegalArgumentException("Unknown enum value: $value")
}
}
}
Loading