Skip to content
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

Support name and number accessors on enum values #120

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from

Conversation

LINKIWI
Copy link

@LINKIWI LINKIWI commented Jun 25, 2024

Currently, Protobuf enum values have a String() method defined, which provides a human-readable representation of the value with str(...) in Starlark, including the value's name and number (index). However, there is no interface to retrieve only the name and number of an enum value for use in Skycfg programs.

This PR proposes augmenting the enum value type with two attributes, name and number, which expose the enum value descriptor's name and number, respectively.

This enables a definition like:

enum Foo {
    HELLO = 0;
    WORLD = 1;
}

to be used in Skycfg like:

pb.Foo.HELLO.name  # "HELLO" (string)
pb.Foo.HELLO.number  # 0 (int)

I added unit tests to cover the new logic (as well as some additional tests to cover existing logic for the str(...) representation of values).

$ bazelisk test --cache_test_results=no //...
INFO: Invocation ID: 0c384b38-64b0-461b-985b-ed281b040175
INFO: Analyzed 14 targets (0 packages loaded, 1 target configured).
INFO: Found 8 targets and 6 test targets...
INFO: Elapsed time: 0.102s, Critical Path: 0.03s
INFO: 7 processes: 1 internal, 6 linux-sandbox.
INFO: Build completed successfully, 7 total actions
//:skycfg_test                                                           PASSED in 0.0s
//go/assertmodule:assertmodule_test                                      PASSED in 0.0s
//go/hashmodule:hashmodule_test                                          PASSED in 0.0s
//go/protomodule:protomodule_test                                        PASSED in 0.0s
//go/urlmodule:urlmodule_test                                            PASSED in 0.0s
//go/yamlmodule:yamlmodule_test                                          PASSED in 0.0s

Executed 6 out of 6 tests: 6 tests pass.
There were tests whose specified size is too big. Use the --test_verbose_timeout_warnings command line option to see which ones these are.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant