runner icon indicating copy to clipboard operation
runner copied to clipboard

Add HCL

Open Revlig opened this issue 5 years ago • 8 comments

Please complete the following information:

  • Name: HashiCorp Configuration Language (HCL)
  • Version: 0.12
  • Website: https://www.terraform.io/docs/configuration/syntax.html

:+1: reaction might help.

Revlig avatar Mar 31 '20 21:03 Revlig

What kind of challenges are you thinking of? Any examples? Any suggestion on how to test?

We can probably use https://github.com/hashicorp/hcl/tree/hcl2 and compare the result with the expected. I've been using HCL through Terraform, but never used it directly.

kazk avatar Apr 09 '20 21:04 kazk

I sincerely asked to add it because I am learning about this language and did not find it in codewars. Initially I thought of simple katas based on exercises that I am creating to improve my learning.

I'm following this list of functions: https://www.terraform.io/docs/configuration/functions.html. I'm also thinking about more practical things, like creating modules as well.

[Edit] I just started to find a way to test the kata:

image

Is more difficult to me because I have 3 childrens, hahaha I will post when find a better way to test.

Maybe this will help: https://github.com/hashicorp/terraform/issues/24269 But...

Revlig avatar Apr 10 '20 02:04 Revlig

@kazk Do you need someone to write a test framework? Once I learn more about HCL, I can try write one if you need one.

Steffan153 avatar Apr 22 '20 01:04 Steffan153

I don't think we need to write a new test framework. What I'm currently thinking is to let users submit solution.hcl and do something like the following to test:

package example_test

import (
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"log"
	"github.com/hashicorp/hcl/v2/hclsimple"
)

type Expected struct {
	Field string `hcl:"field"`
}

var _ = Describe("HCL", func() {
	It("should have some field with value", func() {
		var expected Expected
		err := hclsimple.DecodeFile("/workspace/solution.hcl", nil, &expected)
		if err != nil {
			log.Fatalf("Failed to load: %s", err)
		}
		Expect(expected.Field).To(Equal("bar"))
	})
})

hcl.EvalContext can be used to pass variables and functions when decoding with DecodeFile.

@Steffan153 Maybe you can experiment with this idea?

kazk avatar Apr 22 '20 02:04 kazk

I couldn't get HCL v2 installed, I got this error: C source files not allowed when not using cgo or SWIG: main.c temp.c :(

BTW, what's up with the Python 3.4.3? I finished all of those kata.

Steffan153 avatar Apr 22 '20 18:04 Steffan153

@Steffan153 Did you use Go modules?

# Initialize project
go mod init github.com/Steffan153/example
# Get HCL v2
go get github.com/hashicorp/hcl/v2

# Get test framework and matcher
go get github.com/onsi/ginkgo
go get github.com/onsi/gomega

# Generate tests
ginkgo bootstrap
ginkgo generate

# Add solution.hcl
echo 'field = "bar"' > solution.hcl

# Replace the contents of generated tests and
# run tests
go test

kazk avatar Apr 22 '20 22:04 kazk

Thanks, I got it working. So this is the basic idea: goproj_test.go:

package goproj_test

import (
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"log"
	"github.com/hashicorp/hcl/v2"
	"github.com/hashicorp/hcl/v2/hclsimple"
	"github.com/zclconf/go-cty/cty"
)

type Expected struct {
	Field int `hcl:"field"`
}

func tester(n int64, exp int) {
	var expected Expected
	err := hclsimple.DecodeFile("solution.hcl", &hcl.EvalContext{Variables: map[string]cty.Value{"n": cty.NumberIntVal(n)}}, &expected)
	if err != nil {
		log.Fatalf("Failed to load: %s", err)
	}
	Expect(expected.Field).To(Equal(exp))
}

var _ = Describe("HCL", func() {
	It("basic tests", func() {
		tester(5, 6)
		tester(7, 8)
	})
})

solution.hcl:

field = n + 1

Note that github.com/zclconf/go-cty/cty needs to be installed too.

Edit: I just made an entire Multiply translation btw.

Steffan153 avatar Apr 22 '20 23:04 Steffan153

Any updates on this one?

Fibonaccios avatar May 06 '24 19:05 Fibonaccios