github.com/hashicorp/hcl/v2@v2.20.0/hcldec/decode.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package hcldec
     5  
     6  import (
     7  	"github.com/hashicorp/hcl/v2"
     8  	"github.com/zclconf/go-cty/cty"
     9  )
    10  
    11  func decode(body hcl.Body, blockLabels []blockLabel, ctx *hcl.EvalContext, spec Spec, partial bool) (cty.Value, hcl.Body, hcl.Diagnostics) {
    12  	schema := ImpliedSchema(spec)
    13  
    14  	var content *hcl.BodyContent
    15  	var diags hcl.Diagnostics
    16  	var leftovers hcl.Body
    17  
    18  	if partial {
    19  		content, leftovers, diags = body.PartialContent(schema)
    20  	} else {
    21  		content, diags = body.Content(schema)
    22  	}
    23  
    24  	val, valDiags := spec.decode(content, blockLabels, ctx)
    25  	diags = append(diags, valDiags...)
    26  
    27  	return val, leftovers, diags
    28  }
    29  
    30  func impliedType(spec Spec) cty.Type {
    31  	return spec.impliedType()
    32  }
    33  
    34  func sourceRange(body hcl.Body, blockLabels []blockLabel, spec Spec) hcl.Range {
    35  	schema := ImpliedSchema(spec)
    36  	content, _, _ := body.PartialContent(schema)
    37  
    38  	return spec.sourceRange(content, blockLabels)
    39  }