mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 03:11:40 +00:00
refactor generator
refactor funcs out of generator
This commit is contained in:
parent
1d2e804287
commit
f37f63ab9f
@ -43,7 +43,7 @@ func PrintModelDescription(
|
||||
|
||||
// Factored out for testability
|
||||
func printModelDescriptionWithGenerator(
|
||||
generator *generator,
|
||||
generator Generator,
|
||||
fieldsPath []string,
|
||||
w io.Writer,
|
||||
client openapi.Client,
|
||||
|
40
staging/src/k8s.io/kubectl/pkg/explain/v2/funcs.go
Normal file
40
staging/src/k8s.io/kubectl/pkg/explain/v2/funcs.go
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
Copyright 2022 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package v2
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/go-openapi/jsonreference"
|
||||
"k8s.io/kubectl/pkg/util/term"
|
||||
)
|
||||
|
||||
func WithBuiltinTemplateFuncs(tmpl *template.Template) *template.Template {
|
||||
return tmpl.Funcs(map[string]interface{}{
|
||||
"toJson": func(obj any) (string, error) {
|
||||
res, err := json.Marshal(obj)
|
||||
return string(res), err
|
||||
},
|
||||
})
|
||||
}
|
||||
|
@ -17,7 +17,6 @@ limitations under the License.
|
||||
package v2
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"text/template"
|
||||
@ -25,6 +24,26 @@ import (
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
)
|
||||
|
||||
type Generator interface {
|
||||
AddTemplate(name string, contents string) error
|
||||
|
||||
Render(
|
||||
// Template to use for rendering
|
||||
templateName string,
|
||||
// Self-Contained OpenAPI Document Containing all schemas used by $ref
|
||||
// Only OpenAPI V3 documents are supported
|
||||
document map[string]interface{},
|
||||
// Resource within OpenAPI document for which to render explain schema
|
||||
gvr schema.GroupVersionResource,
|
||||
// Field path of child of resource to focus output onto
|
||||
fieldSelector []string,
|
||||
// Boolean indicating whether the fields should be rendered recursively/deeply
|
||||
recursive bool,
|
||||
// Output writer
|
||||
writer io.Writer,
|
||||
) error
|
||||
}
|
||||
|
||||
type TemplateContext struct {
|
||||
GVR schema.GroupVersionResource
|
||||
Document map[string]interface{}
|
||||
@ -36,22 +55,14 @@ type generator struct {
|
||||
templates map[string]*template.Template
|
||||
}
|
||||
|
||||
func NewGenerator() *generator {
|
||||
func NewGenerator() Generator {
|
||||
return &generator{
|
||||
templates: make(map[string]*template.Template),
|
||||
}
|
||||
}
|
||||
|
||||
func (g *generator) AddTemplate(name string, contents string) error {
|
||||
compiled, err := template.
|
||||
New(name).
|
||||
Funcs(map[string]interface{}{
|
||||
"toJson": func(obj any) (string, error) {
|
||||
res, err := json.Marshal(obj)
|
||||
return string(res), err
|
||||
},
|
||||
}).
|
||||
Parse(contents)
|
||||
compiled, err := WithBuiltinTemplateFuncs(template.New(name)).Parse(contents)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
|
Loading…
Reference in New Issue
Block a user