chaokunyang commented on issue #3003:
URL: https://github.com/apache/fory/issues/3003#issuecomment-3690300932
## Implementation Progress
The `fory::field<>` template has been implemented with a refined API design
based on discussions. Here's a summary:
### Implemented Features
#### 1. Core API (`cpp/fory/meta/field.h`)
```cpp
namespace fory {
// Tag types
struct nullable {}; // Mark shared_ptr/unique_ptr as nullable
struct not_null {}; // Explicit non-null (for future pointer types like
weak_ptr)
struct ref {}; // Enable reference tracking (shared_ptr only)
template <typename T, int16_t Id, typename... Options>
class field { /* ... */ };
}
```
#### 2. Design Decisions
| Type | Default Nullable | Default Ref | Allowed Options |
|------|-----------------|-------------|-----------------|
| Primitives, `std::string` | **false** | false | **None** |
| `std::optional<T>` | **true** (inherent) | false | **None** |
| `std::shared_ptr<T>` | **false** | false | `nullable`, `not_null`, `ref` |
| `std::unique_ptr<T>` | **false** | false | `nullable` only |
#### 3. Compile-Time Validation
```cpp
// ✅ Valid usage
fory::field<std::string, 0> name;
fory::field<std::optional<int32_t>, 1> optional_age;
fory::field<std::shared_ptr<Node>, 2, fory::ref> parent;
fory::field<std::shared_ptr<Node>, 3, fory::nullable, fory::ref> link;
fory::field<std::unique_ptr<Data>, 4, fory::nullable> data;
// ❌ Compile errors
fory::field<int32_t, 0, fory::nullable> age; // nullable on
primitive
fory::field<std::unique_ptr<T>, 1, fory::ref> ptr; // ref on unique_ptr
fory::field<std::optional<T>, 2, fory::nullable> opt; // nullable on
optional
```
#### 4. Usage Example
```cpp
struct Document {
fory::field<std::string, 0> title;
fory::field<int32_t, 1> version;
fory::field<std::optional<std::string>, 2> description;
fory::field<std::shared_ptr<User>, 3> author; //
non-nullable
fory::field<std::shared_ptr<User>, 4, fory::nullable> reviewer; //
nullable
fory::field<std::shared_ptr<Node>, 5, fory::ref> parent; //
ref tracking
fory::field<std::shared_ptr<Node>, 6, fory::nullable, fory::ref> p; //
both
};
FORY_STRUCT(Document, title, version, description, author, reviewer, parent,
p);
```
### Files Created/Modified
#### New Files
- `cpp/fory/meta/field.h` - Core field template and type traits
- `cpp/fory/meta/field_test.cc` - 22 unit tests for field metadata
- `cpp/fory/serialization/field_serializer_test.cc` - 20 serialization
round-trip tests
#### Modified Files
- `cpp/fory/meta/BUILD` - Added `field_test` target
- `cpp/fory/meta/CMakeLists.txt` - Added `field.h` and test
- `cpp/fory/serialization/BUILD` - Added `field_serializer_test` target
- `cpp/fory/serialization/CMakeLists.txt` - Added test
- `cpp/fory/serialization/struct_serializer.h` - Integration with field
metadata
- `cpp/fory/serialization/type_resolver.h` - Field type unwrapping for
registration
### Test Results
All tests pass:
- 22 meta/field tests (type traits, field options, struct usage)
- 20 serialization tests (primitives, optional, shared_ptr, unique_ptr, ref
tracking, vectors)
### Deferred to Follow-up PR
The `FORY_FIELD_TAGS` macro (non-invasive API for existing structs) was
deferred due to complexity. The current implementation focuses on the
`fory::field<>` wrapper approach.
### Branch
Implementation is on branch `add_cpp_field_support`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]