Skip to content

Commit

Permalink
refactoring styles
Browse files Browse the repository at this point in the history
Signed-off-by: Varsha Munishwar <[email protected]>
  • Loading branch information
gcheadle-vmware authored and vmunishwar committed Apr 29, 2022
1 parent c3389b5 commit 6b1a0a7
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 83 deletions.
46 changes: 22 additions & 24 deletions pkg/schema/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,22 @@ import (

// Declare @schema/... annotation names
const (
AnnotationNullable template.AnnotationName = "schema/nullable"
AnnotationType template.AnnotationName = "schema/type"
AnnotationDefault template.AnnotationName = "schema/default"
AnnotationDescription template.AnnotationName = "schema/desc"
AnnotationTitle template.AnnotationName = "schema/title"
AnnotationExamples template.AnnotationName = "schema/examples"
AnnotationDeprecated template.AnnotationName = "schema/deprecated"
TypeAnnotationKwargAny string = "any"
AnnotationValidation template.AnnotationName = "schema/validation"
AnnotationAssertValidate template.AnnotationName = "assert/validate"
AnnotationNullable template.AnnotationName = "schema/nullable"
AnnotationType template.AnnotationName = "schema/type"
AnnotationDefault template.AnnotationName = "schema/default"
AnnotationDescription template.AnnotationName = "schema/desc"
AnnotationTitle template.AnnotationName = "schema/title"
AnnotationExamples template.AnnotationName = "schema/examples"
AnnotationDeprecated template.AnnotationName = "schema/deprecated"
TypeAnnotationKwargAny string = "any"
AnnotationValidation template.AnnotationName = "schema/validation"
)

type Annotation interface {
NewTypeFromAnn() (Type, error)
GetPosition() *filepos.Position
}

// ValidationAnnotation is a wrapper for a value provided via @schema/validation annotation
type ValidationAnnotation struct {
rules []assertions.Rule
pos *filepos.Position
}

// GetRules gets the node annotation from @schema/validation annotation
// Maybe we change name rules to validations
func (v *ValidationAnnotation) GetRules() []assertions.Rule {
return v.rules
}

type TypeAnnotation struct {
any bool
node yamlmeta.Node
Expand Down Expand Up @@ -87,6 +74,12 @@ type ExampleAnnotation struct {
pos *filepos.Position
}

// ValidationAnnotation is a wrapper for validations provided via @schema/validation annotation
type ValidationAnnotation struct {
rules []assertions.Rule
pos *filepos.Position
}

// Example contains a yaml example and its description
type Example struct {
description string
Expand Down Expand Up @@ -496,7 +489,7 @@ func (t *TitleAnnotation) NewTypeFromAnn() (Type, error) {
}

// NewTypeFromAnn returns type information given by annotation.
func (ValidationAnnotation) NewTypeFromAnn() (Type, error) {
func (v *ValidationAnnotation) NewTypeFromAnn() (Type, error) {
return nil, nil
}

Expand Down Expand Up @@ -536,10 +529,15 @@ func (t *TitleAnnotation) GetPosition() *filepos.Position {
}

// GetPosition returns position of the source comment used to create this annotation.
func (ValidationAnnotation) GetPosition() *filepos.Position {
func (v *ValidationAnnotation) GetPosition() *filepos.Position {
return nil
}

// GetRules gets the validation rules from @schema/validation annotation
func (v *ValidationAnnotation) GetRules() []assertions.Rule {
return v.rules
}

func (t *TypeAnnotation) IsAny() bool {
return t.any
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/schema/assign.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package schema

import (
"fmt"
"github.com/vmware-tanzu/carvel-ytt/pkg/assertions"

"github.com/vmware-tanzu/carvel-ytt/pkg/assertions"
"github.com/vmware-tanzu/carvel-ytt/pkg/yamlmeta"
)

Expand Down Expand Up @@ -161,10 +161,10 @@ func (n NullType) AssignTypeTo(node yamlmeta.Node) TypeCheck {
return chk
}

//AssignSchemaValidations implements the visitor interface to assign @assert:validate annotation for @schema/validation annotation
// AssignSchemaValidations implements the visitor interface to set validations from schema validation rules
type AssignSchemaValidations struct{}

// Visit if node's assigned type contains @schema/validation, the assert/validate annotation is added to the node
// Visit Extracts the validations from Node's Type and sets them in Node's meta
// This visitor returns nil if node has no assigned type or when the execution is completed
func (AssignSchemaValidations) Visit(node yamlmeta.Node) error {
if schemaType := GetType(node); schemaType != nil {
Expand Down
5 changes: 4 additions & 1 deletion pkg/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func NewDocumentType(doc *yamlmeta.Document) (*DocumentType, error) {
}

typeOfValue.SetDefaultValue(defaultValue)

return &DocumentType{Source: doc, Position: doc.Position, ValueType: typeOfValue, defaultValue: defaultValue, validations: validations}, nil
}

Expand Down Expand Up @@ -62,7 +63,7 @@ func NewMapItemType(item *yamlmeta.MapItem) (*MapItemType, error) {
return nil, err
}

typeOfValue.SetDefaultValue(defaultValue) //<- sets default value of array(child) if @schema/default is on this map item
typeOfValue.SetDefaultValue(defaultValue)

return &MapItemType{Key: item.Key, ValueType: typeOfValue, defaultValue: defaultValue, Position: item.Position, validations: validations}, nil
}
Expand Down Expand Up @@ -166,10 +167,12 @@ func getValidations(node yamlmeta.Node) ([]assertions.Rule, error) {
if err != nil {
return nil, err
}

validationAnn, ok := ann.(*ValidationAnnotation)
if !ok {
return nil, nil
}

return validationAnn.GetRules(), nil
}

Expand Down
104 changes: 51 additions & 53 deletions pkg/schema/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,12 @@ type DocumentType struct {
validations []assertions.Rule
}

// GetValidations provides validations from @schema/validation for a node
func (t *DocumentType) GetValidations() []assertions.Rule {
if t.validations != nil {
return t.validations
}
return nil
}

type MapType struct {
Items []*MapItemType
Position *filepos.Position
documentation documentation
}

// GetValidations provides validations from @schema/validation for a node
func (m *MapType) GetValidations() []assertions.Rule {
return nil
}

type MapItemType struct {
Key interface{} // usually a string
ValueType Type
Expand All @@ -77,75 +64,39 @@ type MapItemType struct {
validations []assertions.Rule
}

// GetValidations provides validations from @schema/validation for a node
func (t *MapItemType) GetValidations() []assertions.Rule {
if t.validations != nil {
return t.validations
}
return nil
}

type ArrayType struct {
ItemsType Type
Position *filepos.Position
defaultValue interface{}
documentation documentation
}

// GetValidations provides validations from @schema/validation for a node
func (a *ArrayType) GetValidations() []assertions.Rule {
return nil
}

type ArrayItemType struct {
ValueType Type
Position *filepos.Position
defaultValue interface{}
validations []assertions.Rule
}

// GetValidations provides validations from @schema/validation for a node
func (a *ArrayItemType) GetValidations() []assertions.Rule {
if a.validations != nil {
return a.validations
}
return nil
}

type ScalarType struct {
ValueType interface{}
Position *filepos.Position
defaultValue interface{}
documentation documentation
}

// GetValidations provides validations from @schema/validation for a node
func (s *ScalarType) GetValidations() []assertions.Rule {
return nil
}

type AnyType struct {
defaultValue interface{}
Position *filepos.Position
documentation documentation
}

// GetValidations provides validations from @schema/validation for a node
func (a AnyType) GetValidations() []assertions.Rule {
return nil
}

type NullType struct {
ValueType Type
Position *filepos.Position
documentation documentation
}

// GetValidations provides validations from @schema/validation for a node
func (n NullType) GetValidations() []assertions.Rule {
return nil
}

// The total set of supported scalars.
const (
FloatType = float64(0)
Expand Down Expand Up @@ -196,8 +147,7 @@ func (n NullType) GetValueType() Type {

// GetDefaultValue provides the default value
func (t DocumentType) GetDefaultValue() interface{} {
returnItem := &yamlmeta.Document{Value: t.defaultValue, Position: t.Position}
return returnItem
return &yamlmeta.Document{Value: t.defaultValue, Position: t.Position}
}

// GetDefaultValue provides the default value
Expand All @@ -212,8 +162,7 @@ func (m MapType) GetDefaultValue() interface{} {

// GetDefaultValue provides the default value
func (t MapItemType) GetDefaultValue() interface{} {
returnItem := &yamlmeta.MapItem{Key: t.Key, Value: t.defaultValue, Position: t.Position}
return returnItem
return &yamlmeta.MapItem{Key: t.Key, Value: t.defaultValue, Position: t.Position}
}

// GetDefaultValue provides the default value
Expand Down Expand Up @@ -627,6 +576,55 @@ func (n *NullType) SetDeprecated(deprecated bool, notice string) {
n.documentation.deprecated = deprecated
}

// GetValidations provides validations from @schema/validation for a node
func (t *DocumentType) GetValidations() []assertions.Rule {
if t.validations != nil {
return t.validations
}
return nil
}

// GetValidations provides validations from @schema/validation for a node
func (m *MapType) GetValidations() []assertions.Rule {
return nil
}

// GetValidations provides validations from @schema/validation for a node
func (t *MapItemType) GetValidations() []assertions.Rule {
if t.validations != nil {
return t.validations
}
return nil
}

// GetValidations provides validations from @schema/validation for a node
func (a *ArrayType) GetValidations() []assertions.Rule {
return nil
}

// GetValidations provides validations from @schema/validation for a node
func (a *ArrayItemType) GetValidations() []assertions.Rule {
if a.validations != nil {
return a.validations
}
return nil
}

// GetValidations provides validations from @schema/validation for a node
func (s *ScalarType) GetValidations() []assertions.Rule {
return nil
}

// GetValidations provides validations from @schema/validation for a node
func (a AnyType) GetValidations() []assertions.Rule {
return nil
}

// GetValidations provides validations from @schema/validation for a node
func (n NullType) GetValidations() []assertions.Rule {
return nil
}

// String produces a user-friendly name of the expected type.
func (t *DocumentType) String() string {
return yamlmeta.TypeName(&yamlmeta.Document{})
Expand Down
4 changes: 2 additions & 2 deletions pkg/workspace/data_values_pre_processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ func (pp DataValuesPreProcessing) apply(files []*FileInLibrary) (*datavalues.Env
if len(typeCheck.Violations) > 0 {
return nil, nil, schema.NewSchemaError("One or more data values were invalid", typeCheck.Violations...)
}
//Walk updates node's validations meta from Node's assigned type
yamlmeta.Walk(dvsDoc, schema.AssignSchemaValidations{})
}

if dvsDoc == nil {
Expand Down Expand Up @@ -111,8 +113,6 @@ func (pp DataValuesPreProcessing) typeAndCheck(dataValuesDoc *yamlmeta.Document)
if len(chk.Violations) > 0 {
return chk
}
//Walk function is used to update node's meta from @schema/validation annotation
_ = yamlmeta.Walk(dataValuesDoc, schema.AssignSchemaValidations{})
chk = schema.CheckNode(dataValuesDoc)
return chk
}
Expand Down

0 comments on commit 6b1a0a7

Please sign in to comment.