Skip to content

Tags: jfremy/hcl

Tags

v2.16.2

Toggle v2.16.2's commit message
[FOR DISCUSSION] Fix serialization of nomad API objects

Nomad GO API code makes unsupported use of the hcl tag which results in panic

Here's a simple repro case:
```
package main

import (
  "github.com/hashicorp/nomad/api"
  "github.com/hashicorp/hcl/v2/gohcl"
  "github.com/hashicorp/hcl/v2/hclwrite"
)

func main() {
  tg := api.TaskGroup{}
  f := hclwrite.NewEmptyFile()
  gohcl.EncodeIntoBody(&tg, f.Body())
}
```

This code will panic

There are three things being fixed:
- use of string pointer for block label - current code would "print" the pointer instead of the pointer value. Change to use the value of the pointer
- use of block tag on map[string]interface{} - this is used for the freeform blocks (like docker driver config) and the expectation is to create an "anonymous" block containing the key/values of the map
- use of block tag on map[string]Struct (or *Struct) - this is used I think for convenience (allows the code to access the correct object through its name - so avoid iterating over the usual []Struct to find the right entry) - the Struct has adequate hcl tags (and even an hcl label). The expectation is to create multiple blocks whose content is each Struct value. The "name" of the block is actually duplicated between the label hcl tag and the key of the map. In this case to limit the amount of changes, I've been relying on the hcl label tag

Following those changes, I can serialize a simple Job object without the hcl library triggering a panic