Comments (11)
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.
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.
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.
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
'sMarshal()
, 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? aMarshalHCL()
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.
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.
@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 bek {}
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.
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.
Done!
from hcl.
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.
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.
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)
- GoHCL does not support interpolation HOT 3
- hclsyntax: Template expression produces too large value refinements HOT 3
- Tuple Specification is inconsistent with Terraform
- StringLit is not part of the Expression Grammar
- PartialContent documentation mismatch with behavior? HOT 4
- hclwrite: SetAttributeRaw() after Clear() is not present in hcl.Bytes() output
- how to convert HCL2 into json? HOT 9
- Conditionals with unknown conditions and marked branches are erroring.
- how to convert json to hcl using hclv2? HOT 4
- merged: set attributes even if diagnosed as duplicated HOT 2
- raw parse and decode without evaluating variables and functions? HOT 3
- hclsyntax: Recovery of outer `ObjectConsExpr` with empty `TemplateExpr` HOT 1
- Question: How to evaluate unknown functions without errors? HOT 2
- hclsyntax: Recovery of incomplete `ConditionalExpr` HOT 1
- Introduce `Code` to `Diagnostic`
- Report ambiguous attribute keys earlier than in `expression.Value()` HOT 2
- hclsyntax: Recovery of incomplete parenthesis
- Wrong `NameRange` for namespaced function call expressions
- Allow for block types with optional labels HOT 5
- hcldec: how to use `block_attrs`? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hcl.