hcl2-rb 0.9.1 → 0.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 73d51dffee490023cce7a09c6ddec262ea198ef287e24d4e8ea536335ca1c37a
4
- data.tar.gz: cbcc946ca6cd61ada79732035840fd768dda94546e55fbe030c2c0a203d9e606
3
+ metadata.gz: da8df68511ccd65a048c216406ece161d869e2b8180a3ad3ac52e77b7526bcd4
4
+ data.tar.gz: 7b247239df05a67799097c6bf254f6ad15bd4afdfd36ac7fbb2d965786db8eff
5
5
  SHA512:
6
- metadata.gz: 3a71b1889c3bdfb4ce20b6634a5d5b2ece063b87d82c5c1f90dbe68c8c31615e736cfd18bef16a5c1d676af2c5fbadee4a77f99f02682c795ef69dff2212e60e
7
- data.tar.gz: 55d7a452386ab3c8146afc4feddf9aee1b95ba0f51e7b450afc3adb801d36b8b47c8a6d31ed95fad82bb6008aeee5bd8abae397f6aa73f3bdddf5c5a269b9b9f
6
+ metadata.gz: 00cba379c4e4c17690c5112b5a86c2fdd2057040c5266b06b4f96dc018c2784744280d99a09ecb5536bfd2d56efaa4f69d7304bf2e589c75d6de1b79e132d21b
7
+ data.tar.gz: ba89c043c9a91503e8716aeb8280f76e1889d9727ec7ae4ac8405e9f3f08e8ca18f49a2992b520f9fce7f1ed3c4bd2b47fe0396e64fb9dc2d952361be3f6b6ec
@@ -9,25 +9,33 @@ class Hash
9
9
  true
10
10
  end
11
11
 
12
- def to_hcl2(level: 0, spaces: 2)
12
+ def to_hcl2(spaces: 2, tab: 0)
13
13
  hcl2 = []
14
14
 
15
15
  each do |k, v|
16
16
  case v
17
- when Numeric, String, TrueClass, FalseClass, Array
17
+ when Numeric, String, TrueClass, FalseClass
18
18
  hcl2.push("#{k} = #{v.to_hcl2}\n")
19
+ when Array
20
+ if v.has_child?
21
+ v.each do |child|
22
+ hcl2.push({ "#{k}": child }.to_hcl2)
23
+ end
24
+ else
25
+ hcl2.push("#{k} = #{v.to_hcl2}")
26
+ end
19
27
  when Hash
20
28
  labels = HCL2::Helpers.dfs(v)
21
29
  labels.each do |label|
22
30
  # It creates a label string and we shouldn't
23
31
  # forget about addition space before this one
24
- n = label.empty? ? nil : label.map { |l| l&.to_hcl2 }.join('').prepend(' ') # It creates label string
32
+ n = label.empty? ? nil : label.map { |l| l&.to_hcl2 }.join(' ').prepend(' ') # It creates label string
25
33
 
26
34
  # We should go deeper to format child attributes
27
35
  c = label.empty? ? v : v.dig(*label)
28
36
 
29
37
  hcl2.push("#{k}#{n} {")
30
- hcl2.push(c.to_hcl2(level: level + 1))
38
+ hcl2.push(c.to_hcl2(spaces: spaces, tab: 1))
31
39
  hcl2.push("}\n")
32
40
  end
33
41
  else
@@ -35,7 +43,23 @@ class Hash
35
43
  end
36
44
  end
37
45
 
38
- hcl2.map { |v| v.prepend(' ' * (spaces * level)) }.join("\n")
46
+ hcl2.map { |v| v.prepend(' ' * spaces * tab) }.join("\n")
47
+ end
48
+ end
49
+
50
+ class Array
51
+ def has_child?
52
+ return false if empty?
53
+
54
+ each do |v|
55
+ return false unless v.is_a?(Hash)
56
+ end
57
+
58
+ true
59
+ end
60
+
61
+ def to_hcl2
62
+ to_s
39
63
  end
40
64
  end
41
65
 
@@ -68,9 +92,3 @@ class FalseClass
68
92
  to_s
69
93
  end
70
94
  end
71
-
72
- class Array
73
- def to_hcl2
74
- to_s
75
- end
76
- end
@@ -2,7 +2,7 @@
2
2
  module HCL2
3
3
  module Helpers
4
4
  # This is just the Deep Find Search algorithm
5
- # using to flatten Hash like it make HCL2
5
+ # using to flatten Hash like it made at HCL2
6
6
  def self.dfs(g)
7
7
  def self.f(g, r, c)
8
8
  unless g.has_child?
@@ -1,100 +1,187 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  RSpec.describe Numeric do
4
- it 'format Numeric to_hcl2' do
4
+ it 'format to hcl2' do
5
5
  expect(12_345.to_hcl2).to eq('12345')
6
6
  end
7
7
  end
8
8
 
9
9
  RSpec.describe String do
10
- it 'format String to_hcl2' do
10
+ it 'format to hcl2' do
11
11
  expect('string'.to_hcl2).to eq('"string"')
12
12
  end
13
13
  end
14
14
 
15
15
  RSpec.describe Symbol do
16
- it 'format Symbol to_hcl2' do
16
+ it 'format to hcl2' do
17
17
  expect(:symbol.to_hcl2).to eq('"symbol"')
18
18
  end
19
19
  end
20
20
 
21
21
  RSpec.describe TrueClass do
22
- it 'format TrueClass to_hcl2' do
22
+ it 'format to hcl2' do
23
23
  expect(true.to_hcl2).to eq('true')
24
24
  end
25
25
  end
26
26
 
27
27
  RSpec.describe FalseClass do
28
- it 'format FalseClass to_hcl2' do
28
+ it 'format to hcl2' do
29
29
  expect(false.to_hcl2).to eq('false')
30
30
  end
31
31
  end
32
32
 
33
33
  RSpec.describe Array do
34
- it 'format Array to_hcl2' do
34
+ it 'format to hcl2' do
35
35
  expect([1, 2, 'three'].to_hcl2).to eq('[1, 2, "three"]')
36
36
  end
37
37
  end
38
38
 
39
39
  RSpec.describe Hash do
40
- fix = {
40
+ fixture = {
41
41
  ui: true,
42
42
  listener: {
43
43
  tcp: {
44
- address: '127.0.0.1:8200'
44
+ address: '0.0.0.0:8200'
45
45
  },
46
46
  unix: {
47
47
  address: '/run/vault.sock'
48
48
  }
49
- },
50
- telemetry: {
51
- statsite_address: 'statsite.company.local:8125'
52
- },
53
- advanced: {
54
- foo: {
55
- bar: {
56
- bim: 'bam'
57
- }
58
- },
59
- it: 'works?'
60
49
  }
61
50
  }
62
51
 
63
- it 'has child' do
64
- expect(fix.has_child?).to eq(false)
65
- expect(fix[:listener].has_child?).to eq(true)
52
+ expectation = <<~EXP
53
+ ui = true
54
+
55
+ listener "tcp" {
56
+ address = "0.0.0.0:8200"
57
+
58
+ }
59
+
60
+ listener "unix" {
61
+ address = "/run/vault.sock"
62
+
63
+ }
64
+ EXP
65
+
66
+ it 'check childs' do
67
+ expect(fixture[:listener].has_child?).to eq(true)
68
+ expect(fixture[:listener][:tcp].has_child?).to eq(false)
66
69
  end
67
70
 
68
- it 'format Hash to_hcl2' do
69
- exp = <<~EXP
70
- ui = true
71
+ it 'format to hcl2' do
72
+ expect(fixture.to_hcl2).to eq(expectation)
73
+ end
74
+ end
75
+
76
+ RSpec.describe 'Advanced cases' do
77
+ it 'check array objects formating' do
78
+ fixture = {
79
+ listener: [{
80
+ tcp: {
81
+ address: '0.0.0.0:8200'
82
+ }
83
+ }, {
84
+ tcp: {
85
+ address: '127.0.0.1:8201'
86
+ }
87
+ }]
88
+ }
71
89
 
90
+ expectation = <<~EXP
72
91
  listener "tcp" {
73
- address = "127.0.0.1:8200"
92
+ address = "0.0.0.0:8200"
74
93
 
75
94
  }
76
95
 
77
- listener "unix" {
78
- address = "/run/vault.sock"
96
+ listener "tcp" {
97
+ address = "127.0.0.1:8201"
79
98
 
80
99
  }
100
+ EXP
81
101
 
82
- telemetry {
83
- statsite_address = "statsite.company.local:8125"
102
+ expect(fixture.to_hcl2).to eq(expectation)
103
+ end
104
+
105
+ it 'check nested object' do
106
+ fixture = {
107
+ resource: {
108
+ openstack_compute_flavor_v2: {
109
+ this: {
110
+ name: 'this',
111
+ region: 'region_a',
112
+ ram: 4,
113
+ vcpus: 2
114
+ }
115
+ },
116
+ openstack_networking_port_v2: {
117
+ this: {
118
+ name: 'this',
119
+ region: 'region_a'
120
+ }
121
+ }
122
+ }
123
+ }
124
+
125
+ expectation = <<~EXP
126
+ resource "openstack_compute_flavor_v2" "this" {
127
+ name = "this"
128
+
129
+ region = "region_a"
130
+
131
+ ram = 4
132
+
133
+ vcpus = 2
134
+
135
+ }
136
+
137
+ resource "openstack_networking_port_v2" "this" {
138
+ name = "this"
139
+
140
+ region = "region_a"
84
141
 
85
142
  }
143
+ EXP
86
144
 
87
- advanced {
88
- foo "bar" {
89
- bim = "bam"
145
+ expect(fixture.to_hcl2).to eq(expectation)
146
+ end
147
+ end
90
148
 
149
+ RSpec.describe 'HashiCorp examples' do
150
+ it 'Vault' do
151
+ fixture = {
152
+ ui: true,
153
+ listener: {
154
+ tcp: {
155
+ address: '127.0.0.1:8200'
156
+ },
157
+ unix: {
158
+ address: '/run/vault.sock'
91
159
  }
160
+ },
161
+ telemetry: {
162
+ statsite_address: 'statsite.company.local:8125'
163
+ }
164
+ }
92
165
 
93
- it = "works?"
166
+ expectation = <<~EXP
167
+ ui = true
168
+
169
+ listener "tcp" {
170
+ address = "127.0.0.1:8200"
171
+
172
+ }
173
+
174
+ listener "unix" {
175
+ address = "/run/vault.sock"
176
+
177
+ }
178
+
179
+ telemetry {
180
+ statsite_address = "statsite.company.local:8125"
94
181
 
95
182
  }
96
183
  EXP
97
184
 
98
- expect(fix.to_hcl2).to eq(exp)
185
+ expect(fixture.to_hcl2).to eq(expectation)
99
186
  end
100
187
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hcl2-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - iveahugeship
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-30 00:00:00.000000000 Z
11
+ date: 2023-06-01 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |2
14
14
  Ruby common data types formatter to HCL2.