Skip to content

Commit

Permalink
Use most common suffix for IDL inline IO
Browse files Browse the repository at this point in the history
This commit updates IDL serialization to use the most common suffix
for serializing IDL models when inlining input/output shapes. Before
this update, the first suffix encountered would be used, even if it
was only used that one time.
  • Loading branch information
kstich committed Sep 17, 2024
1 parent 0b9ffc4 commit b114596
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ private Pair<String, String> determineInlineSuffixes(Model fullModel, Collection
return Pair.of(inlineInputSuffix, inlineOutputSuffix);
}

Set<String> inputSuffixes = new HashSet<>();
Set<String> outputSuffixes = new HashSet<>();
Map<String, Integer> inputSuffixes = new LinkedHashMap<>();
Map<String, Integer> outputSuffixes = new LinkedHashMap<>();
for (Shape shape : shapes) {
if (!shape.isOperationShape()) {
continue;
Expand All @@ -255,15 +255,28 @@ private Pair<String, String> determineInlineSuffixes(Model fullModel, Collection
StructureShape output = fullModel.expectShape(operation.getOutputShape(), StructureShape.class);

if (shapes.contains(input) && input.getId().getName().startsWith(operation.getId().getName())) {
inputSuffixes.add(input.getId().getName().substring(operation.getId().getName().length()));
String inputSuffix = input.getId().getName().substring(operation.getId().getName().length());
int inputCount = inputSuffixes.getOrDefault(inputSuffix, 0);
inputSuffixes.put(inputSuffix, ++inputCount);
}

if (shapes.contains(output) && output.getId().getName().startsWith(operation.getId().getName())) {
outputSuffixes.add(output.getId().getName().substring(operation.getId().getName().length()));
String outputSuffix = output.getId().getName().substring(operation.getId().getName().length());
int outputCount = outputSuffixes.getOrDefault(outputSuffix, 0);
outputSuffixes.put(outputSuffix, ++outputCount);
}
}
String inputSuffix = inputSuffixes.size() == 1 ? inputSuffixes.iterator().next() : inlineInputSuffix;
String outputSuffix = outputSuffixes.size() == 1 ? outputSuffixes.iterator().next() : inlineOutputSuffix;

String inputSuffix = inputSuffixes.entrySet()
.stream()
.max(Map.Entry.comparingByValue())
.map(Map.Entry::getKey)
.orElse(inlineInputSuffix);
String outputSuffix = outputSuffixes.entrySet()
.stream()
.max(Map.Entry.comparingByValue())
.map(Map.Entry::getKey)
.orElse(inlineInputSuffix);
return Pair.of(inputSuffix, outputSuffix);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ $operationOutputSuffix: "Response"

namespace com.example

operation NotShared {
input: NotSharedInput
output: NotSharedOutput
}

operation SharedCustomA {
input := {}
output := {}
Expand All @@ -13,3 +18,9 @@ operation SharedCustomB {
input := {}
output := {}
}

@input
structure NotSharedInput {}

@output
structure NotSharedOutput {}

0 comments on commit b114596

Please sign in to comment.