-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added "insert" methods to the data model for collections
- Loading branch information
1 parent
997b374
commit 313e4e1
Showing
10 changed files
with
905 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
generator/src/main/resources/base/io/apicurio/umg/base/util/DataModelUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.apicurio.umg.base.util; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class DataModelUtil { | ||
|
||
public static <V> Map<String, V> insertMapEntry(Map<String, V> map, String key, V value, int atIndex) { | ||
// If the new key is present, then return without modification. | ||
if (map.containsKey(key)) { | ||
return map; | ||
} | ||
// If the map isn't an LHM then ordering can't be maintained anyway | ||
// If the atIndex is null then we're trying to undo a command that was persisted prior to this functionality being added | ||
// If the atIndex is >= the map size it has to go at the end anyway | ||
if (!(map instanceof LinkedHashMap) || atIndex >= map.size()){ | ||
map.put(key, value); | ||
return map; | ||
} | ||
|
||
final LinkedHashMap<String, V> newMap = new LinkedHashMap<>(); | ||
// In order to maintain ordering of the elements when replacing a key in a LinkedHashMap, | ||
// create a new instance and populate it in insertion order | ||
int index = 0; | ||
for (Map.Entry<String, V> entry : map.entrySet()) { | ||
if (index++ == atIndex) { | ||
newMap.put(key, value); | ||
} | ||
newMap.put(entry.getKey(), entry.getValue()); | ||
} | ||
return newMap; | ||
} | ||
|
||
public static <V> List<V> insertListEntry(List<V> list, V value, int atIndex) { | ||
if (atIndex >= list.size()) { | ||
list.add(value); | ||
} else if (atIndex < 0) { | ||
list.add(0, value); | ||
} else { | ||
list.add(atIndex, value); | ||
} | ||
return list; | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
generator/src/test/java/io/apicurio/umg/GeneratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.apicurio.umg; | ||
|
||
import io.apicurio.umg.io.SpecificationLoader; | ||
import io.apicurio.umg.models.spec.SpecificationModel; | ||
import org.apache.commons.io.FileUtils; | ||
import org.junit.Test; | ||
|
||
import java.io.File; | ||
import java.nio.file.Files; | ||
import java.util.List; | ||
|
||
public class GeneratorTest { | ||
|
||
@Test | ||
public void testGenerator() throws Exception { | ||
File outputDir; | ||
|
||
String outputDirPath = System.getenv("GENERATE_TEST_OUTPUT_DIR"); | ||
if (outputDirPath != null) { | ||
outputDir = new File(outputDirPath); | ||
outputDir.mkdirs(); | ||
} else { | ||
outputDir = Files.createTempDirectory(GeneratorTest.class.getSimpleName()).toFile(); | ||
} | ||
|
||
File umgTestOutputDir = Files.createTempDirectory(GeneratorTest.class.getSimpleName() + "-test").toFile(); | ||
UnifiedModelGeneratorConfig config = UnifiedModelGeneratorConfig.builder() | ||
.outputDirectory(outputDir) | ||
.testOutputDirectory(umgTestOutputDir) | ||
.generateTestFixtures(false) | ||
.rootNamespace("io.apicurio.umg.test").build(); | ||
// Load the specs | ||
List<SpecificationModel> specs = List.of( | ||
SpecificationLoader.loadSpec(GeneratorTest.class.getResource("openapi.yaml")) | ||
); | ||
// Create a unified model generator | ||
UnifiedModelGenerator generator = new UnifiedModelGenerator(config, specs); | ||
// Generate the source code into the target output directory. | ||
try { | ||
generator.generate(); | ||
} finally { | ||
if (outputDirPath == null) { | ||
FileUtils.deleteDirectory(outputDir); | ||
FileUtils.deleteDirectory(umgTestOutputDir); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.