github.com/mineiros-io/terradoc@v0.0.9-0.20220711062319-018bd4ae81f5/internal/validators/validator.go (about)

     1  package validators
     2  
     3  import (
     4  	"github.com/mineiros-io/terradoc/internal/entities"
     5  	"github.com/mineiros-io/terradoc/internal/types"
     6  )
     7  
     8  type TypeMismatchResult struct {
     9  	Name           string
    10  	DefinedType    string
    11  	DocumentedType string
    12  }
    13  
    14  type Summary struct {
    15  	Type                 string
    16  	MissingDefinition    []string
    17  	MissingDocumentation []string
    18  	TypeMismatch         []TypeMismatchResult
    19  }
    20  
    21  func (vs Summary) Success() bool {
    22  	return len(vs.MissingDocumentation) == 0 &&
    23  		len(vs.MissingDefinition) == 0 &&
    24  		len(vs.TypeMismatch) == 0
    25  }
    26  
    27  func TypesMatch(typeA, typeB *entities.Type) bool {
    28  	if typeA == nil && typeB == nil {
    29  		return true
    30  	}
    31  
    32  	if typeA.TFType == types.TerraformAny || typeB.TFType == types.TerraformAny {
    33  		if typeA.TFType == types.TerraformObject || typeB.TFType == types.TerraformObject {
    34  			return true
    35  		}
    36  
    37  		if typeA.HasNestedType() {
    38  			return TypesMatch(typeA.Nested, typeB)
    39  		}
    40  
    41  		if typeB.HasNestedType() {
    42  			return TypesMatch(typeA, typeB.Nested)
    43  		}
    44  	}
    45  
    46  	if typeA.TFType == types.TerraformObject && typeB.TFType == types.TerraformObject {
    47  		return true
    48  	}
    49  
    50  	return (typeA.TFType == typeB.TFType) &&
    51  		TypesMatch(typeA.Nested, typeB.Nested)
    52  }