Comments (2)
Seems like this is possible by asserting the body of the parsed file to *hclsyntax.Body
. The docs are pointing to something else so I didn't know that. A basic example:
parser := hclparse.NewParser()
file, diags := parser.ParseHCLFile("demo.hcl")
if diags.HasErrors() {
return diags
}
body, ok := file.Body.(*hclsyntax.Body)
if !ok {
return errors.New("could not retrieve *hclsyntax.Body")
}
// iterate over all top level blocks
for _, block := range body.Blocks { }
// iterate over all top level attributes
for _, attr := range body.Attributes { }
Opened a PR that updates the docs: #8
from hcl2.
Hi @fatih! Nice to see you over here.
Sorry things are still rather ill-documented in here. It's all still evolving and so I've not yet managed to write down a good overview of how it all fits together. The design philosophy here is quite a bit different than the prior HCL, and specifically more tailored to configuration with a well-defined structure, rather than arbitrary data structures.
As I think you've concluded, the main API in the hcl
package requires schema-driven decoding. It's built this way so that we can robustly handle ambiguous syntaxes like JSON, where the schema allows us to distinguish between maps, lists of maps, and nested blocks. When parsing the "native syntax" (as implemented in hclsyntax
), this ambiguity doesn't exist but we still use it to automatically report an error to the user if they use a block where an attribute is expected, or vice-versa.
Type-asserting to the hclsyntax
concrete types is a reasonable thing to do if you need to do things that the hcl
package abstractions won't allow, but note that in this case you're coupling yourself to the native syntax and what you did here won't work for the JSON syntax, and likely not for any other syntaxes that might be defined in future (depending on how they are implemented).
Totally dynamic parsing was not a goal I had in mind when defining the primary API here. The API design assumes applications will behave more like Terraform, having a generally-rigid structure but allowing arbitrary attributes in some specific blocks. The schema-driven parsing is key to the better-defined handling of JSON in HCL2, but indeed it does make some use-cases that were possible with HCL harder to achieve.
Sorry for the remaining references to "zcl" left in some spots. That's the name of my side-project that was forked and renamed to HCL2 to give a sense of continuity from HCL/HIL, and although all of the code is updated (I think!) there are still numerous leftover references in comments and docs which I'm cleaning up as I find them. Thanks for pointing out and fixing several of them!
from hcl2.
Related Issues (20)
- Scanning error for quoted templates ending with $ or % HOT 1
- heredocs aren't properly closed during indent fomartting HOT 1
- Merge into hashicorp/hcl HOT 3
- Avoid Duplicate HOT 1
- Argument or block definition required terraform HOT 1
- Clarify whether tabs are accepted or not HOT 2
- Unable to parse terraform variables HOT 4
- Continuous Fuzzing
- Formatter inserts unwanted space between two consecutive interpolation sequences
- Splat on a null should return an empty sequence
- hclpack: Attributes being mixed up in content()
- gohcl.EncodeIntoBody ignores optional elements HOT 1
- Template parsing allowing mixed escapes HOT 1
- Format doesn't correct handle block content after newline HOT 2
- Confusing error in HCL1-style map HOT 2
- Forbid zero-prefixed number literals to avoid octal confusion HOT 1
- Missing newline after comment at EOF causes error HOT 2
- Question: how to decode a custom type and validate it? HOT 2
- Inaccurate diagnostics context for nested fields HOT 2
- feat: struct field tag should support both block and optional HOT 2
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 hcl2.