fixes kubectl explain to shows enum for field types if they were defined

This commit is contained in:
ah8ad3 2024-01-29 19:27:27 +03:30
parent bb3030bf0e
commit 2216361c59
2 changed files with 71 additions and 2 deletions

View File

@ -109,7 +109,7 @@ 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) }}>{{"\n"}}
FIELD: {{first $.FieldPath}} <{{ template "typeGuess" (dict "schema" $subschema "Document" $.Document) }}>{{ template "extractEnum" (dict "schema" $subschema "Document" $.Document) }}{{"\n"}}
{{- "\n" -}}
{{- end -}}
{{- template "output" (set $nextContext "history" (dict) "FieldPath" (slice $.FieldPath 1) "schema" $subschema ) -}}
@ -201,7 +201,7 @@ 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) }}>
{{- $.name | indent $indentAmount -}}{{"\t"}}<{{ template "typeGuess" (dict "schema" $fieldSchema "Document" $.Document) }}>{{ template "extractEnum" (dict "schema" $fieldSchema "Document" $.Document) }}
{{- if contains $.schema.required $.name}} -required-{{- end -}}
{{- "\n" -}}
{{- if not $.short -}}
@ -280,4 +280,27 @@ Takes dictionary as argument with keys:
{{- else -}}
{{- fail "expected schema argument to subtemplate 'typeguess'" -}}
{{- end -}}
{{- end -}}
{{- /* Check if there is any enum returns it in this format e.g.:
(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
*/ -}}
{{- define "extractEnum" -}}
{{- with $.schema -}}
{{- if .enum -}}
{{- " (enum: " -}}
{{- range $index, $element := .enum -}}
{{- if gt $index 0 -}} {{- ", " -}} {{- end -}}
{{- $element -}}
{{- end -}}
{{- ")" -}}
{{- end -}}
{{- end -}}
{{- end -}}

View File

@ -648,6 +648,51 @@ func TestPlaintext(t *testing.T) {
checkEquals(" thefield\t<string> -required-\n"),
},
},
{
// show that extractEnum can skip empty enum slice
Name: "Enum",
Subtemplate: "extractEnum",
Context: map[string]any{
"schema": map[string]any{
"type": "string",
"description": "a description that should not be printed",
"enum": []string{},
},
},
Checks: []check{
checkEquals(""),
},
},
{
// show that extractEnum can extract string enum and style it
Name: "Enum",
Subtemplate: "extractEnum",
Context: map[string]any{
"schema": map[string]any{
"type": "string",
"description": "a description that should not be printed",
"enum": []string{"!=", "!", "=="},
},
},
Checks: []check{
checkEquals(" (enum: !=, !, ==)"),
},
},
{
// show that extractEnum can extract integer enum and style it
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},
},
},
Checks: []check{
checkEquals(" (enum: 1, 2, 3)"),
},
},
}
tmpl, err := v2.WithBuiltinTemplateFuncs(template.New("")).Parse(plaintextSource)
@ -668,6 +713,7 @@ func TestPlaintext(t *testing.T) {
} else {
outputErr = tmpl.ExecuteTemplate(buf, tcase.Subtemplate, tcase.Context)
}
fmt.Println(outputErr)
output := buf.String()
for _, check := range tcase.Checks {