use of io.sundr.model.TypeDef 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));
});
}
use of io.sundr.model.TypeDef 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();
}
}
});
}
use of io.sundr.model.TypeDef in project kubernetes-client by fabric8io.
the class ClassDependenciesVisitor method visit.
@Override
public void visit(TypeDefBuilder builder) {
TypeDef type = builder.build();
// finish quickly if we're ignoring the class or we already have processed it
// note that we cannot simply check the traversed class set to know if a class has been processed because classes
// are usually added to the traversed set before they're looked at in depth
final String className = type.getFullyQualifiedName();
if (ignore(className) || processed.contains(className)) {
return;
}
// process all references to see if they need to be added or not
type.getReferences().forEach(c -> {
final String fqn = c.getFullyQualifiedName();
if (ignore(fqn)) {
return;
}
// check if we're dealing with a collection type to extract parameter types if existing
if (fqn.startsWith("java.util") && (Collections.isCollection(c) || Collections.IS_MAP.apply(c))) {
c.getArguments().forEach(this::processTypeRef);
} else {
// otherwise, process all non-JDK classes that we haven't already processed
if (!ignore(fqn) && classesForCR.add(fqn)) {
// deal with generic arguments if present
c.getArguments().forEach(this::processTypeRef);
}
}
});
// add classes from extends list
type.getExtendsList().forEach(this::processTypeRef);
// add class to the processed classes
processed.add(className);
}
use of io.sundr.model.TypeDef in project kubernetes-client by fabric8io.
the class JsonSchemaTest method shouldThrowIfSchemaSwapHasUnmatchedField.
@Test
void shouldThrowIfSchemaSwapHasUnmatchedField() {
TypeDef incorrectExtraction = Types.typeDefFrom(IncorrectExtraction.class);
assertThrows(IllegalArgumentException.class, () -> JsonSchema.from(incorrectExtraction));
}
use of io.sundr.model.TypeDef in project kubernetes-client by fabric8io.
the class JsonSchemaTest method shouldThrowIfSchemaSwapHasUnmatchedClass.
@Test
void shouldThrowIfSchemaSwapHasUnmatchedClass() {
TypeDef incorrectExtraction2 = Types.typeDefFrom(IncorrectExtraction2.class);
assertThrows(IllegalArgumentException.class, () -> JsonSchema.from(incorrectExtraction2));
}
Aggregations