Avoid manual step for creating array setters

This commit is contained in:
Phani Teja Marupaka
2020-06-15 17:18:38 -07:00
parent 3e9276271a
commit 0f0efe2a4c
9 changed files with 219 additions and 26 deletions

View File

@@ -30,6 +30,9 @@ type Add struct {
// Ref is the OpenAPI reference to set on the matching fields as a comment.
Ref string
// ListValues are the value of a list setter.
ListValues []string
}
// Filter implements yaml.Filter
@@ -48,6 +51,37 @@ func (a *Add) visitSequence(_ *yaml.RNode, _ string, _ *openapi.ResourceSchema)
return nil
}
// visitMapping implements visitor
// visitMapping visits the fields in input MappingNode and adds setter/subst ref
// if the path path spec matches with input FiledName
func (a *Add) visitMapping(object *yaml.RNode, p string, _ *openapi.ResourceSchema) error {
return object.VisitFields(func(node *yaml.MapNode) error {
if node.Value.YNode().Kind != yaml.SequenceNode {
return nil
}
key, err := node.Key.String()
if err != nil {
return err
}
// derive the list values for the sequence node to write it to openAPI definitions
var values []string
for _, sc := range node.Value.Content() {
values = append(values, sc.Value)
}
a.ListValues = values
// pathToKey refers to the path address of the key node ex: metadata.annotations
// p is the path till parent node, pathToKey is obtained by appending child key
pathToKey := p + "." + strings.Trim(key, "\n")
if a.FieldName != "" && strings.HasSuffix(pathToKey, a.FieldName) {
return a.addRef(node.Key)
}
return nil
})
}
// visitScalar implements visitor
// 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 {
@@ -58,7 +92,11 @@ func (a *Add) visitScalar(object *yaml.RNode, p string, _ *openapi.ResourceSchem
if a.FieldValue != "" && a.FieldValue != object.YNode().Value {
return nil
}
return a.addRef(object)
}
// addRef adds the setter/subst ref to the object node as a line comment
func (a *Add) addRef(object *yaml.RNode) error {
// read the field metadata
fm := fieldmeta.FieldMeta{}
if err := fm.Read(object); err != nil {
@@ -85,7 +123,7 @@ type SetterDefinition struct {
Name string `yaml:"name"`
// Value is the value of the setter.
Value string `yaml:"value"`
Value string `yaml:"value,omitempty"`
// ListValues are the value of a list setter.
ListValues []string `yaml:"listValues,omitempty"`