Skip to content

Commit

Permalink
scope openapi methods to OpenAPIDocument
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitriy Kalinin <[email protected]>
  • Loading branch information
Dmitriy Kalinin committed Apr 14, 2024
1 parent 43dee5f commit 9663687
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions pkg/schema/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ func (o *OpenAPIDocument) calculateProperties(schemaVal interface{}) *yamlmeta.M
switch typedValue := schemaVal.(type) {
case *DocumentType:
result := o.calculateProperties(typedValue.GetValueType())
result.Items = append(result.Items, convertValidations(typedValue)...)
result.Items = append(result.Items, o.convertValidations(typedValue)...)
sort.Sort(openAPIKeys(result.Items))
return result

case *MapType:
var items openAPIKeys
items = append(items, collectDocumentation(typedValue)...)
items = append(items, convertValidations(typedValue)...)
items = append(items, o.collectDocumentation(typedValue)...)
items = append(items, o.convertValidations(typedValue)...)
items = append(items, &yamlmeta.MapItem{Key: typeProp, Value: "object"})
items = append(items, &yamlmeta.MapItem{Key: additionalPropsProp, Value: false})

Expand All @@ -124,14 +124,14 @@ func (o *OpenAPIDocument) calculateProperties(schemaVal interface{}) *yamlmeta.M

case *MapItemType:
result := o.calculateProperties(typedValue.GetValueType())
result.Items = append(result.Items, convertValidations(typedValue)...)
result.Items = append(result.Items, o.convertValidations(typedValue)...)
sort.Sort(openAPIKeys(result.Items))
return result

case *ArrayType:
var items openAPIKeys
items = append(items, collectDocumentation(typedValue)...)
items = append(items, convertValidations(typedValue)...)
items = append(items, o.collectDocumentation(typedValue)...)
items = append(items, o.convertValidations(typedValue)...)
items = append(items, &yamlmeta.MapItem{Key: typeProp, Value: "array"})
items = append(items, &yamlmeta.MapItem{Key: defaultProp, Value: typedValue.GetDefaultValue()})

Expand All @@ -144,8 +144,8 @@ func (o *OpenAPIDocument) calculateProperties(schemaVal interface{}) *yamlmeta.M

case *ScalarType:
var items openAPIKeys
items = append(items, collectDocumentation(typedValue)...)
items = append(items, convertValidations(typedValue)...)
items = append(items, o.collectDocumentation(typedValue)...)
items = append(items, o.convertValidations(typedValue)...)
items = append(items, &yamlmeta.MapItem{Key: defaultProp, Value: typedValue.GetDefaultValue()})

typeString := o.openAPITypeFor(typedValue)
Expand All @@ -160,8 +160,8 @@ func (o *OpenAPIDocument) calculateProperties(schemaVal interface{}) *yamlmeta.M

case *NullType:
var items openAPIKeys
items = append(items, collectDocumentation(typedValue)...)
items = append(items, convertValidations(typedValue)...)
items = append(items, o.collectDocumentation(typedValue)...)
items = append(items, o.convertValidations(typedValue)...)
items = append(items, &yamlmeta.MapItem{Key: nullableProp, Value: true})

properties := o.calculateProperties(typedValue.GetValueType())
Expand All @@ -172,8 +172,8 @@ func (o *OpenAPIDocument) calculateProperties(schemaVal interface{}) *yamlmeta.M

case *AnyType:
var items openAPIKeys
items = append(items, collectDocumentation(typedValue)...)
items = append(items, convertValidations(typedValue)...)
items = append(items, o.collectDocumentation(typedValue)...)
items = append(items, o.convertValidations(typedValue)...)
items = append(items, &yamlmeta.MapItem{Key: nullableProp, Value: true})
items = append(items, &yamlmeta.MapItem{Key: defaultProp, Value: typedValue.GetDefaultValue()})

Expand All @@ -185,7 +185,7 @@ func (o *OpenAPIDocument) calculateProperties(schemaVal interface{}) *yamlmeta.M
}
}

func collectDocumentation(typedValue Type) []*yamlmeta.MapItem {
func (*OpenAPIDocument) collectDocumentation(typedValue Type) []*yamlmeta.MapItem {
var items []*yamlmeta.MapItem
if typedValue.GetTitle() != "" {
items = append(items, &yamlmeta.MapItem{Key: titleProp, Value: typedValue.GetTitle()})
Expand All @@ -205,7 +205,7 @@ func collectDocumentation(typedValue Type) []*yamlmeta.MapItem {
}

// convertValidations converts the starlark validation map to a list of OpenAPI properties
func convertValidations(schemaVal Type) []*yamlmeta.MapItem {
func (*OpenAPIDocument) convertValidations(schemaVal Type) []*yamlmeta.MapItem {
validation := schemaVal.GetValidation()
if validation == nil {
return nil
Expand Down Expand Up @@ -251,7 +251,7 @@ func convertValidations(schemaVal Type) []*yamlmeta.MapItem {
return items
}

func (o *OpenAPIDocument) openAPITypeFor(astType *ScalarType) string {
func (*OpenAPIDocument) openAPITypeFor(astType *ScalarType) string {
switch astType.ValueType {
case StringType:
return "string"
Expand Down

0 comments on commit 9663687

Please sign in to comment.