change style of enums

This commit is contained in:
ahmad.zo 2024-01-30 10:01:12 +03:30
parent c222db1429
commit 0c7281624e
2 changed files with 22 additions and 12 deletions

View File

@ -109,9 +109,11 @@ Takes dictionary as argument with keys:
{{- $subschema := index $resolved.properties (first $.FieldPath) -}}
{{- if eq 1 (len $.FieldPath) -}}
{{- /* TODO: The original explain would say RESOURCE instead of FIELD here under some circumstances */ -}}
FIELD: {{first $.FieldPath}} <{{ template "typeGuess" (dict "schema" $subschema "Document" $.Document) }}>{{ template "extractEnum" (dict "schema" $subschema "Document" $.Document) }}{{"\n"}}
FIELD: {{first $.FieldPath}} <{{ template "typeGuess" (dict "schema" $subschema "Document" $.Document) }}>{{"\n"}}
{{- template "extractEnum" (dict "schema" $subschema "Document" $.Document "singleView" true) -}}{{"\n"}}
{{- "\n" -}}
{{- end -}}
{{- template "output" (set $nextContext "history" (dict) "FieldPath" (slice $.FieldPath 1) "schema" $subschema ) -}}
{{- else if $resolved.items -}}
{{- /* If the schema is an array then traverse the array item fields */ -}}
@ -201,8 +203,9 @@ Takes dictionary as argument with keys:
{{- $level := or $.level 0 -}}
{{- $indentAmount := mul $level 2 -}}
{{- $fieldSchema := index $.schema.properties $.name -}}
{{- $.name | indent $indentAmount -}}{{"\t"}}<{{ template "typeGuess" (dict "schema" $fieldSchema "Document" $.Document) }}>{{ template "extractEnum" (dict "schema" $fieldSchema "Document" $.Document) }}
{{- $.name | indent $indentAmount -}}{{"\t"}}<{{ template "typeGuess" (dict "schema" $fieldSchema "Document" $.Document) }}>
{{- if contains $.schema.required $.name}} -required-{{- end -}}
{{- template "extractEnum" (dict "schema" $fieldSchema "Document" $.Document "singleView" false) -}}
{{- "\n" -}}
{{- if not $.short -}}
{{- or $fieldSchema.description "<no description>" | wrap (sub 78 $indentAmount) | indent (add $indentAmount 2) -}}{{- "\n" -}}
@ -284,23 +287,28 @@ Takes dictionary as argument with keys:
{{- /* Check if there is any enum returns it in this format e.g.:
(enum: !=, =, =~, !~)
ENUM: !=, =, =~, !~
enum: !=, =, =~, !~
Can change the style of enum in future by modifying this function
Takes dictionary as argument with keys:
schema: openapiv3 JSON schema
Document: openapi document
singleView: determine if ENUM should be printed uppercase or not, used in single Field view
*/ -}}
{{- define "extractEnum" -}}
{{- with $.schema -}}
{{- if .enum -}}
{{- " (enum: " -}}
{{- if $.singleView -}}
{{- "ENUM: " -}}
{{- else -}}
{{- " enum: " -}}
{{- end -}}
{{- range $index, $element := .enum -}}
{{- if gt $index 0 -}} {{- ", " -}} {{- end -}}
{{- $element -}}
{{- end -}}
{{- ")" -}}
{{- end -}}
{{- end -}}
{{- end -}}

View File

@ -656,7 +656,7 @@ func TestPlaintext(t *testing.T) {
"schema": map[string]any{
"type": "string",
"description": "a description that should not be printed",
"enum": []string{},
"enum": []any{},
},
},
Checks: []check{
@ -664,33 +664,35 @@ func TestPlaintext(t *testing.T) {
},
},
{
// show that extractEnum can extract string enum and style it
// show that extractEnum can extract any enum slice and style it uppercase
Name: "Enum",
Subtemplate: "extractEnum",
Context: map[string]any{
"schema": map[string]any{
"type": "string",
"description": "a description that should not be printed",
"enum": []string{"!=", "!", "=="},
"enum": []any{1, 2, 3},
},
"singleView": true,
},
Checks: []check{
checkEquals(" (enum: !=, !, ==)"),
checkEquals("ENUM: 1, 2, 3"),
},
},
{
// show that extractEnum can extract integer enum and style it
// show that extractEnum can extract any enum slice and style it lowercase
Name: "Enum",
Subtemplate: "extractEnum",
Context: map[string]any{
"schema": map[string]any{
"type": "string",
"description": "a description that should not be printed",
"enum": []int{1, 2, 3},
"enum": []any{1, 2, 3},
},
"singleView": false,
},
Checks: []check{
checkEquals(" (enum: 1, 2, 3)"),
checkEquals(" enum: 1, 2, 3"),
},
},
}