Skip to content

Commit

Permalink
Fix loading optional labels of same type
Browse files Browse the repository at this point in the history
Replaced chicken-and-egg retry method with bodyToContent
  • Loading branch information
Marcel Ludwig committed Mar 30, 2021
1 parent 00d76b0 commit 9f7e16f
Showing 1 changed file with 39 additions and 33 deletions.
72 changes: 39 additions & 33 deletions config/configload/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io/ioutil"
"path/filepath"
"regexp"
"strings"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
Expand Down Expand Up @@ -221,17 +220,11 @@ func mergeBackendBodies(definedBackends Backends, inline config.Inline) (hcl.Bod
// getBackendReference tries to fetch a backend from `definitions`
// block by a reference name, e.g. `backend = "name"`.
func getBackendReference(definedBackends Backends, body hcl.Body) (hcl.Body, error) {
content, _, diags := body.PartialContent(&hcl.BodySchema{
Attributes: []hcl.AttributeSchema{
{Name: backend},
}})
if diags.HasErrors() {
return nil, diags
}
content := bodyToContent(body)

// read out possible attribute reference
var name string
if attr, ok := content.Attributes["backend"]; ok {
if attr, ok := content.Attributes[backend]; ok {
val, valDiags := attr.Expr.Value(envContext)
if valDiags.HasErrors() {
return nil, valDiags
Expand Down Expand Up @@ -263,22 +256,7 @@ func getBackendReference(definedBackends Backends, body hcl.Body) (hcl.Body, err

func refineEndpoints(definedBackends Backends, endpoints config.Endpoints) error {
for _, endpoint := range endpoints {
// try to obtain proxy and request block with a chicken-and-egg situation:
// hcl labels are required if set, to make them optional we must know the content
// which could not unwrapped without label errors. We will handle this by block type
// and may have to throw an error which hints the user to configure the file properly.
endpointContent := &hcl.BodyContent{Attributes: make(hcl.Attributes)}
for _, t := range []string{proxy, request} {
c, err := contentByType(t, endpoint.Remain)
if err != nil {
return err
}
endpointContent.MissingItemRange = c.MissingItemRange
endpointContent.Blocks = append(endpointContent.Blocks, c.Blocks...)
for n, attr := range c.Attributes { // possible same key and content override, it's ok.
endpointContent.Attributes[n] = attr
}
}
endpointContent := bodyToContent(endpoint.Remain)

proxies := endpointContent.Blocks.OfType(proxy)
requests := endpointContent.Blocks.OfType(request)
Expand Down Expand Up @@ -422,6 +400,41 @@ func uniqueLabelName(unique map[string]struct{}, name string, hr *hcl.Range) err
return nil
}

func bodyToContent(body hcl.Body) *hcl.BodyContent {
content := &hcl.BodyContent{
MissingItemRange: body.MissingItemRange(),
}
b, ok := body.(*hclsyntax.Body)
if !ok {
return content
}

if len(b.Attributes) > 0 {
content.Attributes = make(hcl.Attributes)
}
for name, attr := range b.Attributes {
content.Attributes[name] = &hcl.Attribute{
Name: attr.Name,
Expr: attr.Expr,
Range: attr.Range(),
NameRange: attr.NameRange,
}
}

for _, block := range b.Blocks {
content.Blocks = append(content.Blocks, &hcl.Block{
Body: block.Body,
DefRange: block.DefRange(),
LabelRanges: block.LabelRanges,
Labels: block.Labels,
Type: block.Type,
TypeRange: block.TypeRange,
})
}

return content
}

func contentByType(blockType string, body hcl.Body) (*hcl.BodyContent, error) {
headerSchema := &hcl.BodySchema{
Blocks: []hcl.BlockHeaderSchema{
Expand Down Expand Up @@ -501,18 +514,11 @@ func newOAuthBackend(definedBackends Backends, parent hcl.Body) (hcl.Body, error
return nil, err
}

b, err := newBackend(definedBackends, &config.OAuth2{Remain: hclbody.New(&hcl.BodyContent{
return newBackend(definedBackends, &config.OAuth2{Remain: hclbody.New(&hcl.BodyContent{
Blocks: []*hcl.Block{
{Type: backend, Body: oauthBackend},
},
})})
if err != nil {
diags := err.(hcl.Diagnostics)
if strings.HasPrefix(diags[0].Summary, "The host of 'url'") {
diags[0].Summary = strings.Replace(diags[0].Summary, "The host of 'url'", "The host of 'token_endpoint'", 1)
}
}
return b, err
}

func renameAttribute(content *hcl.BodyContent, old, new string) {
Expand Down

0 comments on commit 9f7e16f

Please sign in to comment.