Skip to content

Commit

Permalink
simple support to the Partial<T> annotation (#35960)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #35960

This feature allows using `$Partial<Obj>` in flow and `Partial<Obj>` in TypeScript based on the spec mentioned here: https://flow.org/en/docs/types/utilities/#toc-partial.

We currently only allow passing an Obj to Partial so
```
export type SomeObj = {
  a: string,
  b?: boolean,
};

export type PartialSomeObj = Partial<SomeObj>;
```
should work.
and also-
```
export type PartialSomeObj = Partial<{
  a: string,
  b?: boolean,
}>;
```
But not
```
export type PartialSomeObj = Partial<Partial<{
  a: string,
  b?: boolean,
}>>;
```
This can be improved in the future by a recursive unwrapping of the value inside the `Partial` annotation.

Changelog:
[General] [Added] -  Allow the use of "Partial<T>" in Turbo Module specs.

Reviewed By: christophpurrer, cipolleschi

Differential Revision: D42640880

fbshipit-source-id: 414653ab443969a0d0f15aab5131dc27c05a91fe
  • Loading branch information
vzaidman authored and facebook-github-bot committed Jan 25, 2023
1 parent 2bfb53c commit 1589efc
Show file tree
Hide file tree
Showing 10 changed files with 984 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* 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.
*
* @flow strict-local
* @format
*/

'use strict';

import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport';
import * as TurboModuleRegistry from 'react-native/Libraries/TurboModule/TurboModuleRegistry';

export type SomeObj = {|
a: string,
b?: boolean,
|};

export type PartialSomeObj = $Partial<SomeObj>;

export interface Spec extends TurboModule {
+getSomeObj: () => SomeObj;
+getPartialSomeObj: () => $Partial<SomeObj>;
+getSomeObjFromPartialSomeObj: (value: $Partial<SomeObj>) => SomeObj;
+getPartialPartial: (
value1: $Partial<SomeObj>,
value2: PartialSomeObj,
) => SomeObj;
}

export default (TurboModuleRegistry.getEnforcing<Spec>(
'NativePartialAnnotationTurboModule',
): Spec);
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,74 @@ namespace facebook {
};
} // namespace react
} // namespace facebook
namespace JS {
namespace NativePartialAnnotationTurboModule {
struct SpecGetSomeObjFromPartialSomeObjValue {
NSString *a() const;
std::optional<bool> b() const;
SpecGetSomeObjFromPartialSomeObjValue(NSDictionary *const v) : _v(v) {}
private:
NSDictionary *_v;
};
}
}
@interface RCTCxxConvert (NativePartialAnnotationTurboModule_SpecGetSomeObjFromPartialSomeObjValue)
+ (RCTManagedPointer *)JS_NativePartialAnnotationTurboModule_SpecGetSomeObjFromPartialSomeObjValue:(id)json;
@end
namespace JS {
namespace NativePartialAnnotationTurboModule {
struct SpecGetPartialPartialValue1 {
NSString *a() const;
std::optional<bool> b() const;
SpecGetPartialPartialValue1(NSDictionary *const v) : _v(v) {}
private:
NSDictionary *_v;
};
}
}
@interface RCTCxxConvert (NativePartialAnnotationTurboModule_SpecGetPartialPartialValue1)
+ (RCTManagedPointer *)JS_NativePartialAnnotationTurboModule_SpecGetPartialPartialValue1:(id)json;
@end
namespace JS {
namespace NativePartialAnnotationTurboModule {
struct SpecGetPartialPartialValue2 {
NSString *a() const;
std::optional<bool> b() const;
SpecGetPartialPartialValue2(NSDictionary *const v) : _v(v) {}
private:
NSDictionary *_v;
};
}
}
@interface RCTCxxConvert (NativePartialAnnotationTurboModule_SpecGetPartialPartialValue2)
+ (RCTManagedPointer *)JS_NativePartialAnnotationTurboModule_SpecGetPartialPartialValue2:(id)json;
@end
@protocol NativePartialAnnotationTurboModuleSpec <RCTBridgeModule, RCTTurboModule>
- (NSDictionary *)getSomeObj;
- (NSDictionary *)getPartialSomeObj;
- (NSDictionary *)getSomeObjFromPartialSomeObj:(JS::NativePartialAnnotationTurboModule::SpecGetSomeObjFromPartialSomeObjValue &)value;
- (NSDictionary *)getPartialPartial:(JS::NativePartialAnnotationTurboModule::SpecGetPartialPartialValue1 &)value1
value2:(JS::NativePartialAnnotationTurboModule::SpecGetPartialPartialValue2 &)value2;
@end
namespace facebook {
namespace react {
/**
* ObjC++ class for module 'NativePartialAnnotationTurboModule'
*/
class JSI_EXPORT NativePartialAnnotationTurboModuleSpecJSI : public ObjCTurboModule {
public:
NativePartialAnnotationTurboModuleSpecJSI(const ObjCTurboModule::InitParams &params);
};
} // namespace react
} // namespace facebook
@protocol NativePromiseTurboModuleSpec <RCTBridgeModule, RCTTurboModule>
Expand Down Expand Up @@ -1069,6 +1137,36 @@ inline JS::NativeOptionalObjectTurboModule::Constants::Builder::Builder(const In
inline JS::NativeOptionalObjectTurboModule::Constants::Builder::Builder(Constants i) : _factory(^{
return i.unsafeRawValue();
}) {}
inline NSString *JS::NativePartialAnnotationTurboModule::SpecGetSomeObjFromPartialSomeObjValue::a() const
{
id const p = _v[@\\"a\\"];
return RCTBridgingToOptionalString(p);
}
inline std::optional<bool> JS::NativePartialAnnotationTurboModule::SpecGetSomeObjFromPartialSomeObjValue::b() const
{
id const p = _v[@\\"b\\"];
return RCTBridgingToOptionalBool(p);
}
inline NSString *JS::NativePartialAnnotationTurboModule::SpecGetPartialPartialValue1::a() const
{
id const p = _v[@\\"a\\"];
return RCTBridgingToOptionalString(p);
}
inline std::optional<bool> JS::NativePartialAnnotationTurboModule::SpecGetPartialPartialValue1::b() const
{
id const p = _v[@\\"b\\"];
return RCTBridgingToOptionalBool(p);
}
inline NSString *JS::NativePartialAnnotationTurboModule::SpecGetPartialPartialValue2::a() const
{
id const p = _v[@\\"a\\"];
return RCTBridgingToOptionalString(p);
}
inline std::optional<bool> JS::NativePartialAnnotationTurboModule::SpecGetPartialPartialValue2::b() const
{
id const p = _v[@\\"b\\"];
return RCTBridgingToOptionalBool(p);
}
inline double JS::NativeSampleTurboModule::SpecGetObjectShapeArg::prop() const
{
Expand Down Expand Up @@ -1622,6 +1720,74 @@ namespace facebook {
};
} // namespace react
} // namespace facebook
namespace JS {
namespace NativePartialAnnotationTurboModule {
struct SpecGetSomeObjFromPartialSomeObjValue {
NSString *a() const;
std::optional<bool> b() const;
SpecGetSomeObjFromPartialSomeObjValue(NSDictionary *const v) : _v(v) {}
private:
NSDictionary *_v;
};
}
}
@interface RCTCxxConvert (NativePartialAnnotationTurboModule_SpecGetSomeObjFromPartialSomeObjValue)
+ (RCTManagedPointer *)JS_NativePartialAnnotationTurboModule_SpecGetSomeObjFromPartialSomeObjValue:(id)json;
@end
namespace JS {
namespace NativePartialAnnotationTurboModule {
struct SpecGetPartialPartialValue1 {
NSString *a() const;
std::optional<bool> b() const;
SpecGetPartialPartialValue1(NSDictionary *const v) : _v(v) {}
private:
NSDictionary *_v;
};
}
}
@interface RCTCxxConvert (NativePartialAnnotationTurboModule_SpecGetPartialPartialValue1)
+ (RCTManagedPointer *)JS_NativePartialAnnotationTurboModule_SpecGetPartialPartialValue1:(id)json;
@end
namespace JS {
namespace NativePartialAnnotationTurboModule {
struct SpecGetPartialPartialValue2 {
NSString *a() const;
std::optional<bool> b() const;
SpecGetPartialPartialValue2(NSDictionary *const v) : _v(v) {}
private:
NSDictionary *_v;
};
}
}
@interface RCTCxxConvert (NativePartialAnnotationTurboModule_SpecGetPartialPartialValue2)
+ (RCTManagedPointer *)JS_NativePartialAnnotationTurboModule_SpecGetPartialPartialValue2:(id)json;
@end
@protocol NativePartialAnnotationTurboModuleSpec <RCTBridgeModule, RCTTurboModule>
- (NSDictionary *)getSomeObj;
- (NSDictionary *)getPartialSomeObj;
- (NSDictionary *)getSomeObjFromPartialSomeObj:(JS::NativePartialAnnotationTurboModule::SpecGetSomeObjFromPartialSomeObjValue &)value;
- (NSDictionary *)getPartialPartial:(JS::NativePartialAnnotationTurboModule::SpecGetPartialPartialValue1 &)value1
value2:(JS::NativePartialAnnotationTurboModule::SpecGetPartialPartialValue2 &)value2;
@end
namespace facebook {
namespace react {
/**
* ObjC++ class for module 'NativePartialAnnotationTurboModule'
*/
class JSI_EXPORT NativePartialAnnotationTurboModuleSpecJSI : public ObjCTurboModule {
public:
NativePartialAnnotationTurboModuleSpecJSI(const ObjCTurboModule::InitParams &params);
};
} // namespace react
} // namespace facebook
@protocol NativePromiseTurboModuleSpec <RCTBridgeModule, RCTTurboModule>
Expand Down Expand Up @@ -2261,6 +2427,36 @@ inline JS::NativeOptionalObjectTurboModule::Constants::Builder::Builder(const In
inline JS::NativeOptionalObjectTurboModule::Constants::Builder::Builder(Constants i) : _factory(^{
return i.unsafeRawValue();
}) {}
inline NSString *JS::NativePartialAnnotationTurboModule::SpecGetSomeObjFromPartialSomeObjValue::a() const
{
id const p = _v[@\\"a\\"];
return RCTBridgingToOptionalString(p);
}
inline std::optional<bool> JS::NativePartialAnnotationTurboModule::SpecGetSomeObjFromPartialSomeObjValue::b() const
{
id const p = _v[@\\"b\\"];
return RCTBridgingToOptionalBool(p);
}
inline NSString *JS::NativePartialAnnotationTurboModule::SpecGetPartialPartialValue1::a() const
{
id const p = _v[@\\"a\\"];
return RCTBridgingToOptionalString(p);
}
inline std::optional<bool> JS::NativePartialAnnotationTurboModule::SpecGetPartialPartialValue1::b() const
{
id const p = _v[@\\"b\\"];
return RCTBridgingToOptionalBool(p);
}
inline NSString *JS::NativePartialAnnotationTurboModule::SpecGetPartialPartialValue2::a() const
{
id const p = _v[@\\"a\\"];
return RCTBridgingToOptionalString(p);
}
inline std::optional<bool> JS::NativePartialAnnotationTurboModule::SpecGetPartialPartialValue2::b() const
{
id const p = _v[@\\"b\\"];
return RCTBridgingToOptionalBool(p);
}
inline double JS::NativeSampleTurboModule::SpecGetObjectShapeArg::prop() const
{
Expand Down Expand Up @@ -2620,6 +2816,61 @@ namespace facebook {
}
} // namespace react
} // namespace facebook
@implementation RCTCxxConvert (NativePartialAnnotationTurboModule_SpecGetSomeObjFromPartialSomeObjValue)
+ (RCTManagedPointer *)JS_NativePartialAnnotationTurboModule_SpecGetSomeObjFromPartialSomeObjValue:(id)json
{
return facebook::react::managedPointer<JS::NativePartialAnnotationTurboModule::SpecGetSomeObjFromPartialSomeObjValue>(json);
}
@end
@implementation RCTCxxConvert (NativePartialAnnotationTurboModule_SpecGetPartialPartialValue1)
+ (RCTManagedPointer *)JS_NativePartialAnnotationTurboModule_SpecGetPartialPartialValue1:(id)json
{
return facebook::react::managedPointer<JS::NativePartialAnnotationTurboModule::SpecGetPartialPartialValue1>(json);
}
@end
@implementation RCTCxxConvert (NativePartialAnnotationTurboModule_SpecGetPartialPartialValue2)
+ (RCTManagedPointer *)JS_NativePartialAnnotationTurboModule_SpecGetPartialPartialValue2:(id)json
{
return facebook::react::managedPointer<JS::NativePartialAnnotationTurboModule::SpecGetPartialPartialValue2>(json);
}
@end
namespace facebook {
namespace react {
static facebook::jsi::Value __hostFunction_NativePartialAnnotationTurboModuleSpecJSI_getSomeObj(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, ObjectKind, \\"getSomeObj\\", @selector(getSomeObj), args, count);
}
static facebook::jsi::Value __hostFunction_NativePartialAnnotationTurboModuleSpecJSI_getPartialSomeObj(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, ObjectKind, \\"getPartialSomeObj\\", @selector(getPartialSomeObj), args, count);
}
static facebook::jsi::Value __hostFunction_NativePartialAnnotationTurboModuleSpecJSI_getSomeObjFromPartialSomeObj(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, ObjectKind, \\"getSomeObjFromPartialSomeObj\\", @selector(getSomeObjFromPartialSomeObj:), args, count);
}
static facebook::jsi::Value __hostFunction_NativePartialAnnotationTurboModuleSpecJSI_getPartialPartial(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, ObjectKind, \\"getPartialPartial\\", @selector(getPartialPartial:value2:), args, count);
}
NativePartialAnnotationTurboModuleSpecJSI::NativePartialAnnotationTurboModuleSpecJSI(const ObjCTurboModule::InitParams &params)
: ObjCTurboModule(params) {
methodMap_[\\"getSomeObj\\"] = MethodMetadata {0, __hostFunction_NativePartialAnnotationTurboModuleSpecJSI_getSomeObj};
methodMap_[\\"getPartialSomeObj\\"] = MethodMetadata {0, __hostFunction_NativePartialAnnotationTurboModuleSpecJSI_getPartialSomeObj};
methodMap_[\\"getSomeObjFromPartialSomeObj\\"] = MethodMetadata {1, __hostFunction_NativePartialAnnotationTurboModuleSpecJSI_getSomeObjFromPartialSomeObj};
setMethodArgConversionSelector(@\\"getSomeObjFromPartialSomeObj\\", 0, @\\"JS_NativePartialAnnotationTurboModule_SpecGetSomeObjFromPartialSomeObjValue:\\");
methodMap_[\\"getPartialPartial\\"] = MethodMetadata {2, __hostFunction_NativePartialAnnotationTurboModuleSpecJSI_getPartialPartial};
setMethodArgConversionSelector(@\\"getPartialPartial\\", 0, @\\"JS_NativePartialAnnotationTurboModule_SpecGetPartialPartialValue1:\\");
setMethodArgConversionSelector(@\\"getPartialPartial\\", 1, @\\"JS_NativePartialAnnotationTurboModule_SpecGetPartialPartialValue2:\\");
}
} // namespace react
} // namespace facebook
namespace facebook {
namespace react {
Expand Down
Loading

0 comments on commit 1589efc

Please sign in to comment.