diff --git a/schemas.go b/schemas.go index 30d221b..2360231 100644 --- a/schemas.go +++ b/schemas.go @@ -223,6 +223,36 @@ type SchemaAttribute struct { Sensitive bool `json:"sensitive,omitempty"` } +type jsonSchemaAttribute struct { + AttributeType json.RawMessage `json:"type,omitempty"` + AttributeNestedType *SchemaNestedAttributeType `json:"nested_type,omitempty"` + Description string `json:"description,omitempty"` + DescriptionKind SchemaDescriptionKind `json:"description_kind,omitempty"` + Deprecated bool `json:"deprecated,omitempty"` + Required bool `json:"required,omitempty"` + Optional bool `json:"optional,omitempty"` + Computed bool `json:"computed,omitempty"` + Sensitive bool `json:"sensitive,omitempty"` +} + +func (as *SchemaAttribute) MarshalJSON() ([]byte, error) { + jsonSa := &jsonSchemaAttribute{ + AttributeNestedType: as.AttributeNestedType, + Description: as.Description, + DescriptionKind: as.DescriptionKind, + Deprecated: as.Deprecated, + Required: as.Required, + Optional: as.Optional, + Computed: as.Computed, + Sensitive: as.Sensitive, + } + if as.AttributeType != cty.NilType { + attrTy, _ := as.AttributeType.MarshalJSON() + jsonSa.AttributeType = attrTy + } + return json.Marshal(jsonSa) +} + // SchemaNestedAttributeType describes a nested attribute // which could also be just expressed simply as cty.Object(...), // cty.List(cty.Object(...)) etc. but this allows tracking additional diff --git a/schemas_test.go b/schemas_test.go index f6b5c93..a02ffba 100644 --- a/schemas_test.go +++ b/schemas_test.go @@ -22,3 +22,20 @@ func TestProviderSchemasValidate(t *testing.T) { t.Fatal(err) } } + +func TestProviderSchemasValidate_nestedAttributes(t *testing.T) { + f, err := os.Open("testdata/nested_attributes/schemas.json") + if err != nil { + t.Fatal(err) + } + defer f.Close() + + var schemas *ProviderSchemas + if err := json.NewDecoder(f).Decode(&schemas); err != nil { + t.Fatal(err) + } + + if err := schemas.Validate(); err != nil { + t.Fatal(err) + } +} diff --git a/testdata/nested_attributes/schemas.json b/testdata/nested_attributes/schemas.json new file mode 100644 index 0000000..618e413 --- /dev/null +++ b/testdata/nested_attributes/schemas.json @@ -0,0 +1 @@ +{"format_version":"0.2","provider_schemas":{"registry.terraform.io/hashicorp/awscc":{"provider":{"version":0,"block":{"attributes":{"access_key":{"type":"string","description_kind":"plain","optional":true},"assume_role":{"nested_type":{"attributes":{"duration":{"type":"string","description_kind":"plain","optional":true},"external_id":{"type":"string","description_kind":"plain","optional":true}},"nesting_mode":"single"},"description_kind":"plain","optional":true}},"description_kind":"plain"}}}}}