Generate array<object> props

Summary: Adds the cxx generators for arrays of object props

Reviewed By: JoshuaGross, TheSavior

Differential Revision: D16814136

fbshipit-source-id: fa4600f60c063bfb460033f5fde43e26c04b5a3b
This commit is contained in:
Rick Hanlon
2019-08-15 06:38:15 -07:00
committed by Facebook Github Bot
parent bc1c7b1096
commit 243ccc541e
7 changed files with 81 additions and 7 deletions
@@ -73,8 +73,7 @@ static inline std::string toString(const ::_ENUM_NAME_:: &value) {
}
`.trim();
const structTemplate = `
struct ::_STRUCT_NAME_:: {
const structTemplate = `struct ::_STRUCT_NAME_:: {
::_FIELDS_::
};
@@ -84,12 +83,20 @@ static inline void fromRawValue(const RawValue &value, ::_STRUCT_NAME_:: &result
::_FROM_CASES_::
}
static inline std::string toString(const ::_STRUCT_NAME_:: &value) {
return "[Object ::_STRUCT_NAME_::]";
}
`.trim();
const arrayConversionFunction = `static inline void fromRawValue(const RawValue &value, std::vector<::_STRUCT_NAME_::> &result) {
auto items = std::vector<better::map<std::string, RawValue>>{value};
for (const auto &item : items) {
auto newItem = fromRawValue(item, ::_STRUCT_NAME_::{})
result.push(newItem);
}
}
`;
const arrayEnumTemplate = `
using ::_ENUM_MASK_:: = uint32_t;
@@ -187,6 +194,10 @@ function getNativeTypeFromAnnotation(componentName: string, prop): string {
'ArrayTypeAnnotation of type ArrayTypeAnnotation not supported',
);
}
if (typeAnnotation.elementType.type === 'ObjectTypeAnnotation') {
const structName = generateStructName(componentName, [prop.name]);
return `std::vector<${structName}>`;
}
if (typeAnnotation.elementType.type === 'StringEnumTypeAnnotation') {
const enumName = getEnumName(componentName, prop.name);
return getEnumMaskName(enumName);
@@ -488,6 +499,19 @@ function generateStructs(componentName: string, component): string {
[prop.name],
prop.typeAnnotation.properties,
);
} else if (
prop.typeAnnotation.type === 'ArrayTypeAnnotation' &&
prop.typeAnnotation.elementType.type === 'ObjectTypeAnnotation'
) {
const properties = prop.typeAnnotation.elementType.properties;
generateStruct(structs, componentName, [prop.name], properties);
structs.set(
`${componentName}ArrayStruct`,
arrayConversionFunction.replace(
/::_STRUCT_NAME_::/g,
generateStructName(componentName, [prop.name]),
),
);
}
});
@@ -534,6 +558,8 @@ function generateStruct(
return;
case 'StringEnumTypeAnnotation':
return;
case 'DoubleTypeAnnotation':
return;
case 'ObjectTypeAnnotation':
const props = property.typeAnnotation.properties;
if (props == null) {
@@ -584,6 +584,26 @@ const ARRAY_PROPS: SchemaType = {
},
},
},
{
name: 'object',
optional: true,
typeAnnotation: {
type: 'ArrayTypeAnnotation',
elementType: {
type: 'ObjectTypeAnnotation',
properties: [
{
name: 'stringProp',
optional: true,
typeAnnotation: {
type: 'StringTypeAnnotation',
default: '',
},
},
],
},
},
},
],
commands: [],
},
@@ -28,7 +28,8 @@ ArrayPropsNativeComponentProps::ArrayPropsNativeComponentProps(
colors(convertRawProp(rawProps, \\"colors\\", sourceProps.colors, colors)),
srcs(convertRawProp(rawProps, \\"srcs\\", sourceProps.srcs, srcs)),
points(convertRawProp(rawProps, \\"points\\", sourceProps.points, points)),
sizes(convertRawProp(rawProps, \\"sizes\\", sourceProps.sizes, sizes))
sizes(convertRawProp(rawProps, \\"sizes\\", sourceProps.sizes, sizes)),
object(convertRawProp(rawProps, \\"object\\", sourceProps.object, object))
{}
} // namespace react
@@ -76,6 +76,30 @@ static inline std::string toString(const ArrayPropsNativeComponentSizesMask &val
}
return result;
}
struct ArrayPropsNativeComponentObjectStruct {
std::string stringProp;
};
static inline void fromRawValue(const RawValue &value, ArrayPropsNativeComponentObjectStruct &result) {
auto map = (better::map<std::string, RawValue>)value;
auto stringProp = map.find(\\"stringProp\\");
if (stringProp != map.end()) {
fromRawValue(stringProp->second, result.stringProp);
}
}
static inline std::string toString(const ArrayPropsNativeComponentObjectStruct &value) {
return \\"[Object ArrayPropsNativeComponentObjectStruct]\\";
}
static inline void fromRawValue(const RawValue &value, std::vector<ArrayPropsNativeComponentObjectStruct> &result) {
auto items = std::vector<better::map<std::string, RawValue>>{value};
for (const auto &item : items) {
auto newItem = fromRawValue(item, ArrayPropsNativeComponentObjectStruct{})
result.push(newItem);
}
}
class ArrayPropsNativeComponentProps final : public ViewProps {
public:
@@ -92,6 +116,7 @@ class ArrayPropsNativeComponentProps final : public ViewProps {
const std::vector<ImageSource> srcs{};
const std::vector<Point> points{};
const ArrayPropsNativeComponentSizesMask sizes{static_cast<ArrayPropsNativeComponentSizesMask>(ArrayPropsNativeComponentSizes::Small)};
const std::vector<ArrayPropsNativeComponentObjectStruct> object{};
};
} // namespace react
@@ -662,7 +687,6 @@ static inline void fromRawValue(const RawValue &value, ObjectPropsObjectPropObje
}
}
static inline std::string toString(const ObjectPropsObjectPropObjectArrayPropStruct &value) {
return \\"[Object ObjectPropsObjectPropObjectArrayPropStruct]\\";
}
@@ -690,7 +714,6 @@ static inline void fromRawValue(const RawValue &value, ObjectPropsObjectPropObje
}
}
static inline std::string toString(const ObjectPropsObjectPropObjectPrimitiveRequiredPropStruct &value) {
return \\"[Object ObjectPropsObjectPropObjectPrimitiveRequiredPropStruct]\\";
}
@@ -738,7 +761,6 @@ static inline void fromRawValue(const RawValue &value, ObjectPropsObjectPropStru
}
}
static inline std::string toString(const ObjectPropsObjectPropStruct &value) {
return \\"[Object ObjectPropsObjectPropStruct]\\";
}
@@ -43,6 +43,9 @@ public class ArrayPropsNativeComponentManagerDelegate<T extends View, U extends
case \\"sizes\\":
mViewManager.setSizes(view, (ReadableArray) value);
break;
case \\"object\\":
mViewManager.setObject(view, (ReadableArray) value);
break;
default:
super.setProperty(view, propName, value);
}
@@ -18,6 +18,7 @@ public interface ArrayPropsNativeComponentManagerInterface<T extends View> {
void setSrcs(T view, @Nullable ReadableArray value);
void setPoints(T view, @Nullable ReadableArray value);
void setSizes(T view, @Nullable ReadableArray value);
void setObject(T view, @Nullable ReadableArray value);
}
",
}
@@ -28,6 +28,7 @@ const ArrayPropsNativeComponentViewConfig = {
srcs: true,
points: true,
sizes: true,
object: true,
},
};