Skip to content

Commit

Permalink
Improve formatGoResponseArrayValue
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-balitskyi committed Oct 3, 2024
1 parent 91b34b0 commit d37b42d
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions src/lib/code-sample/go.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,29 +301,25 @@ const formatGoResponseArrayValue = (
return 'nil'
}

const item = value[0]
if (item == null) {
const rawItem = value[0]
if (rawItem == null) {
throw new Error(`Null value in response array for '${key}'`)
}

const updatedPropertyChain = [...propertyChain, key]

if (isPrimitiveValue(item)) {
const arrayType = getPrimitiveTypeName(item)
const formattedItems = value.map((v) =>
formatGoResponseParamValue(
{ key, value: v, propertyChain: updatedPropertyChain },
responseResourceGoStructName,
),
)
const formattedItems = value.map((v) =>
formatGoResponseParamValue(
{ key, value: v, propertyChain: updatedPropertyChain },
responseResourceGoStructName,
),
)

if (isPrimitiveValue(rawItem)) {
const arrayType = getPrimitiveTypeName(rawItem)

return `[]${arrayType}{${formattedItems.join(', ')}}`
} else {
const formattedItems = value.map((v) =>
formatGoResponseParamValue(
{ key, value: v, propertyChain: updatedPropertyChain },
responseResourceGoStructName,
),
)
const structName = getStructName(
updatedPropertyChain,
responseResourceGoStructName,
Expand Down

0 comments on commit d37b42d

Please sign in to comment.