Search in sources :

Example 1 with Construct

use of io.sundr.builder.internal.functions.Construct in project sundrio by sundrio.

the class BuilderUtils method buildableField.

public static Property buildableField(Property property) {
    TypeRef typeRef = property.getTypeRef();
    ClassRef targetType = (ClassRef) TypeAs.combine(UNWRAP_COLLECTION_OF, UNWRAP_ARRAY_OF, UNWRAP_OPTIONAL_OF).apply(typeRef);
    boolean isArray = Types.isArray(typeRef);
    boolean isSet = Types.isSet(typeRef);
    boolean isAbstractSet = isSet && Types.isAbstract(typeRef);
    boolean isList = Types.isList(typeRef);
    boolean isAbstractList = isList && Types.isAbstract(typeRef);
    boolean isMap = Types.isMap(typeRef);
    boolean isOptional = Types.isOptional(typeRef);
    boolean isOptionalDouble = Types.isOptionalDouble(typeRef);
    boolean isOptionalLong = Types.isOptionalLong(typeRef);
    // For fields that are concrete we can possibly create an instance of a VisitableBuilder.
    // For everything else we can have a builder e.g. Builder<Foo> = () -> fooInstance but it won't be visitable
    ClassRef builderType = Types.isConcrete(targetType) ? TypeAs.BUILDER.apply(GetDefinition.of(targetType)).toInternalReference() : TypeAs.VISITABLE_BUILDER.apply(targetType);
    if (isArray || isList) {
        ClassRef listRef = isArray || isAbstractList ? Collections.ARRAY_LIST.toReference(builderType) : new ClassRefBuilder((ClassRef) typeRef).withArguments(builderType).withDimensions(0).build();
        TypeDef listDef = new TypeDefBuilder(TypeDef.forName(listRef.getFullyQualifiedName())).addNewConstructor().endConstructor().addNewConstructor().addNewArgument().withTypeRef(Collections.LIST.toReference(builderType)).withName("l").endArgument().endConstructor().build();
        return new PropertyBuilder(property).withTypeRef(listRef).addToAttributes(LAZY_INIT, " new " + listRef + "()").addToAttributes(INIT_FUNCTION, new Construct(listDef, targetType)).addToAttributes(ALSO_IMPORT, alsoImport(property, listRef, builderType)).build();
    }
    if (isSet) {
        ClassRef setRef = isAbstractSet ? Collections.LINKED_HASH_SET.toReference(builderType) : new ClassRefBuilder((ClassRef) typeRef).withArguments(builderType).build();
        TypeDef setDef = new TypeDefBuilder(TypeDef.forName(setRef.getFullyQualifiedName())).addNewConstructor().endConstructor().addNewConstructor().addNewArgument().withTypeRef(Collections.SET.toReference(builderType)).withName("s").endArgument().endConstructor().build();
        return new PropertyBuilder(property).withTypeRef(setRef).addToAttributes(LAZY_INIT, " new " + setRef + "()").addToAttributes(INIT_FUNCTION, new Construct(setDef, targetType)).addToAttributes(ALSO_IMPORT, alsoImport(property, setRef, builderType)).build();
    }
    if (isOptionalLong) {
        ClassRef optionalRef = Optionals.OPTIONAL_LONG.toReference(builderType);
        return new PropertyBuilder(property).withTypeRef(optionalRef).addToAttributes(INIT, " OptionalLong.empty()").build();
    }
    if (isOptionalDouble) {
        ClassRef optionalRef = Optionals.OPTIONAL_DOUBLE.toReference(builderType);
        return new PropertyBuilder(property).withTypeRef(optionalRef).addToAttributes(INIT, " OptionalDouble.empty()").build();
    }
    if (Types.isOptionalInt(targetType)) {
        ClassRef optionalRef = Optionals.OPTIONAL_INT.toReference(builderType);
        return new PropertyBuilder(property).withTypeRef(optionalRef).addToAttributes(INIT, " OptionalInt.empty()").build();
    }
    if (isOptional) {
        ClassRef optionalRef = Optionals.OPTIONAL.toReference(builderType);
        return new PropertyBuilder(property).withTypeRef(optionalRef).addToAttributes(INIT, " Optional.empty()").addToAttributes(ALSO_IMPORT, alsoImport(property, optionalRef, builderType)).build();
    }
    if (Types.isConcrete(builderType) && BuilderUtils.hasDefaultConstructor(builderType) && property.hasAttribute(DEFAULT_VALUE)) {
        return new PropertyBuilder(property).withTypeRef(builderType).addToAttributes(ALSO_IMPORT, alsoImport(property, builderType)).addToAttributes(INIT, "new " + builderType + "()").build();
    }
    return new PropertyBuilder(property).withTypeRef(builderType).addToAttributes(ALSO_IMPORT, alsoImport(property, builderType)).removeFromAttributes(INIT).build();
}
Also used : ClassRef(io.sundr.model.ClassRef) TypeDef(io.sundr.model.TypeDef) TypeRef(io.sundr.model.TypeRef) ClassRefBuilder(io.sundr.model.ClassRefBuilder) Construct(io.sundr.builder.internal.functions.Construct) TypeDefBuilder(io.sundr.model.TypeDefBuilder) PropertyBuilder(io.sundr.model.PropertyBuilder)

Example 2 with Construct

use of io.sundr.builder.internal.functions.Construct in project sundrio by sundrio.

the class InitEnricher method visit.

@Override
public void visit(PropertyBuilder builder) {
    TypeRef typeRef = builder.buildTypeRef();
    TypeRef unwrapped = TypeAs.combine(TypeAs.UNWRAP_ARRAY_OF, TypeAs.UNWRAP_COLLECTION_OF, TypeAs.UNWRAP_OPTIONAL_OF).apply(typeRef);
    boolean isBuildable = isBuildable(unwrapped);
    boolean hasDescendants = false;
    if (!(typeRef instanceof ClassRef)) {
        return;
    }
    Property parent = (Property) builder.getAttributes().get(DESCENDANT_OF);
    if (parent != null) {
        typeRef = parent.getTypeRef();
        unwrapped = TypeAs.combine(TypeAs.UNWRAP_ARRAY_OF, TypeAs.UNWRAP_COLLECTION_OF, TypeAs.UNWRAP_OPTIONAL_OF).apply(typeRef);
    } else if (builder.getAttributes().containsKey(DESCENDANTS) && !((Collection) builder.getAttributes().get(DESCENDANTS)).isEmpty()) {
        hasDescendants = true;
    }
    List<TypeRef> arguments = ((ClassRef) typeRef).getArguments();
    TypeRef targetType = unwrapped;
    if (isBuildable || hasDescendants) {
        ClassRef unwarppedClassRef = (unwrapped instanceof ClassRef) ? (ClassRef) unwrapped : null;
        targetType = isAbstract(unwarppedClassRef) || GetDefinition.of(unwarppedClassRef).getKind() == Kind.INTERFACE ? TypeAs.VISITABLE_BUILDER.apply(unwarppedClassRef) : TypeAs.BUILDER.apply(GetDefinition.of(unwarppedClassRef)).toInternalReference();
    }
    boolean isArray = Types.isArray(typeRef);
    boolean isSet = Types.isSet(typeRef);
    boolean isAbstractSet = isSet && Types.isAbstract(typeRef);
    boolean isList = Types.isList(typeRef);
    boolean isAbstractList = isList && Types.isAbstract(typeRef);
    boolean isMap = Types.isMap(typeRef);
    boolean isAbstractMap = isMap && Types.isAbstract(typeRef);
    boolean isOptional = Types.isOptional(typeRef);
    boolean isOptionalInt = Types.isOptionalInt(typeRef);
    boolean isOptionalDouble = Types.isOptionalDouble(typeRef);
    boolean isOptionalLong = Types.isOptionalLong(typeRef);
    if (isArray || isList) {
        ClassRef listRef = isArray || isAbstractList ? Collections.ARRAY_LIST.toReference(targetType) : new ClassRefBuilder((ClassRef) typeRef).withArguments(targetType).withDimensions(0).build();
        TypeDef listDef = new TypeDefBuilder(TypeDef.forName(listRef.getFullyQualifiedName())).addNewConstructor().endConstructor().addNewConstructor().addNewArgument().withTypeRef(Collections.LIST.toReference(targetType)).withName("l").endArgument().endConstructor().build();
        builder.addToAttributes(LAZY_INIT, "new " + listRef + "()").addToAttributes(INIT, builder.getAttributes().containsKey(LAZY_COLLECTIONS_INIT_ENABLED) && (Boolean) builder.getAttributes().get(LAZY_COLLECTIONS_INIT_ENABLED) ? null : builder.getAttributes().get(LAZY_INIT)).addToAttributes(INIT_FUNCTION, new Construct(listDef, targetType)).addToAttributes(ALSO_IMPORT, Arrays.asList(targetType, listRef));
    } else if (isSet) {
        ClassRef setRef = isAbstractSet ? Collections.LINKED_HASH_SET.toReference(targetType) : new ClassRefBuilder((ClassRef) typeRef).withArguments(targetType).build();
        TypeDef setDef = new TypeDefBuilder(TypeDef.forName(setRef.getFullyQualifiedName())).addNewConstructor().endConstructor().addNewConstructor().addNewArgument().withTypeRef(Collections.SET.toReference(targetType)).withName("s").endArgument().endConstructor().build();
        builder.addToAttributes(LAZY_INIT, "new " + setRef + "()").addToAttributes(INIT, builder.getAttributes().containsKey(LAZY_COLLECTIONS_INIT_ENABLED) && (Boolean) builder.getAttributes().get(LAZY_COLLECTIONS_INIT_ENABLED) ? null : builder.getAttributes().get(LAZY_INIT)).addToAttributes(INIT_FUNCTION, new Construct(setDef, unwrapped)).addToAttributes(ALSO_IMPORT, Arrays.asList(targetType, setRef));
    } else if (isMap) {
        ClassRef mapRef = isAbstractMap ? Collections.LINKED_HASH_MAP.toReference(arguments) : new ClassRefBuilder((ClassRef) typeRef).withArguments(arguments).build();
        TypeDef mapDef = new TypeDefBuilder(TypeDef.forName(mapRef.getFullyQualifiedName())).addNewConstructor().endConstructor().addNewConstructor().addNewArgument().withTypeRef(Collections.MAP.toReference(arguments)).withName("m").endArgument().endConstructor().build();
        builder.addToAttributes(LAZY_INIT, "new " + mapRef + "()").addToAttributes(INIT, builder.getAttributes().containsKey(LAZY_MAP_INIT_ENABLED) && (Boolean) builder.getAttributes().get(LAZY_MAP_INIT_ENABLED) ? null : builder.getAttributes().get(LAZY_INIT)).addToAttributes(INIT_FUNCTION, new Construct(mapDef, arguments)).addToAttributes(ALSO_IMPORT, Arrays.asList(targetType, mapRef));
    } else if (isOptional) {
        final ClassRef ref = new ClassRefBuilder(Optionals.OPTIONAL.toReference()).withArguments(java.util.Collections.EMPTY_LIST).build();
        builder.addToAttributes(INIT, "Optional.empty()").addToAttributes(INIT_FUNCTION, new Construct(Optionals.OPTIONAL, targetType, "of")).addToAttributes(ALSO_IMPORT, Arrays.asList(targetType, ref));
    } else if (isOptionalDouble) {
        final ClassRef ref = Optionals.OPTIONAL_DOUBLE.toReference();
        builder.addToAttributes(INIT, "OptionalDouble.empty()").addToAttributes(INIT_FUNCTION, new Construct(Optionals.OPTIONAL_DOUBLE, targetType, "of")).addToAttributes(ALSO_IMPORT, Arrays.asList(targetType, ref));
    } else if (isOptionalInt) {
        final ClassRef ref = Optionals.OPTIONAL_INT.toReference();
        builder.addToAttributes(INIT, "OptionalInt.empty()").addToAttributes(INIT_FUNCTION, new Construct(Optionals.OPTIONAL_INT, targetType, "of")).addToAttributes(ALSO_IMPORT, Arrays.asList(targetType, ref));
    } else if (isOptionalLong) {
        final ClassRef ref = Optionals.OPTIONAL_LONG.toReference();
        builder.addToAttributes(INIT, "OptionalLong.empty()").addToAttributes(INIT_FUNCTION, new Construct(Optionals.OPTIONAL_LONG, targetType, "of")).addToAttributes(ALSO_IMPORT, Arrays.asList(targetType, ref));
    }
}
Also used : ClassRef(io.sundr.model.ClassRef) TypeDef(io.sundr.model.TypeDef) TypeRef(io.sundr.model.TypeRef) ClassRefBuilder(io.sundr.model.ClassRefBuilder) Construct(io.sundr.builder.internal.functions.Construct) Property(io.sundr.model.Property) TypeDefBuilder(io.sundr.model.TypeDefBuilder)

Aggregations

Construct (io.sundr.builder.internal.functions.Construct)2 ClassRef (io.sundr.model.ClassRef)2 ClassRefBuilder (io.sundr.model.ClassRefBuilder)2 TypeDef (io.sundr.model.TypeDef)2 TypeDefBuilder (io.sundr.model.TypeDefBuilder)2 TypeRef (io.sundr.model.TypeRef)2 Property (io.sundr.model.Property)1 PropertyBuilder (io.sundr.model.PropertyBuilder)1