@@ -64,68 +64,57 @@ func convertRepSpecs(resourceb *hclwrite.Body, diskSizeGB hclwrite.Tokens) error
64
64
if dSpec .IsPresent () {
65
65
return convertDynamicRepSpecs (resourceb , dSpec , diskSizeGB )
66
66
}
67
-
68
67
repSpecBlocks := collectBlocks (resourceb , nRepSpecs )
69
68
if len (repSpecBlocks ) == 0 {
70
69
return fmt .Errorf ("must have at least one replication_specs" )
71
70
}
72
-
71
+ var tokens hclwrite. Tokens
73
72
if hasVariableNumShards (repSpecBlocks ) {
74
- tokens , err := processVariableNumShards (repSpecBlocks , diskSizeGB )
75
- if err != nil {
76
- return err
77
- }
78
- resourceb .SetAttributeRaw (nRepSpecs , hcl .TokensFuncConcat (tokens ... ))
73
+ tokens , err = processVariableNumShards (repSpecBlocks , diskSizeGB )
79
74
} else {
80
- tokens , err := processStaticNumShards (repSpecBlocks , diskSizeGB )
81
- if err != nil {
82
- return err
83
- }
84
- resourceb .SetAttributeRaw (nRepSpecs , tokens )
75
+ tokens , err = processStaticNumShards (repSpecBlocks , diskSizeGB )
85
76
}
86
-
77
+ if err != nil {
78
+ return err
79
+ }
80
+ resourceb .SetAttributeRaw (nRepSpecs , tokens )
87
81
return nil
88
82
}
89
83
90
84
func convertDynamicRepSpecs (resourceb * hclwrite.Body , dSpec dynamicBlock , diskSizeGB hclwrite.Tokens ) error {
91
85
transformDynamicBlockReferences (dSpec .content .Body (), nRepSpecs , nSpec )
92
-
93
86
dConfig , err := findDynamicConfigBlock (dSpec .content .Body ())
94
87
if err != nil {
95
88
return err
96
89
}
97
90
if dConfig .IsPresent () {
98
91
return convertDynamicRepSpecsWithDynamicConfig (resourceb , dSpec , dConfig , diskSizeGB )
99
92
}
100
-
101
93
tokens , err := processDynamicRepSpecsWithoutConfig (dSpec , diskSizeGB )
102
94
if err != nil {
103
95
return err
104
96
}
105
-
106
97
resourceb .RemoveBlock (dSpec .block )
107
98
resourceb .SetAttributeRaw (nRepSpecs , tokens )
108
99
return nil
109
100
}
110
101
111
- func processVariableNumShards (repSpecBlocks []* hclwrite.Block , diskSizeGB hclwrite.Tokens ) ([] hclwrite.Tokens , error ) {
102
+ func processVariableNumShards (repSpecBlocks []* hclwrite.Block , diskSizeGB hclwrite.Tokens ) (hclwrite.Tokens , error ) {
112
103
var concatParts []hclwrite.Tokens
113
104
for _ , block := range repSpecBlocks {
114
105
blockb := block .Body ()
115
106
numShardsAttr := blockb .GetAttribute (nNumShards )
116
107
blockb .RemoveAttribute (nNumShards )
117
-
118
108
if err := convertConfig (blockb , diskSizeGB ); err != nil {
119
109
return nil , err
120
110
}
121
-
122
111
tokens , err := processNumShards (numShardsAttr , blockb )
123
112
if err != nil {
124
113
return nil , err
125
114
}
126
115
concatParts = append (concatParts , tokens )
127
116
}
128
- return concatParts , nil
117
+ return hcl . TokensFuncConcat ( concatParts ... ) , nil
129
118
}
130
119
131
120
func processStaticNumShards (repSpecBlocks []* hclwrite.Block , diskSizeGB hclwrite.Tokens ) (hclwrite.Tokens , error ) {
@@ -134,11 +123,9 @@ func processStaticNumShards(repSpecBlocks []*hclwrite.Block, diskSizeGB hclwrite
134
123
blockb := block .Body ()
135
124
numShardsAttr := blockb .GetAttribute (nNumShards )
136
125
blockb .RemoveAttribute (nNumShards )
137
-
138
126
if err := convertConfig (blockb , diskSizeGB ); err != nil {
139
127
return nil , err
140
128
}
141
-
142
129
if numShardsAttr != nil {
143
130
numShardsVal , _ := hcl .GetAttrInt (numShardsAttr , errNumShards )
144
131
for range numShardsVal {
@@ -168,15 +155,12 @@ func findDynamicConfigBlock(body *hclwrite.Body) (dynamicBlock, error) {
168
155
func processDynamicRepSpecsWithoutConfig (dSpec dynamicBlock , diskSizeGB hclwrite.Tokens ) (hclwrite.Tokens , error ) {
169
156
numShardsAttr := dSpec .content .Body ().GetAttribute (nNumShards )
170
157
dSpec .content .Body ().RemoveAttribute (nNumShards )
171
-
172
158
if err := convertConfig (dSpec .content .Body (), diskSizeGB ); err != nil {
173
159
return nil , err
174
160
}
175
-
176
161
if numShardsAttr != nil {
177
162
return buildDynamicRepSpecsWithShards (dSpec , numShardsAttr )
178
163
}
179
-
180
164
return buildSimpleDynamicRepSpecs (dSpec )
181
165
}
182
166
@@ -202,22 +186,18 @@ func buildDynamicRepSpecsWithNumShards(dSpec, dConfig dynamicBlock, diskSizeGB h
202
186
configBlockName string , numShardsAttr * hclwrite.Attribute ) (hclwrite.Tokens , error ) {
203
187
numShardsExpr := replaceDynamicBlockReferences (hcl .GetAttrExpr (numShardsAttr ), nRepSpecs , nSpec )
204
188
transformDynamicBlockReferencesRecursive (dConfig .content .Body (), configBlockName , nRegion )
205
-
206
189
transform := func (expr string ) string {
207
190
return replaceDynamicBlockReferences (expr , nRepSpecs , nSpec )
208
191
}
209
192
transformAttributesSorted (dConfig .content .Body (), dConfig .content .Body ().Attributes (), transform )
210
193
for _ , block := range dConfig .content .Body ().Blocks () {
211
194
transformAttributesSorted (block .Body (), block .Body ().Attributes (), transform )
212
195
}
213
-
214
196
regionConfigBody := buildRegionConfigBody (dConfig , diskSizeGB )
215
197
regionTokens := buildRegionForExpr (nSpec , regionConfigBody )
216
-
217
198
repSpecBody := buildRepSpecBody (dSpec , regionTokens )
218
199
innerTokens := buildInnerForExpr (numShardsExpr , repSpecBody )
219
200
outerTokens := buildOuterForExpr (dSpec , innerTokens )
220
-
221
201
return hcl .TokensFuncFlatten (outerTokens ), nil
222
202
}
223
203
0 commit comments