change the extractEnum function to add limit, intent and add tests for new behaviours
Kubernetes-commit: 9d4997ea69eb1d33572f292effe1969376df79f4
This commit is contained in:
parent
16b821ae90
commit
c4d840c46e
|
@ -110,7 +110,7 @@ Takes dictionary as argument with keys:
|
|||
{{- 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) }}>{{"\n"}}
|
||||
{{- template "extractEnum" (dict "schema" $subschema "Document" $.Document "singleView" true) -}}{{"\n"}}
|
||||
{{- template "extractEnum" (dict "schema" $subschema "Document" $.Document "longFormView" false "limit" 3) -}}{{"\n"}}
|
||||
{{- "\n" -}}
|
||||
{{- end -}}
|
||||
|
||||
|
@ -205,7 +205,7 @@ Takes dictionary as argument with keys:
|
|||
{{- $fieldSchema := index $.schema.properties $.name -}}
|
||||
{{- $.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) -}}
|
||||
{{- template "extractEnum" (dict "schema" $fieldSchema "Document" $.Document "longFormView" true "limit" -1 "indentAmount" $indentAmount) -}}
|
||||
{{- "\n" -}}
|
||||
{{- if not $.short -}}
|
||||
{{- 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.:
|
||||
|
||||
ENUM: !=, =, =~, !~
|
||||
enum: !=, =, =~, !~
|
||||
ENUM: "", BlockDevice, CharDevice, Directory
|
||||
enum: "", BlockDevice, CharDevice, Directory
|
||||
|
||||
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
|
||||
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" -}}
|
||||
{{- with $.schema -}}
|
||||
{{- if .enum -}}
|
||||
{{- if $.singleView -}}
|
||||
{{- "ENUM: " -}}
|
||||
{{- $enumLen := len .enum -}}
|
||||
{{- $limit := or $.limit -1 -}}
|
||||
{{- if $.longFormView -}}
|
||||
{{- "\n" -}}
|
||||
{{- "" | indent $.indentAmount -}}
|
||||
{{- "enum: " -}}
|
||||
{{- else -}}
|
||||
{{- " enum: " -}}
|
||||
{{- "ENUM: " -}}
|
||||
{{- end -}}
|
||||
{{- 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 -}}
|
||||
{{- $element -}}
|
||||
{{- end -}}
|
||||
{{- if and (eq "string" $elementType) (eq $element "") -}}
|
||||
{{- `""` -}}
|
||||
{{- else -}}
|
||||
{{- $element -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
{{- end -}}
|
|
@ -650,13 +650,13 @@ func TestPlaintext(t *testing.T) {
|
|||
},
|
||||
{
|
||||
// show that extractEnum can skip empty enum slice
|
||||
Name: "Enum",
|
||||
Name: "extractEmptyEnum",
|
||||
Subtemplate: "extractEnum",
|
||||
Context: map[string]any{
|
||||
"schema": map[string]any{
|
||||
"type": "string",
|
||||
"description": "a description that should not be printed",
|
||||
"enum": []int{},
|
||||
"enum": []any{},
|
||||
},
|
||||
},
|
||||
Checks: []check{
|
||||
|
@ -665,34 +665,87 @@ func TestPlaintext(t *testing.T) {
|
|||
},
|
||||
{
|
||||
// show that extractEnum can extract any enum slice and style it uppercase
|
||||
Name: "Enum",
|
||||
Name: "extractEnumSimpleForm",
|
||||
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{0, 1, 2, 3},
|
||||
},
|
||||
"singleView": true,
|
||||
"longFormView": false,
|
||||
},
|
||||
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
|
||||
Name: "Enum",
|
||||
Name: "extractEnumLongFormWithIndent",
|
||||
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{0, 1, 2, 3},
|
||||
},
|
||||
"singleView": false,
|
||||
"longFormView": true,
|
||||
"indentAmount": 2,
|
||||
},
|
||||
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, \"\""),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue