Code Monkey home page Code Monkey logo

Comments (2)

fatih avatar fatih commented on August 29, 2024

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.

apparentlymart avatar apparentlymart commented on August 29, 2024

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)

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.