Skip to content

Conversation

@zsy056
Copy link
Contributor

@zsy056 zsy056 commented Dec 31, 2025

Add a new cpp:private_optional generator option for C++ that emits optional fields as private members and provides const getters, enabling stricter encapsulation while preserving access for generated helpers.

To keep the feature stable and exercised in automation, add fixture-based compiler tests and the minimal build/CI wiring required for those tests to build and run in the workflow (including MSVC).

Example generated code (behavior change only, from TestStruct)

Default (no cpp:private_optional): optional fields stay public

public:
  int32_t required_field;
  int32_t optional_field;
  std::string optional_string;

With cpp:private_optional: optional fields become private + const getters

public:
  int32_t required_field;

  const int32_t& __get_optional_field() const { return optional_field; }
  const std::string& __get_optional_string() const { return optional_string; }

private:
  int32_t optional_field;
  std::string optional_string;

  friend void swap(TestStruct &a, TestStruct &b) noexcept;
  friend std::ostream& operator<<(std::ostream& out, const TestStruct& obj);
  • Did you create an Apache Jira ticket? (Request account here, not required for trivial changes)
  • If a ticket exists: Does your pull request title follow the pattern "THRIFT-NNNN: describe my issue"?
  • Did you squash your changes to a single commit? (not required, but preferred)
  • Did you do your best to avoid breaking changes? If one was needed, did you label the Jira ticket with "Breaking-Change"?
  • If your change does not involve any code, include [skip ci] anywhere in the commit message to free up build resources.

@Jens-G Jens-G added the c++ label Jan 7, 2026
@Jens-G Jens-G changed the title cpp: add private_optional support (and wire up tests/CI) add private_optional support (and wire up tests/CI) Jan 7, 2026
@@ -0,0 +1,132 @@
// Minimal parser support for compiler unit tests.
Copy link
Member

@Jens-G Jens-G Jan 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a number of new files lacking the ASF license header

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, added the ASF license header.

Add a new `cpp:private_optional` generator option for C++ that emits optional fields as private members and provides const getters, enabling stricter encapsulation while preserving access for generated helpers.

To keep the feature stable and exercised in automation, add fixture-based compiler tests and the minimal build/CI wiring required for those tests to build and run in the workflow (including MSVC).

 ### Example generated code (behavior change only, from `TestStruct`)

 #### Default (no `cpp:private_optional`): optional fields stay public
```cpp
public:
  int32_t required_field;
  int32_t optional_field;
  std::string optional_string;
```

With cpp:private_optional: optional fields become private + const getters

```cpp
public:
  int32_t required_field;

  const int32_t& __get_optional_field() const { return optional_field; }
  const std::string& __get_optional_string() const { return optional_string; }

private:
  int32_t optional_field;
  std::string optional_string;

  friend void swap(TestStruct &a, TestStruct &b) noexcept;
  friend std::ostream& operator<<(std::ostream& out, const TestStruct& obj);

```
@mergeable mergeable bot added compiler github_actions Pull requests that update GitHub Actions code labels Jan 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ compiler github_actions Pull requests that update GitHub Actions code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants