pyhcl icon indicating copy to clipboard operation
pyhcl copied to clipboard

Dumping HCL

Open bkabrda opened this issue 8 years ago • 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] https://github.com/bkabrda/anymarkup/issues/6

bkabrda avatar Oct 16 '15 06:10 bkabrda

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.

virtuald avatar Oct 16 '15 14:10 virtuald

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 avatar Oct 19 '15 11:10 bkabrda

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

josegonzalez avatar Jul 13 '17 12:07 josegonzalez

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

kwent avatar Nov 29 '17 23:11 kwent

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

virtuald avatar Nov 30 '17 04:11 virtuald

@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.

daluu avatar Mar 15 '19 19:03 daluu

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

virtuald avatar Mar 16 '19 18:03 virtuald

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 avatar May 24 '19 12:05 derfalx

@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.

virtuald avatar May 25 '19 05:05 virtuald

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

derfalx avatar May 27 '19 08:05 derfalx

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.

pensnarik avatar Feb 19 '20 14:02 pensnarik