github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/jsonprovider/attribute.go (about) 1 package jsonprovider 2 3 import ( 4 "encoding/json" 5 6 "github.com/hashicorp/terraform/internal/configs/configschema" 7 "github.com/zclconf/go-cty/cty" 8 ) 9 10 type Attribute struct { 11 AttributeType json.RawMessage `json:"type,omitempty"` 12 AttributeNestedType *NestedType `json:"nested_type,omitempty"` 13 Description string `json:"description,omitempty"` 14 DescriptionKind string `json:"description_kind,omitempty"` 15 Deprecated bool `json:"deprecated,omitempty"` 16 Required bool `json:"required,omitempty"` 17 Optional bool `json:"optional,omitempty"` 18 Computed bool `json:"computed,omitempty"` 19 Sensitive bool `json:"sensitive,omitempty"` 20 } 21 22 type NestedType struct { 23 Attributes map[string]*Attribute `json:"attributes,omitempty"` 24 NestingMode string `json:"nesting_mode,omitempty"` 25 } 26 27 func marshalStringKind(sk configschema.StringKind) string { 28 switch sk { 29 default: 30 return "plain" 31 case configschema.StringMarkdown: 32 return "markdown" 33 } 34 } 35 36 func marshalAttribute(attr *configschema.Attribute) *Attribute { 37 ret := &Attribute{ 38 Description: attr.Description, 39 DescriptionKind: marshalStringKind(attr.DescriptionKind), 40 Required: attr.Required, 41 Optional: attr.Optional, 42 Computed: attr.Computed, 43 Sensitive: attr.Sensitive, 44 Deprecated: attr.Deprecated, 45 } 46 47 // we're not concerned about errors because at this point the schema has 48 // already been checked and re-checked. 49 if attr.Type != cty.NilType { 50 attrTy, _ := attr.Type.MarshalJSON() 51 ret.AttributeType = attrTy 52 } 53 54 if attr.NestedType != nil { 55 nestedTy := NestedType{ 56 NestingMode: nestingModeString(attr.NestedType.Nesting), 57 } 58 attrs := make(map[string]*Attribute, len(attr.NestedType.Attributes)) 59 for k, attr := range attr.NestedType.Attributes { 60 attrs[k] = marshalAttribute(attr) 61 } 62 nestedTy.Attributes = attrs 63 ret.AttributeNestedType = &nestedTy 64 } 65 66 return ret 67 }