Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dumping HCL #4

Open
bkabrda opened this issue Oct 16, 2015 · 11 comments
Open

Dumping HCL #4

bkabrda opened this issue Oct 16, 2015 · 11 comments

Comments

@bkabrda
Copy link

bkabrda commented Oct 16, 2015

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

@virtuald
Copy link
Owner

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.

@bkabrda
Copy link
Author

bkabrda commented Oct 19, 2015

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 :)

@josegonzalez
Copy link

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

@kwent
Copy link

kwent commented Nov 29, 2017

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

@virtuald
Copy link
Owner

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

@daluu
Copy link

daluu commented Mar 15, 2019

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

@virtuald
Copy link
Owner

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

@derfalx
Copy link

derfalx commented May 24, 2019

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
	}
}

@virtuald
Copy link
Owner

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

@derfalx
Copy link

derfalx commented May 27, 2019

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

@pensnarik
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants