hcl/hclsimple/hclsimple_test.go
Martin Atkins 34955ebf80 hclsimple: Simple one-shot parse/decode/evaluate API
For programs that don't need fine control over the process of decoding a
configuration, this allow a one-shot decode into a value of a Go struct
type.
2019-10-01 16:18:16 -07:00

83 lines
1.4 KiB
Go

package hclsimple_test
import (
"fmt"
"log"
"reflect"
"testing"
"github.com/hashicorp/hcl/v2/hclsimple"
)
func Example_nativeSyntax() {
type Config struct {
Foo string `hcl:"foo"`
Baz string `hcl:"baz"`
}
const exampleConfig = `
foo = "bar"
baz = "boop"
`
var config Config
err := hclsimple.Decode(
"example.hcl", []byte(exampleConfig),
nil, &config,
)
if err != nil {
log.Fatalf("Failed to load configuration: %s", err)
}
fmt.Printf("Configuration is %v\n", config)
// Output:
// Configuration is {bar boop}
}
func Example_jsonSyntax() {
type Config struct {
Foo string `hcl:"foo"`
Baz string `hcl:"baz"`
}
const exampleConfig = `
{
"foo": "bar",
"baz": "boop"
}
`
var config Config
err := hclsimple.Decode(
"example.json", []byte(exampleConfig),
nil, &config,
)
if err != nil {
log.Fatalf("Failed to load configuration: %s", err)
}
fmt.Printf("Configuration is %v\n", config)
// Output:
// Configuration is {bar boop}
}
func TestDecodeFile(t *testing.T) {
type Config struct {
Foo string `hcl:"foo"`
Baz string `hcl:"baz"`
}
var got Config
err := hclsimple.DecodeFile("testdata/test.hcl", nil, &got)
if err != nil {
t.Fatalf("unexpected error(s): %s", err)
}
want := Config{
Foo: "bar",
Baz: "boop",
}
if !reflect.DeepEqual(got, want) {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, want)
}
}