From 66acd3fc796fc6637eff4026891f98ec6dbf6485 Mon Sep 17 00:00:00 2001 From: Femi3211 Date: Thu, 4 Jul 2024 16:32:24 +0200 Subject: [PATCH 1/2] added: model override yaml functionality --- .../java/com/adaptivescale/rosetta/cli/Cli.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/cli/src/main/java/com/adaptivescale/rosetta/cli/Cli.java b/cli/src/main/java/com/adaptivescale/rosetta/cli/Cli.java index 3373243c..2e1f3f4d 100644 --- a/cli/src/main/java/com/adaptivescale/rosetta/cli/Cli.java +++ b/cli/src/main/java/com/adaptivescale/rosetta/cli/Cli.java @@ -535,6 +535,20 @@ private Stream getDatabaseForModel(Path directory, Stri .map(path -> { try { Database input = new ObjectMapper(new YAMLFactory()).readValue(path.toFile(), Database.class); + Path overrideFile = Path.of(FilenameUtils.removeExtension(path.toString()) + ".override.yaml"); + if (!Files.isDirectory(overrideFile)) { + Database override = new ObjectMapper(new YAMLFactory()).readValue(overrideFile.toFile(), Database.class); + HashMap tables = new HashMap(); + input.getTables().stream().forEach(tbl -> tables.put(tbl.getSchema() + tbl.getName(), tbl)); + override.getTables().stream().forEach(tbl -> { + if (tables.containsKey(tbl.getSchema() + tbl.getName())) { + //TODO: this currently overrides the whole table, we can extend this to include table or column level specific override + tables.put(tbl.getSchema() + tbl.getName(), tbl); + } + }); + input.setTables(tables.values()); + } + return new FileNameAndDatabasePair(path.getFileName().toString(), input); } catch (Exception exception) { throw new RuntimeException(exception); From 52423bbb552da8df2ec4cc8036ca3f97b242be42 Mon Sep 17 00:00:00 2001 From: Femi3211 Date: Fri, 5 Jul 2024 11:47:38 +0200 Subject: [PATCH 2/2] enhancement: moved the part of the code for overriding the code into a function, and removed unnecessary condition --- .../com/adaptivescale/rosetta/cli/Cli.java | 38 ++++++++++++------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/cli/src/main/java/com/adaptivescale/rosetta/cli/Cli.java b/cli/src/main/java/com/adaptivescale/rosetta/cli/Cli.java index 2e1f3f4d..9004efeb 100644 --- a/cli/src/main/java/com/adaptivescale/rosetta/cli/Cli.java +++ b/cli/src/main/java/com/adaptivescale/rosetta/cli/Cli.java @@ -535,20 +535,7 @@ private Stream getDatabaseForModel(Path directory, Stri .map(path -> { try { Database input = new ObjectMapper(new YAMLFactory()).readValue(path.toFile(), Database.class); - Path overrideFile = Path.of(FilenameUtils.removeExtension(path.toString()) + ".override.yaml"); - if (!Files.isDirectory(overrideFile)) { - Database override = new ObjectMapper(new YAMLFactory()).readValue(overrideFile.toFile(), Database.class); - HashMap tables = new HashMap(); - input.getTables().stream().forEach(tbl -> tables.put(tbl.getSchema() + tbl.getName(), tbl)); - override.getTables().stream().forEach(tbl -> { - if (tables.containsKey(tbl.getSchema() + tbl.getName())) { - //TODO: this currently overrides the whole table, we can extend this to include table or column level specific override - tables.put(tbl.getSchema() + tbl.getName(), tbl); - } - }); - input.setTables(tables.values()); - } - + applyOverride(path, input); return new FileNameAndDatabasePair(path.getFileName().toString(), input); } catch (Exception exception) { throw new RuntimeException(exception); @@ -556,6 +543,29 @@ private Stream getDatabaseForModel(Path directory, Stri }); } + + /** + * Checks if an override file exists for the selected model. If it does, + * it applies the changes from the override file to the original model. + * + * @param path {@link Path} The path to the original model. + * @param input {@link Database} The original model. + * @throws IOException If an I/O error occurs. + */ + private void applyOverride(Path path,Database input) throws IOException { + Path overrideFile = Path.of(FilenameUtils.removeExtension(path.toString()) + ".override.yaml"); + if (!Files.isDirectory(overrideFile)) { + Database override = new ObjectMapper(new YAMLFactory()).readValue(overrideFile.toFile(), Database.class); + HashMap tables = new HashMap(); + input.getTables().stream().forEach(tbl -> tables.put(tbl.getSchema() + tbl.getName(), tbl)); + override.getTables().stream().forEach(tbl -> { + //TODO: this currently overrides the whole table, we can extend this to include table or column level specific override + tables.put(tbl.getSchema() + tbl.getName(), tbl); + }); + input.setTables(tables.values()); + } + } + private Function translateDatabases(Translator translator) { return fileNameAndModelPair -> { try {