hcl2-rb 0.9.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 73d51dffee490023cce7a09c6ddec262ea198ef287e24d4e8ea536335ca1c37a
4
+ data.tar.gz: cbcc946ca6cd61ada79732035840fd768dda94546e55fbe030c2c0a203d9e606
5
+ SHA512:
6
+ metadata.gz: 3a71b1889c3bdfb4ce20b6634a5d5b2ece063b87d82c5c1f90dbe68c8c31615e736cfd18bef16a5c1d676af2c5fbadee4a77f99f02682c795ef69dff2212e60e
7
+ data.tar.gz: 55d7a452386ab3c8146afc4feddf9aee1b95ba0f51e7b450afc3adb801d36b8b47c8a6d31ed95fad82bb6008aeee5bd8abae397f6aa73f3bdddf5c5a269b9b9f
data/LICENSE.md ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 iveahugeship
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # hcl2-rb
2
+
3
+ Ruby common data types formatter to HCL2.
4
+
5
+ Using this tool you can format general Ruby data types to HCL2.
6
+ Execute .to_hcl2 method, get the string and write it to a file
7
+
8
+ ## Example
9
+
10
+ It's very simple! You have the Hash:
11
+
12
+ ```
13
+ vault_config = {
14
+ ui: true,
15
+ listener: {
16
+ tcp: {
17
+ address: '127.0.0.1:8200'
18
+ },
19
+ unix: {
20
+ address: '/run/vault.sock'
21
+ }
22
+ },
23
+ telemetry: {
24
+ statsite_address: 'statsite.company.local:8125'
25
+ }
26
+ }
27
+ ```
28
+
29
+ Now format it to HCL2:
30
+
31
+ ```
32
+ >> puts vault_config.to_hcl2
33
+ ui = true
34
+
35
+ listener "tcp" {
36
+ address = "127.0.0.1:8200"
37
+
38
+ }
39
+
40
+ listener "unix" {
41
+ address = "/run/vault.sock"
42
+
43
+ }
44
+
45
+ telemetry {
46
+ statsite_address = "statsite.company.local:8125"
47
+
48
+ }
49
+ ```
50
+
51
+ ## Contributing
52
+
53
+ Create a PR or a simple issue c:
54
+
55
+ Before PR creating, please, execute `rspec` and `rubocop`.
@@ -0,0 +1,76 @@
1
+ class Hash
2
+ def has_child?
3
+ return false if empty?
4
+
5
+ each do |_k, v|
6
+ return false unless v.is_a?(Hash)
7
+ end
8
+
9
+ true
10
+ end
11
+
12
+ def to_hcl2(level: 0, spaces: 2)
13
+ hcl2 = []
14
+
15
+ each do |k, v|
16
+ case v
17
+ when Numeric, String, TrueClass, FalseClass, Array
18
+ hcl2.push("#{k} = #{v.to_hcl2}\n")
19
+ when Hash
20
+ labels = HCL2::Helpers.dfs(v)
21
+ labels.each do |label|
22
+ # It creates a label string and we shouldn't
23
+ # forget about addition space before this one
24
+ n = label.empty? ? nil : label.map { |l| l&.to_hcl2 }.join('').prepend(' ') # It creates label string
25
+
26
+ # We should go deeper to format child attributes
27
+ c = label.empty? ? v : v.dig(*label)
28
+
29
+ hcl2.push("#{k}#{n} {")
30
+ hcl2.push(c.to_hcl2(level: level + 1))
31
+ hcl2.push("}\n")
32
+ end
33
+ else
34
+ raise "#{v.class.name} can't be formatted to HCL2"
35
+ end
36
+ end
37
+
38
+ hcl2.map { |v| v.prepend(' ' * (spaces * level)) }.join("\n")
39
+ end
40
+ end
41
+
42
+ class Numeric
43
+ def to_hcl2
44
+ to_s
45
+ end
46
+ end
47
+
48
+ class String
49
+ def to_hcl2
50
+ inspect
51
+ end
52
+ end
53
+
54
+ class Symbol
55
+ def to_hcl2
56
+ to_s.to_hcl2
57
+ end
58
+ end
59
+
60
+ class TrueClass
61
+ def to_hcl2
62
+ to_s
63
+ end
64
+ end
65
+
66
+ class FalseClass
67
+ def to_hcl2
68
+ to_s
69
+ end
70
+ end
71
+
72
+ class Array
73
+ def to_hcl2
74
+ to_s
75
+ end
76
+ end
@@ -0,0 +1,28 @@
1
+ # rubocop:disable Naming/MethodParameterName, Lint/NestedMethodDefinition
2
+ module HCL2
3
+ module Helpers
4
+ # This is just the Deep Find Search algorithm
5
+ # using to flatten Hash like it make HCL2
6
+ def self.dfs(g)
7
+ def self.f(g, r, c)
8
+ unless g.has_child?
9
+ r.push(c.flatten)
10
+ return
11
+ end
12
+
13
+ g.each do |k, v|
14
+ c.push(k)
15
+ f(v, r, c)
16
+ c.delete(k)
17
+ end
18
+ end
19
+
20
+ res = []
21
+ cur = []
22
+ f(g, res, cur)
23
+
24
+ res
25
+ end
26
+ end
27
+ end
28
+ # rubocop:enable Naming/MethodParameterName, Lint/NestedMethodDefinition
data/lib/hcl2-rb.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'hcl2-rb/formatter'
2
+ require 'hcl2-rb/helpers'
@@ -0,0 +1,100 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Numeric do
4
+ it 'format Numeric to_hcl2' do
5
+ expect(12_345.to_hcl2).to eq('12345')
6
+ end
7
+ end
8
+
9
+ RSpec.describe String do
10
+ it 'format String to_hcl2' do
11
+ expect('string'.to_hcl2).to eq('"string"')
12
+ end
13
+ end
14
+
15
+ RSpec.describe Symbol do
16
+ it 'format Symbol to_hcl2' do
17
+ expect(:symbol.to_hcl2).to eq('"symbol"')
18
+ end
19
+ end
20
+
21
+ RSpec.describe TrueClass do
22
+ it 'format TrueClass to_hcl2' do
23
+ expect(true.to_hcl2).to eq('true')
24
+ end
25
+ end
26
+
27
+ RSpec.describe FalseClass do
28
+ it 'format FalseClass to_hcl2' do
29
+ expect(false.to_hcl2).to eq('false')
30
+ end
31
+ end
32
+
33
+ RSpec.describe Array do
34
+ it 'format Array to_hcl2' do
35
+ expect([1, 2, 'three'].to_hcl2).to eq('[1, 2, "three"]')
36
+ end
37
+ end
38
+
39
+ RSpec.describe Hash do
40
+ fix = {
41
+ ui: true,
42
+ listener: {
43
+ tcp: {
44
+ address: '127.0.0.1:8200'
45
+ },
46
+ unix: {
47
+ address: '/run/vault.sock'
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
+ }
61
+ }
62
+
63
+ it 'has child' do
64
+ expect(fix.has_child?).to eq(false)
65
+ expect(fix[:listener].has_child?).to eq(true)
66
+ end
67
+
68
+ it 'format Hash to_hcl2' do
69
+ exp = <<~EXP
70
+ ui = true
71
+
72
+ listener "tcp" {
73
+ address = "127.0.0.1:8200"
74
+
75
+ }
76
+
77
+ listener "unix" {
78
+ address = "/run/vault.sock"
79
+
80
+ }
81
+
82
+ telemetry {
83
+ statsite_address = "statsite.company.local:8125"
84
+
85
+ }
86
+
87
+ advanced {
88
+ foo "bar" {
89
+ bim = "bam"
90
+
91
+ }
92
+
93
+ it = "works?"
94
+
95
+ }
96
+ EXP
97
+
98
+ expect(fix.to_hcl2).to eq(exp)
99
+ end
100
+ end
@@ -0,0 +1,5 @@
1
+ require 'bundler/setup'
2
+ require 'hcl2-rb'
3
+
4
+ RSpec.configure do |c|
5
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hcl2-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.1
5
+ platform: ruby
6
+ authors:
7
+ - iveahugeship
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-03-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |2
14
+ Ruby common data types formatter to HCL2.
15
+
16
+ Using this tool you can format general Ruby data types to HCL2.
17
+ Execute .to_hcl2 method, get the string and write it to a file.
18
+ email:
19
+ - iveahugeship@gmail.com
20
+ executables: []
21
+ extensions: []
22
+ extra_rdoc_files: []
23
+ files:
24
+ - LICENSE.md
25
+ - README.md
26
+ - lib/hcl2-rb.rb
27
+ - lib/hcl2-rb/formatter.rb
28
+ - lib/hcl2-rb/helpers.rb
29
+ - spec/formatter_spec.rb
30
+ - spec/spec_helper.rb
31
+ homepage: https://github.com/iveahugeship/hcl2-rb
32
+ licenses:
33
+ - MIT
34
+ metadata: {}
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 2.6.0
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 3.3.5
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Ruby common data types formatter to HCL2
54
+ test_files: []