Last active
July 16, 2023 16:13
-
-
Save eslof/cde1677b6e72dc19a517347c3c4c115e to your computer and use it in GitHub Desktop.
build gpt 0613 typescript function definition from openai spec php array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function generateTypeScript($dummy): string { | |
$output = "// " . $dummy['description'] . "\n"; | |
$output .= "type " . $dummy['name'] . " = (_: {\n"; | |
$output .= generateProperties($dummy['parameters']['properties'], $dummy['parameters']['required'], false); | |
$output .= "}) => any;\n"; | |
return $output; | |
} | |
function generateProperties($properties, $required, $indent): string { | |
$output = ''; | |
$indentation = $indent ? ' ' : ''; | |
foreach ($properties as $name => $property) { | |
if (isset($property['description'])) { | |
$output .= $indentation . "// " . $property['description'] . "\n"; | |
} | |
$output .= $indentation . $name . (in_array($name, $required) ? ": " : "?: "); | |
$output .= generateType($property, $indent) . ','; | |
if (isset($property['default'])) { | |
if (is_array($property['default'])) { | |
$default = "['" . implode("', '", $property['default']) . "']"; | |
} else if (is_bool($property['default']) || is_null($property['default'])) { | |
$default = json_encode($property['default']); | |
} else { | |
$default = $property['default']; | |
} | |
$output .= " // default: " . $default; | |
} | |
$output .= "\n"; | |
} | |
return $output; | |
} | |
function generateType($property, $indent) { | |
switch ($property['type']) { | |
case 'object': | |
$output = "{\n"; | |
$output .= generateProperties($property['properties'], $property['required'] ?? [], $indent + 1); | |
$output .= '}'; | |
return $output; | |
case 'array': | |
$isItemsEnum = isset($property['items']['enum']); | |
switch ($property['items']['type']) { | |
case 'object': | |
$output = "{\n"; | |
$output .= generateProperties($property['items']['properties'], $property['items']['required'] ?? [], $indent + 1); | |
$output .= "}[]"; | |
break; | |
case 'array': | |
$output = generateType($property['items'], $indent + 1) . "[]"; | |
break; | |
case 'string': | |
if ($isItemsEnum) { | |
$output = "\"" . implode("\" | \"", $property['items']['enum']) . "\"[]"; | |
break; | |
} | |
$output = 'string[]'; | |
break; | |
case 'number': | |
if ($isItemsEnum) { | |
$output = implode(" | ", $property['items']['enum']) . "[]"; | |
break; | |
} | |
$output = 'number[]'; | |
break; | |
default: | |
$output = $property['items']['type'] . '[]'; | |
} | |
return $output; | |
case 'string': | |
if (isset($property['enum'])) { | |
return "\"" . implode("\" | \"", $property['enum'] ?? []) . "\""; | |
} | |
return 'string'; | |
case 'number': | |
if (isset($property['enum'])) { | |
return implode(" | ", $property['enum']) . "[]"; | |
} | |
return 'number'; | |
default: | |
return $property['type']; | |
} | |
} |
Author
eslof
commented
Jul 16, 2023
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment