Skip to content

Commit

Permalink
Use concrete type for default stub map key (#76)
Browse files Browse the repository at this point in the history
Some models use specific non-primitive type
for a map key, such as an enum. Not resolving
to that results in a compilation failure.
  • Loading branch information
knovichikhin authored Nov 4, 2023
1 parent 40631a9 commit 65e5e80
Showing 1 changed file with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ internal extension ServiceModelCodeGenerator {
return fieldValue
}

private func getMapFieldValue(lengthConstraint: LengthRangeConstraint<Int>, valueType: String,
private func getMapFieldValue(lengthConstraint: LengthRangeConstraint<Int>,
keyType: String,
valueType: String,
fileBuilder: FileBuilder) -> String {
let fieldValue: String

Expand All @@ -173,8 +175,14 @@ internal extension ServiceModelCodeGenerator {
// create a map that satifies at least the minimum constraint
var mapConstructor: [String] = []
let defaultValue = getDefaultValue(type: valueType, fileBuilder: fileBuilder)
for index in 0..<requiredSize {
mapConstructor.append("\"Entry_\(index)\": \(defaultValue)")

if requiredSize == 1 {
let defaultKeyValue = getDefaultValue(type: keyType, fileBuilder: fileBuilder)
mapConstructor.append("\(defaultKeyValue): \(defaultValue)")
} else {
for index in 0..<requiredSize {
mapConstructor.append("\"Entry_\(index)\": \(defaultValue)")
}
}

fieldValue = "[\(mapConstructor.joined(separator: ", "))]"
Expand Down Expand Up @@ -211,9 +219,10 @@ internal extension ServiceModelCodeGenerator {
fieldValue = overrideDefaultValue ?? "\"\(exampleDateString)\""
case .list(type: let listType, lengthConstraint: let lengthConstraint):
fieldValue = getListFieldValue(lengthConstraint: lengthConstraint, listType: listType, fileBuilder: fileBuilder)
case .map(keyType: _, valueType: let valueType,
case .map(keyType: let keyType,
valueType: let valueType,
lengthConstraint: let lengthConstraint):
fieldValue = getMapFieldValue(lengthConstraint: lengthConstraint, valueType: valueType, fileBuilder: fileBuilder)
fieldValue = getMapFieldValue(lengthConstraint: lengthConstraint, keyType: keyType, valueType: valueType, fileBuilder: fileBuilder)
}

return fieldValue
Expand Down

0 comments on commit 65e5e80

Please sign in to comment.