mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-30 06:54:01 +00:00
fixes kubectl explain to shows enum for field types if they were defined
This commit is contained in:
parent
bb3030bf0e
commit
2216361c59
@ -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 -}}
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user