(schema *hcl.BodySchema)
| 78 | } |
| 79 | |
| 80 | func (b *body) PartialContent(schema *hcl.BodySchema) (*hcl.BodyContent, hcl.Body, hcl.Diagnostics) { |
| 81 | var diags hcl.Diagnostics |
| 82 | |
| 83 | jsonAttrs, attrDiags := b.collectDeepAttrs(b.val, nil) |
| 84 | diags = append(diags, attrDiags...) |
| 85 | |
| 86 | usedNames := map[string]struct{}{} |
| 87 | if b.hiddenAttrs != nil { |
| 88 | for k := range b.hiddenAttrs { |
| 89 | usedNames[k] = struct{}{} |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | content := &hcl.BodyContent{ |
| 94 | Attributes: map[string]*hcl.Attribute{}, |
| 95 | Blocks: nil, |
| 96 | |
| 97 | MissingItemRange: b.MissingItemRange(), |
| 98 | } |
| 99 | |
| 100 | // Create some more convenient data structures for our work below. |
| 101 | attrSchemas := map[string]hcl.AttributeSchema{} |
| 102 | blockSchemas := map[string]hcl.BlockHeaderSchema{} |
| 103 | for _, attrS := range schema.Attributes { |
| 104 | attrSchemas[attrS.Name] = attrS |
| 105 | } |
| 106 | for _, blockS := range schema.Blocks { |
| 107 | blockSchemas[blockS.Type] = blockS |
| 108 | } |
| 109 | |
| 110 | for _, jsonAttr := range jsonAttrs { |
| 111 | attrName := jsonAttr.Name |
| 112 | if _, used := b.hiddenAttrs[attrName]; used { |
| 113 | continue |
| 114 | } |
| 115 | |
| 116 | if attrS, defined := attrSchemas[attrName]; defined { |
| 117 | if existing, exists := content.Attributes[attrName]; exists { |
| 118 | diags = append(diags, &hcl.Diagnostic{ |
| 119 | Severity: hcl.DiagError, |
| 120 | Summary: "Duplicate argument", |
| 121 | Detail: fmt.Sprintf("The argument %q was already set at %s.", attrName, existing.Range), |
| 122 | Subject: &jsonAttr.NameRange, |
| 123 | Context: jsonAttr.Range().Ptr(), |
| 124 | }) |
| 125 | continue |
| 126 | } |
| 127 | |
| 128 | content.Attributes[attrS.Name] = &hcl.Attribute{ |
| 129 | Name: attrS.Name, |
| 130 | Expr: &expression{src: jsonAttr.Value}, |
| 131 | Range: hcl.RangeBetween(jsonAttr.NameRange, jsonAttr.Value.Range()), |
| 132 | NameRange: jsonAttr.NameRange, |
| 133 | } |
| 134 | usedNames[attrName] = struct{}{} |
| 135 | |
| 136 | } else if blockS, defined := blockSchemas[attrName]; defined { |
| 137 | bv := jsonAttr.Value |
no test coverage detected