Git Product home page Git Product logo

Comments (11)

mitchellh avatar mitchellh commented on June 18, 2024

We want to make a writer, but haven't gotten around to it yet. In the interim, generating JSON is the way to go. Alternately, if someone were to make the beginnings of a writer, I'd be happy to merge that in.

from hcl.

rubenv avatar rubenv commented on June 18, 2024

Great. I'll definitely needs this in my product, but there's no timeframe on when. If it's not there when I need it, I'll happily have a look at it.

Would you like the writer to try to reproduce the input format (whitespace etc), or shall we make it opinionated like "go fmt" and output a recommended style?

from hcl.

mitchellh avatar mitchellh commented on June 18, 2024

That is the difficulty (the styling), versus producing an actually correct format. I think if we were opinionated with the format it'd likely be good enough.

from hcl.

josephholsten avatar josephholsten commented on June 18, 2024

I've started to work on this. A few open questions I've come up with:

  • I'm starting this as a direct port of encoding/json's Marshal(), which is covered under go's license, any thoughts on how that will play with the MPL of this codebase?
  • what to do with value that reflect.ValueOf() returns as not valid? 'encoding/json' uses "null"
  • confirmation that input of octal or scientific notation are returned in decimal notation
  • confirmation that heredocs are returned as normal strings
  • should any characters beside \, ", \n, \r, \t, be escaped? encoding/json will escape <, >, &, \u2028 (LINE SEPARATOR), \u2029 (PARAGRAPH SEPARATOR), < 0x20 (encodeState.string())
  • should marshaling attempt to use a MarshalJSON() method? a MarshalHCL() method? both?
  • what should be done with trees of the sort {"k":{}}. Perhaps it should be equivalant to
if bs, err := json.Marshal(src); err != nil {
  return nil, err
}
var v interface{}
if err := hcl.Unmarshal(bs, v); err != nil {
  return nil, err
}
return hcl.Marshal(v)

from hcl.

josephholsten avatar josephholsten commented on June 18, 2024

Well, I've made progress. It appears that a state stack will be required to identify the right way to render an array, since arrays may either require surrounding [ & ] (string, num arrays), or may require multiple writes of the parent key (map arrays). Fortunately, we'd need a stack to handle proper indentation.

New open question: should we always use the key subKey {...} form, or always use the key { subKey {...}} form, or intelligently decide? I'll probably implement both so we can either use the simplest code or support intelligently deciding.

from hcl.

mitchellh avatar mitchellh commented on June 18, 2024

@josephholsten Amazing! Let me answer your questions to the best of my ability.

  • No idea how it interacts with that license.
  • I'm not sure, let's see where that comes up in practice? (Write some test cases)
  • Use best guess with types (scientific, heredoc, etc.) and escaping looks correct.
  • {"k":{}} should probably be k {} in HCL.

Next, one idea I had when I was planning on doing this (but didn't start yet, and now that you started, won't!): Take a look at go/format and see how it builds and outputs Go code. I think it'd be easiest to just make the HCL marshaller output untabbed/terrible HCL output, and then have an HCL formatter make it pretty.

That will give us hclfmt for free without affecting the data, since we can't Unmarshal/Remarshal for that since it could turn things like scientific notation into decimal and the point of the format should just be to get formatting right without changing things like that.

I think the easiest way might be getting it into an AST tree or lexer token stream and doing manipulations on that.

from hcl.

josephholsten avatar josephholsten commented on June 18, 2024

Oh, keeping indentation naïve to keep from messing up notations is a much better idea!

I'm currently a little unsure about what characters can safely be in a key without it being quoted, so we'll probably be getting a few new test cases for that.

from hcl.

mitchellh avatar mitchellh commented on June 18, 2024

Done!

from hcl.

savaki avatar savaki commented on June 18, 2024

I see this is closed, but I don't see an Encode method. Did this ever get merged into master? Seems super useful and I'd love to be able to use it.

from hcl.

mitchellh avatar mitchellh commented on June 18, 2024

Hey @savaki, there is a hcl/writer package now that takes a raw AST and writes. We don't have a more easy to use Encode method yet.

from hcl.

kvz avatar kvz commented on June 18, 2024

Hey there, you can use the HCL printer (and terraform fmt) to do this now. @Acconut and I took the liberty of wrapping a few of HashiCorp's libraries and release json2hcl (and hcl2json via -reverse) binaries for this. In the offchance this is still useful to somebody: https://github.com/kvz/json2hcl

from hcl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.