Skip to content

Commit

Permalink
refactor @schema/title into struct with other documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
gcheadle-vmware committed Jan 14, 2022
1 parent 5b72996 commit a321bea
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 171 deletions.
16 changes: 8 additions & 8 deletions pkg/cmd/template/schema_inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,40 +656,40 @@ paths: {}
components:
schemas:
dataValues:
title: Network configuration values
type: object
additionalProperties: false
title: Network configuration values
properties:
db_conn:
title: List of database connections
type: array
title: List of database connections
items:
title: A network entry
type: object
additionalProperties: false
title: A network entry
properties:
hostname:
title: The host
type: string
default: ""
title: The host
port:
title: The Port
type: integer
default: 0
title: The Port
timeout:
title: The Timeout
type: number
default: 1
format: float
title: The Timeout
any_key:
title: Any type
nullable: true
default: thing
title: Any type
null_key:
type: string
default: null
title: When not provided, the default is null
nullable: true
title: When not provided, the default is null
default: []
`
filesToProcess := files.NewSortedFiles([]*files.File{
Expand Down
5 changes: 4 additions & 1 deletion pkg/schema/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ func getTypeFromAnnotations(anns []Annotation, pos *filepos.Position) (Type, err
}

type documentation struct {
title string
description string
exampleDescription string
exampleYAML interface{}
Expand All @@ -589,8 +590,10 @@ func setDocumentationFromAnns(docAnns []Annotation, typeOfValue Type) error {
var documentationInfo documentation
for _, a := range docAnns {
switch ann := a.(type) {
case *TitleAnnotation:
documentationInfo.title = ann.title
case *DescriptionAnnotation:
documentationInfo.description = documentationInfo.description + ann.description
documentationInfo.description = ann.description
case *ExampleAnnotation:
err := checkExamplesValue(ann, typeOfValue)
if err != nil {
Expand Down
57 changes: 12 additions & 45 deletions pkg/schema/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (
descriptionProp = "description"
exampleProp = "example"
exampleDescriptionProp = "x-example-description"
titleProp = "title"
titleProp = "title"
)

// OpenAPIDocument holds the document type used for creating an OpenAPI document
Expand Down Expand Up @@ -60,37 +60,17 @@ func (o *OpenAPIDocument) calculateProperties(schemaVal interface{}) *yamlmeta.M
mi := yamlmeta.MapItem{Key: i.Key, Value: o.calculateProperties(i.GetValueType())}
properties = append(properties, &mi)
}
var property yamlmeta.Map
if typedValue.GetTitle() != "" {
property.Items = append(property.Items, &yamlmeta.MapItem{Key: titleProp, Value: typedValue.GetTitle()})
}
property.Items = append(property.Items, &yamlmeta.MapItem{Key: typeProp, Value: "object"})
property.Items = append(property.Items, &yamlmeta.MapItem{Key: "additionalProperties", Value: false})
if typedValue.GetDescription() != "" {
property.Items = append(property.Items, &yamlmeta.MapItem{Key: descriptionProp, Value: typedValue.GetDescription()})
}
property = yamlmeta.Map{Items: []*yamlmeta.MapItem{
property := yamlmeta.Map{Items: []*yamlmeta.MapItem{
{Key: typeProp, Value: "object"},
{Key: "additionalProperties", Value: false},
}}

property.Items = append(property.Items, collectDocumentation(typedValue)...)
property.Items = append(property.Items, &yamlmeta.MapItem{Key: "properties", Value: &yamlmeta.Map{Items: properties}})
return &property
case *ArrayType:
valueType := typedValue.GetValueType().(*ArrayItemType)
properties := o.calculateProperties(valueType.GetValueType())
var property yamlmeta.Map
if typedValue.GetTitle() != "" {
property.Items = append(property.Items, &yamlmeta.MapItem{Key: titleProp, Value: typedValue.GetTitle()})
}
property.Items = append(property.Items, &yamlmeta.MapItem{Key: typeProp, Value: "array"})
if typedValue.GetDescription() != "" {
property.Items = append(property.Items, &yamlmeta.MapItem{Key: descriptionProp, Value: typedValue.GetDescription()})
}
property.Items = append(property.Items, &yamlmeta.MapItem{Key: "items", Value: properties})
property.Items = append(property.Items, &yamlmeta.MapItem{Key: defaultProp, Value: typedValue.GetDefaultValue()})
property = yamlmeta.Map{Items: []*yamlmeta.MapItem{
property := yamlmeta.Map{Items: []*yamlmeta.MapItem{
{Key: typeProp, Value: "array"},
}}

Expand All @@ -100,17 +80,14 @@ func (o *OpenAPIDocument) calculateProperties(schemaVal interface{}) *yamlmeta.M
{Key: "items", Value: properties},
{Key: defaultProp, Value: typedValue.GetDefaultValue()},
}

property.Items = append(property.Items, items...)
return &property
case *ScalarType:
typeString := o.openAPITypeFor(typedValue)
defaultVal := typedValue.GetDefaultValue()
var property yamlmeta.Map
if typedValue.GetTitle() != "" {
property.Items = append(property.Items, &yamlmeta.MapItem{Key: titleProp, Value: typedValue.GetTitle()})
}
property.Items = append(property.Items, &yamlmeta.MapItem{Key: typeProp, Value: typeString})
property := yamlmeta.Map{Items: []*yamlmeta.MapItem{
{Key: typeProp, Value: typeString},
}}
property.Items = append(property.Items, &yamlmeta.MapItem{Key: defaultProp, Value: defaultVal})
if typedValue.String() == "float" {
property.Items = append(property.Items, &yamlmeta.MapItem{Key: "format", Value: "float"})
Expand All @@ -119,23 +96,11 @@ func (o *OpenAPIDocument) calculateProperties(schemaVal interface{}) *yamlmeta.M
return &property
case *NullType:
properties := o.calculateProperties(typedValue.GetValueType())
if typedValue.GetTitle() != "" {
properties.Items = append(properties.Items, &yamlmeta.MapItem{Key: titleProp, Value: typedValue.GetTitle()})
}
properties.Items = append(properties.Items, &yamlmeta.MapItem{Key: nullableProp, Value: true})
properties.Items = append(properties.Items, collectDocumentation(typedValue)...)
return properties
case *AnyType:
properties := &yamlmeta.Map{Items: []*yamlmeta.MapItem{}}
if typedValue.GetTitle() != "" {
properties.Items = append(properties.Items, &yamlmeta.MapItem{Key: titleProp, Value: typedValue.GetTitle()})
}
properties.Items = append(properties.Items, &yamlmeta.MapItem{Key: nullableProp, Value: true})
properties.Items = append(properties.Items, &yamlmeta.MapItem{Key: defaultProp, Value: typedValue.GetDefaultValue()})
if typedValue.GetDescription() != "" {
properties.Items = append(properties.Items, &yamlmeta.MapItem{Key: descriptionProp, Value: typedValue.GetDescription()})
}
properties = &yamlmeta.Map{Items: []*yamlmeta.MapItem{
properties := &yamlmeta.Map{Items: []*yamlmeta.MapItem{
{Key: nullableProp, Value: true},
{Key: defaultProp, Value: typedValue.GetDefaultValue()},
}}
Expand All @@ -148,9 +113,11 @@ func (o *OpenAPIDocument) calculateProperties(schemaVal interface{}) *yamlmeta.M

func collectDocumentation(t Type) []*yamlmeta.MapItem {
var annDocumentation []*yamlmeta.MapItem
description := t.GetDescription()
if description != "" {
annDocumentation = append(annDocumentation, &yamlmeta.MapItem{Key: descriptionProp, Value: description})
if t.GetTitle() != "" {
annDocumentation = append(annDocumentation, &yamlmeta.MapItem{Key: titleProp, Value: t.GetTitle()})
}
if t.GetDescription() != "" {
annDocumentation = append(annDocumentation, &yamlmeta.MapItem{Key: descriptionProp, Value: t.GetDescription()})
}
exampleDescription, exampleYAML := t.GetExample()
if exampleYAML != nil {
Expand Down
6 changes: 0 additions & 6 deletions pkg/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,6 @@ func getType(node yamlmeta.Node) (Type, error) {
if err != nil {
return nil, NewSchemaError("Invalid schema", err)
}
for _, ann := range docAnns {
switch annType := ann.(type) {
case *TitleAnnotation:
typeOfValue.SetTitle(annType.title)
}
}
err = setDocumentationFromAnns(docAnns, typeOfValue)
if err != nil {
return nil, err
Expand Down

0 comments on commit a321bea

Please sign in to comment.