stripe / skycfg

Skycfg is an extension library for the Starlark language that adds support for constructing Protocol Buffer messages.
Apache License 2.0
643 stars 54 forks source link

Accommodate Starlark reserved keywords as Protobuf message field names #119

Open LINKIWI opened 3 months ago

LINKIWI commented 3 months ago

The Protobuf language permits identifiers that are reserved in Starlark, like pass. Consider, for example:

package foo;

message Foo {
    bool pass = 1;
}

There is currently no way to express this in Skycfg, since this is (correctly) flagged as a Starlark syntax error. For example:

foopb = proto.package("foo");

def main(ctx):
    return [
        foopb.Foo(pass = True),
    ]
got pass, want primary expression

Language-specific Protobuf code generation plugins typically work around such collisions by suffixing identifiers with an underscore. This PR proposes using the same convention to enable use of such reserved keywords as attribute names when creating Protobuf messages in Skycfg.

This enables the above example to be expressed as:

foopb = proto.package("foo");

def main(ctx):
    return [
        foopb.Foo(pass_ = True),
    ]

Verification

I updated the unit tests.

$ bazelisk test //...
INFO: Invocation ID: 2aa156b0-26c2-4577-a35e-51ba751a7d18
Analyzing: 14 targets (15 packages loaded, 2996 targets configured)
INFO: Analyzed 14 targets (93 packages loaded, 7937 targets configured).
INFO: Found 8 targets and 6 test targets...
INFO: Elapsed time: 148.927s, Critical Path: 26.24s
INFO: 358 processes: 25 internal, 333 linux-sandbox.
INFO: Build completed successfully, 358 total actions
//:skycfg_test                                                           PASSED in 0.1s
//go/assertmodule:assertmodule_test                                      PASSED in 0.1s
//go/hashmodule:hashmodule_test                                          PASSED in 0.3s
//go/protomodule:protomodule_test                                        PASSED in 0.1s
//go/urlmodule:urlmodule_test                                            PASSED in 0.3s
//go/yamlmodule:yamlmodule_test                                          PASSED in 0.4s

Executed 6 out of 6 tests: 6 tests pass.
CLAassistant commented 3 months ago

CLA assistant check
All committers have signed the CLA.

LINKIWI commented 3 months ago

@seena-stripe @dl-stripe Apologies for the direct tag; would one of you be the right person to review this change? Thanks.