Codegen: JavaGenerator [1] - introduce type model to represent the parsed JSON schema

Summary:
These Java types are to represent the schema structure based on the definition here: https://github.com/facebook/react-native/blob/master/packages/react-native-codegen/src/CodegenSchema.js#L260
This commit only deals with NativeModules related types, not Fabric yet.

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D23100170

fbshipit-source-id: f3d837c04b35bef099cb0132bccaca117c22f211
This commit is contained in:
Kevin Gozali
2020-08-13 14:52:09 -07:00
committed by Facebook GitHub Bot
parent 3f1a4535d9
commit 59a4d6f52b
16 changed files with 405 additions and 0 deletions
@@ -0,0 +1,18 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.codegen.generator.model;
public final class AliasType extends Type {
public final TypeId referredTypeId;
public AliasType(final TypeId typeId, final TypeId referredTypeId) {
super(typeId);
this.referredTypeId = referredTypeId;
}
}
@@ -0,0 +1,15 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.codegen.generator.model;
public final class AnyType extends Type {
public AnyType(final TypeId typeId) {
super(typeId);
}
}
@@ -0,0 +1,18 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.codegen.generator.model;
public final class ArrayType extends Type {
public final Type elementType;
public ArrayType(final TypeId typeId, final Type elementType) {
super(typeId);
this.elementType = elementType;
}
}
@@ -0,0 +1,15 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.codegen.generator.model;
public final class BooleanType extends Type {
public BooleanType(final TypeId typeId) {
super(typeId);
}
}
@@ -0,0 +1,15 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.codegen.generator.model;
public final class DoubleType extends NumberType {
public DoubleType(final TypeId typeId) {
super(typeId);
}
}
@@ -0,0 +1,15 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.codegen.generator.model;
public final class FloatType extends NumberType {
public FloatType(final TypeId typeId) {
super(typeId);
}
}
@@ -0,0 +1,71 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.codegen.generator.model;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
public final class FunctionType extends Type {
public static class ArgumentType {
public final String name;
public final Type type;
private ArgumentType(String name, Type type) {
this.name = name;
this.type = type;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ArgumentType that = (ArgumentType) o;
return Objects.equals(this.name, that.name) && Objects.equals(this.type, that.type);
}
@Override
public int hashCode() {
return Objects.hash(name, type);
}
}
public final List<ArgumentType> parameters;
public final Type returnType;
public FunctionType(final TypeId typeId, List<ArgumentType> parameters, Type returnType) {
super(typeId);
this.parameters = Collections.unmodifiableList(parameters);
this.returnType = returnType;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass() || !super.equals(o)) {
return false;
}
FunctionType that = (FunctionType) o;
return Objects.equals(this.parameters, that.parameters)
&& Objects.equals(this.returnType, that.returnType);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), parameters, returnType);
}
}
@@ -0,0 +1,15 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.codegen.generator.model;
public final class GenericObjectType extends Type {
public GenericObjectType(final TypeId typeId) {
super(typeId);
}
}
@@ -0,0 +1,15 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.codegen.generator.model;
public final class Int32Type extends NumberType {
public Int32Type(final TypeId typeId) {
super(typeId);
}
}
@@ -0,0 +1,15 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.codegen.generator.model;
public class NumberType extends Type {
public NumberType(final TypeId typeId) {
super(typeId);
}
}
@@ -0,0 +1,32 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.codegen.generator.model;
public final class ObjectType extends Type {
public ObjectType(final TypeId typeId) {
super(typeId);
}
public static class Property {
public final String propertyName;
public final Type type;
public final boolean optional;
public Property(String propertyName, Type type, boolean optional) {
this.propertyName = propertyName;
this.type = type;
this.optional = optional;
}
@Override
public String toString() {
return propertyName + " " + type;
}
}
}
@@ -0,0 +1,21 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.codegen.generator.model;
public final class ReservedFunctionValueType extends Type {
public enum ReservedName {
RootTag,
}
public ReservedName reservedName;
public ReservedFunctionValueType(final TypeId typeId, ReservedName reservedName) {
super(typeId);
this.reservedName = reservedName;
}
}
@@ -0,0 +1,15 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.codegen.generator.model;
public final class StringType extends Type {
public StringType(final TypeId typeId) {
super(typeId);
}
}
@@ -0,0 +1,44 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.codegen.generator.model;
import java.util.Objects;
public abstract class Type {
protected final TypeId mTypeId;
public Type(final TypeId typeId) {
mTypeId = typeId;
}
public TypeId getTypeId() {
return mTypeId;
}
@Override
public String toString() {
return mTypeId.toString();
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final Type type = (Type) o;
return Objects.equals(mTypeId, type.mTypeId);
}
@Override
public int hashCode() {
return Objects.hash(mTypeId);
}
}
@@ -0,0 +1,66 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.codegen.generator.model;
import java.util.Objects;
import javax.annotation.Nullable;
/** Represents the fully qualified name for a Flow type. */
public final class TypeId {
public final String moduleName;
public final String typeName;
private static final String EMPTY_TYPE_NAME = "";
private TypeId(final String moduleName, final String typeName) {
this.moduleName = moduleName;
this.typeName = typeName;
}
public static TypeId of(final String moduleName) {
return new TypeId(moduleName, EMPTY_TYPE_NAME);
}
public static TypeId of(final String moduleName, @Nullable final String typeName) {
if (typeName == null) {
return TypeId.of(moduleName);
}
if (moduleName.equals(typeName)) {
return TypeId.of(moduleName);
}
return new TypeId(moduleName, typeName);
}
@Override
public String toString() {
return String.format(
"<moduleName = %s, typeName = %s>",
moduleName, EMPTY_TYPE_NAME.equals(typeName) ? moduleName : typeName);
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final TypeId typeId = (TypeId) o;
return Objects.equals(moduleName, typeId.moduleName)
&& Objects.equals(typeName, typeId.typeName);
}
@Override
public int hashCode() {
return Objects.hash(moduleName, typeName);
}
}
@@ -0,0 +1,15 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.codegen.generator.model;
public final class VoidType extends Type {
public VoidType(final TypeId typeId) {
super(typeId);
}
}