Skip to content

Decoding issue for complex type #718

@valentinwidmer

Description

@valentinwidmer

I'm receiving a strange error message while trying to parse the following HCL structure:

user_namespaces_object = {
  ns1 = {
    accountid    = 46464
    assigned_psp = "default"
    git_token_secret_name = ""
    git_repository        = ""
    flux_custom_path = ""
    blackhole_port_fis       = ""
    extra_labels            = {}
  }
  ns2 = {
    accountid    = 46464
    assigned_psp = "default"
    git_token_secret_name = ""
    git_repository        = ""
    flux_custom_path = ""
    blackhole_port_fis       = ""
    extra_labels            = {}
  }
  ns3 = {
    accountid    = 46464
    assigned_psp = "default"
    git_token_secret_name = ""
    git_repository        = ""
    flux_custom_path = ""
    blackhole_port_fis       = ""
    extra_labels            = {}
  }
}

I would like to convert this HCL structure into a Go struct:

type Root struct {
	UserNamespacesObject []Namespace `hcl:"user_namespaces_object"`
}

type Namespace struct {
	AccountID                       int                               `hcl:"accountid" json:"accountid"`
	AssignedPSP                     string                            `hcl:"assigned_psp" json:"assigned_psp"`
	GitTokenSecretName              string                            `hcl:"git_token_secret_name" json:"git_token_secret_name"`
	GitRepository                   string                            `hcl:"git_repository" json:"git_repository"`
	FluxCustomPath                  string                            `hcl:"flux_custom_path" json:"flux_custom_path"`
	BlackholePortFIS                string                            `hcl:"blackhole_port_fis" json:"blackhole_port_fis"`
	ExtraLabels                     map[string]string                 `hcl:"extra_labels" json:"extra_labels"`
}

func handler(request json.RawMessage) (events.APIGatewayProxyResponse, error) {

	parser := hclparse.NewParser()
	file, diagnostics := parser.ParseHCL([]byte(input), "test")

	if diagnostics.HasErrors() {
		panic(diagnostics)
	}

	var root Root
	diags := gohcl.DecodeBody(file.Body, nil, &root)
	if diags.HasErrors() {
		return events.APIGatewayProxyResponse{
			Body:       diags.Error(),
			StatusCode: 400,
		}, err
	}

	fmt.Println(root)
}

While running the code I receive the following error message:

"errorMessage": "unsuitable DecodeExpression target: no cty.Type for main.Namespace (no cty field tags)", "errorType": "string", "stackTrace": [{"path": "github.com/aws/[email protected]/lambda/errors.go", "line": 39, "label": "lambdaPanicResponse"}, {"path": "github.com/aws/[email protected]/lambda/invoke_loop.go", "line": 102, "label": "callBytesHandlerFunc.func1"}, {"path": "runtime/panic.go", "line": 785, "label": "gopanic"}, {"path": "github.com/hashicorp/hcl/[email protected]/gohcl/decode.go", "line": 296, "label": "DecodeExpression"}, {"path": "github.com/hashicorp/hcl/[email protected]/gohcl/decode.go", "line": 127, "label": "decodeBodyToStruct"}, {"path": "github.com/hashicorp/hcl/[email protected]/gohcl/decode.go", "line": 46, "label": "decodeBodyToValue"}, {"path": "github.com/hashicorp/hcl/[email protected]/gohcl/decode.go", "line": 39, "label": "DecodeBody"}, {"path": "hello-world/main.go", "line": 63, "label": "handler"}, {"path": "reflect/value.go", "line": 581, "label": "Value.call"}, {"path": "reflect/value.go", "line": 365, "label": "Value.Call"}, {"path": "github.com/aws/[email protected]/lambda/handler.go", "line": 253, "label": "reflectHandler.func1"}, {"path": "github.com/aws/[email protected]/lambda/handler.go", "line": 194, "label": "bytesHandlerFunc.Invoke"}, {"path": "github.com/aws/[email protected]/lambda/invoke_loop.go", "line": 105, "label": "callBytesHandlerFunc"}, {"path": "github.com/aws/[email protected]/lambda/invoke_loop.go", "line": 73, "label": "handleInvoke"}, {"path": "github.com/aws/[email protected]/lambda/invoke_loop.go", "line": 37, "label": "startRuntimeAPILoop"}, {"path": "github.com/aws/[email protected]/lambda/entry.go", "line": 103, "label": "start"}, {"path": "github.com/aws/[email protected]/lambda/entry.go", "line": 66, "label": "StartWithOptions"}, {"path": "github.com/aws/[email protected]/lambda/entry.go", "line": 42, "label": "Start"}, {"path": "hello-world/main.go", "line": 157, "label": "main"}, {"path": "runtime/proc.go", "line": 272, "label": "main"}, {"path": "runtime/asm_amd64.s", "line": 1700, "label": "goexit"}]}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions