winebarrel/rhcl

Parsing arrays?

Opened this issue · 8 comments

phstc commented

Hey

How can I parse an array of "objects" attribute?

resource "aws_dynamodb_table" "my_table" {
  name = "my_table"
  read_capacity = 1
  write_capacity = 1
  hash_key = "hash"
  range_key = "range"
  attribute {
    name = "hash"
    type = "S"
  }
  attribute {
    name = "range"
    type = "S"
  }
}

Parsing the snippet above, I'm getting:

{ "resource" =>
 { "aws_dynamodb_table" =>
  { "my_table" =>
   { "name" => "my_table",
     "read_capacity" => 1,
     "write_capacity" => 1,
     "hash_key" => "hash",
     "range_key" => "range",
     "attribute" => { "name" => "range", "type" => "S" }
   }
  }
 }
}

but I was expecting attribute to be an array "attribute" => [{ "name" => "hash", "type" => "S" }, { "name" => "range", "type" => "S" }]

attribute is going to be a child hash.

Here is what I got. I hope this helps ya.

var = Rhcl.parse(<<-EOS)
  resource "aws_dynamodb_table" "my_table" {
    name = "my_table"
    read_capacity = 1
    write_capacity = 1
    hash_key = "hash"
    range_key = "range"
    attribute {
      name = "hash"
      type = "S"
    }
    attribute {
      name = "range"
      type = "S"
    }
  }
EOS

var['resource']['aws_dynamodb_table']['my_table']['attribute']

output: 
{"name"=>"hash", "type"=>"S"}
phstc commented

@xsmaster but how did you get the second attribute?

    attribute {
      name = "range"
      type = "S"
    }

I want to output an array of attributes.

@phstc here is what I did. I use the pry gem for all my testing. Killer tool.

So in a pry session or irb session I did the following:

1] pry(main)> require 'rhcl'
=> true
[2] pry(main)> var = Rhcl.parse(<<-EOS)
[2] pry(main)*   resource "aws_dynamodb_table" "my_table" {
[2] pry(main)*     name = "my_table"
[2] pry(main)*     read_capacity = 1
[2] pry(main)*     write_capacity = 1
[2] pry(main)*     hash_key = "hash"
[2] pry(main)*     range_key = "range"
[2] pry(main)*     attribute {
[2] pry(main)*       name = "hash"
[2] pry(main)*       type = "S"
[2] pry(main)*     }
[2] pry(main)*     attribute {
[2] pry(main)*       name = "range"
[2] pry(main)*       type = "S"
[2] pry(main)*     }
[2] pry(main)*   }
[2] pry(main)* EOS
=> {"resource"=>
  {"aws_dynamodb_table"=>
    {"my_table"=>
      {"name"=>"my_table",
       "read_capacity"=>1,
       "write_capacity"=>1,
       "hash_key"=>"hash",
       "range_key"=>"range",
       "attribute"=>{"name"=>"hash", "type"=>"S"}}}}}
[3] pry(main)>

So now we have the a hash variable called var. Since you want to access the "attribute" I did this.

[3] pry(main)> var['resource']['aws_dynamodb_table']['my_table']['attribute']
=> {"name"=>"hash", "type"=>"S"}

So now i have access to the key name or type. if you want it as an array you can do this.

[4] pry(main)> var['resource']['aws_dynamodb_table']['my_table']['attribute'].to_a
=> [["name", "hash"], ["type", "S"]]

The return is an array with two arrays inside. Does this help? What are you trying to do?

phstc commented

I was expecting attribute to be an array "attribute" => [{ "name" => "hash", "type" => "S" }, { "name" => "range", "type" => "S" }]

@xsmaster I was trying to output an array of attributes like that ^ not an array of containing the key and value of the first attribute.

phstc commented

@xsmaster maybe the attribute name being attribute might be leading into this confusion. But check the snippets in the issue description. I have two attributes and I was expecting an array with the two of them.

Sorry mate. You could take the results and just add them to an array.

On Friday, August 19, 2016, Pablo Cantero notifications@github.com wrote:

I was expecting attribute to be an array "attribute" => [{ "name" =>
"hash", "type" => "S" }, { "name" => "range", "type" => "S" }]

@xsmaster https://github.com/xsmaster I was trying to output an array
of attributes like that ^ not an array of containing the key and value of
the first attribute.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#3 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AF2nlYCCGAvH02NcIy4aUxD2isC3f2vHks5qhgOXgaJpZM4FaL_h
.

phstc commented

@xsmaster I believe we are not at same page. I can easily to the appending or whatever using Ruby, of course.

But this gem: "Pure Ruby HCL parser" is meant to parse HCL, and currently it isn't parsing arrays as I explained in the issue description.

resource "aws_dynamodb_table" "my_table" {
  name = "my_table"
  read_capacity = 1
  write_capacity = 1
  hash_key = "hash"
  range_key = "range"
  attribute {
    name = "hash"
    type = "S"
  }
  attribute {
    name = "range"
    type = "S"
  }
}

This is supposed to return an array of attributes, containing:

  • [0] name: hash, type: S
  • [1] name: range, type: S

Sorry a little slow here. I have noticed it only ever returns hashes. :(

On Friday, August 19, 2016, Pablo Cantero notifications@github.com wrote:

@xsmaster https://github.com/xsmaster I believe we are not at same
page. I can easily to the appending or whatever using Ruby, of course.

But this gem: "Pure Ruby HCL parser" is meant to parse HCL
https://github.com/hashicorp/hcl, and currently it isn't parsing arrays
as I explained in the issue description.

resource "aws_dynamodb_table" "my_table" {
name = "my_table"
read_capacity = 1
write_capacity = 1
hash_key = "hash"
range_key = "range"
attribute {
name = "hash"
type = "S"
}
attribute {
name = "range"
type = "S"
}
}

This is supposed to return an array of attributes, containing:

  • [0] name: hash, type: S
  • [1] name: range, type: S


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#3 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AF2nldGDyFZamNjxmGofTrKI_XuRHC5sks5qhgXQgaJpZM4FaL_h
.