hcl_parser 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f8205c8bf81717770d753d6d0965a0892416c8b0bc6c84b7dd08d4ddfe19116a
4
- data.tar.gz: 9549a9834df1a02fe39a91bde951c16ab5e14fe4eed88ab2b0e0352dba014a81
3
+ metadata.gz: 99b2eea4da4681293e2c23c06758471554c56a34811c6ff6c0fff5381cbbdc88
4
+ data.tar.gz: 386ac2862e576b66272a277c44210cceea4b2aac7a25848517e0332f464fd261
5
5
  SHA512:
6
- metadata.gz: adc3a6ea90ade6a327eff78da11219661876ac59195e803ad1803752e880e81b774454b68521d0a3232c7147280c1728881b1ae3a9229f2c29bbc571320d9f62
7
- data.tar.gz: 72b869125b2700a4931c33bb65fcf0134718f7c5a475d2582e12b92837c73c0c32c518469b09bb6d9188954dd28676a9321040c505b9c01d4373b0f84ce7b47a
6
+ metadata.gz: ff94566dcf96f1228370731ff355934bd38dd2f67a7cb6558ec2829d0fd23060cb25bb639ec8559cb7d2cada4658bd84476654cc848c76b50ac4a4767467c553
7
+ data.tar.gz: ece0bd0213983d7619857db074105b950f681f82a395d3ef2acc792f043802987344f9ec684ebfa1a0687b5170cb05cb0ee1bfc7f5be1d975e7ed74becdfc3ec
data/.gitignore CHANGED
@@ -6,6 +6,6 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
-
9
+ /Gemfile.lock
10
10
  # rspec failure tracking
11
11
  .rspec_status
data/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ This project *loosely tries* to adhere to [Semantic Versioning](http://semver.org/), even before v1.0.
5
+
6
+ ## [0.2.0] - 2021-11-25
7
+ - [#1](https://github.com/boltops-tools/hcl_parser/pull/1) Complex types
8
+
9
+ ## [0.1.0]
10
+ * Initial release
@@ -17,6 +17,73 @@ module HclParser
17
17
  def code
18
18
  return @code if @code
19
19
  @code = fix_quotes(@raw)
20
+ @code = rewrite_complex_types(@code)
21
+ end
22
+
23
+ def rewrite_complex_types(raw)
24
+ lines = raw.split("\n")
25
+ results = []
26
+
27
+ variable_start_found = false
28
+ variable_start_index = nil
29
+ variable_end_index = nil
30
+ rewrite_lines = false
31
+
32
+ lines.each_with_index do |l,i|
33
+ # We dont rewrite lines until the next line/iteration
34
+ # This is actually the next iteration since we set rewrite_lines = true previously
35
+ # in rewrite_lines = has_complex_type?(lookahead_lines)
36
+ if rewrite_lines
37
+ if simple_default_assignment?(l)
38
+ results << l
39
+ elsif simple_default_assignment?(l)
40
+ # Doesnt matter what the default value is as long as there is one
41
+ # Doing this in case the default value spans multiple lines
42
+ results << ' default = "any"'
43
+ else
44
+ results << "# #{l}"
45
+ end
46
+ else
47
+ results << l
48
+ end
49
+ # End of logic in the next iteration
50
+
51
+ # Start of logic in the current iteration
52
+ variable_start_found = l.match(/^variable /)
53
+ if variable_start_found
54
+ variable_start_index = i
55
+ variable_end_index = variable_end_index(lines, variable_start_index)
56
+ lookahead_lines = lines[variable_start_index..variable_end_index]
57
+ rewrite_lines = has_complex_type?(lookahead_lines)
58
+ end
59
+
60
+ # Disable rewriting before reaching the end of the variable definition so: i + 1
61
+ if variable_end_index == i + 1
62
+ variable_start_index = nil
63
+ variable_end_index = nil
64
+ rewrite_lines = false
65
+ end
66
+ end
67
+
68
+ results.join("\n")
69
+ end
70
+
71
+ def simple_default_assignment?(line)
72
+ line.match(/default\s*=\s*("|'|null)/)
73
+ end
74
+
75
+ def has_complex_type?(lines)
76
+ !!lines.find do |l|
77
+ l.match(/object\(/)
78
+ end
79
+ end
80
+
81
+ def variable_end_index(lines, variable_start_index)
82
+ end_variable_index = nil
83
+ lines.each_with_index do |l,i|
84
+ next unless i > variable_start_index
85
+ return i if l.match(/^}/)
86
+ end
20
87
  end
21
88
 
22
89
  def empty?
@@ -1,3 +1,3 @@
1
1
  module HclParser
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hcl_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tung Nguyen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-05-26 00:00:00.000000000 Z
11
+ date: 2021-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rhcl
@@ -47,8 +47,8 @@ extra_rdoc_files: []
47
47
  files:
48
48
  - ".gitignore"
49
49
  - ".rspec"
50
+ - CHANGELOG.md
50
51
  - Gemfile
51
- - Gemfile.lock
52
52
  - LICENSE.txt
53
53
  - README.md
54
54
  - Rakefile
@@ -78,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
78
  - !ruby/object:Gem::Version
79
79
  version: '0'
80
80
  requirements: []
81
- rubygems_version: 3.1.2
81
+ rubygems_version: 3.1.6
82
82
  signing_key:
83
83
  specification_version: 4
84
84
  summary: HCL Variables Parser
data/Gemfile.lock DELETED
@@ -1,38 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- hcl_parser (0.1.0)
5
- rhcl
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- deep_merge (1.2.1)
11
- diff-lcs (1.3)
12
- rake (12.3.3)
13
- rhcl (0.1.0)
14
- deep_merge
15
- rspec (3.9.0)
16
- rspec-core (~> 3.9.0)
17
- rspec-expectations (~> 3.9.0)
18
- rspec-mocks (~> 3.9.0)
19
- rspec-core (3.9.2)
20
- rspec-support (~> 3.9.3)
21
- rspec-expectations (3.9.2)
22
- diff-lcs (>= 1.2.0, < 2.0)
23
- rspec-support (~> 3.9.0)
24
- rspec-mocks (3.9.1)
25
- diff-lcs (>= 1.2.0, < 2.0)
26
- rspec-support (~> 3.9.0)
27
- rspec-support (3.9.3)
28
-
29
- PLATFORMS
30
- ruby
31
-
32
- DEPENDENCIES
33
- hcl_parser!
34
- rake (~> 12.0)
35
- rspec (~> 3.0)
36
-
37
- BUNDLED WITH
38
- 2.1.4