Skip to content

Commit bed7002

Browse files
refactor: return context function if schema exist (#456)
1 parent 4c9374e commit bed7002

File tree

1 file changed

+32
-35
lines changed

1 file changed

+32
-35
lines changed

index.js

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -832,11 +832,19 @@ function toJSON (variableName) {
832832
`
833833
}
834834

835-
function buildObject (location, functionName, locationPath) {
835+
function buildObject (location, locationPath) {
836836
const schema = location.schema
837837
if (schema.$id !== undefined) {
838838
schemaReferenceMap.set(schema.$id, schema)
839839
}
840+
841+
if (objectReferenceSerializersMap.has(schema)) {
842+
return objectReferenceSerializersMap.get(schema)
843+
}
844+
845+
const functionName = generateFuncName()
846+
objectReferenceSerializersMap.set(schema, functionName)
847+
840848
let functionCode = `
841849
function ${functionName} (input) {
842850
// ${locationPath}
@@ -849,16 +857,6 @@ function buildObject (location, functionName, locationPath) {
849857
`
850858
}
851859

852-
if (objectReferenceSerializersMap.has(schema) && objectReferenceSerializersMap.get(schema) !== functionName) {
853-
functionCode += `
854-
return ${objectReferenceSerializersMap.get(schema)}(input)
855-
}
856-
`
857-
contextFunctions.push(functionCode)
858-
return
859-
}
860-
objectReferenceSerializersMap.set(schema, functionName)
861-
862860
functionCode += `
863861
var obj = ${toJSON('input')}
864862
var json = '{'
@@ -883,24 +881,14 @@ function buildObject (location, functionName, locationPath) {
883881
`
884882

885883
contextFunctions.push(functionCode)
884+
return functionName
886885
}
887886

888-
function buildArray (location, functionName, locationPath) {
887+
function buildArray (location, locationPath) {
889888
let schema = location.schema
890889
if (schema.$id !== undefined) {
891890
schemaReferenceMap.set(schema.$id, schema)
892891
}
893-
let functionCode = `
894-
function ${functionName} (obj) {
895-
// ${locationPath}
896-
`
897-
if (schema.nullable) {
898-
functionCode += `
899-
if(obj === null) {
900-
return 'null';
901-
}
902-
`
903-
}
904892

905893
// default to any items type
906894
if (!schema.items) {
@@ -916,16 +904,26 @@ function buildArray (location, functionName, locationPath) {
916904

917905
location = refFinder(schema.items.$ref, location)
918906
schema.items = location.schema
907+
}
908+
909+
if (arrayItemsReferenceSerializersMap.has(schema.items)) {
910+
return arrayItemsReferenceSerializersMap.get(schema.items)
911+
}
912+
913+
const functionName = generateFuncName()
914+
arrayItemsReferenceSerializersMap.set(schema.items, functionName)
915+
916+
let functionCode = `
917+
function ${functionName} (obj) {
918+
// ${locationPath}
919+
`
919920

920-
if (arrayItemsReferenceSerializersMap.has(schema.items)) {
921-
functionCode += `
922-
return ${arrayItemsReferenceSerializersMap.get(schema.items)}(obj)
921+
if (schema.nullable) {
922+
functionCode += `
923+
if (obj === null) {
924+
return 'null';
923925
}
924-
`
925-
contextFunctions.push(functionCode)
926-
return
927-
}
928-
arrayItemsReferenceSerializersMap.set(schema.items, functionName)
926+
`
929927
}
930928

931929
let result = { code: '' }
@@ -990,6 +988,7 @@ function buildArray (location, functionName, locationPath) {
990988
}`
991989

992990
contextFunctions.push(functionCode)
991+
return functionName
993992
}
994993

995994
function buildArrayTypeCondition (type, accessor) {
@@ -1109,13 +1108,11 @@ function buildValue (locationPath, input, location) {
11091108
code += `json += ${funcName}(${input})`
11101109
break
11111110
case 'object':
1112-
funcName = generateFuncName()
1113-
buildObject(location, funcName, locationPath)
1111+
funcName = buildObject(location, locationPath)
11141112
code += `json += ${funcName}(${input})`
11151113
break
11161114
case 'array':
1117-
funcName = generateFuncName()
1118-
buildArray(location, funcName, locationPath)
1115+
funcName = buildArray(location, locationPath)
11191116
code += `json += ${funcName}(${input})`
11201117
break
11211118
case undefined:

0 commit comments

Comments
 (0)