change the extractEnum function to add limit, intent and add tests for new behaviours

This commit is contained in:
ahmad.zo 2024-01-31 13:54:53 +03:30
parent 0283498ab4
commit 9d4997ea69
2 changed files with 90 additions and 20 deletions

View File

@ -110,7 +110,7 @@ Takes dictionary as argument with keys:
{{- if eq 1 (len $.FieldPath) -}} {{- if eq 1 (len $.FieldPath) -}}
{{- /* TODO: The original explain would say RESOURCE instead of FIELD here under some circumstances */ -}} {{- /* TODO: The original explain would say RESOURCE instead of FIELD here under some circumstances */ -}}
FIELD: {{first $.FieldPath}} <{{ template "typeGuess" (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"}} {{- template "extractEnum" (dict "schema" $subschema "Document" $.Document "longFormView" false "limit" 3) -}}{{"\n"}}
{{- "\n" -}} {{- "\n" -}}
{{- end -}} {{- end -}}
@ -205,7 +205,7 @@ Takes dictionary as argument with keys:
{{- $fieldSchema := index $.schema.properties $.name -}} {{- $fieldSchema := index $.schema.properties $.name -}}
{{- $.name | indent $indentAmount -}}{{"\t"}}<{{ template "typeGuess" (dict "schema" $fieldSchema "Document" $.Document) }}> {{- $.name | indent $indentAmount -}}{{"\t"}}<{{ template "typeGuess" (dict "schema" $fieldSchema "Document" $.Document) }}>
{{- if contains $.schema.required $.name}} -required-{{- end -}} {{- if contains $.schema.required $.name}} -required-{{- end -}}
{{- template "extractEnum" (dict "schema" $fieldSchema "Document" $.Document "singleView" false) -}} {{- template "extractEnum" (dict "schema" $fieldSchema "Document" $.Document "longFormView" true "limit" -1 "indentAmount" $indentAmount) -}}
{{- "\n" -}} {{- "\n" -}}
{{- if not $.short -}} {{- if not $.short -}}
{{- or $fieldSchema.description "<no description>" | wrap (sub 78 $indentAmount) | indent (add $indentAmount 2) -}}{{- "\n" -}} {{- or $fieldSchema.description "<no description>" | wrap (sub 78 $indentAmount) | indent (add $indentAmount 2) -}}{{- "\n" -}}
@ -287,28 +287,45 @@ Takes dictionary as argument with keys:
{{- /* Check if there is any enum returns it in this format e.g.: {{- /* Check if there is any enum returns it in this format e.g.:
ENUM: !=, =, =~, !~ ENUM: "", BlockDevice, CharDevice, Directory
enum: !=, =, =~, !~ enum: "", BlockDevice, CharDevice, Directory
Can change the style of enum in future by modifying this function Can change the style of enum in future by modifying this function
Takes dictionary as argument with keys: Takes dictionary as argument with keys:
schema: openapiv3 JSON schema schema: openapiv3 JSON schema
Document: openapi document Document: openapi document
singleView: determine if ENUM should be printed uppercase or not, used in single Field view longFormView: (boolean) prints the enums in extended long form view or not
limit: (int) truncate the amount of enums that can be printed in simple view, -1 means all items
indentAmount: intent of the beginning enum line in longform view
*/ -}} */ -}}
{{- define "extractEnum" -}} {{- define "extractEnum" -}}
{{- with $.schema -}} {{- with $.schema -}}
{{- if .enum -}} {{- if .enum -}}
{{- if $.singleView -}} {{- $enumLen := len .enum -}}
{{- "ENUM: " -}} {{- $limit := or $.limit -1 -}}
{{- if $.longFormView -}}
{{- "\n" -}}
{{- "" | indent $.indentAmount -}}
{{- "enum: " -}}
{{- else -}} {{- else -}}
{{- " enum: " -}} {{- "ENUM: " -}}
{{- end -}} {{- end -}}
{{- range $index, $element := .enum -}} {{- range $index, $element := .enum -}}
{{- if and (gt $limit -1) (ge $index $limit) -}}
{{- /* Prints ,.. and return the range when it goes over the limit */ -}}
{{- ",.." -}}
{{- break -}}
{{- end -}}
{{- /* Use to reflect "" when we see empty string */ -}}
{{- $elementType := printf "%T" $element -}}
{{- if gt $index 0 -}} {{- ", " -}} {{- end -}} {{- if gt $index 0 -}} {{- ", " -}} {{- end -}}
{{- $element -}} {{- if and (eq "string" $elementType) (eq $element "") -}}
{{- end -}} {{- `""` -}}
{{- else -}}
{{- $element -}}
{{- end -}}
{{- end -}}
{{- end -}} {{- end -}}
{{- end -}} {{- end -}}
{{- end -}} {{- end -}}

View File

@ -650,13 +650,13 @@ func TestPlaintext(t *testing.T) {
}, },
{ {
// show that extractEnum can skip empty enum slice // show that extractEnum can skip empty enum slice
Name: "Enum", Name: "extractEmptyEnum",
Subtemplate: "extractEnum", Subtemplate: "extractEnum",
Context: map[string]any{ Context: map[string]any{
"schema": map[string]any{ "schema": map[string]any{
"type": "string", "type": "string",
"description": "a description that should not be printed", "description": "a description that should not be printed",
"enum": []int{}, "enum": []any{},
}, },
}, },
Checks: []check{ Checks: []check{
@ -665,34 +665,87 @@ func TestPlaintext(t *testing.T) {
}, },
{ {
// show that extractEnum can extract any enum slice and style it uppercase // show that extractEnum can extract any enum slice and style it uppercase
Name: "Enum", Name: "extractEnumSimpleForm",
Subtemplate: "extractEnum", Subtemplate: "extractEnum",
Context: map[string]any{ Context: map[string]any{
"schema": map[string]any{ "schema": map[string]any{
"type": "string", "type": "string",
"description": "a description that should not be printed", "description": "a description that should not be printed",
"enum": []int{1, 2, 3}, "enum": []any{0, 1, 2, 3},
}, },
"singleView": true, "longFormView": false,
}, },
Checks: []check{ Checks: []check{
checkEquals("ENUM: 1, 2, 3"), checkEquals("ENUM: 0, 1, 2, 3"),
}, },
}, },
{ {
// show that extractEnum can extract any enum slice and style it lowercase // show that extractEnum can extract any enum slice and style it lowercase
Name: "Enum", Name: "extractEnumLongFormWithIndent",
Subtemplate: "extractEnum", Subtemplate: "extractEnum",
Context: map[string]any{ Context: map[string]any{
"schema": map[string]any{ "schema": map[string]any{
"type": "string", "type": "string",
"description": "a description that should not be printed", "description": "a description that should not be printed",
"enum": []int{1, 2, 3}, "enum": []any{0, 1, 2, 3},
}, },
"singleView": false, "longFormView": true,
"indentAmount": 2,
}, },
Checks: []check{ Checks: []check{
checkEquals(" enum: 1, 2, 3"), checkEquals("\n enum: 0, 1, 2, 3"),
},
},
{
// show that extractEnum can extract any enum slice and style it with truncated enums
Name: "extractEnumLongFormWithLimitAndIndent",
Subtemplate: "extractEnum",
Context: map[string]any{
"schema": map[string]any{
"type": "string",
"description": "a description that should not be printed",
"enum": []any{0, 1, 2, 3},
},
"longFormView": true,
"limit": 2,
"indentAmount": 2,
},
Checks: []check{
checkEquals("\n enum: 0, 1,.."),
},
},
{
// show that extractEnum can extract any enum slice and style it with truncated enums
Name: "extractEnumSimpleFormWithLimitAndIndent",
Subtemplate: "extractEnum",
Context: map[string]any{
"schema": map[string]any{
"type": "string",
"description": "a description that should not be printed",
"enum": []any{0, 1, 2, 3},
},
"longFormView": false,
"limit": 2,
"indentAmount": 2,
},
Checks: []check{
checkEquals("ENUM: 0, 1,.."),
},
},
{
// show that extractEnum can extract any enum slice and style it with empty string
Name: "extractEnumSimpleFormEmptyString",
Subtemplate: "extractEnum",
Context: map[string]any{
"schema": map[string]any{
"type": "string",
"description": "a description that should not be printed",
"enum": []any{"Block", "File", ""},
},
"longFormView": false,
},
Checks: []check{
checkEquals("ENUM: Block, File, \"\""),
}, },
}, },
} }