From 2216361c59e9e4863fb83b903a6fc14893c16e67 Mon Sep 17 00:00:00 2001 From: ah8ad3 Date: Mon, 29 Jan 2024 19:27:27 +0330 Subject: [PATCH] fixes kubectl explain to shows enum for field types if they were defined --- .../pkg/explain/v2/templates/plaintext.tmpl | 27 ++++++++++- .../explain/v2/templates/plaintext_test.go | 46 +++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/staging/src/k8s.io/kubectl/pkg/explain/v2/templates/plaintext.tmpl b/staging/src/k8s.io/kubectl/pkg/explain/v2/templates/plaintext.tmpl index 6cf6c3779ff..3b3d40ba05c 100644 --- a/staging/src/k8s.io/kubectl/pkg/explain/v2/templates/plaintext.tmpl +++ b/staging/src/k8s.io/kubectl/pkg/explain/v2/templates/plaintext.tmpl @@ -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 -}} \ No newline at end of file diff --git a/staging/src/k8s.io/kubectl/pkg/explain/v2/templates/plaintext_test.go b/staging/src/k8s.io/kubectl/pkg/explain/v2/templates/plaintext_test.go index 0057164e5f1..83758919142 100644 --- a/staging/src/k8s.io/kubectl/pkg/explain/v2/templates/plaintext_test.go +++ b/staging/src/k8s.io/kubectl/pkg/explain/v2/templates/plaintext_test.go @@ -648,6 +648,51 @@ func TestPlaintext(t *testing.T) { checkEquals(" thefield\t -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 {