Skip to content

Commit

Permalink
Recursively visit all object properties to generate structs
Browse files Browse the repository at this point in the history
Summary: Adds support for generating structs for object properties that are objects or arrays

Reviewed By: TheSavior

Differential Revision: D16896143

fbshipit-source-id: 6682d047e218fc774c8d0593dc2c66fe73615be7
  • Loading branch information
rickhanlonii authored and facebook-github-bot committed Aug 19, 2019
1 parent 5f09a17 commit 4bf5e63
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,18 @@ function generateStructs(
properties.forEach(prop => {
const typeAnnotation = prop.typeAnnotation;
if (typeAnnotation.type === 'ObjectTypeAnnotation') {
// Recursively visit all of the object properties.
// Note: this is depth first so that the nested structs are ordered first.
const elementProperties = typeAnnotation.properties;
const nestedStructs = generateStructs(
componentName,
elementProperties,
nameParts.concat([prop.name]),
);
nestedStructs.forEach(function(value, key) {
structs.set(key, value);
});
generateStruct(
structs,
componentName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,33 @@ static inline std::string toString(const ObjectPropsObjectPropNestedPropAStruct
return \\"[Object ObjectPropsObjectPropNestedPropAStruct]\\";
}
struct ObjectPropsObjectPropNestedArrayAsPropertyArrayPropStruct {
std::string stringProp;
};
static inline void fromRawValue(const RawValue &value, ObjectPropsObjectPropNestedArrayAsPropertyArrayPropStruct &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 ObjectPropsObjectPropNestedArrayAsPropertyArrayPropStruct &value) {
return \\"[Object ObjectPropsObjectPropNestedArrayAsPropertyArrayPropStruct]\\";
}
static inline void fromRawValue(const RawValue &value, std::vector<ObjectPropsObjectPropNestedArrayAsPropertyArrayPropStruct> &result) {
auto items = (std::vector<RawValue>)value;
for (const auto &item : items) {
ObjectPropsObjectPropNestedArrayAsPropertyArrayPropStruct newItem;
fromRawValue(item, newItem);
result.emplace_back(newItem);
}
}
struct ObjectPropsObjectPropNestedArrayAsPropertyStruct {
std::vector<ObjectPropsObjectPropNestedArrayAsPropertyArrayPropStruct> arrayProp;
};
Expand Down

0 comments on commit 4bf5e63

Please sign in to comment.