Use k8s schema to determine formatting if no type on setter

This commit is contained in:
Morten Torkildsen
2020-07-31 13:44:47 -07:00
parent feeaa994b7
commit 5c433ead5e
5 changed files with 26 additions and 15 deletions

View File

@@ -97,7 +97,7 @@ func (a *Add) visitMapping(object *yaml.RNode, p string, _ *openapi.ResourceSche
// visitScalar implements visitor // visitScalar implements visitor
// visitScalar will set the field metadata on each scalar field whose name + value match // visitScalar will set the field metadata on each scalar field whose name + value match
func (a *Add) visitScalar(object *yaml.RNode, p string, _ *openapi.ResourceSchema) error { func (a *Add) visitScalar(object *yaml.RNode, p string, _, _ *openapi.ResourceSchema) error {
// check if the field matches // check if the field matches
if a.Type == "array" { if a.Type == "array" {
return nil return nil

View File

@@ -45,7 +45,7 @@ func (d *Delete) visitMapping(_ *yaml.RNode, _ string, _ *openapi.ResourceSchema
// visitScalar implements visitor // visitScalar implements visitor
// visitScalar will remove the reference on each scalar field whose name matches. // visitScalar will remove the reference on each scalar field whose name matches.
func (d *Delete) visitScalar(object *yaml.RNode, p string, _ *openapi.ResourceSchema) error { func (d *Delete) visitScalar(object *yaml.RNode, p string, _, _ *openapi.ResourceSchema) error {
// check if the field matches // check if the field matches
if d.FieldName != "" && !strings.HasSuffix(p, d.FieldName) { if d.FieldName != "" && !strings.HasSuffix(p, d.FieldName) {
return nil return nil

View File

@@ -81,9 +81,9 @@ func (s *Set) visitSequence(object *yaml.RNode, p string, schema *openapi.Resour
} }
// visitScalar // visitScalar
func (s *Set) visitScalar(object *yaml.RNode, p string, schema *openapi.ResourceSchema) error { func (s *Set) visitScalar(object *yaml.RNode, p string, oa, setterSchema *openapi.ResourceSchema) error {
// get the openAPI for this field describing how to apply the setter // get the openAPI for this field describing how to apply the setter
ext, err := getExtFromComment(schema) ext, err := getExtFromComment(setterSchema)
if err != nil { if err != nil {
return err return err
} }
@@ -91,8 +91,13 @@ func (s *Set) visitScalar(object *yaml.RNode, p string, schema *openapi.Resource
return nil return nil
} }
var k8sSchema *spec.Schema
if oa != nil {
k8sSchema = oa.Schema
}
// perform a direct set of the field if it matches // perform a direct set of the field if it matches
ok, err := s.set(object, ext, schema.Schema) ok, err := s.set(object, ext, k8sSchema, setterSchema.Schema)
if err != nil { if err != nil {
return err return err
} }
@@ -214,7 +219,7 @@ func (s *Set) substituteUtil(ext *CliExtension, visited sets.String, nameMatch *
} }
// set applies the value from ext to field if its name matches s.Name // set applies the value from ext to field if its name matches s.Name
func (s *Set) set(field *yaml.RNode, ext *CliExtension, sch *spec.Schema) (bool, error) { func (s *Set) set(field *yaml.RNode, ext *CliExtension, k8sSch, sch *spec.Schema) (bool, error) {
// check full setter // check full setter
if ext.Setter == nil || !s.isMatch(ext.Setter.Name) { if ext.Setter == nil || !s.isMatch(ext.Setter.Name) {
return false, nil return false, nil
@@ -234,8 +239,14 @@ func (s *Set) set(field *yaml.RNode, ext *CliExtension, sch *spec.Schema) (bool,
// this has a full setter, set its value // this has a full setter, set its value
field.YNode().Value = ext.Setter.Value field.YNode().Value = ext.Setter.Value
// format the node so it is quoted if it is a string // format the node so it is quoted if it is a string. If there is
yaml.FormatNonStringStyle(field.YNode(), *sch) // type information on the setter schema, we use it. Otherwise we
// fall back to the field schema if it exists.
if len(sch.Type) > 0 {
yaml.FormatNonStringStyle(field.YNode(), *sch)
} else if k8sSch != nil {
yaml.FormatNonStringStyle(field.YNode(), *k8sSch)
}
return true, nil return true, nil
} }

View File

@@ -126,7 +126,7 @@ metadata:
}, },
{ {
name: "set-foo-no-type", name: "set-foo-no-type",
description: "if a type is not specified for a setter, keep the existing quoting", description: "if a type is not specified for a setter or k8s schema, keep existing quoting",
setter: "foo", setter: "foo",
openapi: ` openapi: `
openAPI: openAPI:
@@ -138,16 +138,16 @@ openAPI:
value: "4" value: "4"
`, `,
input: ` input: `
apiVersion: apps/v1 apiVersion: custom/v1
kind: Deployment kind: Example
metadata: metadata:
name: nginx-deployment name: nginx-deployment
annotations: annotations:
foo: 3 # {"$ref": "#/definitions/io.k8s.cli.setters.foo"} foo: 3 # {"$ref": "#/definitions/io.k8s.cli.setters.foo"}
`, `,
expected: ` expected: `
apiVersion: apps/v1 apiVersion: custom/v1
kind: Deployment kind: Example
metadata: metadata:
name: nginx-deployment name: nginx-deployment
annotations: annotations:

View File

@@ -16,7 +16,7 @@ type visitor interface {
// node is the scalar field value // node is the scalar field value
// path is the path to the field; path elements are separated by '.' // path is the path to the field; path elements are separated by '.'
// oa is the OpenAPI schema for the field // oa is the OpenAPI schema for the field
visitScalar(node *yaml.RNode, path string, oa *openapi.ResourceSchema) error visitScalar(node *yaml.RNode, path string, oa *openapi.ResourceSchema, fieldOA *openapi.ResourceSchema) error
// visitSequence is called for each sequence field value on a resource // visitSequence is called for each sequence field value on a resource
// node is the sequence field value // node is the sequence field value
@@ -69,7 +69,7 @@ func acceptImpl(v visitor, object *yaml.RNode, p string, oa *openapi.ResourceSch
case yaml.ScalarNode: case yaml.ScalarNode:
// Visit the scalar field // Visit the scalar field
fieldSchema := getSchema(object, oa, "") fieldSchema := getSchema(object, oa, "")
return v.visitScalar(object, p, fieldSchema) return v.visitScalar(object, p, oa, fieldSchema)
} }
return nil return nil
} }