From d37b42d95ee219eaccab93da27200b4350bc4b4a Mon Sep 17 00:00:00 2001 From: Andrii Balitskyi <10balian10@gmail.com> Date: Thu, 3 Oct 2024 11:46:56 +0200 Subject: [PATCH] Improve formatGoResponseArrayValue --- src/lib/code-sample/go.ts | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/lib/code-sample/go.ts b/src/lib/code-sample/go.ts index f1966be..19517f3 100644 --- a/src/lib/code-sample/go.ts +++ b/src/lib/code-sample/go.ts @@ -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,