JustAttributes for JSON bodies interprets all properties of the wrapped JSON object as attributes and returns them.
()
| 170 | // JustAttributes for JSON bodies interprets all properties of the wrapped |
| 171 | // JSON object as attributes and returns them. |
| 172 | func (b *body) JustAttributes() (hcl.Attributes, hcl.Diagnostics) { |
| 173 | var diags hcl.Diagnostics |
| 174 | attrs := make(map[string]*hcl.Attribute) |
| 175 | |
| 176 | obj, ok := b.val.(*objectVal) |
| 177 | if !ok { |
| 178 | diags = append(diags, &hcl.Diagnostic{ |
| 179 | Severity: hcl.DiagError, |
| 180 | Summary: "Incorrect JSON value type", |
| 181 | Detail: "A JSON object is required here, setting the arguments for this block.", |
| 182 | Subject: b.val.StartRange().Ptr(), |
| 183 | }) |
| 184 | return attrs, diags |
| 185 | } |
| 186 | |
| 187 | for _, jsonAttr := range obj.Attrs { |
| 188 | name := jsonAttr.Name |
| 189 | if name == "//" { |
| 190 | // Ignore "//" keys in objects representing bodies, to allow |
| 191 | // their use as comments. |
| 192 | continue |
| 193 | } |
| 194 | |
| 195 | if _, hidden := b.hiddenAttrs[name]; hidden { |
| 196 | continue |
| 197 | } |
| 198 | |
| 199 | if existing, exists := attrs[name]; exists { |
| 200 | diags = append(diags, &hcl.Diagnostic{ |
| 201 | Severity: hcl.DiagError, |
| 202 | Summary: "Duplicate attribute definition", |
| 203 | Detail: fmt.Sprintf("The argument %q was already set at %s.", name, existing.Range), |
| 204 | Subject: &jsonAttr.NameRange, |
| 205 | }) |
| 206 | continue |
| 207 | } |
| 208 | |
| 209 | attrs[name] = &hcl.Attribute{ |
| 210 | Name: name, |
| 211 | Expr: &expression{src: jsonAttr.Value}, |
| 212 | Range: hcl.RangeBetween(jsonAttr.NameRange, jsonAttr.Value.Range()), |
| 213 | NameRange: jsonAttr.NameRange, |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // No diagnostics possible here, since the parser already took care of |
| 218 | // finding duplicates and every JSON value can be a valid attribute value. |
| 219 | return attrs, diags |
| 220 | } |
| 221 | |
| 222 | func (b *body) MissingItemRange() hcl.Range { |
| 223 | switch tv := b.val.(type) { |
nothing calls this directly
no test coverage detected