Skip to content

Commit

Permalink
adjust to new kin-openapi api
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Hoß <[email protected]>
  • Loading branch information
sebhoss committed Apr 16, 2024
1 parent 4bdab6d commit d99e78f
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 82 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ require (
sigs.k8s.io/yaml v1.4.0
)

//replace github.com/hashicorp/terraform-plugin-docs => ../../../../github.com/sebhoss/terraform-plugin-docs

require (
cloud.google.com/go v0.112.0 // indirect
cloud.google.com/go/compute v1.23.3 // indirect
Expand Down
4 changes: 2 additions & 2 deletions tools/internal/generator/openapiv3_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ func openAPIv3Properties(schema *openapi3.Schema, imports *AdditionalImports, pa
}

var nestedProperties []*Property
if prop.Value.Type == "array" && prop.Value.Items != nil && prop.Value.Items.Value != nil && prop.Value.Items.Value.Type == "object" {
if prop.Value.Type.Is(openapi3.TypeArray) && prop.Value.Items != nil && prop.Value.Items.Value != nil && prop.Value.Items.Value.Type.Is(openapi3.TypeObject) {
nestedProperties = openAPIv3Properties(prop.Value.Items.Value, imports, propPath, terraformResourceName)
} else if prop.Value.Type == "object" && prop.Value.AdditionalProperties.Schema != nil && prop.Value.AdditionalProperties.Schema.Value.Type == "object" {
} else if prop.Value.Type.Is(openapi3.TypeObject) && prop.Value.AdditionalProperties.Schema != nil && prop.Value.AdditionalProperties.Schema.Value.Type.Is(openapi3.TypeObject) {
nestedProperties = openAPIv3Properties(prop.Value.AdditionalProperties.Schema.Value, imports, propPath, terraformResourceName)
} else {
nestedProperties = openAPIv3Properties(prop.Value, imports, propPath, terraformResourceName)
Expand Down
46 changes: 23 additions & 23 deletions tools/internal/generator/openapiv3_type_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,40 @@ type openapiv3TypeTranslator struct {
}

func (t *openapiv3TypeTranslator) hasNoType() bool {
return t.property.Type == ""
return t.property.Type == nil || len(t.property.Type.Slice()) == 0
}

func (t *openapiv3TypeTranslator) isIntOrString() bool {
_, ok := t.property.Extensions["x-kubernetes-int-or-string"]
return ok || t.property.Type == "string" && t.property.Format == "int-or-string"
return ok || t.property.Type.Is(openapi3.TypeString) && t.property.Format == "int-or-string"
}

func (t *openapiv3TypeTranslator) isBoolean() bool {
return t.property.Type == "boolean"
return t.property.Type.Is(openapi3.TypeBoolean)
}

func (t *openapiv3TypeTranslator) isString() bool {
return t.property.Type == "string"
return t.property.Type.Is(openapi3.TypeString)
}

func (t *openapiv3TypeTranslator) isInteger() bool {
return t.property.Type == "integer"
return t.property.Type.Is(openapi3.TypeInteger)
}

func (t *openapiv3TypeTranslator) isNumber() bool {
return t.property.Type == "number"
return t.property.Type.Is(openapi3.TypeNumber)
}

func (t *openapiv3TypeTranslator) isFloat() bool {
return t.property.Format == "float" || t.property.Format == "double"
}

func (t *openapiv3TypeTranslator) isArray() bool {
return t.property.Type == "array"
return t.property.Type.Is(openapi3.TypeArray)
}

func (t *openapiv3TypeTranslator) isObject() bool {
return t.property.Type == "object"
return t.property.Type.Is(openapi3.TypeObject)
}

func (t *openapiv3TypeTranslator) hasUnknownFields() bool {
Expand All @@ -67,7 +67,7 @@ func (t *openapiv3TypeTranslator) hasOneOf() bool {

func (t *openapiv3TypeTranslator) isOneOfArray() bool {
for _, oneOf := range t.property.OneOf {
if oneOf.Value.Type == "array" {
if oneOf.Value.Type.Is(openapi3.TypeArray) {
return true
}
}
Expand All @@ -76,41 +76,41 @@ func (t *openapiv3TypeTranslator) isOneOfArray() bool {

func (t *openapiv3TypeTranslator) isOneOfBoolean() bool {
for _, oneOf := range t.property.OneOf {
if oneOf.Value.Type == "boolean" {
if oneOf.Value.Type.Is(openapi3.TypeBoolean) {
return true
}
}
return false
}

func (t *openapiv3TypeTranslator) isObjectWithAdditionalStringProperties() bool {
return t.property.Type == "object" &&
return t.property.Type.Is(openapi3.TypeObject) &&
t.property.AdditionalProperties.Schema != nil &&
t.property.AdditionalProperties.Schema.Value.Type == "string"
t.property.AdditionalProperties.Schema.Value.Type.Is(openapi3.TypeString)
}

func (t *openapiv3TypeTranslator) isObjectWithAdditionalObjectProperties() bool {
return t.property.Type == "object" &&
return t.property.Type.Is(openapi3.TypeObject) &&
t.property.AdditionalProperties.Schema != nil &&
t.property.AdditionalProperties.Schema.Value.Type == "object"
t.property.AdditionalProperties.Schema.Value.Type.Is(openapi3.TypeObject)
}

func (t *openapiv3TypeTranslator) isObjectWithAdditionalArrayProperties() bool {
return t.property.Type == "object" &&
return t.property.Type.Is(openapi3.TypeObject) &&
t.property.AdditionalProperties.Schema != nil &&
t.property.AdditionalProperties.Schema.Value.Type == "array"
t.property.AdditionalProperties.Schema.Value.Type.Is(openapi3.TypeArray)
}

func (t *openapiv3TypeTranslator) isArrayWithObjectItems() bool {
return t.property.Type == "array" &&
return t.property.Type.Is(openapi3.TypeArray) &&
t.property.Items != nil &&
t.property.Items.Value != nil &&
t.property.Items.Value.Type == "object"
t.property.Items.Value.Type.Is(openapi3.TypeObject)
}

func (t *openapiv3TypeTranslator) additionalPropertiesHaveStringItems() bool {
return t.property.AdditionalProperties.Schema.Value.Items != nil &&
t.property.AdditionalProperties.Schema.Value.Items.Value.Type == "string"
t.property.AdditionalProperties.Schema.Value.Items.Value.Type.Is(openapi3.TypeString)
}

func (t *openapiv3TypeTranslator) additionalPropertiesHaveProperties() bool {
Expand All @@ -124,17 +124,17 @@ func (t *openapiv3TypeTranslator) additionalPropertiesHaveUnknownFields() bool {

func (t *openapiv3TypeTranslator) additionalPropertiesHaveAdditionalStringProperties() bool {
return t.property.AdditionalProperties.Schema.Value.AdditionalProperties.Schema != nil &&
t.property.AdditionalProperties.Schema.Value.AdditionalProperties.Schema.Value.Type == "string"
t.property.AdditionalProperties.Schema.Value.AdditionalProperties.Schema.Value.Type.Is(openapi3.TypeString)
}

func (t *openapiv3TypeTranslator) additionalPropertiesHaveAdditionalArrayProperties() bool {
return t.property.AdditionalProperties.Schema.Value.AdditionalProperties.Schema != nil &&
t.property.AdditionalProperties.Schema.Value.AdditionalProperties.Schema.Value.Type == "array"
t.property.AdditionalProperties.Schema.Value.AdditionalProperties.Schema.Value.Type.Is(openapi3.TypeArray)
}

func (t *openapiv3TypeTranslator) additionalPropertiesHaveAdditionalPropertiesWithStringItems() bool {
return t.property.AdditionalProperties.Schema.Value.AdditionalProperties.Schema.Value.Items != nil &&
t.property.AdditionalProperties.Schema.Value.AdditionalProperties.Schema.Value.Items.Value.Type == "string"
t.property.AdditionalProperties.Schema.Value.AdditionalProperties.Schema.Value.Items.Value.Type.Is(openapi3.TypeString)
}

func (t *openapiv3TypeTranslator) itemsHaveUnknownFields() bool {
Expand All @@ -144,5 +144,5 @@ func (t *openapiv3TypeTranslator) itemsHaveUnknownFields() bool {

func (t *openapiv3TypeTranslator) itemsHaveAdditionalStringProperties() bool {
return t.property.Items.Value.AdditionalProperties.Schema != nil &&
t.property.Items.Value.AdditionalProperties.Schema.Value.Type == "string"
t.property.Items.Value.AdditionalProperties.Schema.Value.Type.Is(openapi3.TypeString)
}
24 changes: 12 additions & 12 deletions tools/internal/generator/openapiv3_validator_extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type openapiv3ValidatorExtractor struct {
}

func (v *openapiv3ValidatorExtractor) integerWithMinimum() string {
if v.property.Type == "integer" && v.property.Min != nil {
if v.property.Type.Is(openapi3.TypeInteger) && v.property.Min != nil {
v.imports.Int64Validator = true
min := *v.property.Min
if v.property.ExclusiveMin {
Expand All @@ -30,7 +30,7 @@ func (v *openapiv3ValidatorExtractor) integerWithMinimum() string {
}

func (v *openapiv3ValidatorExtractor) integerWithMaximum() string {
if v.property.Type == "integer" && v.property.Max != nil {
if v.property.Type.Is(openapi3.TypeInteger) && v.property.Max != nil {
v.imports.Int64Validator = true
max := *v.property.Max
if v.property.ExclusiveMax {
Expand All @@ -42,7 +42,7 @@ func (v *openapiv3ValidatorExtractor) integerWithMaximum() string {
}

func (v *openapiv3ValidatorExtractor) integerWithEnums() string {
if v.property.Type == "integer" && len(v.property.Enum) > 0 {
if v.property.Type.Is(openapi3.TypeInteger) && len(v.property.Enum) > 0 {
v.imports.Int64Validator = true
enums := openapiIntEnums(v.property.Enum)
return fmt.Sprintf("int64validator.OneOf(%v)", concatEnums(enums))
Expand All @@ -51,7 +51,7 @@ func (v *openapiv3ValidatorExtractor) integerWithEnums() string {
}

func (v *openapiv3ValidatorExtractor) numberWithMinimum() string {
if v.property.Type == "number" && v.property.Min != nil {
if v.property.Type.Is(openapi3.TypeNumber) && v.property.Min != nil {
v.imports.Float64Validator = true
min := *v.property.Min
if v.property.ExclusiveMin {
Expand All @@ -63,7 +63,7 @@ func (v *openapiv3ValidatorExtractor) numberWithMinimum() string {
}

func (v *openapiv3ValidatorExtractor) numberWithMaximum() string {
if v.property.Type == "number" && v.property.Max != nil {
if v.property.Type.Is(openapi3.TypeNumber) && v.property.Max != nil {
v.imports.Float64Validator = true
max := *v.property.Max
if v.property.ExclusiveMax {
Expand All @@ -75,7 +75,7 @@ func (v *openapiv3ValidatorExtractor) numberWithMaximum() string {
}

func (v *openapiv3ValidatorExtractor) numberWithEnums() string {
if v.property.Type == "number" && len(v.property.Enum) > 0 {
if v.property.Type.Is(openapi3.TypeNumber) && len(v.property.Enum) > 0 {
v.imports.Float64Validator = true
enums := openapiFloatEnums(v.property.Enum)
return fmt.Sprintf("float64validator.OneOf(%v)", concatEnums(enums))
Expand All @@ -84,37 +84,37 @@ func (v *openapiv3ValidatorExtractor) numberWithEnums() string {
}

func (v *openapiv3ValidatorExtractor) stringWithByteFormat() string {
if v.property.Type == "string" && v.property.Format == "byte" {
if v.property.Type.Is(openapi3.TypeString) && v.property.Format == "byte" {
return "validators.Base64Validator()"
}
return ""
}

func (v *openapiv3ValidatorExtractor) stringWithDateTimeFormat() string {
if v.property.Type == "string" && v.property.Format == "date-time" {
if v.property.Type.Is(openapi3.TypeString) && v.property.Format == "date-time" {
return "validators.DateTime64Validator()"
}
return ""
}

func (v *openapiv3ValidatorExtractor) stringWithMinimumLength() string {
if v.property.Type == "string" && v.property.MinLength != 0 {
if v.property.Type.Is(openapi3.TypeString) && v.property.MinLength != 0 {
v.imports.StringValidator = true
return fmt.Sprintf("stringvalidator.LengthAtLeast(%v)", v.property.MinLength)
}
return ""
}

func (v *openapiv3ValidatorExtractor) stringWithMaximumLength() string {
if v.property.Type == "string" && v.property.MaxLength != nil {
if v.property.Type.Is(openapi3.TypeString) && v.property.MaxLength != nil {
v.imports.StringValidator = true
return fmt.Sprintf("stringvalidator.LengthAtMost(%v)", *v.property.MaxLength)
}
return ""
}

func (v *openapiv3ValidatorExtractor) stringWithEnums() string {
if v.property.Type == "string" && len(v.property.Enum) > 0 {
if v.property.Type.Is(openapi3.TypeString) && len(v.property.Enum) > 0 {
v.imports.StringValidator = true
enums := openapiStringEnums(v.property.Enum)
return fmt.Sprintf("stringvalidator.OneOf(%s)", concatEnums(enums))
Expand All @@ -123,7 +123,7 @@ func (v *openapiv3ValidatorExtractor) stringWithEnums() string {
}

func (v *openapiv3ValidatorExtractor) stringWithPattern() string {
if v.property.Type == "string" && v.property.Pattern != "" {
if v.property.Type.Is(openapi3.TypeString) && v.property.Pattern != "" {
v.imports.Regexp = true
v.imports.StringValidator = true
return fmt.Sprintf(`stringvalidator.RegexMatches(regexp.MustCompile(%s), "")`, escapeRegexPattern(v.property.Pattern))
Expand Down
Loading

0 comments on commit d99e78f

Please sign in to comment.