-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform.cue
125 lines (104 loc) · 2.37 KB
/
transform.cue
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package jsonschema
import (
"path"
"rmazur.io/cuetf/internal/tf"
)
// #SchemaTransform can be used to tranform the Terraform provider schema document into a an equivalent JSON Schema.
#SchemaTransform: {
#block: tf.#block
#name: string
"$schema": "https://json-schema.org/draft/2020-12/schema"
"$id": "https://rmazur.io/cuetf/schema/\(#name)"
#blockTransform & {#path: []}
}
// Transform Terraform block representation into an object schema.
#blockTransform: {
#path: [...string]
#block: tf.#block
type: "object"
#ref: {
#name: string
"#/\(path.Join([ for el in #path + [#name] {"$defs/\(el)"}]))"
}
properties: {
for name, info in #block.attributes if !info.deprecated {
(name): #fieldTransform & {#type: info.type}
}
for name, info in #block.block_types {
(name): {
#defRef: "$ref": {#ref, #name: name}
if info.nesting_mode == "single" {
#defRef
}
if info.nesting_mode == "list" || info.nesting_mode == "set" {
"oneOf": [
#defRef,
{
type: "array"
items: #defRef
if info.min_items != _|_ {
minItems: info.min_items
}
if info.max_items != _|_ {
maxItems: info.max_items
}
},
]
}
}
}
}
_defPaths: {
for name, _ in #block.block_types {
(name): #path + [name]
}
}
"$defs": {
for name, info in #block.block_types {
(name): #blockTransform & {#block: info.block, #path: _defPaths[name]}
}
}
required: [ for name, info in #block.attributes if info.required {name}]
additionalProperties: false
}
// Helper to transform into an object property.
#fieldTransform: {
#type: tf.#attr.#primitive
type: _primitivesMap[#type]
} | {
#type: tf.#attr.#complexDef
_complexMap[#type[0]] & {#defs: #type[1]}
}
_complexMap: {
object: {
#defs: _
type: "object"
additionalProperties: false
properties: {
for name, fType in #defs {
(name): #fieldTransform & {#type: fType}
}
}
}
map: {
#defs: _
type: "object"
additionalProperties: #fieldTransform & {#type: #defs}
}
set: {
#defs: _
type: "array"
items: #fieldTransform & {#type: #defs}
}
list: {
#defs: _
type: "array"
items: #fieldTransform & {#type: #defs}
}
}
// Map primitives from Terraform to JSON Schema values.
_primitivesMap: {
number: "number"
string: "string"
"bool": "boolean"
}