Skip to content

Commit

Permalink
refactor(core): move YamlFlowParser to YamlParser
Browse files Browse the repository at this point in the history
since it's already able to handle all types with generic
  • Loading branch information
tchiotludo committed Nov 4, 2024
1 parent 93ff17a commit 6e58b3f
Show file tree
Hide file tree
Showing 26 changed files with 102 additions and 110 deletions.
10 changes: 5 additions & 5 deletions cli/src/main/java/io/kestra/cli/AbstractValidateCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.kestra.core.models.validations.ModelValidator;
import io.kestra.core.models.validations.ValidateConstraintViolation;
import io.kestra.core.serializers.YamlFlowParser;
import io.kestra.core.serializers.YamlParser;
import io.micronaut.core.type.Argument;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.MediaType;
Expand Down Expand Up @@ -62,7 +62,7 @@ public static void handleValidateConstraintViolation(ValidateConstraintViolation
public static String buildYamlBody(Path directory) throws IOException {
try(var files = Files.walk(directory)) {
return files.filter(Files::isRegularFile)
.filter(YamlFlowParser::isValidExtension)
.filter(YamlParser::isValidExtension)
.map(throwFunction(path -> Files.readString(path, Charset.defaultCharset())))
.collect(Collectors.joining("\n---\n"));
}
Expand All @@ -71,7 +71,7 @@ public static String buildYamlBody(Path directory) throws IOException {
// bug in micronaut, we can't inject YamlFlowParser & ModelValidator, so we inject from implementation
public Integer call(
Class<?> cls,
YamlFlowParser yamlFlowParser,
YamlParser yamlParser,
ModelValidator modelValidator,
Function<Object, String> identity,
Function<Object, List<String>> warningsFunction,
Expand All @@ -85,10 +85,10 @@ public Integer call(
if(this.local) {
try(var files = Files.walk(directory)) {
files.filter(Files::isRegularFile)
.filter(YamlFlowParser::isValidExtension)
.filter(YamlParser::isValidExtension)
.forEach(path -> {
try {
Object parse = yamlFlowParser.parse(path.toFile(), cls);
Object parse = yamlParser.parse(path.toFile(), cls);
modelValidator.validate(parse);
stdOut("@|green \u2713|@ - " + identity.apply(parse));
List<String> warnings = warningsFunction.apply(parse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.kestra.cli.AbstractCommand;
import io.kestra.core.models.flows.Flow;
import io.kestra.core.models.hierarchies.GraphCluster;
import io.kestra.core.serializers.YamlFlowParser;
import io.kestra.core.serializers.YamlParser;
import io.kestra.core.services.Graph2DotService;
import io.kestra.core.utils.GraphUtils;
import io.micronaut.context.ApplicationContext;
Expand All @@ -29,7 +29,7 @@ public class FlowDotCommand extends AbstractCommand {
public Integer call() throws Exception {
super.call();

YamlFlowParser parser = applicationContext.getBean(YamlFlowParser.class);
YamlParser parser = applicationContext.getBean(YamlParser.class);
Flow flow = parser.parse(file.toFile(), Flow.class);

GraphCluster graph = GraphUtils.of(flow, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.kestra.cli.AbstractCommand;
import io.kestra.core.models.flows.Flow;
import io.kestra.core.models.validations.ModelValidator;
import io.kestra.core.serializers.YamlFlowParser;
import io.kestra.core.serializers.YamlParser;
import jakarta.inject.Inject;
import picocli.CommandLine;

Expand All @@ -21,7 +21,7 @@ public class FlowExpandCommand extends AbstractCommand {
private Path file;

@Inject
private YamlFlowParser yamlFlowParser;
private YamlParser yamlParser;

@Inject
private ModelValidator modelValidator;
Expand All @@ -31,7 +31,7 @@ public Integer call() throws Exception {
super.call();
stdErr("Warning, this functionality is deprecated and will be removed at some point.");
String content = IncludeHelperExpander.expand(Files.readString(file), file.getParent());
Flow flow = yamlFlowParser.parse(content, Flow.class);
Flow flow = yamlParser.parse(content, Flow.class);
modelValidator.validate(flow);
stdOut(content);
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.kestra.cli.AbstractApiCommand;
import io.kestra.cli.AbstractValidateCommand;
import io.kestra.core.serializers.YamlFlowParser;
import io.kestra.core.serializers.YamlParser;
import io.micronaut.core.type.Argument;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.MediaType;
Expand Down Expand Up @@ -41,7 +41,7 @@ public Integer call() throws Exception {
try (var files = Files.walk(directory)) {
List<String> flows = files
.filter(Files::isRegularFile)
.filter(YamlFlowParser::isValidExtension)
.filter(YamlParser::isValidExtension)
.map(path -> {
try {
return IncludeHelperExpander.expand(Files.readString(path, Charset.defaultCharset()), path.getParent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.kestra.cli.AbstractValidateCommand;
import io.kestra.core.models.flows.Flow;
import io.kestra.core.models.validations.ModelValidator;
import io.kestra.core.serializers.YamlFlowParser;
import io.kestra.core.serializers.YamlParser;
import io.kestra.core.services.FlowService;
import jakarta.inject.Inject;
import picocli.CommandLine;
Expand All @@ -17,7 +17,7 @@
)
public class FlowValidateCommand extends AbstractValidateCommand {
@Inject
private YamlFlowParser yamlFlowParser;
private YamlParser yamlParser;

@Inject
private ModelValidator modelValidator;
Expand All @@ -29,7 +29,7 @@ public class FlowValidateCommand extends AbstractValidateCommand {
public Integer call() throws Exception {
return this.call(
Flow.class,
yamlFlowParser,
yamlParser,
modelValidator,
(Object object) -> {
Flow flow = (Flow) object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.kestra.cli.AbstractValidateCommand;
import io.kestra.cli.commands.AbstractServiceNamespaceUpdateCommand;
import io.kestra.cli.commands.flows.IncludeHelperExpander;
import io.kestra.core.serializers.YamlFlowParser;
import io.kestra.core.serializers.YamlParser;
import io.micronaut.core.type.Argument;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.MediaType;
Expand All @@ -28,7 +28,7 @@
@Slf4j
public class FlowNamespaceUpdateCommand extends AbstractServiceNamespaceUpdateCommand {
@Inject
public YamlFlowParser yamlFlowParser;
public YamlParser yamlParser;

@CommandLine.Option(names = {"--override-namespaces"}, negatable = true, description = "replace namespace of all flows by the one provided")
public boolean override = false;
Expand All @@ -41,7 +41,7 @@ public Integer call() throws Exception {
try (var files = Files.walk(directory)) {
List<String> flows = files
.filter(Files::isRegularFile)
.filter(YamlFlowParser::isValidExtension)
.filter(YamlParser::isValidExtension)
.map(path -> {
try {
return IncludeHelperExpander.expand(Files.readString(path, Charset.defaultCharset()), path.getParent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import io.kestra.core.models.templates.Template;
import io.kestra.core.models.templates.TemplateEnabled;
import io.kestra.core.models.validations.ModelValidator;
import io.kestra.core.serializers.YamlFlowParser;
import io.kestra.core.serializers.YamlParser;
import jakarta.inject.Inject;
import picocli.CommandLine;

Expand All @@ -17,7 +17,7 @@
@TemplateEnabled
public class TemplateValidateCommand extends AbstractValidateCommand {
@Inject
private YamlFlowParser yamlFlowParser;
private YamlParser yamlParser;

@Inject
private ModelValidator modelValidator;
Expand All @@ -26,7 +26,7 @@ public class TemplateValidateCommand extends AbstractValidateCommand {
public Integer call() throws Exception {
return this.call(
Template.class,
yamlFlowParser,
yamlParser,
modelValidator,
(Object object) -> {
Template template = (Template) object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import io.kestra.cli.AbstractValidateCommand;
import io.kestra.cli.commands.AbstractServiceNamespaceUpdateCommand;
import io.kestra.cli.commands.templates.TemplateValidateCommand;
import io.kestra.core.models.templates.Template;
import io.kestra.core.models.templates.TemplateEnabled;
import io.kestra.core.serializers.YamlFlowParser;
import io.kestra.core.serializers.YamlParser;
import io.micronaut.core.type.Argument;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.MutableHttpRequest;
Expand All @@ -17,7 +16,7 @@

import java.nio.file.Files;
import java.util.List;
import java.util.stream.Collectors;

import jakarta.validation.ConstraintViolationException;

@CommandLine.Command(
Expand All @@ -29,7 +28,7 @@
@TemplateEnabled
public class TemplateNamespaceUpdateCommand extends AbstractServiceNamespaceUpdateCommand {
@Inject
public YamlFlowParser yamlFlowParser;
public YamlParser yamlParser;

@Override
public Integer call() throws Exception {
Expand All @@ -38,8 +37,8 @@ public Integer call() throws Exception {
try (var files = Files.walk(directory)) {
List<Template> templates = files
.filter(Files::isRegularFile)
.filter(YamlFlowParser::isValidExtension)
.map(path -> yamlFlowParser.parse(path.toFile(), Template.class))
.filter(YamlParser::isValidExtension)
.map(path -> yamlParser.parse(path.toFile(), Template.class))
.toList();

if (templates.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import io.kestra.core.models.flows.FlowWithSource;
import io.kestra.core.models.validations.ModelValidator;
import io.kestra.core.repositories.FlowRepositoryInterface;
import io.kestra.core.serializers.YamlFlowParser;
import io.kestra.core.serializers.YamlParser;
import io.kestra.core.services.FlowListenersInterface;
import io.micronaut.context.annotation.Requires;
import io.micronaut.context.annotation.Value;
Expand Down Expand Up @@ -37,7 +37,7 @@ public class FileChangedEventListener {
private FlowRepositoryInterface flowRepositoryInterface;

@Inject
private YamlFlowParser yamlFlowParser;
private YamlParser yamlParser;

@Inject
private ModelValidator modelValidator;
Expand Down Expand Up @@ -229,7 +229,7 @@ private void flowToFile(FlowWithSource flow, Path path) {

private Optional<Flow> parseFlow(String content, Path entry) {
try {
Flow flow = yamlFlowParser.parse(content, Flow.class);
Flow flow = yamlParser.parse(content, Flow.class);
modelValidator.validate(flow);
return Optional.of(flow);
} catch (ConstraintViolationException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.kestra.core.models.flows.Flow;
import io.kestra.core.models.validations.ModelValidator;
import io.kestra.core.serializers.YamlFlowParser;
import io.kestra.core.serializers.YamlParser;
import io.kestra.core.services.PluginDefaultService;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
Expand All @@ -19,7 +19,6 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand All @@ -31,7 +30,7 @@
@Slf4j
public class LocalFlowRepositoryLoader {
@Inject
private YamlFlowParser yamlFlowParser;
private YamlParser yamlParser;

@Inject
private FlowRepositoryInterface flowRepository;
Expand Down Expand Up @@ -72,13 +71,13 @@ public void load(File basePath) throws IOException {
Map<String, Flow> flowByUidInRepository = flowRepository.findAllForAllTenants().stream()
.collect(Collectors.toMap(Flow::uidWithoutRevision, Function.identity()));
List<Path> list = Files.walk(basePath.toPath())
.filter(YamlFlowParser::isValidExtension)
.filter(YamlParser::isValidExtension)
.toList();

for (Path file : list) {
try {
String flowSource = Files.readString(Path.of(file.toFile().getPath()), Charset.defaultCharset());
Flow parse = yamlFlowParser.parse(file.toFile(), Flow.class);
Flow parse = yamlParser.parse(file.toFile(), Flow.class);
modelValidator.validate(parse);

Flow inRepository = flowByUidInRepository.get(parse.uidWithoutRevision());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.Set;

@Singleton
public class YamlFlowParser {
public class YamlParser {
private static final ObjectMapper STRICT_MAPPER = JacksonMapper.ofYaml()
.enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION)
.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
Expand All @@ -34,7 +34,7 @@ public static boolean isValidExtension(Path path) {
}

public <T> T parse(String input, Class<T> cls) {
return readFlow(input, cls, type(cls));
return read(input, cls, type(cls));
}


Expand All @@ -59,7 +59,7 @@ private static <T> String type(Class<T> cls) {
public <T> T parse(File file, Class<T> cls) throws ConstraintViolationException {
try {
String input = IOUtils.toString(file.toURI(), StandardCharsets.UTF_8);
return readFlow(input, cls, type(cls));
return read(input, cls, type(cls));

} catch (IOException e) {
throw new ConstraintViolationException(
Expand All @@ -77,7 +77,7 @@ public <T> T parse(File file, Class<T> cls) throws ConstraintViolationException
}
}

private <T> T readFlow(String input, Class<T> objectClass, String resource) {
private <T> T read(String input, Class<T> objectClass, String resource) {
try {
return STRICT_MAPPER.readValue(input, objectClass);
} catch (JsonProcessingException e) {
Expand All @@ -91,8 +91,7 @@ private <T> T readFlow(String input, Class<T> objectClass, String resource) {
private static <T> void jsonProcessingExceptionHandler(T target, String resource, JsonProcessingException e) throws ConstraintViolationException {
if (e.getCause() instanceof ConstraintViolationException constraintViolationException) {
throw constraintViolationException;
}
else if (e instanceof InvalidTypeIdException invalidTypeIdException) {
} else if (e instanceof InvalidTypeIdException invalidTypeIdException) {
// This error is thrown when a non-existing task is used
throw new ConstraintViolationException(
"Invalid type: " + invalidTypeIdException.getTypeId(),
Expand All @@ -113,8 +112,7 @@ else if (e instanceof InvalidTypeIdException invalidTypeIdException) {
)
)
);
}
else if (e instanceof UnrecognizedPropertyException unrecognizedPropertyException) {
} else if (e instanceof UnrecognizedPropertyException unrecognizedPropertyException) {
var message = unrecognizedPropertyException.getOriginalMessage() + unrecognizedPropertyException.getMessageSuffix();
throw new ConstraintViolationException(
message,
Expand All @@ -127,16 +125,15 @@ else if (e instanceof UnrecognizedPropertyException unrecognizedPropertyExceptio
null
)
));
}
else {
} else {
throw new ConstraintViolationException(
"Illegal "+ resource +" yaml: " + e.getMessage(),
Collections.singleton(
ManualConstraintViolation.of(
e.getCause() == null ? e.getMessage() : e.getMessage() + "\nCaused by: " + e.getCause().getMessage(),
target,
(Class<T>) target.getClass(),
"flow",
"yaml",
null
)
)
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/io/kestra/core/services/FlowService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import io.kestra.core.plugins.PluginRegistry;
import io.kestra.core.repositories.FlowRepositoryInterface;
import io.kestra.core.serializers.JacksonMapper;
import io.kestra.core.serializers.YamlFlowParser;
import io.kestra.core.serializers.YamlParser;
import io.kestra.core.utils.ListUtils;
import io.micronaut.core.annotation.Nullable;
import jakarta.inject.Inject;
Expand Down Expand Up @@ -54,7 +54,7 @@ public class FlowService {
Optional<FlowRepositoryInterface> flowRepository;

@Inject
YamlFlowParser yamlFlowParser;
YamlParser yamlParser;

@Inject
PluginDefaultService pluginDefaultService;
Expand All @@ -71,7 +71,7 @@ public FlowWithSource importFlow(String tenantId, String source, boolean dryRun)
throw noRepositoryException();
}

FlowWithSource withTenant = yamlFlowParser.parse(source, Flow.class).toBuilder()
FlowWithSource withTenant = yamlParser.parse(source, Flow.class).toBuilder()
.tenantId(tenantId)
.build()
.withSource(source);
Expand Down
Loading

0 comments on commit 6e58b3f

Please sign in to comment.