-
Notifications
You must be signed in to change notification settings - Fork 9.6k
/
attribute.go
71 lines (62 loc) · 2.22 KB
/
attribute.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package jsonprovider
import (
"encoding/json"
"github.com/hashicorp/terraform/internal/configs/configschema"
"github.com/zclconf/go-cty/cty"
)
type attribute struct {
AttributeType json.RawMessage `json:"type,omitempty"`
AttributeNestedType *nestedType `json:"nested_type,omitempty"`
Description string `json:"description,omitempty"`
DescriptionKind string `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"`
}
type nestedType struct {
Attributes map[string]*attribute `json:"attributes,omitempty"`
NestingMode string `json:"nesting_mode,omitempty"`
MinItems uint64 `json:"min_items,omitempty"`
MaxItems uint64 `json:"max_items,omitempty"`
}
func marshalStringKind(sk configschema.StringKind) string {
switch sk {
default:
return "plain"
case configschema.StringMarkdown:
return "markdown"
}
}
func marshalAttribute(attr *configschema.Attribute) *attribute {
ret := &attribute{
Description: attr.Description,
DescriptionKind: marshalStringKind(attr.DescriptionKind),
Required: attr.Required,
Optional: attr.Optional,
Computed: attr.Computed,
Sensitive: attr.Sensitive,
Deprecated: attr.Deprecated,
}
// we're not concerned about errors because at this point the schema has
// already been checked and re-checked.
if attr.Type != cty.NilType {
attrTy, _ := attr.Type.MarshalJSON()
ret.AttributeType = attrTy
}
if attr.NestedType != nil {
nestedTy := nestedType{
MinItems: uint64(attr.NestedType.MinItems),
MaxItems: uint64(attr.NestedType.MaxItems),
NestingMode: nestingModeString(attr.NestedType.Nesting),
}
attrs := make(map[string]*attribute, len(attr.NestedType.Attributes))
for k, attr := range attr.NestedType.Attributes {
attrs[k] = marshalAttribute(attr)
}
nestedTy.Attributes = attrs
ret.AttributeNestedType = &nestedTy
}
return ret
}