virtuald/pyhcl

Dumping HCL

bkabrda opened this issue · 11 comments

Hi, I'm working on a library called anymarkup [1] and got a request from one of its users to provide support for HCL [2]. I think this project is the only Python library that deals with HCL, but it can't dump it, just load it.

Do you have any plans for implementing HCL dumping? If not, I'd like to work on that and would appreciate any pointers you might have - like if you have an idea how this should work, a design already thought through in your head or something else. I'd like to prevent implementing something that you wouldn't like :)

[1] https://github.com/bkabrda/anymarkup
[2] bkabrda/anymarkup#6

I suspect HCL dumping will be a bit problematic, as it doesn't always have a non-ambiguous mapping between dictionaries / hcl (see #1). I have no plans to implement dumping for HCL, but would welcome the contribution.

If one were to go about doing this, for testing I would probably modify test_decoder.py such that you round-trip to/from json/hcl to/from hcl. That should allow you to get decent coverage on many of the language features.

It would probably be good to modify hcltool such that it could take in json or hcl and output hcl or json.

Thanks for the response.

I actually never realized that there is such ambiguity in HCL itself. I think I'd rather wait for #1 get sorted out before implementing this. I'll keep an eye on it and on related issues and get back to this when I'm sure I know how to do this properly :)

@bkabrda #1 was sorted, and I think there is an hcl.dumps now.

kwent commented

Any updates on this ? json2hcl tool is able to that.

I'm open to pull requests, but won't be developing the functionality myself.

daluu commented

@bkabrda #1 was sorted, and I think there is an hcl.dumps now.

It looks like hcl.dumps() is simply calling json.dumps(), https://github.com/virtuald/pyhcl/blob/master/src/hcl/api.py#L71.

So that's really just converting/dumping to JSON, not a true HCL dump.

Yes, as the documentation says. Pull requests still welcome. :)

Would it be ok, to create a pull request with work-in-progress code?
Currently I am working on a simple dump function, but I am not sure wether I am seeing all edge cases. Therefore I am working on an extra branch in a fork instance.

The idea is to have a base-class which holds a NAME and each instance which may hold a _hcl_label attribute. The NAME is used as name of the Stanza and the _hcl_label as the label of a named-Stanza.
For example:

class Foo(HclBase):
    NAME = "Foo"

    def __init__(self):
        self._hcl_label = 'foo_label'
        self.string = 'Some String'
        self.num = 128
        self.bool = True


class Bar(HclBase):
    NAME = "Bar"

    def __init__(self):
        self._hcl_label = 'bar_label'
        self.foo = Foo()
        self.arr = ['I', 'am', 'a', 'pirate', '!']
        self.map = {'test': 1, 'set': 2, 'tes': 3}

would be dumped to:

Bar "bar_label" {
	arr = ["I", "am", "a", "pirate", "!"]
	Foo "foo_label" {
		bool = true
		num = 128
		string = "Some String"
	}
	map = {
		test = 1
		set = 2
		tes = 3
	}
}

@derfalx isn't the point to take the output of hcl.load and be able to dump it to hcl? Those classes are not (and should not) be the output of hcl.load. What you're creating could be a useful hcl printer, but that's not quite the same thing.

Hmm, yeah this is more kind of a writer/encoder than of a dump.
Is this still in scope of pyhcl?

I use your module to patch terraform HCL files, but unfortunately it lacks the "dump back to HCL file" functionality. It would be great if it will be implemented one day.