Search in sources :

Example 1 with TypeDefBuilder

use of io.sundr.model.TypeDefBuilder in project kubernetes-client by fabric8io.

the class AbstractCustomResourceHandler method handle.

public void handle(CustomResourceInfo config) {
    final String name = config.crdName();
    final String version = config.version();
    TypeDef def = config.definition();
    SpecReplicasPathDetector specReplicasPathDetector = new SpecReplicasPathDetector();
    StatusReplicasPathDetector statusReplicasPathDetector = new StatusReplicasPathDetector();
    LabelSelectorPathDetector labelSelectorPathDetector = new LabelSelectorPathDetector();
    AdditionalPrinterColumnDetector additionalPrinterColumnDetector = new AdditionalPrinterColumnDetector();
    ClassDependenciesVisitor traversedClassesVisitor = new ClassDependenciesVisitor(config.crClassName(), name);
    TypeDefBuilder builder = new TypeDefBuilder(def);
    if (config.specClassName().isPresent()) {
        builder.accept(specReplicasPathDetector);
    }
    if (config.statusClassName().isPresent()) {
        builder.accept(statusReplicasPathDetector);
    }
    def = builder.accept(labelSelectorPathDetector).accept(additionalPrinterColumnDetector).accept(traversedClassesVisitor).build();
    addDecorators(config, def, specReplicasPathDetector.getPath(), statusReplicasPathDetector.getPath(), labelSelectorPathDetector.getPath());
    Map<String, Property> additionalPrinterColumns = new HashMap<>(additionalPrinterColumnDetector.getProperties());
    additionalPrinterColumns.forEach((path, property) -> {
        Map<String, Object> parameters = property.getAnnotations().stream().filter(a -> a.getClassRef().getName().equals("PrinterColumn")).map(AnnotationRef::getParameters).findFirst().orElse(Collections.emptyMap());
        String type = AbstractJsonSchema.getSchemaTypeFor(property.getTypeRef());
        String column = (String) parameters.get("name");
        if (Utils.isNullOrEmpty(column)) {
            column = property.getName().toUpperCase();
        }
        String description = property.getComments().stream().filter(l -> !l.trim().startsWith("@")).collect(Collectors.joining(" ")).trim();
        String format = (String) parameters.get("format");
        resources.decorate(getPrinterColumnDecorator(name, version, path, type, column, description, format));
    });
}
Also used : Utils(io.fabric8.kubernetes.client.utils.Utils) Decorator(io.fabric8.crd.generator.decorator.Decorator) io.fabric8.crd.generator.visitor(io.fabric8.crd.generator.visitor) Map(java.util.Map) TypeDef(io.sundr.model.TypeDef) Optional(java.util.Optional) HashMap(java.util.HashMap) AnnotationRef(io.sundr.model.AnnotationRef) TypeDefBuilder(io.sundr.model.TypeDefBuilder) Property(io.sundr.model.Property) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) HashMap(java.util.HashMap) TypeDefBuilder(io.sundr.model.TypeDefBuilder) TypeDef(io.sundr.model.TypeDef) Property(io.sundr.model.Property) AnnotationRef(io.sundr.model.AnnotationRef)

Example 2 with TypeDefBuilder

use of io.sundr.model.TypeDefBuilder in project kubernetes-client by fabric8io.

the class AnnotatedPropertyPathDetector method visit.

@Override
public void visit(TypeDefBuilder builder) {
    TypeDef type = builder.build();
    final List<Property> properties = type.getProperties();
    for (Property p : properties) {
        if (parents.contains(p)) {
            continue;
        }
        List<Property> newParents = new ArrayList<>(parents);
        boolean match = p.getAnnotations().stream().anyMatch(a -> a.getClassRef().getName().equals(annotationName));
        if (match) {
            newParents.add(p);
            reference.set(Optional.of(newParents.stream().map(Property::getName).collect(Collectors.joining(DOT, prefix, ""))));
            return;
        }
    }
    properties.stream().filter(p -> p.getTypeRef() instanceof ClassRef).forEach(p -> {
        if (!parents.contains(p)) {
            ClassRef classRef = (ClassRef) p.getTypeRef();
            TypeDef propertyType = Types.typeDefFrom(classRef);
            if (!propertyType.isEnum()) {
                List<Property> newParents = new ArrayList<>(parents);
                newParents.add(p);
                new TypeDefBuilder(propertyType).accept(new AnnotatedPropertyPathDetector(prefix, annotationName, newParents, reference)).build();
            }
        }
    });
}
Also used : Types(io.fabric8.crd.generator.utils.Types) List(java.util.List) TypedVisitor(io.sundr.builder.TypedVisitor) TypeDef(io.sundr.model.TypeDef) Optional(java.util.Optional) TypeDefBuilder(io.sundr.model.TypeDefBuilder) Property(io.sundr.model.Property) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collectors(java.util.stream.Collectors) ClassRef(io.sundr.model.ClassRef) ArrayList(java.util.ArrayList) TypeDef(io.sundr.model.TypeDef) ClassRef(io.sundr.model.ClassRef) ArrayList(java.util.ArrayList) Property(io.sundr.model.Property) TypeDefBuilder(io.sundr.model.TypeDefBuilder)

Example 3 with TypeDefBuilder

use of io.sundr.model.TypeDefBuilder in project kubernetes-client by fabric8io.

the class SpecReplicasPathDetectorTest method shoudDetectSpecReplicasPath.

@Test
public void shoudDetectSpecReplicasPath() throws Exception {
    TypeDef def = Adapters.adaptType(WebServerWithStatusProperty.class, CONTEXT);
    SpecReplicasPathDetector detector = new SpecReplicasPathDetector();
    def = new TypeDefBuilder(def).accept(detector).build();
    assertTrue(detector.getPath().isPresent());
    assertEquals(".replicas", detector.getPath().get());
}
Also used : TypeDef(io.sundr.model.TypeDef) TypeDefBuilder(io.sundr.model.TypeDefBuilder) Test(org.junit.jupiter.api.Test)

Example 4 with TypeDefBuilder

use of io.sundr.model.TypeDefBuilder in project kubernetes-client by fabric8io.

the class SpecReplicasPathDetectorTest method shoudDetectNestedSpecReplicasPath.

@Test
public void shoudDetectNestedSpecReplicasPath() throws Exception {
    TypeDef def = Adapters.adaptType(WebServerWithSpec.class, CONTEXT);
    SpecReplicasPathDetector detector = new SpecReplicasPathDetector();
    def = new TypeDefBuilder(def).accept(detector).build();
    assertTrue(detector.getPath().isPresent());
    assertEquals(".spec.replicas", detector.getPath().get());
}
Also used : TypeDef(io.sundr.model.TypeDef) TypeDefBuilder(io.sundr.model.TypeDefBuilder) Test(org.junit.jupiter.api.Test)

Example 5 with TypeDefBuilder

use of io.sundr.model.TypeDefBuilder in project sundrio by sundrio.

the class GetDefinition method apply.

@Override
public TypeDef apply(ClassRef t) {
    String fullyQualifiedName = t.getFullyQualifiedName();
    TypeDef def = DefinitionRepository.getRepository().getDefinition(fullyQualifiedName);
    if (def != null) {
        return def;
    }
    Predicate<String> isLowerCase = w -> Character.isUpperCase(w.charAt(0));
    Predicate<String> inPackage = until(isLowerCase);
    Predicate<String> outOfPackage = after(isLowerCase);
    String packageName = Arrays.stream(fullyQualifiedName.split("\\.")).filter(inPackage).collect(Collectors.joining("."));
    String className = Arrays.stream(fullyQualifiedName.split("\\.")).filter(outOfPackage).collect(Collectors.joining("."));
    String ownerClassName = className.contains(".") ? className.substring(0, className.indexOf(".")) : null;
    if (ownerClassName != null) {
        className = className.substring(ownerClassName.length() + 1);
        return new TypeDefBuilder().withName(className).withPackageName(packageName).withOuterTypeName(packageName + "." + ownerClassName).build();
    }
    return new TypeDefBuilder().withName(className).withPackageName(packageName).build();
}
Also used : Predicates.until(io.sundr.utils.Predicates.until) Arrays(java.util.Arrays) Predicates.after(io.sundr.utils.Predicates.after) Predicate(java.util.function.Predicate) DefinitionRepository(io.sundr.model.repo.DefinitionRepository) TypeDef(io.sundr.model.TypeDef) TypeDefBuilder(io.sundr.model.TypeDefBuilder) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ClassRef(io.sundr.model.ClassRef) TypeDef(io.sundr.model.TypeDef) TypeDefBuilder(io.sundr.model.TypeDefBuilder)

Aggregations

TypeDefBuilder (io.sundr.model.TypeDefBuilder)30 TypeDef (io.sundr.model.TypeDef)25 ClassRef (io.sundr.model.ClassRef)14 ArrayList (java.util.ArrayList)14 TypeRef (io.sundr.model.TypeRef)11 Property (io.sundr.model.Property)10 Method (io.sundr.model.Method)9 HashMap (java.util.HashMap)8 Collectors (java.util.stream.Collectors)8 Element (javax.lang.model.element.Element)8 TypeElement (javax.lang.model.element.TypeElement)8 MethodBuilder (io.sundr.model.MethodBuilder)7 PropertyBuilder (io.sundr.model.PropertyBuilder)7 TypeParamDef (io.sundr.model.TypeParamDef)7 List (java.util.List)7 AnnotationRef (io.sundr.model.AnnotationRef)6 HashSet (java.util.HashSet)6 TypedVisitor (io.sundr.builder.TypedVisitor)5 Map (java.util.Map)5 Set (java.util.Set)5