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

Add HCL #2

Merged
merged 3 commits into from
Jan 10, 2019
Merged

Add HCL #2

merged 3 commits into from
Jan 10, 2019

Conversation

ccakes
Copy link
Contributor

@ccakes ccakes commented Dec 27, 2018

First - thanks for yj, it's great!

I was looking today for a tool to use in some script to convert to HCL and couldn't find anything. It seemed fairly straightforward to add it here so I figured I'd have a go.

This is the first time I've written any Go so it's basically a copy-paste job. I vendored the hcl package because (a) that's how the TOML dependency is handled and (b) because I don't know how the Go package system works :/

Been using it locally and it seems to work ok enough, at least for my usage. If you want any changes to the PR just let me know.

@ccakes
Copy link
Contributor Author

ccakes commented Dec 27, 2018

A couple of issues with the HCL package, there may be workarounds with better language knowledge.

  1. It doesn't have an easy-to-use hcl.Encode function, it's possible to build an AST object from JSON though, hence the double conversion is toHCL
  2. Similarly, I couldn't find a nice way to return a []byte containing the output. Right now I'm just printing the result in toHCL and returning nil to main. I dug around for 5mins in the package code but couldn't see an internal method that would be easy to use for me

@sclevine
Copy link
Owner

sclevine commented Dec 27, 2018

Many thanks for the PR!

Seems very reasonable for yj to support HCL, given its popularity (Terraform). I wonder what HCL2 will eventually mean for this integration, but I'm happy to include support for HCL for now.

I agree that converting to JSON to support encoding seems appropriate, given hashicorp/hcl#8.

Just a few things to fix up:

  1. Let's leverage toJSON instead of copying it here:

    yj/main.go

    Lines 187 to 193 in 2dd1d82

    output := &bytes.Buffer{}
    encoder := json.NewEncoder(output)
    encoder.SetEscapeHTML(config.EscapeHTML)
    err := encoder.Encode(input)
    if err != nil {
    return nil, err
    }
  2. You can print this into another bytes.Buffer and return the bytes.Buffer.Bytes():

    yj/main.go

    Line 196 in 2dd1d82

    err = printer.Fprint(os.Stdout, ast)
  3. Don't forget to check this error:

    yj/main.go

    Line 195 in 2dd1d82

    ast, err := jsonParser.Parse(output.Bytes())
  4. HTML escaping for HCL should be mentioned in the usage (currently it says JSON-only):

    yj/main.go

    Line 189 in 2dd1d82

    encoder.SetEscapeHTML(config.EscapeHTML)

This project currently uses dep for vendoring, but I'd like it to use go modules instead. Don't worry about including the dependency for now -- I'll handle that with the merge.

This package needs more tests for things outside of the complex YAML normalization it was originally written for. I'd usually ask for tests in a PR, but I'll handle testing HCL when I add integration tests for the rest of the conversions.

@ccakes
Copy link
Contributor Author

ccakes commented Jan 2, 2019

Thanks for the review! I've fixed the issues you mentioned but while testing, I've noticed that HCL -> YAML throws an error at times. I haven't had a chance to dig into it yet, but this isn't ready to merge

@sclevine
Copy link
Owner

sclevine commented Jan 2, 2019

Fixes look great! Let me know when it's ready.

Happy to help diagnose the HCL -> YAML errors if you could provide more detail.

@sclevine
Copy link
Owner

@ccakes I fixed the HCL -> YAML issue: https://github.com/sclevine/yj/tree/hcl

Mind if I merge this now? 😄

@ccakes
Copy link
Contributor Author

ccakes commented Jan 10, 2019

@sclevine Hey! Sorry for the delay, yeah definitely happy if you managed to sort it 😊

@sclevine sclevine merged commit 1a840b5 into sclevine:master Jan 10, 2019
@sclevine
Copy link
Owner

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

Successfully merging this pull request may close these issues.

None yet

2 participants