Skip to content

Commit 7417148

Browse files
authored
Fixed infinite choice option generation (#783)
* Fixed infinite choice option generate * Fixed mistake
1 parent 9a27524 commit 7417148

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

rosetta-lang/model/RosettaSimple.xcore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,14 @@ class Choice extends Data {
100100
}
101101

102102
class ChoiceOption extends Attribute {
103+
String _hardcodedName
103104
contains RosettaCardinality _hardcodedCardinality
104105

105106
op String getName() {
106-
NodeModelUtils.findNodesForFeature(typeCall, Literals.TYPE_CALL__TYPE).head.text.strip
107+
if (_hardcodedName === null) {
108+
_hardcodedName = NodeModelUtils.getTokenText(NodeModelUtils.findNodesForFeature(typeCall, Literals.TYPE_CALL__TYPE).head)
109+
}
110+
return _hardcodedName
107111
}
108112

109113
op RosettaCardinality getCard() {

rosetta-lang/src/main/java/com/regnosys/rosetta/generator/GeneratorScope.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,24 @@ private void computeActualNames() {
269269
List<GeneratedIdentifier> ids = idsByDesiredName.get(desiredName);
270270
for (int i = 0; i < ids.size(); i++) {
271271
GeneratedIdentifier id = ids.get(i);
272-
String name = id.getDesiredName();
272+
String name = desiredName;
273273
if (ids.size() > 1) {
274274
name += i;
275275
}
276-
while (takenNames.contains(name) || !isValidIdentifier(name)) {
277-
name = escapeName(name);
276+
boolean lastWasValid = true;
277+
while (true) {
278+
boolean isValid = isValidIdentifier(name);
279+
if (!lastWasValid && !isValid) {
280+
// Escaping the invalid identifier did not work - throw an exception. Otherwise we could end up in an infinite loop.
281+
// If this is thrown, this usually indicates there is an implementation error in `escapeName` or `isValidIdentifier`.
282+
throw new RuntimeException("Tried escaping the identifier `" + name + "`, but it is still not a valid identifier.");
283+
}
284+
if (takenNames.contains(name) || !isValid) {
285+
name = escapeName(name);
286+
} else {
287+
break;
288+
}
289+
lastWasValid = isValid;
278290
}
279291
takenNames.add(name);
280292
this.actualNames.put(id, name);

0 commit comments

Comments
 (0)