From 60622791b382ab42d4ca7d284c9da9013cd532e5 Mon Sep 17 00:00:00 2001 From: Alberto Date: Tue, 14 Jan 2025 15:06:14 -0500 Subject: [PATCH] Implement getting inline specs --- .../generators/AbstractTypeGenerator.java | 35 ++++++++++++++----- .../fern/java/generators/TypesGenerator.java | 8 +++-- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/generators/java/generator-utils/src/main/java/com/fern/java/generators/AbstractTypeGenerator.java b/generators/java/generator-utils/src/main/java/com/fern/java/generators/AbstractTypeGenerator.java index 81fc5f4e16a..1dfcff88a1c 100644 --- a/generators/java/generator-utils/src/main/java/com/fern/java/generators/AbstractTypeGenerator.java +++ b/generators/java/generator-utils/src/main/java/com/fern/java/generators/AbstractTypeGenerator.java @@ -9,7 +9,9 @@ import com.squareup.javapoet.ClassName; import com.squareup.javapoet.JavaFile; import com.squareup.javapoet.TypeSpec; +import java.util.ArrayList; import java.util.List; +import java.util.Optional; import java.util.Set; public abstract class AbstractTypeGenerator extends AbstractFileGenerator { @@ -33,15 +35,30 @@ public TypeSpec getTypeSpec() { } private List getInlineTypeSpecs() { - // if (generatorContext.getCustomConfig().enableInlineTypes()) { - // return List.of(); - // } - // - // List declarations = getInlineTypeDeclarations(); - // for (TypeDeclaration declaration : declarations) { - // - // } - return List.of(); + if (generatorContext.getCustomConfig().enableInlineTypes()) { + return List.of(); + } + + List declarations = getInlineTypeDeclarations(); + List result = new ArrayList<>(); + for (TypeDeclaration declaration : declarations) { + Optional generator = declaration + .getShape() + .visit(new SingleTypeGenerator( + generatorContext, + declaration.getName(), + className.nestedClass(declaration + .getName() + .getName() + .getPascalCase() + .getSafeName()), + TypesGenerator.getGeneratedInterfaces(generatorContext), + false, + reservedTypeNames)); + generator.map(AbstractTypeGenerator::getTypeSpec).ifPresent(result::add); + } + + return result; } protected String resolveName(String rawName) { diff --git a/generators/java/generator-utils/src/main/java/com/fern/java/generators/TypesGenerator.java b/generators/java/generator-utils/src/main/java/com/fern/java/generators/TypesGenerator.java index 55b8bf2096e..52d5997d098 100644 --- a/generators/java/generator-utils/src/main/java/com/fern/java/generators/TypesGenerator.java +++ b/generators/java/generator-utils/src/main/java/com/fern/java/generators/TypesGenerator.java @@ -43,7 +43,7 @@ public TypesGenerator(AbstractGeneratorContext generatorContext) { } public Result generateFiles() { - Map generatedInterfaces = getGeneratedInterfaces(); + Map generatedInterfaces = getGeneratedInterfaces(generatorContext); Map generatedTypes = KeyedStream.stream(typeDeclarations) .map(typeDeclaration -> { ClassName className = @@ -65,10 +65,12 @@ public Result generateFiles() { return new Result(generatedInterfaces, generatedTypes); } - private Map getGeneratedInterfaces() { + public static Map getGeneratedInterfaces( + AbstractGeneratorContext generatorContext) { Set interfaceCandidates = generatorContext.getInterfaceIds(); return interfaceCandidates.stream().collect(Collectors.toMap(Function.identity(), typeId -> { - TypeDeclaration typeDeclaration = typeDeclarations.get(typeId); + TypeDeclaration typeDeclaration = + generatorContext.getTypeDeclarations().get(typeId); ObjectTypeDeclaration objectTypeDeclaration = typeDeclaration .getShape() .getObject()