MCPcopy
hub / github.com/hashicorp/hcl / mergedContent

Method mergedContent

merged.go:143–227  ·  view source on GitHub ↗
(schema *BodySchema, partial bool)

Source from the content-addressed store, hash-verified

141}
142
143func (mb mergedBodies) mergedContent(schema *BodySchema, partial bool) (*BodyContent, Body, Diagnostics) {
144 // We need to produce a new schema with none of the attributes marked as
145 // required, since _any one_ of our bodies can contribute an attribute value.
146 // We'll separately check that all required attributes are present at
147 // the end.
148 mergedSchema := &BodySchema{
149 Blocks: schema.Blocks,
150 }
151 for _, attrS := range schema.Attributes {
152 mergedAttrS := attrS
153 mergedAttrS.Required = false
154 mergedSchema.Attributes = append(mergedSchema.Attributes, mergedAttrS)
155 }
156
157 var mergedLeftovers []Body
158 content := &BodyContent{
159 Attributes: map[string]*Attribute{},
160 }
161
162 var diags Diagnostics
163 for _, body := range mb {
164 var thisContent *BodyContent
165 var thisLeftovers Body
166 var thisDiags Diagnostics
167
168 if partial {
169 thisContent, thisLeftovers, thisDiags = body.PartialContent(mergedSchema)
170 } else {
171 thisContent, thisDiags = body.Content(mergedSchema)
172 }
173
174 if thisLeftovers != nil {
175 mergedLeftovers = append(mergedLeftovers, thisLeftovers)
176 }
177 if len(thisDiags) != 0 {
178 diags = append(diags, thisDiags...)
179 }
180
181 if thisContent.Attributes != nil {
182 for name, attr := range thisContent.Attributes {
183 if existing := content.Attributes[name]; existing != nil {
184 diags = diags.Append(&Diagnostic{
185 Severity: DiagError,
186 Summary: "Duplicate argument",
187 Detail: fmt.Sprintf(
188 "Argument %q was already set at %s",
189 name, existing.NameRange.String(),
190 ),
191 Subject: &attr.NameRange,
192 })
193 continue
194 }
195 content.Attributes[name] = attr
196 }
197 }
198
199 if len(thisContent.Blocks) != 0 {
200 content.Blocks = append(content.Blocks, thisContent.Blocks...)

Callers 2

ContentMethod · 0.95
PartialContentMethod · 0.95

Calls 5

AppendMethod · 0.95
MergeBodiesFunction · 0.85
PartialContentMethod · 0.65
ContentMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected